view src/kryshen/catalina/startup/UserConfig.java @ 3:3ab011b6e6e8

Cleaner refactored version.
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 30 Oct 2009 05:12:58 +0300
parents src/kryshen/catalina/startup/UserConfig2.java@bd283605f2ae
children 64c05808a046
line source
1 package kryshen.catalina.startup;
3 import java.io.File;
4 import java.util.Map;
5 import org.apache.catalina.startup.HostConfig;
7 /**
8 *
9 * @author Mikhail Kryshen
10 */
11 public class UserConfig extends HostConfig {
13 /**
14 * The directory name to be searched for within each user home directory.
15 */
16 private String directoryName = "public_webapps";
18 /**
19 * The base directory containing user home directories.
20 */
21 private String homeBase = null;
23 /**
24 * The Java class name of the user database class we should use.
25 */
26 private String userClass =
27 "org.apache.catalina.startup.PasswdUserDatabase";
29 /**
30 * Return the directory name for user web applications.
31 */
32 public String getDirectoryName() {
33 return (this.directoryName);
34 }
36 /**
37 * Set the directory name for user web applications.
38 *
39 * @param directoryName The new directory name
40 */
41 public void setDirectoryName(String directoryName) {
42 this.directoryName = directoryName;
43 }
45 /**
46 * Return the base directory containing user home directories.
47 */
48 public String getHomeBase() {
49 return (this.homeBase);
50 }
52 /**
53 * Set the base directory containing user home directories.
54 *
55 * @param homeBase The new base directory
56 */
57 public void setHomeBase(String homeBase) {
58 this.homeBase = homeBase;
59 }
61 /**
62 * Return the user database class name for this component.
63 */
64 public String getUserClass() {
65 return (this.userClass);
66 }
68 /**
69 * Set the user database class name for this component.
70 */
71 public void setUserClass(String userClass) {
72 this.userClass = userClass;
73 }
75 @Override
76 protected void deployApps() {
77 // Load the user database object for this host
78 UserDatabase database = null;
79 try {
80 Class clazz = Class.forName(userClass);
81 database = (UserDatabase) clazz.newInstance();
82 database.setUserConfig(this);
83 } catch (Exception e) {
84 host.getLogger().error(sm.getString("userConfig.database"), e);
85 return;
86 }
88 // Deploy the web application (if any) for each defined user
89 Map<String, File> userHomes = database.getUserHomes();
90 for (Map.Entry<String, File> entry :userHomes.entrySet()) {
91 deployUserApps(entry.getKey(), entry.getValue());
92 }
93 }
95 @Override
96 protected void deployApps(String name) {
97 throw new UnsupportedOperationException
98 ("deployApps(String) is not supported.");
99 }
101 protected void deployUserApps(String user, File home) {
102 File base = new File(home, directoryName);
104 if (!base.exists() || !base.isDirectory()) {
105 return;
106 }
108 // TODO: deployWARs
109 // // Deploy WARs, and loop if additional descriptors are found
110 // deployWARs(appBase, appBase.list());
112 // Deploy expanded folders
113 deployDirectories(user, base, base.list());
114 }
116 /**
117 * Deploy user webapp directories.
118 */
119 protected void deployDirectories(String user, File base, String[] files) {
121 if (files == null) {
122 return;
123 }
125 for (int i = 0; i < files.length; i++) {
126 if (files[i].equalsIgnoreCase("META-INF")) {
127 continue;
128 }
130 if (files[i].equalsIgnoreCase("WEB-INF")) {
131 continue;
132 }
134 File dir = new File(base, files[i]);
135 if (dir.isDirectory()) {
136 String contextPath;
138 if (files[i].equals("ROOT")) {
139 contextPath = "/~" + user;
140 } else {
141 contextPath = "/~" + user + '/' + files[i].replace('#', '/');
142 }
144 if (isServiced(contextPath)) {
145 continue;
146 }
148 deployDirectory(contextPath, dir, dir.getAbsolutePath());
149 }
150 }
151 }
152 }