view src/kryshen/catalina/startup/UserConfig2.java @ 1:bd283605f2ae

First working version.
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 29 Oct 2009 16:03:12 +0300
parents
children
line source
1 package kryshen.catalina.startup;
3 import java.io.File;
4 import java.util.Enumeration;
5 import org.apache.catalina.startup.UserDatabase;
6 import org.apache.catalina.startup.HostConfig;
7 import org.apache.catalina.startup.UserConfig;
9 /**
10 *
11 * @author Mikhail Kryshen
12 */
13 public class UserConfig2 extends HostConfig {
15 /**
16 * The directory name to be searched for within each user home directory.
17 */
18 private String directoryName = "public_webapps";
20 /**
21 * The base directory containing user home directories.
22 */
23 private String homeBase = null;
25 /**
26 * The Java class name of the user database class we should use.
27 */
28 private String userClass =
29 "org.apache.catalina.startup.PasswdUserDatabase";
31 /**
32 * UserConfig instance is required by UserDatabase.
33 */
34 private UserConfig userConfig = new UserConfig();
36 /**
37 * Return the directory name for user web applications.
38 */
39 public String getDirectoryName() {
40 return (this.directoryName);
41 }
43 /**
44 * Set the directory name for user web applications.
45 *
46 * @param directoryName The new directory name
47 */
48 public void setDirectoryName(String directoryName) {
49 this.directoryName = directoryName;
50 userConfig.setDirectoryName(directoryName);
51 }
53 /**
54 * Return the base directory containing user home directories.
55 */
56 public String getHomeBase() {
57 return (this.homeBase);
58 }
60 /**
61 * Set the base directory containing user home directories.
62 *
63 * @param homeBase The new base directory
64 */
65 public void setHomeBase(String homeBase) {
66 this.homeBase = homeBase;
67 userConfig.setHomeBase(homeBase);
68 }
70 /**
71 * Return the user database class name for this component.
72 */
73 public String getUserClass() {
74 return (this.userClass);
75 }
77 /**
78 * Set the user database class name for this component.
79 */
80 public void setUserClass(String userClass) {
81 this.userClass = userClass;
82 userConfig.setUserClass(userClass);
83 }
85 @Override
86 protected void deployApps() {
87 // Load the user database object for this host
88 UserDatabase database = null;
89 try {
90 Class clazz = Class.forName(userClass);
91 database = (UserDatabase) clazz.newInstance();
92 database.setUserConfig(userConfig);
93 } catch (Exception e) {
94 host.getLogger().error(sm.getString("userConfig.database"), e);
95 return;
96 }
98 // Deploy the web application (if any) for each defined user
99 Enumeration users = database.getUsers();
100 while (users.hasMoreElements()) {
101 String user = (String) users.nextElement();
102 String home = database.getHome(user);
103 deployUserApps(user, home);
104 }
105 }
107 @Override
108 protected void deployApps(String name) {
109 throw new UnsupportedOperationException
110 ("deployApps(String) is not supported.");
111 }
113 protected void deployUserApps(String user, String home) {
114 File base = new File(home, directoryName);
116 if (!base.exists() || !base.isDirectory()) {
117 return;
118 }
120 // TODO: deployWARs
121 // // Deploy WARs, and loop if additional descriptors are found
122 // deployWARs(appBase, appBase.list());
124 // Deploy expanded folders
125 deployDirectories(user, base, base.list());
126 }
128 /**
129 * Deploy user webapp directories.
130 */
131 protected void deployDirectories(String user, File base, String[] files) {
133 if (files == null) {
134 return;
135 }
137 for (int i = 0; i < files.length; i++) {
138 if (files[i].equalsIgnoreCase("META-INF")) {
139 continue;
140 }
142 if (files[i].equalsIgnoreCase("WEB-INF")) {
143 continue;
144 }
146 File dir = new File(base, files[i]);
147 if (dir.isDirectory()) {
148 String contextPath;
150 if (files[i].equals("ROOT")) {
151 contextPath = "/~" + user;
152 } else {
153 contextPath = "/~" + user + '/' + files[i].replace('#', '/');
154 }
156 if (isServiced(contextPath)) {
157 continue;
158 }
160 deployDirectory(contextPath, dir, dir.getAbsolutePath());
161 }
162 }
163 }
164 }