view src/kryshen/catalina/startup/UserConfig.java @ 12:2f82b0faeb89

Javadoc.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 03 Nov 2009 00:36:43 +0300
parents 3a71cbe721f9
children 618d0df5d46c
line source
1 package kryshen.catalina.startup;
3 import java.io.File;
4 import org.apache.catalina.startup.HostConfig;
5 import org.apache.juli.logging.LogFactory;
7 /**
8 * Event listener for Host that deploys and updates applications
9 * provided by users. In multi-app mode (appsPerUser property &gt 1)
10 * each user's application is mapped to "/~username/application" path,
11 * ROOT is mapped to "/~username". In single-app mode (appsPerUser = 1)
12 * each user could have single webapp mapped to "/~username".
13 *
14 * @author Mikhail Kryshen
15 */
16 public abstract class UserConfig extends HostConfig {
18 /**
19 * The directory name to be searched for within each user home directory.
20 */
21 private String directoryName = "public_webapps";
23 /**
24 * Maximum number of application each user is allowed to deploy.
25 */
26 private int appsPerUser = -1;
29 protected UserConfig() {
30 log = LogFactory.getLog(UserConfig.class);
31 }
33 /**
34 * Returns the directory name to be searched for webapps for each user
35 * (relative to the user's home direcotry).
36 */
37 public String getDirectoryName() {
38 return directoryName;
39 }
41 /**
42 * Set the directory name to be searched for webapps for each user
43 * (relative to the user's home direcotry).
44 *
45 * @param directoryName The new directory name.
46 */
47 public void setDirectoryName(String directoryName) {
48 this.directoryName = directoryName;
49 }
52 /**
53 * Returns the maximum number of application each user is allowed to
54 * deploy.
55 */
56 public int getAppsPerUser() {
57 return appsPerUser;
58 }
60 /**
61 * Set the maximum number of application each user is allowed to deploy.
62 */
63 public void setAppsPerUser(int appsPerUser) {
64 if (appsPerUser < 1 && appsPerUser != -1) {
65 throw new IllegalArgumentException("Invalid appsPerUser value.");
66 }
68 this.appsPerUser = appsPerUser;
69 }
72 @Override
73 protected void deployApps() {
74 deployUserApps();
75 }
77 @Override
78 protected void deployApps(String name) {
79 throw new UnsupportedOperationException
80 ("deployApps(String) is not supported.");
81 }
83 /**
84 * Deploy applications for all available users.
85 */
86 protected abstract void deployUserApps();
88 /**
89 * Deploy applications (if any) for specific user.
90 *
91 * @param user Username.
92 * @param home User home directory.
93 */
94 protected void deployUserApps(String user, File home) {
95 File base = new File(home, directoryName);
97 if (!base.isDirectory() || !base.canRead()) {
98 return;
99 }
101 String[] files = base.list();
103 if (files == null) {
104 log.warn("Error reading base directory: " + base.getPath() + ".");
105 return;
106 }
108 // TODO: deployWars
110 if (appsPerUser == 1) {
111 // Single application mode.
112 deployUserApp(user, base, "ROOT");
113 } else {
114 // Multiple applications mode.
115 deployUserDirectories(user, base, files);
116 }
117 }
119 /**
120 * Deploy user application directories.
121 *
122 * @param user Username.
123 * @param base Application base directory.
124 * @param files Array of directories to deploy.
125 */
126 protected void deployUserDirectories(String user, File base,
127 String[] files) {
129 int appsNum = files.length;
131 if (appsNum > appsPerUser) {
132 appsNum = appsPerUser;
133 }
135 for (int i = 0; i < appsNum; i++) {
136 if (files[i].equalsIgnoreCase("META-INF")) {
137 continue;
138 }
140 if (files[i].equalsIgnoreCase("WEB-INF")) {
141 continue;
142 }
144 File dir = new File(base, files[i]);
146 if (!dir.isDirectory() || !dir.canRead()) {
147 continue;
148 }
150 deployUserApp(user, dir, files[i]);
151 }
152 }
154 /**
155 * Deploy user application.
156 *
157 * @param user Username.
158 * @param dir Application directory.
159 * @param app Application name.
160 */
161 protected void deployUserApp(String user, File dir, String app) {
162 String contextPath;
164 if (app.equals("ROOT")) {
165 contextPath = "/~" + user;
166 } else {
167 contextPath = "/~" + user + '/' + app.replace('#', '/');
168 }
170 if (isServiced(contextPath)) {
171 return;
172 }
174 deployDirectory(contextPath, dir, dir.getAbsolutePath());
175 }
176 }