[Idrs-commit] CVS: Idrs/dev/src/net/sourceforge/idrs/axis IdrsHandler.java,NONE,1.1 IdrsResourceMgr.
Brought to you by:
bigman921
|
From: Marc B. <big...@us...> - 2004-08-22 05:17:57
|
Update of /cvsroot/idrs/Idrs/dev/src/net/sourceforge/idrs/axis In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13680/src/net/sourceforge/idrs/axis Added Files: IdrsHandler.java IdrsResourceMgr.java DOMElement.java IdrsProvider.java Log Message: First pass at integration with Axis --- NEW FILE: IdrsHandler.java --- /* * Created on Aug 6, 2004 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package net.sourceforge.idrs.axis; import java.sql.Connection; import java.util.HashMap; import java.util.Iterator; import net.sourceforge.idrs.core.servlet.ConfigInfo; import net.sourceforge.idrs.jdbc.JDBCInfo; import net.sourceforge.idrs.pool.DbPool; import net.sourceforge.idrs.pool.ScriptPool; import net.sourceforge.idrs.utils.Application; import org.apache.axis.AxisFault; import org.apache.axis.MessageContext; import org.apache.axis.handlers.BasicHandler; /** * @author mlb * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class IdrsHandler extends BasicHandler { static final String DEFAULT_DB_POOL = "net.sourceforge.idrs.pool.JDBCPool"; static final String DEFAULT_REPORT_POOL = "net.sourceforge.idrs.pool.IDRSRepPool"; static final String DEFAULT_SCRIPT_POOL = "net.sourceforge.idrs.pool.ScriptContextPool"; public static final String CONFIG_LOCATION_OPTION = "config"; public static final String CONFIG_PARSE_OPTION = "parseClass"; ConfigInfo config; IdrsResourceMgr mgr; public static final String IDRS_RESOURCES = "IDRS_RESOURCE"; /* (non-Javadoc) * @see org.apache.axis.Handler#invoke(org.apache.axis.MessageContext) */ public void invoke(MessageContext ctx) throws AxisFault { if (! ctx.getPastPivot()) { System.out.println("inbound : " + this); ctx.setProperty(IDRS_RESOURCES,this.mgr); } else { System.out.println("outbound : " + this); } } /* (non-Javadoc) * @see org.apache.axis.Handler#init() */ public void init() { super.init(); // retrieve the configuration and servlet context String location = (String) this.getOption(CONFIG_LOCATION_OPTION); String parseClass = (String) this.getOption(CONFIG_PARSE_OPTION); this.mgr = new IdrsResourceMgr(); //configure the system config = new ConfigInfo(); try { config.loadFromConfigPath(location,parseClass); System.out.println("Completed config! " + config.getVars()); //create the application object this.mgr.app = new Application(config.getVars()); //load database pools retrieveDocDBs(); //load script contexts buildScriptContexts(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(System.out); } } protected void buildScriptContexts() throws Exception { String scriptPooler = config.getScriptPoolClass(); scriptPooler = scriptPooler != null ? scriptPooler : DEFAULT_SCRIPT_POOL; try { mgr.scriptPool = (net.sourceforge.idrs.pool.ScriptPool) Class .forName(scriptPooler) .newInstance(); String scriptClass = config.getScriptClass(); int poolmin = config.getScriptPoolMin(); int poolmax = config.getScriptPoolMax(); int idrsDaysOpen = config.getScriptDaysOpen(); mgr.scriptPool.build( poolmin, poolmax, scriptClass, 2000, "contexts.log", 10); } catch (Exception e) { e.printStackTrace(System.out); } } public void retrieveDocDBs() throws Exception { Connection con; String dbPool; int i; int numDBs; dbPool = config.getDbPoolClass() != null ? config.getDbPoolClass() : DEFAULT_DB_POOL; String tmpUser, tmpPass, tmpDriver, tmpName, tmpLog, tmpAlias = ""; long tmpDaysOpen; int tmpMin, tmpMax; mgr.dbs = new HashMap(); net.sourceforge.idrs.pool.DbPool tmpDB; JDBCInfo cur; Iterator it = config.getDbs().keySet().iterator(); while (it.hasNext()) { tmpAlias = (String) it.next(); //System.out.println("DB Loaded : " + tmpAlias); cur = (JDBCInfo) config.getDbs().get(tmpAlias); tmpDriver = cur.getDrivername(); tmpName = cur.getUrl(); tmpUser = cur.getUsername(); tmpPass = cur.getPassword(); tmpMin = cur.getMin(); tmpMax = cur.getMax(); tmpDaysOpen = cur.getDaysOpen(); tmpLog = cur.getLogPath(); try { tmpDB = (DbPool) Class.forName(dbPool).newInstance(); tmpDB.build( tmpDriver, tmpDriver, tmpName, tmpUser, tmpPass, tmpMin, tmpMax, tmpDaysOpen, 2000L, tmpLog); //System.out.println("Adding " + tmpName); //System.out.println("Adding " + tmpAlias); mgr.dbs.put(tmpAlias, tmpDB); //con = tmpDB.getConnection(); //System.out.println(tmpAlias + " " + con); //tmpDB.returnConnection(con); } catch (Exception e) { e.printStackTrace(System.out); } tmpAlias = null; } } } --- NEW FILE: IdrsResourceMgr.java --- /* * Created on Aug 7, 2004 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package net.sourceforge.idrs.axis; import java.util.HashMap; import net.sourceforge.idrs.pool.ScriptPool; import net.sourceforge.idrs.utils.Application; /** * @author mlb * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class IdrsResourceMgr { public Application app; public HashMap dbs; public ScriptPool scriptPool; } --- NEW FILE: DOMElement.java --- /* * Created on Aug 1, 2004 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package net.sourceforge.idrs.axis; import org.apache.axis.encoding.SerializationContext; import org.apache.axis.encoding.ser.DocumentSerializer; import org.apache.axis.message.SOAPBodyElement; import org.w3c.dom.Document; /** * @author mlb * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class DOMElement extends SOAPBodyElement { private Document document; public DOMElement(Document doc) { this.document = doc; } /* (non-Javadoc) * @see org.apache.axis.message.MessageElement#outputImpl(org.apache.axis.encoding.SerializationContext) */ protected void outputImpl(SerializationContext context) throws Exception { context.writeDOMElement(document.getDocumentElement()); } } --- NEW FILE: IdrsProvider.java --- /* * Created on Aug 7, 2004 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package net.sourceforge.idrs.axis; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileReader; import java.io.ObjectOutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StringBufferInputStream; import java.sql.Connection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import net.sourceforge.idrs.core.report.IDRSRep; import net.sourceforge.idrs.deploy.compile.RmlCompiler; import net.sourceforge.idrs.pool.DbPool; import net.sourceforge.idrs.script.embedable.IDRSScriptLanguage; import net.sourceforge.idrs.utils.Application; import net.sourceforge.idrs.utils.UserInfo; import org.apache.axis.AxisFault; import org.apache.axis.Message; import org.apache.axis.MessageContext; import org.apache.axis.handlers.soap.SOAPService; import org.apache.axis.message.SOAPBodyElement; import org.apache.axis.message.SOAPEnvelope; import org.apache.axis.providers.BasicProvider; import org.apache.axis.transport.http.AxisServlet; import org.apache.axis.transport.http.HTTPConstants; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * @author mlb * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class IdrsProvider extends BasicProvider { public static final String CONFIG_IDRS_RML_PATH = "rml"; public static final String CONFIG_IDRS_BASE_PACKAGE = "basePackage"; String rmlSrcPath; String basePackage; private IDRSRep report; static int count; private boolean doneinit; /* (non-Javadoc) * @see org.apache.axis.providers.BasicProvider#initServiceDesc(org.apache.axis.handlers.soap.SOAPService, org.apache.axis.MessageContext) */ public void initServiceDesc(SOAPService srvc, MessageContext ctx) throws AxisFault { } public void invoke(MessageContext msgContext) throws AxisFault { if (! this.doneinit) { this.firstinit(msgContext); } IdrsResourceMgr mgr = (IdrsResourceMgr) msgContext.getProperty(IdrsHandler.IDRS_RESOURCES); HashMap dbs = new HashMap(); IDRSScriptLanguage script = null; String buffer = null; try { List dbnames = this.report.getHead().getRequiredDBs(); Iterator it = dbnames.iterator(); while (it.hasNext()) { String name = (String) it.next(); dbs.put(name,((DbPool) mgr.dbs.get(name)).getConnection()); } HttpServletRequest req = (HttpServletRequest) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST); HttpServletResponse resp = (HttpServletResponse) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE); Application app = mgr.app; AxisServlet servlet = (AxisServlet) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLET); UserInfo user = new UserInfo("-1",0,"user"); script = mgr.scriptPool.getContext(); SOAPBodyElement bodypart = (SOAPBodyElement) msgContext.getRequestMessage().getSOAPEnvelope().getBodyElements().get(0); Document bodyDoc = null; try { bodyDoc = bodypart.getAsDocument(); } catch (Exception e) { e.printStackTrace(System.out); throw AxisFault.makeFault(e); } System.out.println("Base Package : " + basePackage); this.report.getHead().init(dbs, 0, 0, "", req.getSession(), req, resp, app, script, user, "NONE", servlet.getServletContext(), bodyDoc.getDocumentElement(), basePackage); report.getHead().setSendToClient(true); buffer = report.buildReport(); } catch (Throwable e1) { // TODO Auto-generated catch block e1.printStackTrace(System.out); } finally { if (script != null) { try { mgr.scriptPool.returnContext(script); } catch (Exception e2) { // TODO Auto-generated catch block e2.printStackTrace(System.out); } Iterator it = dbs.keySet().iterator(); while (it.hasNext()) { String name = (String) it.next(); Connection con = (Connection) dbs.get(name); try { ((DbPool) mgr.dbs.get(name)).returnConnection(con); } catch (Exception e3) { // TODO Auto-generated catch block e3.printStackTrace(System.out); } } } } if (buffer == null) throw new AxisFault("No report content"); System.out.println("buffer :\n" + buffer); DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder domBuilder = null; Document doc = null; try { domBuilder = domFactory.newDocumentBuilder(); doc = domBuilder.parse(new StringBufferInputStream(buffer)); } catch (Exception e) { e.printStackTrace(System.out); } Message resMsg = msgContext.getResponseMessage(); SOAPEnvelope resEnv; // If we didn't have a response message, make sure we set one up // with the appropriate versions of SOAP and Schema if (resMsg == null) { resEnv = new SOAPEnvelope(msgContext.getSOAPConstants(), msgContext .getSchemaVersion()); resMsg = new Message(resEnv); msgContext.setResponseMessage(resMsg); } else { resEnv = resMsg.getSOAPEnvelope(); } Message reqMsg = msgContext.getRequestMessage(); SOAPEnvelope reqEnv = reqMsg.getSOAPEnvelope(); DOMElement body = null; body = new DOMElement(doc); resEnv.addBodyElement(body); } /* (non-Javadoc) * @see org.apache.axis.Handler#init() */ public void init() { System.out.println("in init"); System.out.println(); //get needed info this.rmlSrcPath = (String) this.getOption(CONFIG_IDRS_RML_PATH); this.doneinit = false; AxisServlet servlet = (AxisServlet) this.getOption(HTTPConstants.MC_HTTP_SERVLET); String webinf = servlet.getServletContext().getRealPath("WEB-INF"); System.out.println("webinf directory : " + webinf); } private void firstinit(MessageContext ctx) { try { // get needed info //System.out.println(ctx.getService().getOptions()); this.rmlSrcPath = (String) ctx.getService().getOption(CONFIG_IDRS_RML_PATH); // compile the RML page AxisServlet servlet = (AxisServlet) ctx.getProperty(HTTPConstants.MC_HTTP_SERVLET); String webinf = servlet.getServletContext().getRealPath("WEB-INF"); System.out.println("webinf directory" + webinf); String rmlTrans = webinf + "/rmlTrans.xml"; String rmlSchema = webinf + "/rmlSchema.xml"; String parseClass = "org.apache.xerces.parsers.SAXParser"; rmlSrcPath = servlet.getServletContext().getRealPath(rmlSrcPath); System.out.println("rmlSrcPath : " + rmlSrcPath); this.basePackage = (String) ctx.getService().getOption(IdrsProvider.CONFIG_IDRS_BASE_PACKAGE); StringBuffer buf = new StringBuffer(); BufferedReader in = new BufferedReader(new FileReader(rmlSrcPath)); String line = null; while ((line = in.readLine()) != null) { buf.append(line).append('\n'); } this.compilePage(buf.toString(),new PrintWriter(new OutputStreamWriter(System.out)),parseClass,rmlTrans,true,rmlSchema); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(System.out); } this.doneinit = true; } public void compilePage ( String src, PrintWriter logger, String parseClass, String rmlTrans, boolean isFile, String rmlSchema) throws Exception { //System.out.println("Compile : " + compile); RmlCompiler compiler; try { //report = (new IDRSCompiler((String) src, false)).setLogger(logger).buildReport(); compiler = new RmlCompiler( logger, src, parseClass, rmlTrans, isFile, rmlSchema); report = compiler.getReport(); } catch (Exception e) { e.printStackTrace(logger); } } } |