view src/kryshen/catalina/userconfig/UserConfig.java @ 20:d9b6abd14326

Fix appsPerUser limit.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 03 Nov 2009 04:06:30 +0300
parents d3495301ca01
children 282500da9a6c
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 org.apache.catalina.startup.HostConfig;
21 import org.apache.juli.logging.LogFactory;
23 /**
24 * Event listener for Host that deploys and updates applications
25 * provided by users. In multi-app mode (appsPerUser property &gt 1)
26 * each user's application is mapped to "/~username/application" path,
27 * ROOT is mapped to "/~username". In single-app mode (appsPerUser = 1)
28 * each user could have single webapp mapped to "/~username".
29 *
30 * @author Mikhail Kryshen
31 */
32 public abstract class UserConfig extends HostConfig {
34 /**
35 * The directory name to be searched for within each user home directory.
36 */
37 private String directoryName = "public_webapps";
39 /**
40 * Maximum number of application each user is allowed to deploy.
41 */
42 private int appsPerUser = -1;
45 protected UserConfig() {
46 log = LogFactory.getLog(UserConfig.class);
47 }
49 /**
50 * Returns the directory name to be searched for webapps for each user
51 * (relative to the user's home direcotry).
52 */
53 public String getDirectoryName() {
54 return directoryName;
55 }
57 /**
58 * Set the directory name to be searched for webapps for each user
59 * (relative to the user's home direcotry).
60 *
61 * @param directoryName The new directory name.
62 */
63 public void setDirectoryName(String directoryName) {
64 this.directoryName = directoryName;
65 }
68 /**
69 * Returns the maximum number of application each user is allowed to
70 * deploy.
71 */
72 public int getAppsPerUser() {
73 return appsPerUser;
74 }
76 /**
77 * Set the maximum number of application each user is allowed to deploy.
78 */
79 public void setAppsPerUser(int appsPerUser) {
80 if (appsPerUser < 1 && appsPerUser != -1) {
81 throw new IllegalArgumentException("Invalid appsPerUser value.");
82 }
84 this.appsPerUser = appsPerUser;
85 }
88 @Override
89 protected void deployApps() {
90 deployUserApps();
91 }
93 @Override
94 protected void deployApps(String name) {
95 throw new UnsupportedOperationException
96 ("deployApps(String) is not supported.");
97 }
99 /**
100 * Deploy applications for all available users.
101 */
102 protected abstract void deployUserApps();
104 /**
105 * Deploy applications (if any) for specific user.
106 *
107 * @param user Username.
108 * @param home User home directory.
109 */
110 protected void deployUserApps(String user, File home) {
111 File base = new File(home, directoryName);
113 if (!base.isDirectory() || !base.canRead()) {
114 return;
115 }
117 String[] files = base.list();
119 if (files == null) {
120 log.warn("Error reading base directory: " + base.getPath() + ".");
121 return;
122 }
124 // TODO: deployWars
126 if (appsPerUser == 1) {
127 // Single application mode.
128 deployUserApp(user, base, "ROOT");
129 } else {
130 // Multiple applications mode.
131 deployUserDirectories(user, base, files);
132 }
133 }
135 /**
136 * Deploy user application directories.
137 *
138 * @param user Username.
139 * @param base Application base directory.
140 * @param files Array of directories to deploy.
141 */
142 protected void deployUserDirectories(String user, File base,
143 String[] files) {
145 int appCount = 0;
147 for (int i = 0; i < files.length; i++) {
148 if (files[i].equalsIgnoreCase("META-INF")) {
149 continue;
150 }
152 if (files[i].equalsIgnoreCase("WEB-INF")) {
153 continue;
154 }
156 File dir = new File(base, files[i]);
158 if (!dir.isDirectory() || !dir.canRead()) {
159 continue;
160 }
162 deployUserApp(user, dir, files[i]);
164 appCount++;
166 if (appsPerUser > 0 && appCount >= appsPerUser) {
167 break;
168 }
169 }
170 }
172 /**
173 * Deploy user application.
174 *
175 * @param user Username.
176 * @param dir Application directory.
177 * @param app Application name.
178 */
179 protected void deployUserApp(String user, File dir, String app) {
180 String contextPath;
182 if (app.equals("ROOT")) {
183 contextPath = "/~" + user;
184 } else {
185 contextPath = "/~" + user + '/' + app.replace('#', '/');
186 }
188 if (isServiced(contextPath)) {
189 return;
190 }
192 deployDirectory(contextPath, dir, dir.getAbsolutePath());
193 }
194 }