1 17 18 19 package org.apache.catalina.startup; 20 21 22 import java.io.File ; 23 import java.util.Enumeration ; 24 25 import org.apache.catalina.Context; 26 import org.apache.catalina.Host; 27 import org.apache.catalina.Lifecycle; 28 import org.apache.catalina.LifecycleEvent; 29 import org.apache.catalina.LifecycleListener; 30 import org.apache.catalina.util.StringManager; 31 32 33 43 44 public final class UserConfig 45 implements LifecycleListener { 46 47 48 private static org.apache.commons.logging.Log log= 49 org.apache.commons.logging.LogFactory.getLog( UserConfig.class ); 50 51 52 54 55 58 private String configClass = "org.apache.catalina.startup.ContextConfig"; 59 60 61 64 private String contextClass = "org.apache.catalina.core.StandardContext"; 65 66 67 70 private String directoryName = "public_html"; 71 72 73 76 private String homeBase = null; 77 78 79 82 private Host host = null; 83 84 85 88 private static final StringManager sm = 89 StringManager.getManager(Constants.Package); 90 91 92 95 private String userClass = 96 "org.apache.catalina.startup.PasswdUserDatabase"; 97 98 99 101 102 105 public String getConfigClass() { 106 107 return (this.configClass); 108 109 } 110 111 112 117 public void setConfigClass(String configClass) { 118 119 this.configClass = configClass; 120 121 } 122 123 124 127 public String getContextClass() { 128 129 return (this.contextClass); 130 131 } 132 133 134 139 public void setContextClass(String contextClass) { 140 141 this.contextClass = contextClass; 142 143 } 144 145 146 149 public String getDirectoryName() { 150 151 return (this.directoryName); 152 153 } 154 155 156 161 public void setDirectoryName(String directoryName) { 162 163 this.directoryName = directoryName; 164 165 } 166 167 168 171 public String getHomeBase() { 172 173 return (this.homeBase); 174 175 } 176 177 178 183 public void setHomeBase(String homeBase) { 184 185 this.homeBase = homeBase; 186 187 } 188 189 190 193 public String getUserClass() { 194 195 return (this.userClass); 196 197 } 198 199 200 203 public void setUserClass(String userClass) { 204 205 this.userClass = userClass; 206 207 } 208 209 210 212 213 218 public void lifecycleEvent(LifecycleEvent event) { 219 220 try { 222 host = (Host) event.getLifecycle(); 223 } catch (ClassCastException e) { 224 log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e); 225 return; 226 } 227 228 if (event.getType().equals(Lifecycle.START_EVENT)) 230 start(); 231 else if (event.getType().equals(Lifecycle.STOP_EVENT)) 232 stop(); 233 234 } 235 236 237 239 240 244 private void deploy() { 245 246 if (host.getLogger().isDebugEnabled()) 247 host.getLogger().debug(sm.getString("userConfig.deploying")); 248 249 UserDatabase database = null; 251 try { 252 Class clazz = Class.forName(userClass); 253 database = (UserDatabase) clazz.newInstance(); 254 database.setUserConfig(this); 255 } catch (Exception e) { 256 host.getLogger().error(sm.getString("userConfig.database"), e); 257 return; 258 } 259 260 Enumeration users = database.getUsers(); 262 while (users.hasMoreElements()) { 263 String user = (String ) users.nextElement(); 264 String home = database.getHome(user); 265 deploy(user, home); 266 } 267 268 } 269 270 271 278 private void deploy(String user, String home) { 279 280 String contextPath = "/~" + user; 282 if (host.findChild(contextPath) != null) 283 return; 284 File app = new File (home, directoryName); 285 if (!app.exists() || !app.isDirectory()) 286 return; 287 292 host.getLogger().info(sm.getString("userConfig.deploy", user)); 293 294 try { 296 Class clazz = Class.forName(contextClass); 297 Context context = 298 (Context) clazz.newInstance(); 299 context.setPath(contextPath); 300 context.setDocBase(app.toString()); 301 if (context instanceof Lifecycle) { 302 clazz = Class.forName(configClass); 303 LifecycleListener listener = 304 (LifecycleListener) clazz.newInstance(); 305 ((Lifecycle) context).addLifecycleListener(listener); 306 } 307 host.addChild(context); 308 } catch (Exception e) { 309 host.getLogger().error(sm.getString("userConfig.error", user), e); 310 } 311 312 } 313 314 315 318 private void start() { 319 320 if (host.getLogger().isDebugEnabled()) 321 host.getLogger().debug(sm.getString("userConfig.start")); 322 323 deploy(); 324 325 } 326 327 328 331 private void stop() { 332 333 if (host.getLogger().isDebugEnabled()) 334 host.getLogger().debug(sm.getString("userConfig.stop")); 335 336 } 337 338 339 } 340 | Popular Tags |