1 16 17 18 package org.apache.tomcat.util.loader; 19 20 21 import java.io.File ; 22 import java.io.Serializable ; 23 import java.lang.reflect.Constructor ; 24 import java.net.URL ; 25 26 28 38 public class Module implements Serializable { 39 40 42 43 46 public Module() { 47 } 48 49 50 52 private static final boolean DEBUG=false; 54 57 private transient ModuleClassLoader classLoader = null; 58 59 63 private boolean delegate = false; 64 65 private Class classLoaderClass; 66 67 72 private String loaderClass = null; 73 75 80 private transient ClassLoader parentClassLoader = null; 81 private Repository parent; 82 83 private Repository repository; 84 85 88 private String repositories[] = new String [0]; 89 private URL classpath[] ; 90 91 private File workDir; 92 93 96 private boolean started = false; 97 98 boolean hasIndex=false; 99 100 102 103 106 public ClassLoader getClassLoader() { 107 return classLoader; 108 } 109 110 111 115 public boolean getDelegate() { 116 return (this.delegate); 117 } 118 119 120 126 public void setDelegate(boolean delegate) { 127 boolean oldDelegate = this.delegate; 128 this.delegate = delegate; 129 if( classLoader != null ) classLoader.setDelegate(delegate); 130 } 131 132 133 135 139 public boolean modified() { 140 return (classLoader.modified()); 141 } 142 143 public boolean isStarted() { 144 return started; 145 } 146 147 public String getClasspathString() { 148 if(classpath==null ) { 149 return null; 150 } 151 StringBuffer sb=new StringBuffer (); 152 for( int i=0; i<classpath.length; i++ ) { 153 if( i>0 ) sb.append(":"); 154 sb.append( classpath[i].getFile() ); 155 } 156 return sb.toString(); 157 } 158 159 164 public void start() { 165 if (started) 167 throw new RuntimeException 168 ("Already started"); 169 started = true; 170 171 try { 173 174 classLoader = createClassLoader(); 175 176 classLoader.setDelegate(this.delegate); 178 179 for (int i = 0; i < repositories.length; i++) { 180 classLoader.addRepository(repositories[i]); 181 } 182 183 classLoader.start(); 184 185 getRepository().getLoader().notifyModuleStart(this); 186 187 } catch (Throwable t) { 188 log( "LifecycleException ", t ); 189 throw new RuntimeException ("start: ", t); 190 } 191 192 } 193 194 195 200 public void stop() { 201 if (!started) 202 throw new RuntimeException ("stop: started=false"); 203 204 log("stop()", null); 206 207 getRepository().getLoader().notifyModuleStop(this); 208 209 started = false; 210 211 if( repository != null ) repository.removeClassLoader(classLoader); 213 214 classLoader.stop(); 216 217 classLoader = null; 218 219 } 220 221 223 230 public void setClassLoaderClass( Class c ) { 231 classLoaderClass=c; 232 } 233 234 237 ModuleClassLoader createClassLoader() 238 throws Exception 239 { 240 241 if( classLoader != null ) return classLoader; 242 if( classLoaderClass==null && loaderClass!=null) { 243 classLoaderClass = Class.forName(loaderClass); 244 } 245 246 ModuleClassLoader classLoader = null; 247 248 if (parentClassLoader == null) { 249 if( parent != null ) { 250 parentClassLoader = parent.getClassLoader(); 251 } 252 } 253 if (parentClassLoader == null) { 254 parentClassLoader = Thread.currentThread().getContextClassLoader(); 255 } 256 257 if( classLoaderClass != null ) { 258 Class [] argTypes = { URL [].class, ClassLoader .class }; 259 Object [] args = { classpath, parentClassLoader }; 260 Constructor constr = classLoaderClass.getConstructor(argTypes); 261 classLoader = (ModuleClassLoader) constr.newInstance(args); 262 } else { 263 classLoader=new ModuleClassLoader( classpath, parentClassLoader); 264 } 265 266 classLoader.setModule(this); 267 classLoader.setDelegate( delegate ); 268 269 classLoader.start(); 270 repository.addClassLoader(classLoader); 271 272 return classLoader; 273 } 274 275 276 279 public void setParent(Repository parent) { 280 this.parent=parent; 281 } 282 283 286 public void setClasspath(URL [] array) { 287 this.classpath=array; 288 } 289 290 291 294 public void setRepository(Repository lg) { 295 this.repository=lg; 296 } 297 298 301 public String toString() { 302 303 StringBuffer sb = new StringBuffer ("ModuleLoader["); 304 sb.append(getClasspathString()); 305 sb.append("]"); 306 return (sb.toString()); 307 308 } 309 310 private void log( String s ) { 311 log(s,null); 312 } 313 314 private void log(String s, Throwable t ) { 315 System.err.println("Module: " + s ); 316 if( t!=null) 317 t.printStackTrace(); 318 } 319 320 321 324 public Repository getRepository() { 325 return repository; 326 } 327 328 329 332 public String getName() { 333 return classpath[0].getFile(); } 335 336 337 } 338 | Popular Tags |