changeset 12:2f82b0faeb89

Javadoc.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 03 Nov 2009 00:36:43 +0300
parents 3a71cbe721f9
children 618d0df5d46c
files src/kryshen/catalina/startup/HomesUserConfig.java src/kryshen/catalina/startup/PasswdUserConfig.java src/kryshen/catalina/startup/UserConfig.java
diffstat 3 files changed, 94 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/kryshen/catalina/startup/HomesUserConfig.java	Mon Nov 02 23:47:14 2009 +0300
+++ b/src/kryshen/catalina/startup/HomesUserConfig.java	Tue Nov 03 00:36:43 2009 +0300
@@ -9,14 +9,14 @@
 import org.apache.juli.logging.LogFactory;
 
 /**
+ * Event listener for Host that deploys and updates applications 
+ * provided by users. Files in the specified base directory are 
+ * considered tp be user home directories.
  *
  * @author Mikhail Kryshen
  */
 public class HomesUserConfig extends UserConfig {
 
-    protected static org.apache.juli.logging.Log log =
-            org.apache.juli.logging.LogFactory.getLog(HomesUserConfig.class);
-
     /**
      * The base directory containing user home directories.
      */
--- a/src/kryshen/catalina/startup/PasswdUserConfig.java	Mon Nov 02 23:47:14 2009 +0300
+++ b/src/kryshen/catalina/startup/PasswdUserConfig.java	Tue Nov 03 00:36:43 2009 +0300
@@ -9,6 +9,8 @@
 import org.apache.juli.logging.LogFactory;
 
 /**
+ * Event listener for Host that deploys and updates applications 
+ * provided by users listed in passwd database.
  *
  * @author Mikhail Kryshen
  */
@@ -31,6 +33,7 @@
      */
     private String passwd = "/etc/passwd";
 
+    
     public PasswdUserConfig() {
         log = LogFactory.getLog(PasswdUserConfig.class);
     }
--- a/src/kryshen/catalina/startup/UserConfig.java	Mon Nov 02 23:47:14 2009 +0300
+++ b/src/kryshen/catalina/startup/UserConfig.java	Tue Nov 03 00:36:43 2009 +0300
@@ -5,6 +5,11 @@
 import org.apache.juli.logging.LogFactory;
 
 /**
+ * Event listener for Host that deploys and updates applications
+ * provided by users. In multi-app mode (appsPerUser property &gt 1)
+ * each user's application is mapped to "/~username/application" path,
+ * ROOT is mapped to "/~username". In single-app mode (appsPerUser = 1)
+ * each user could have single webapp mapped to "/~username".
  *
  * @author Mikhail Kryshen
  */
@@ -15,7 +20,12 @@
      */
     private String directoryName = "public_webapps";
 
-    
+    /**
+     * Maximum number of application each user is allowed to deploy.
+     */
+    private int appsPerUser = -1;
+
+
     protected UserConfig() {
         log = LogFactory.getLog(UserConfig.class);
     }
@@ -32,12 +42,33 @@
      *  Set the directory name to be searched for webapps for each user
      *  (relative to the user's home direcotry).
      * 
-     * @param directoryName The new directory name
+     * @param directoryName The new directory name.
      */
     public void setDirectoryName(String directoryName) {
         this.directoryName = directoryName;
     }
 
+    
+    /**
+     * Returns the maximum number of application each user is allowed to
+     * deploy.
+     */
+    public int getAppsPerUser() {
+        return appsPerUser;
+    }
+
+    /**
+     * Set the maximum number of application each user is allowed to deploy.
+     */
+    public void setAppsPerUser(int appsPerUser) {
+        if (appsPerUser < 1 && appsPerUser != -1) {
+            throw new IllegalArgumentException("Invalid appsPerUser value.");
+        }
+
+        this.appsPerUser = appsPerUser;
+    }
+
+
     @Override
     protected void deployApps() {       
         deployUserApps();
@@ -57,8 +88,8 @@
     /**
      * Deploy applications (if any) for specific user.
      *
-     * @param user Username
-     * @param home User home directory
+     * @param user Username.
+     * @param home User home directory.
      */
     protected void deployUserApps(String user, File home) {
         File base = new File(home, directoryName);
@@ -67,22 +98,41 @@
             return;
         }
 
-        // TODO: deployWars
-
-        deployDirectories(user, base, base.list());
-    }
-
-    /**
-     * Deploy user webapp directories.
-     */
-    protected void deployDirectories(String user, File base, String[] files) {
+        String[] files = base.list();
 
         if (files == null) {
             log.warn("Error reading base directory: " + base.getPath() + ".");
             return;
         }
 
-        for (int i = 0; i < files.length; i++) {
+        // TODO: deployWars
+
+        if (appsPerUser == 1) {
+            // Single application mode.
+            deployUserApp(user, base, "ROOT");
+        } else {
+            // Multiple applications mode.
+            deployUserDirectories(user, base, files);
+        }
+    }
+
+    /**
+     * Deploy user application directories.
+     *
+     * @param user Username.
+     * @param base Application base directory.
+     * @param files Array of directories to deploy.
+     */
+    protected void deployUserDirectories(String user, File base, 
+            String[] files) {
+
+        int appsNum = files.length;
+
+        if (appsNum > appsPerUser) {
+            appsNum = appsPerUser;
+        }
+
+        for (int i = 0; i < appsNum; i++) {
             if (files[i].equalsIgnoreCase("META-INF")) {
                 continue;
             }
@@ -97,19 +147,30 @@
                 continue;
             }
 
-            String contextPath;
-
-            if (files[i].equals("ROOT")) {
-                contextPath = "/~" + user;
-            } else {
-                contextPath = "/~" + user + '/' + files[i].replace('#', '/');
-            }
-
-            if (isServiced(contextPath)) {
-                continue;
-            }
-
-            deployDirectory(contextPath, dir, dir.getAbsolutePath());
+            deployUserApp(user, dir, files[i]);
         }
     }
+
+    /**
+     * Deploy user application.
+     *
+     * @param user Username.
+     * @param dir Application directory.
+     * @param app Application name.
+     */
+    protected void deployUserApp(String user, File dir, String app) {
+        String contextPath;
+
+        if (app.equals("ROOT")) {
+            contextPath = "/~" + user;
+        } else {
+            contextPath = "/~" + user + '/' + app.replace('#', '/');
+        }
+
+        if (isServiced(contextPath)) {
+            return;
+        }
+
+        deployDirectory(contextPath, dir, dir.getAbsolutePath());
+    }
 }