playing around with helma.servlet.StandaloneServletClient

View: New views
2 Messages — Rating Filter:   Alert me  

playing around with helma.servlet.StandaloneServletClient

by Franz Philipp Moser :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi list,

this weekend I played around with helma and tomcat. First of all it
works nice with some drawbacks.

   * you can't use app.__app__.getAppDir
   * you can't use repositories

These 2 things are really annoying so I tried to overcome them with some
java in helma.servlet.StandaloneServletClient.

The first things is simple:
in helma/servlet/StandaloneServletClient.java FIND:
{{{
app = new Application(appName, repositories, dbHome);
}}}

REPLACE WITH:
{{{
File appHome = new File(appDir);
app = new Application(appName, null, repositories, appHome, dbHome);
}}}

So everything is OK with the getAppDir function. Maybe we should ad a
new constructor to the Application class, would be more elegant.

Second is to copy the code from the ApplicationManager where the
repositories are read from apps.properties. This is double code and
should be solved different!

add import java.util.* to the imports.


IN helma/servlet/StandaloneServletClient.java FIND:
{{{
public final class StandaloneServletClient extends AbstractServletClient {
     private Application app = null;
     private String appName;
     private String appDir;
     private String dbDir;
}}}

INSERT AFTER:
{{{
private Repository[] repositories;
}}}

IN helma/servlet/StandaloneServletClient.java FIND:
{{{
         dbDir = init.getInitParameter("dbdir");

         if ((dbDir == null) || (dbDir.trim().length() == 0)) {
             throw new ServletException("dbdir parameter not specified");
         }
}}}

INSERT AFTER:
{{{
         Class[] parameters = { String.class };
         ArrayList repositoryList = new ArrayList();
         for (int i = 0; true; i++) {
             String repositoryArgs = init.getInitParameter("repository."
+ i);
             if (repositoryArgs != null) {
                     // lookup repository implementation
                 String repositoryImpl =
init.getInitParameter("repository." + i +
                           ".implementation");
                 if (repositoryImpl == null) {
                     // implementation not set manually, have to guess it
                     if (repositoryArgs.endsWith(".zip")) {
                         repositoryImpl =
"helma.framework.repository.ZipRepository";
                     } else if (repositoryArgs.endsWith(".js")) {
                         repositoryImpl =
"helma.framework.repository.SingleFileRepository";
                     } else {
                         repositoryImpl =
"helma.framework.repository.FileRepository";
                     }
                 }

                 try {
                     Repository newRepository = (Repository)
Class.forName(repositoryImpl)
                     .getConstructor(parameters)
                     .newInstance(new Object[] { repositoryArgs });
                     repositoryList.add(newRepository);
                     log("adding repository: " + repositoryArgs);
                 } catch (Exception ex) {
                     log("Adding repository " + repositoryArgs + "
failed. " +
                    "Will not use that repository. Check your
initArgs!", ex);
                 }
             } else {
                 // we always scan repositories 0-9, beyond that only if
defined
                 if (i > 9) {
                     break;
                 }
             }
         }

         // add app dir
         FileRepository appRep = new FileRepository(appDir);
         log("adding repository: " + appDir);
         if (!repositoryList.contains(appRep)) {
             repositoryList.add(appRep);
         }
         repositories = new Repository[repositoryList.size()];
         repositoryList.toArray(repositories);
}}}

IN helma/servlet/StandaloneServletClient.java REMOVE this to lines:
{{{
             Repository[] repositories = new Repository[1];
             repositories[0] = new FileRepository(new File(appDir));
}}}
This is it and you have a full helma with repositories and getAppDir ;)

Maybe these code snippets go into svn.

I added the svn diff so the patch can be applied easy.

Please tell me what you think about these modifications.

cu Philipp


Index: helma/servlet/StandaloneServletClient.java
===================================================================
--- helma/servlet/StandaloneServletClient.java (Revision 8795)
+++ helma/servlet/StandaloneServletClient.java (Arbeitskopie)
@@ -21,6 +21,7 @@
 import helma.framework.repository.FileRepository;
 import java.io.*;
 import javax.servlet.*;
+import java.util.*;
 
 /**
  *  Standalone servlet client that runs a Helma application all by itself
@@ -39,6 +40,7 @@
     private String appName;
     private String appDir;
     private String dbDir;
+    private Repository[] repositories;
 
     /**
      *
@@ -67,6 +69,52 @@
         if ((dbDir == null) || (dbDir.trim().length() == 0)) {
             throw new ServletException("dbdir parameter not specified");
         }
+        Class[] parameters = { String.class };
+        ArrayList repositoryList = new ArrayList();
+        for (int i = 0; true; i++) {
+            String repositoryArgs = init.getInitParameter("repository." + i);
+            if (repositoryArgs != null) {
+                    // lookup repository implementation
+                String repositoryImpl = init.getInitParameter("repository." + i +
+                          ".implementation");
+                if (repositoryImpl == null) {
+                    // implementation not set manually, have to guess it
+                    if (repositoryArgs.endsWith(".zip")) {
+                        repositoryImpl = "helma.framework.repository.ZipRepository";
+                    } else if (repositoryArgs.endsWith(".js")) {
+                        repositoryImpl = "helma.framework.repository.SingleFileRepository";
+                    } else {
+                        repositoryImpl = "helma.framework.repository.FileRepository";
+                    }
+                }
+        
+                try {
+                    Repository newRepository = (Repository) Class.forName(repositoryImpl)
+                    .getConstructor(parameters)
+                    .newInstance(new Object[] { repositoryArgs });
+                    repositoryList.add(newRepository);
+                    log("adding repository: " + repositoryArgs);
+                } catch (Exception ex) {
+                    log("Adding repository " + repositoryArgs + " failed. " +
+                   "Will not use that repository. Check your initArgs!", ex);
+                }
+            } else {
+                // we always scan repositories 0-9, beyond that only if defined
+                if (i > 9) {
+                    break;
+                }
+            }
+        }
+        
+        // add app dir
+        FileRepository appRep = new FileRepository(appDir);
+        log("adding repository: " + appDir);
+        if (!repositoryList.contains(appRep)) {
+            repositoryList.add(appRep);
+        }
+        repositories = new Repository[repositoryList.size()];
+        repositoryList.toArray(repositories);
+
     }
 
     /**
@@ -88,16 +136,16 @@
      * do another check if the app already exists and immediately return if it does.
      */
     synchronized void createApp() {
+
         if (app != null) {
             return;
         }
 
         try {
-            Repository[] repositories = new Repository[1];
-            repositories[0] = new FileRepository(new File(appDir));
             File dbHome = new File(dbDir);
+            File appHome = new File(appDir);
 
-            app = new Application(appName, repositories, dbHome);
+            app = new Application(appName, null, repositories, appHome, dbHome);
             app.init();
             app.start();
         } catch (Exception x) {

_______________________________________________
Helma-user mailing list
Helma-user@...
http://helma.org/mailman/listinfo/helma-user

Re: playing around with helma.servlet.StandaloneServletClient

by Hannes Wallnoefer-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks, Philipp. I committed your patch and closed bug 544.
http://helma.org/bugs/show_bug.cgi?id=544

hannes

2008/3/17, Franz Philipp Moser <philipp.moser@...>:

> Hi list,
>
>  this weekend I played around with helma and tomcat. First of all it
>  works nice with some drawbacks.
>
>   * you can't use app.__app__.getAppDir
>   * you can't use repositories
>
>  These 2 things are really annoying so I tried to overcome them with some
>  java in helma.servlet.StandaloneServletClient.
>
>  The first things is simple:
>  in helma/servlet/StandaloneServletClient.java FIND:
>  {{{
>  app = new Application(appName, repositories, dbHome);
>  }}}
>
>  REPLACE WITH:
>  {{{
>  File appHome = new File(appDir);
>  app = new Application(appName, null, repositories, appHome, dbHome);
>  }}}
>
>  So everything is OK with the getAppDir function. Maybe we should ad a
>  new constructor to the Application class, would be more elegant.
>
>  Second is to copy the code from the ApplicationManager where the
>  repositories are read from apps.properties. This is double code and
>  should be solved different!
>
>  add import java.util.* to the imports.
>
>
>  IN helma/servlet/StandaloneServletClient.java FIND:
>  {{{
>  public final class StandaloneServletClient extends AbstractServletClient {
>     private Application app = null;
>     private String appName;
>     private String appDir;
>     private String dbDir;
>  }}}
>
>  INSERT AFTER:
>  {{{
>  private Repository[] repositories;
>  }}}
>
>  IN helma/servlet/StandaloneServletClient.java FIND:
>  {{{
>         dbDir = init.getInitParameter("dbdir");
>
>         if ((dbDir == null) || (dbDir.trim().length() == 0)) {
>             throw new ServletException("dbdir parameter not specified");
>         }
>  }}}
>
>  INSERT AFTER:
>  {{{
>         Class[] parameters = { String.class };
>         ArrayList repositoryList = new ArrayList();
>         for (int i = 0; true; i++) {
>             String repositoryArgs =
> init.getInitParameter("repository." + i);
>             if (repositoryArgs != null) {
>                     // lookup repository implementation
>                 String repositoryImpl =
>  init.getInitParameter("repository." + i +
>                           ".implementation");
>                 if (repositoryImpl == null) {
>                     // implementation not set manually, have to guess it
>                     if (repositoryArgs.endsWith(".zip")) {
>                         repositoryImpl =
>  "helma.framework.repository.ZipRepository";
>                     } else if
> (repositoryArgs.endsWith(".js")) {
>                         repositoryImpl =
>  "helma.framework.repository.SingleFileRepository";
>                     } else {
>                         repositoryImpl =
>  "helma.framework.repository.FileRepository";
>                     }
>                 }
>
>                 try {
>                     Repository newRepository = (Repository)
>  Class.forName(repositoryImpl)
>                     .getConstructor(parameters)
>                     .newInstance(new Object[] { repositoryArgs });
>                     repositoryList.add(newRepository);
>                     log("adding repository: " + repositoryArgs);
>                 } catch (Exception ex) {
>                     log("Adding repository " + repositoryArgs + " failed. "
> +
>                    "Will not use that repository. Check your initArgs!",
> ex);
>                 }
>             } else {
>                 // we always scan repositories 0-9, beyond that only if
> defined
>                 if (i > 9) {
>                     break;
>                 }
>             }
>         }
>
>         // add app dir
>         FileRepository appRep = new FileRepository(appDir);
>         log("adding repository: " + appDir);
>         if (!repositoryList.contains(appRep)) {
>             repositoryList.add(appRep);
>         }
>         repositories = new
> Repository[repositoryList.size()];
>         repositoryList.toArray(repositories);
>  }}}
>
>  IN helma/servlet/StandaloneServletClient.java REMOVE this
> to lines:
>  {{{
>             Repository[] repositories = new Repository[1];
>             repositories[0] = new FileRepository(new File(appDir));
>  }}}
>  This is it and you have a full helma with repositories and getAppDir ;)
>
>  Maybe these code snippets go into svn.
>
>  I added the svn diff so the patch can be applied easy.
>
>  Please tell me what you think about these modifications.
>
>  cu Philipp
>
>
> _______________________________________________
>  Helma-user mailing list
>  Helma-user@...
>  http://helma.org/mailman/listinfo/helma-user
>
>
>
_______________________________________________
Helma-user mailing list
Helma-user@...
http://helma.org/mailman/listinfo/helma-user