From: rk Date: Fri, 15 Feb 2002 20:25:29 +0000 (+0000) Subject: some remarks and servletmodule cache in Mir.java X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=f6f7101f7955c909f063d9a817cb368d447c4adb;p=mir.git some remarks and servletmodule cache in Mir.java --- diff --git a/source/Mir.java b/source/Mir.java index 88ab04e5..be881e52 100755 --- a/source/Mir.java +++ b/source/Mir.java @@ -26,12 +26,12 @@ import mircoders.storage.*; */ -public class Mir extends AbstractServlet -{ +public class Mir extends AbstractServlet { + + private static ModuleUsers usersModule=null; + private static ModuleMessage messageModule=null; + private final static HashMap servletModuleInstanceHash = new HashMap(); - private static ModuleUsers usersModule=null; - private static ModuleMessage messageModule=null; - //private static boolean confed=false; public HttpSession session; public void doGet(HttpServletRequest req, HttpServletResponse res) @@ -43,8 +43,6 @@ public class Mir extends AbstractServlet throws ServletException, IOException, UnavailableException { long startTime = (new java.util.Date()).getTime(); - Class theServletModule; - ServletModule smod; long sessionConnectTime=0; String http=""; @@ -152,44 +150,65 @@ public class Mir extends AbstractServlet _sendStartPage(res,req, res.getWriter(),userEntity); return; } + // end of auth - //From now on regular dispatching... - - /** @todo i do already have the code for a ServletModuleInstanceMap - * i will insert it soon. the code is 100times faster than doing - * a classForName-lookup with every execution. will be moved to - * getServletModuleForName //rk - */ - + // From now on regular dispatching... try { - try { - theServletModule = Class.forName("mircoders.servlet.ServletModule" + moduleName); - } catch (ClassNotFoundException e) { - // searching servletmodule in mir.servlet-classes - theServletModule = Class.forName("mir.servlet.ServletModule" + moduleName); - } - //Instantiate the ServletModule - Method m = theServletModule.getMethod("getInstance",null); - smod = (ServletModule)m.invoke(null,null); + // get servletmodule by parameter and continue with dispacher + ServletModule smod = getServletModuleForName(moduleName); ServletModuleDispatch.dispatch(smod,req,res); } - catch (NoSuchMethodException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " getInstance() not found."); } - catch (InvocationTargetException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " target not found."); } - catch (ClassNotFoundException e) { handleError(res, res.getWriter(), "ServletModule" + moduleName + " not found."); } - catch (IllegalArgumentException e) { handleError( res,res.getWriter(), "ServletModule" + moduleName + " not found."); } + catch (ServletModuleException e) { + handleError(res, res.getWriter(), "ServletException in Module " + moduleName + " -- " + e.toString()); + } catch (ServletModuleUserException e) { - handleUserError(res,res.getWriter(), e.getMsg()); + handleUserError(res,res.getWriter(), "User error" + e.toString()); } - catch (ServletModuleException e){ handleError(res,res.getWriter(), "ServletException in Module ServletModule" + moduleName + " -- " + e.toString()); } - catch (IllegalAccessException e){ - handleError(res,res.getWriter(), "No access to class ServletModule" + moduleName + " -- " + e.toString()); } // timing... sessionConnectTime = new java.util.Date().getTime() - startTime; theLog.printInfo("EXECTIME (" + moduleName + "): " + sessionConnectTime + " ms"); } + + /** + * Private method getServletModuleForName returns ServletModule + * from Cache + * + * @return ServletModule + * + */ + private static ServletModule getServletModuleForName(String moduleName) + throws ServletModuleException { + + // Instance in Map ? + if (!servletModuleInstanceHash.containsKey(moduleName)) { + // was not found in hash... + try { + Class theServletModuleClass=null; + try { + // first we try to get ServletModule from stern.che3.servlet + theServletModuleClass = Class.forName("mircoders.servlet.ServletModule" + moduleName); + } catch (ClassNotFoundException e) { + // on failure, we try to get it from lib-layer + theServletModuleClass = Class.forName("mir.servlet.ServletModule"+ moduleName); + } + Method m = theServletModuleClass.getMethod("getInstance", null); + ServletModule smod = (ServletModule)m.invoke(null, null); + // we put it into map for further reference + servletModuleInstanceHash.put(moduleName,smod); + return smod; + } catch (Exception e) { + throw new ServletModuleException("*** error resolving classname for " + + moduleName +" -- "+ e.toString()); + } + } + else return (ServletModule)servletModuleInstanceHash.get(moduleName); + } + + private void handleError(HttpServletResponse res, PrintWriter out, String errorString) { + try { theLog.printError(errorString); SimpleHash modelRoot = new SimpleHash(); diff --git a/source/mir/entity/Entity.java b/source/mir/entity/Entity.java index 80a75860..0d2f4ce3 100755 --- a/source/mir/entity/Entity.java +++ b/source/mir/entity/Entity.java @@ -273,9 +273,7 @@ public class Entity implements TemplateHashModel, TemplateModelRoot public boolean isEmpty() throws TemplateModelException { - if (theValuesHash==null || theValuesHash.isEmpty()) - return true; - return false; + return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false; } public TemplateModel get(java.lang.String key) throws TemplateModelException @@ -285,7 +283,10 @@ public class Entity implements TemplateHashModel, TemplateModelRoot public void put(java.lang.String key, TemplateModel model) { - // putting should only take place via setValue + // putting should only take place via setValue and is limited to the + // database fields associated with the entity. no additional freemarker + // stuff will be available via Entity. + theLog.printWarning("### put is called on entity! - the values will be lost!"); } public void remove(java.lang.String key) diff --git a/source/mir/misc/HTMLTemplateProcessor.java b/source/mir/misc/HTMLTemplateProcessor.java index d2e224cf..cc596f7f 100755 --- a/source/mir/misc/HTMLTemplateProcessor.java +++ b/source/mir/misc/HTMLTemplateProcessor.java @@ -198,6 +198,10 @@ public final class HTMLTemplateProcessor { session=res.encodeURL(""); } + /** @todo why do we double those? should be cleaned up and + * statically initialized, we do not need to assemble a config + * hash everytime we give out a page, only exception is + * date "now" // rk */ // put standard configuration into tempalteRootmodel SimpleHash configHash = new SimpleHash(); configHash.put("docroot", new SimpleScalar(producerDocRoot)); @@ -215,6 +219,8 @@ public final class HTMLTemplateProcessor { tmr.put("videoHost", new SimpleScalar(videoHost)); tmr.put("audioHost", new SimpleScalar(audioHost)); tmr.put("imageHost", new SimpleScalar(imageHost)); + // this conform to updated freemarker syntax + tmr.put("compressWhitespace", new freemarker.template.utility.CompressWhitespace() ); tmr.put("config", configHash); tmpl.process(tmr, out); @@ -266,7 +272,7 @@ public final class HTMLTemplateProcessor { * * @deprecated This method is deprecated and will be deleted in the next * release. Entity interfaces freemarker.template.TemplateHashModel now - * and can be used in the same way as SimpleHash. It is not necessary any + * and can be used in the same way as SimpleHash. It is not necessary any * more to make a SimpleHash from an Entity */ public static SimpleHash makeSimpleHash(Entity entity) { @@ -345,7 +351,7 @@ public final class HTMLTemplateProcessor { { Template returnTemplate = null; if (templateFilename!=null) - returnTemplate = templateCache.getTemplate(templateFilename); + returnTemplate = (Template)templateCache.getItem(templateFilename,"template"); if (returnTemplate==null) { diff --git a/source/mircoders/producer/Producer.java b/source/mircoders/producer/Producer.java index cb2ead17..529a956c 100755 --- a/source/mircoders/producer/Producer.java +++ b/source/mircoders/producer/Producer.java @@ -29,15 +29,19 @@ abstract public class Producer { static { // init - try { - contentModule = new ModuleContent(DatabaseContent.getInstance()); - topicsModule = new ModuleTopics(DatabaseTopics.getInstance()); - linksImcsModule = new ModuleLinksImcs(DatabaseLinksImcs.getInstance()); - schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance()); - featureModule = new ModuleFeature(DatabaseFeature.getInstance()); - imageModule = new ModuleImages(DatabaseImages.getInstance()); - uploadedMediaModule = new ModuleUploadedMedia(DatabaseImages.getInstance()); - } catch(StorageObjectException e) {;} + try { + contentModule = new ModuleContent(DatabaseContent.getInstance()); + topicsModule = new ModuleTopics(DatabaseTopics.getInstance()); + linksImcsModule = new ModuleLinksImcs(DatabaseLinksImcs.getInstance()); + schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance()); + featureModule = new ModuleFeature(DatabaseFeature.getInstance()); + imageModule = new ModuleImages(DatabaseImages.getInstance()); + uploadedMediaModule = new ModuleUploadedMedia(DatabaseImages.getInstance()); + } + catch(StorageObjectException e) + { + System.err.println("*** failed to initialize Producer " + e.toString()); + } } public void handle(PrintWriter htmlout, EntityUsers user)