view src/kryshen/catalina/userconfig/UserConfig.java @ 25:620ec7263ee1

Formatting.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 03 Nov 2009 05:10:39 +0300
parents 497bd05771eb
children 9501a363dcbb
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 }
67 /**
68 * Returns the maximum number of application each user is allowed to
69 * deploy.
70 */
71 public int getAppsPerUser() {
72 return appsPerUser;
73 }
75 /**
76 * Set the maximum number of application each user is allowed to deploy.
77 */
78 public void setAppsPerUser(int appsPerUser) {
79 if (appsPerUser < 1 && appsPerUser != -1) {
80 throw new IllegalArgumentException("Invalid appsPerUser value.");
81 }
83 this.appsPerUser = appsPerUser;
84 }
86 @Override
87 protected void deployApps() {
88 deployUserApps();
89 }
91 @Override
92 protected void deployApps(String name) {
93 throw new UnsupportedOperationException
94 ("deployApps(String) is not supported.");
95 }
97 /**
98 * Deploy applications for all available users.
99 */
100 protected abstract void deployUserApps();
102 /**
103 * Deploy applications (if any) for specific user.
104 *
105 * @param user Username.
106 * @param home User home directory.
107 */
108 protected void deployUserApps(String user, File home) {
109 File base = new File(home, directoryName);
111 if (!base.isDirectory() || !base.canRead()) {
112 return;
113 }
115 String[] files = base.list();
117 if (files == null) {
118 log.warn("Error reading base directory: " + base.getPath() + ".");
119 return;
120 }
122 // TODO: deployWars
124 if (appsPerUser == 1) {
125 // Single application mode.
126 String contextPath = getContextPath(user, "ROOT");
128 if (isServiced(contextPath)) {
129 return;
130 }
132 deployDirectory(contextPath, base, base.getAbsolutePath());
133 } else {
134 // Multiple applications mode.
135 deployUserDirectories(user, base, files);
136 }
137 }
139 /**
140 * Deploy user application directories.
141 *
142 * @param user Username.
143 * @param base Application base directory.
144 * @param files Array of directories to deploy.
145 */
146 protected void deployUserDirectories(String user, File base,
147 String[] files) {
149 int appCount = 0;
151 if (appsPerUser > 0) {
152 // Count number of already deployed user applications.
153 for (int i = 0; i < files.length; i++) {
154 String contextPath = getContextPath(user, files[i]);
156 if (isDeployed(contextPath)) {
157 appCount++;
158 }
159 }
160 }
162 // Deploy new applications.
163 for (int i = 0; i < files.length; i++) {
164 if (appsPerUser > 0 && appCount >= appsPerUser) {
165 break;
166 }
168 if (files[i].equalsIgnoreCase("META-INF") ||
169 files[i].equalsIgnoreCase("WEB-INF")) {
170 continue;
171 }
173 File dir = new File(base, files[i]);
175 if (!dir.isDirectory() || !dir.canRead()) {
176 continue;
177 }
179 String contextPath = getContextPath(user, files[i]);
181 if (isDeployed(contextPath) || isServiced(contextPath)) {
182 continue;
183 }
185 deployDirectory(contextPath, dir, dir.getAbsolutePath());
186 appCount++;
187 }
188 }
190 protected String getContextPath(String user, String app) {
191 if (app.equals("ROOT")) {
192 return "/~" + user;
193 }
195 return "/~" + user + '/' + app.replace('#', '/');
196 }
197 }