first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[mir.git] / source / Mir.java
index 23b5852..123f32a 100755 (executable)
@@ -4,12 +4,14 @@ import java.net.*;
 import java.lang.reflect.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
+import javax.servlet.UnavailableException;
 import java.sql.*;
 
 import freemarker.template.*;
 
 import mir.misc.*;
 import mir.servlet.*;
+import mir.storage.StorageObjectException;
 
 import mircoders.servlet.*;
 import mircoders.module.*;
@@ -24,14 +26,13 @@ import mircoders.storage.*;
  */
 
 
-public class Mir extends AbstractServlet
-{
+public class Mir extends AbstractServlet {
 
-  private static ModuleUsers usersModule=null;
-  private static ModuleMessage messageModule=null;
-  private static boolean confed=false;
-  public HttpSession session;
+  private static ModuleUsers      usersModule=null;
+  private static ModuleMessage    messageModule=null;
+  private final static HashMap    servletModuleInstanceHash = new HashMap();
 
+  public HttpSession session;
 
   public void doGet(HttpServletRequest req, HttpServletResponse res)
     throws ServletException, IOException {
@@ -39,17 +40,15 @@ public class Mir extends AbstractServlet
   }
 
   public void doPost(HttpServletRequest req, HttpServletResponse res)
-    throws ServletException, IOException {
+    throws ServletException, IOException, UnavailableException {
 
-    long            startTime = (new java.util.Date()).getTime();
-    Class           theServletModule;
-    ServletModule   smod;
+    long            startTime = System.currentTimeMillis();
     long            sessionConnectTime=0;
     String          http="";
 
     // get the configration
-    if (!confed){
-      confed = getConfig(req);
+    if(getServletContext().getAttribute("mir.confed") == null) {
+      getConfig(req);
     }
     MirConfig.setServletName(getServletName());
 
@@ -63,6 +62,9 @@ public class Mir extends AbstractServlet
       setLanguage(session,getAcceptLanguage(req));
     }
 
+    /** @todo for cleanup and readability this should be moved to
+     *  method loginIfNecessary() */
+
     // Authentifizierung
     if (moduleName!=null && moduleName.equals("login")) {
       String user=req.getParameter("login");
@@ -148,34 +150,65 @@ public class Mir extends AbstractServlet
       _sendStartPage(res,req, res.getWriter(),userEntity);
       return;
     }
+    // end of auth
 
-    //From now on regular dispatching...
+    // 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 ServletModule" + moduleName + " -- " + e.toString()); }
-    catch (IllegalAccessException e){
-        handleError(res,res.getWriter(), "No access to class ServletModule" + moduleName + " -- " + e.toString()); }
+    catch (ServletModuleException e) {
+                       handleError(res, res.getWriter(), "ServletException in Module " + moduleName + " -- " + e.toString());
+               }
+    catch (ServletModuleUserException e) {
+      handleUserError(res,res.getWriter(), "User error" + e.toString());
+    }
 
     // timing...
-    sessionConnectTime = new java.util.Date().getTime() - startTime;
+    sessionConnectTime = System.currentTimeMillis() - 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();
@@ -189,6 +222,21 @@ public class Mir extends AbstractServlet
     }
   }
 
+  private void handleUserError(HttpServletResponse res,PrintWriter out, String errorString) {
+
+    try {
+      theLog.printError(errorString);
+      SimpleHash modelRoot = new SimpleHash();
+      modelRoot.put("errorstring", new SimpleScalar(errorString));
+      modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
+      HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.UserErrorTemplate"),modelRoot,out);
+      out.close();
+    }
+    catch (Exception e) {
+      System.err.println("Fehler in UserErrorTemplate");
+    }
+
+  }
   /**
    *  evaluate login for user / password
    */
@@ -196,8 +244,11 @@ public class Mir extends AbstractServlet
     try {
       if (usersModule == null) usersModule = new ModuleUsers(DatabaseUsers.getInstance());
       return usersModule.getUserForLogin(user, password);
+    } catch(Exception e) {
+      theLog.printDebugInfo(e.toString());
+      e.printStackTrace();
+      return null;
     }
-    catch(Exception e) { theLog.printDebugInfo(e.toString()); return null; }
   }
 
   // Redirect-methods
@@ -225,9 +276,9 @@ public class Mir extends AbstractServlet
       // merge with logged in user and messages
       SimpleHash mergeData = new SimpleHash();
       mergeData.put("session",sessionUrl);
-      mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(userEntity));
+      mergeData.put("login_user", userEntity);
       if (messageModule == null) messageModule = new ModuleMessage(DatabaseMessages.getInstance());
-      mergeData.put("messages", HTMLTemplateProcessor.makeSimpleList(messageModule.getByWhereClause(null, "webdb_create desc",0,10)));
+      mergeData.put("messages", messageModule.getByWhereClause(null, "webdb_create desc",0,10));
       HTMLTemplateProcessor.process(res,getLanguage(req,session)+"/"+startTemplate, mergeData,out);
     }
     catch(Exception e) {