view src/kryshen/catalina/userconfig/UserConfig.java @ 21:282500da9a6c

Fix appsPerUser limit calculation.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 03 Nov 2009 04:48:52 +0300
parents d9b6abd14326
children e67c9a4b6763
line source
1 /*
2 * Copyright 2009 Mikhail Kryshen
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
17 package kryshen.catalina.userconfig;
19 import java.io.File;
20 import java.util.Arrays;
21 import java.util.List;
22 import org.apache.catalina.startup.HostConfig;
23 import org.apache.juli.logging.LogFactory;
25 /**
26 * Event listener for Host that deploys and updates applications
27 * provided by users. In multi-app mode (appsPerUser property &gt 1)
28 * each user's application is mapped to "/~username/application" path,
29 * ROOT is mapped to "/~username". In single-app mode (appsPerUser = 1)
30 * each user could have single webapp mapped to "/~username".
31 *
32 * @author Mikhail Kryshen
33 */
34 public abstract class UserConfig extends HostConfig {
36 /**
37 * The directory name to be searched for within each user home directory.
38 */
39 private String directoryName = "public_webapps";
41 /**
42 * Maximum number of application each user is allowed to deploy.
43 */
44 private int appsPerUser = -1;
47 protected UserConfig() {
48 log = LogFactory.getLog(UserConfig.class);
49 }
51 /**
52 * Returns the directory name to be searched for webapps for each user
53 * (relative to the user's home direcotry).
54 */
55 public String getDirectoryName() {
56 return directoryName;
57 }
59 /**
60 * Set the directory name to be searched for webapps for each user
61 * (relative to the user's home direcotry).
62 *
63 * @param directoryName The new directory name.
64 */
65 public void setDirectoryName(String directoryName) {
66 this.directoryName = directoryName;
67 }
70 /**
71 * Returns the maximum number of application each user is allowed to
72 * deploy.
73 */
74 public int getAppsPerUser() {
75 return appsPerUser;
76 }
78 /**
79 * Set the maximum number of application each user is allowed to deploy.
80 */
81 public void setAppsPerUser(int appsPerUser) {
82 if (appsPerUser < 1 && appsPerUser != -1) {
83 throw new IllegalArgumentException("Invalid appsPerUser value.");
84 }
86 this.appsPerUser = appsPerUser;
87 }
90 @Override
91 protected void deployApps() {
92 deployUserApps();
93 }
95 @Override
96 protected void deployApps(String name) {
97 throw new UnsupportedOperationException
98 ("deployApps(String) is not supported.");
99 }
101 /**
102 * Deploy applications for all available users.
103 */
104 protected abstract void deployUserApps();
106 /**
107 * Deploy applications (if any) for specific user.
108 *
109 * @param user Username.
110 * @param home User home directory.
111 */
112 protected void deployUserApps(String user, File home) {
113 File base = new File(home, directoryName);
115 if (!base.isDirectory() || !base.canRead()) {
116 return;
117 }
119 String[] files = base.list();
121 if (files == null) {
122 log.warn("Error reading base directory: " + base.getPath() + ".");
123 return;
124 }
126 // TODO: deployWars
128 if (appsPerUser == 1) {
129 // Single application mode.
130 String contextPath = getContextPath(user, "ROOT");
132 if (isServiced(contextPath)) {
133 return;
134 }
136 deployDirectory(contextPath, base, base.getAbsolutePath());
137 } else {
138 // Multiple applications mode.
139 deployUserDirectories(user, base, files);
140 }
141 }
143 /**
144 * Deploy user application directories.
145 *
146 * @param user Username.
147 * @param base Application base directory.
148 * @param files Array of directories to deploy.
149 */
150 protected void deployUserDirectories(String user, File base,
151 String[] files) {
153 int appCount = 0;
155 if (appsPerUser > 0) {
156 // Count number of already deployed user applications.
157 for (int i = 0; i < files.length; i++) {
158 String contextPath = getContextPath(user, files[i]);
160 if (isDeployed(contextPath)) {
161 appCount++;
162 }
163 }
164 }
166 // Deploy new applications
167 for (int i = 0; i < files.length; i++) {
168 if (appsPerUser > 0 && appCount >= appsPerUser) {
169 break;
170 }
172 if (files[i].equalsIgnoreCase("META-INF")) {
173 continue;
174 }
176 if (files[i].equalsIgnoreCase("WEB-INF")) {
177 continue;
178 }
180 File dir = new File(base, files[i]);
182 if (!dir.isDirectory() || !dir.canRead()) {
183 continue;
184 }
186 String contextPath = getContextPath(user, files[i]);
188 if (isDeployed(contextPath)) {
189 continue;
190 }
192 if (isServiced(contextPath)) {
193 continue;
194 }
196 deployDirectory(contextPath, dir, dir.getAbsolutePath());
197 appCount++;
198 }
199 }
201 protected String getContextPath(String user, String app) {
202 if (app.equals("ROOT")) {
203 return "/~" + user;
204 }
206 return "/~" + user + '/' + app.replace('#', '/');
207 }
208 }