view src/kryshen/catalina/userconfig/UserConfig.java @ 22:e67c9a4b6763

Ununsed imports.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 03 Nov 2009 05:04:35 +0300
parents 282500da9a6c
children d5f928a0c600
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 String contextPath = getContextPath(user, "ROOT");
130 if (isServiced(contextPath)) {
131 return;
132 }
134 deployDirectory(contextPath, base, base.getAbsolutePath());
135 } else {
136 // Multiple applications mode.
137 deployUserDirectories(user, base, files);
138 }
139 }
141 /**
142 * Deploy user application directories.
143 *
144 * @param user Username.
145 * @param base Application base directory.
146 * @param files Array of directories to deploy.
147 */
148 protected void deployUserDirectories(String user, File base,
149 String[] files) {
151 int appCount = 0;
153 if (appsPerUser > 0) {
154 // Count number of already deployed user applications.
155 for (int i = 0; i < files.length; i++) {
156 String contextPath = getContextPath(user, files[i]);
158 if (isDeployed(contextPath)) {
159 appCount++;
160 }
161 }
162 }
164 // Deploy new applications
165 for (int i = 0; i < files.length; i++) {
166 if (appsPerUser > 0 && appCount >= appsPerUser) {
167 break;
168 }
170 if (files[i].equalsIgnoreCase("META-INF")) {
171 continue;
172 }
174 if (files[i].equalsIgnoreCase("WEB-INF")) {
175 continue;
176 }
178 File dir = new File(base, files[i]);
180 if (!dir.isDirectory() || !dir.canRead()) {
181 continue;
182 }
184 String contextPath = getContextPath(user, files[i]);
186 if (isDeployed(contextPath)) {
187 continue;
188 }
190 if (isServiced(contextPath)) {
191 continue;
192 }
194 deployDirectory(contextPath, dir, dir.getAbsolutePath());
195 appCount++;
196 }
197 }
199 protected String getContextPath(String user, String app) {
200 if (app.equals("ROOT")) {
201 return "/~" + user;
202 }
204 return "/~" + user + '/' + app.replace('#', '/');
205 }
206 }