changed the behaviour of entity uploaded media so that articles/comments linking...
[mir.git] / source / Mir.java
index 24c111e..c9ffdce 100755 (executable)
  * If you do not wish to do so, delete this exception statement from your version.
  */
 
-import java.io.IOException;
-import java.lang.reflect.Method;
-import java.util.GregorianCalendar;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Vector;
+import mir.bundle.Bundle;
+import mir.config.MirPropertiesConfiguration;
+import mir.log.LoggerWrapper;
+import mir.servlet.*;
+import mir.util.StringRoutines;
+import mir.util.ExceptionRoutines;
+import multex.Failure;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.UnavailableException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-import javax.servlet.http.HttpSessionBindingEvent;
-import javax.servlet.http.HttpSessionBindingListener;
+import javax.servlet.http.*;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.*;
 
-import mir.config.MirPropertiesConfiguration;
-import mir.servlet.AbstractServlet;
-import mir.servlet.ServletModule;
-import mir.servlet.ServletModuleDispatch;
-import mir.servlet.ServletModuleExc;
-import mir.servlet.ServletModuleUserExc;
-import mir.util.ExceptionFunctions;
-import mir.util.StringRoutines;
-import mircoders.entity.EntityUsers;
-import mircoders.global.MirGlobal;
-import mircoders.module.ModuleMessage;
 import mircoders.module.ModuleUsers;
+import mircoders.global.MirGlobal;
 import mircoders.servlet.ServletHelper;
-import mircoders.storage.DatabaseUsers;
-
-import org.apache.struts.util.MessageResources;
-
-
-
+import mircoders.entity.EntityUsers;
 
-/**
- * Mir.java - main servlet, that dispatches to servletmodules
- *
- * @author $Author: zapata $
- * @version $Id: Mir.java,v 1.49.2.9 2003/12/21 13:32:02 zapata Exp $
- *
- */
 public class Mir extends AbstractServlet {
   private static ModuleUsers usersModule = null;
-  private static ModuleMessage messageModule = null;
-  private final static Map servletModuleInstanceHash = new HashMap();
   private static Locale fallbackLocale = null;
 
   private static List loginLanguages = null;
 
-  protected List getLoginLanguages() throws ServletException {
+  private List getLoginLanguages() throws Failure {
     synchronized (Mir.class) {
       try {
         if (loginLanguages == null) {
-          MessageResources messageResources =
-            MessageResources.getMessageResources("bundles.adminlocal");
-          MessageResources messageResources2 =
-            MessageResources.getMessageResources("bundles.admin");
-
           List languages =
             StringRoutines.splitString(MirGlobal.config().getString("Mir.Login.Languages", "en"), ";");
 
-          loginLanguages = new Vector();
+          loginLanguages = new ArrayList();
 
           Iterator i = languages.iterator();
 
           while (i.hasNext()) {
             String code = (String) i.next();
-            Locale locale = new Locale(code, "");
-            String name = messageResources.getMessage(locale, "languagename");
+
+            Bundle bundle =
+                MirGlobal.getBundleFactory().getBundle("etc/bundles/adminlocal", new String[] { code });
+            Bundle defaultBundle =
+                MirGlobal.getBundleFactory().getBundle("bundles/admin", new String[] { code });
+
+            String name = bundle.getValue("languagename", Collections.EMPTY_LIST);
 
             if (name == null) {
-              name = messageResources2.getMessage(locale, "languagename");
+              name = defaultBundle.getValue("languagename", Collections.EMPTY_LIST);
             }
 
             if (name == null) {
@@ -121,7 +95,7 @@ public class Mir extends AbstractServlet {
         return loginLanguages;
       }
       catch (Throwable t) {
-        throw new ServletException(t.getMessage());
+        throw new Failure("Error while retrieving the available login languages", t);
       }
     }
   }
@@ -129,7 +103,7 @@ public class Mir extends AbstractServlet {
   public void init(ServletConfig config) throws ServletException {
     super.init(config);
 
-    usersModule = new ModuleUsers(DatabaseUsers.getInstance());
+    usersModule = new ModuleUsers();
   }
 
   protected String getDefaultLanguage(HttpServletRequest aRequest) {
@@ -177,9 +151,7 @@ public class Mir extends AbstractServlet {
       return user;
     }
     catch (Throwable t) {
-      t.printStackTrace();
-
-      throw new ServletException(t.toString());
+      throw new ServletException(t.getMessage());
     }
   }
 
@@ -190,7 +162,6 @@ public class Mir extends AbstractServlet {
 
       HttpSession session = aRequest.getSession(true);
       setNoCaching(aResponse);
-      Locale locale = new Locale(getDefaultLanguage(aRequest), "");
       aResponse.setContentType("text/html; charset=" +
                                configuration.
                                getString("Mir.DefaultHTMLCharset", "UTF-8"));
@@ -216,7 +187,6 @@ public class Mir extends AbstractServlet {
           moduleName="Admin";
         }
 
-
         if (moduleName.equals("login")) {
           String target = (String) session.getAttribute("login.target");
 
@@ -228,21 +198,21 @@ public class Mir extends AbstractServlet {
           }
         }
         else if (moduleName.equals("logout")) {
-          logger.info(userEntity.getValue("login") + " has logged out");
+          logger.info(userEntity.getFieldValue("login") + " has logged out");
           session.invalidate();
           _sendLoginPage(aResponse, aRequest);
           return;
         }
         else {
           try {
-            ServletModule servletModule = getServletModuleForName(moduleName);
-            ServletModuleDispatch.dispatch(servletModule, aRequest, aResponse);
+            AdminServletModule servletModule = getServletModuleForName(moduleName);
+            servletModule.handleRequest(aRequest, aResponse);
 
             sessionConnectTime = System.currentTimeMillis() - startTime;
             logger.info("EXECTIME (" + moduleName + "): " + sessionConnectTime + " ms");
           }
           catch (Throwable e) {
-            Throwable cause = ExceptionFunctions.traceCauseException(e);
+            Throwable cause = ExceptionRoutines.traceCauseException(e);
 
             if (cause instanceof ServletModuleUserExc)
               handleUserError(aRequest, aResponse, (ServletModuleUserExc) cause);
@@ -256,8 +226,6 @@ public class Mir extends AbstractServlet {
       }
     }
     catch (Throwable t) {
-      t.printStackTrace();
-
       throw new ServletException(t.toString());
     }
   }
@@ -265,44 +233,12 @@ public class Mir extends AbstractServlet {
   /**
    * caching routine to get a module for a module name
    *
-   * @param moduleName the module name
+   * @param aModuleName the module name
    * @return the requested module
    * @throws ServletModuleExc
    */
-
-  private static ServletModule getServletModuleForName(String moduleName) throws ServletModuleExc {
-    // 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 ServletModuleExc("*** error resolving classname for " + moduleName + " -- " + e.getMessage());
-      }
-    }
-    else {
-      return (ServletModule) servletModuleInstanceHash.get(moduleName);
-    }
+  private static AdminServletModule getServletModuleForName(String aModuleName) throws ServletModuleExc {
+    return ServletHelper.getServletModule(aModuleName);
   }
 
   private void handleUserError(HttpServletRequest aRequest, HttpServletResponse aResponse, ServletModuleUserExc anException) {
@@ -311,8 +247,21 @@ public class Mir extends AbstractServlet {
 
       Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
 
-      MessageResources messages = MessageResources.getMessageResources("bundles.admin");
-      responseData.put("errorstring", messages.getMessage(getLocale(aRequest), anException.getMessage(), anException.getParameters()));
+      Bundle bundle =
+          MirGlobal.getBundleFactory().getBundle("etc/bundles/adminlocal", new
+              String[] { getLocale(aRequest).getLanguage() });
+      Bundle defaultBundle =
+          MirGlobal.getBundleFactory().getBundle("bundles/admin", new
+              String[] { getLocale(aRequest).getLanguage() });
+      String message =
+        bundle.getValue(anException.getMessage(), Arrays.asList(anException.getParameters()));
+
+      if (message==null) {
+        message =
+          defaultBundle.getValue(anException.getMessage(), Arrays.asList(anException.getParameters()));
+      }
+
+      responseData.put("errorstring", message);
       responseData.put("date", new GregorianCalendar().getTime());
 
       ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.UserErrorTemplate"));
@@ -329,12 +278,31 @@ public class Mir extends AbstractServlet {
       Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale()});
 
       responseData.put("errorstring", anException.toString());
+      StringWriter writer = new StringWriter();
+      anException.printStackTrace(new PrintWriter(writer));
+      responseData.put("stacktrace", writer.toString());
       responseData.put("date", new GregorianCalendar().getTime());
 
       ServletHelper.generateResponse(aResponse.getWriter(), responseData, MirPropertiesConfiguration.instance().getString("Mir.ErrorTemplate"));
     }
     catch (Throwable e) {
       logger.error("Error handling error: " + e.toString());
+
+      try {
+        Throwable rootException = ExceptionRoutines.traceCauseException(anException);
+
+        PrintWriter writer = aResponse.getWriter();
+        writer.println("<html><head><title>FATAL Error</title><body>");
+        writer.println("<h1>" + rootException.toString()+"</h1>");
+        writer.println("<code>");
+        rootException.printStackTrace(writer);
+        writer.println("</code>");
+        writer.println("</body></html>");
+        writer.close();
+      }
+      catch (Throwable t) {
+
+      }
     }
   }