1 5 package org.exoplatform.container.groovy; 6 7 import java.net.URL ; 8 import java.util.* ; 9 import org.picocontainer.Startable ; 10 import org.exoplatform.container.configuration.ConfigurationManager; 11 import org.exoplatform.container.configuration.ServiceConfiguration; 12 import org.exoplatform.container.configuration.ValueParam; 13 18 public class GroovyManagerContainer implements Startable { 19 private Map managers_ ; 20 private Thread scanner_ ; 21 22 public GroovyManagerContainer(ConfigurationManager cService) throws Exception { 23 ServiceConfiguration sconf = cService.getServiceConfiguration(getClass()) ; 24 ValueParam param = sconf.getValueParam("check.modified.period") ; 25 long period = Long.parseLong(param.getValue()) ; 26 if(period > 0) { 27 scanner_ = new ScannerThread(period) ; 28 scanner_.start() ; 29 } 30 managers_ = new HashMap() ; 31 } 32 33 public GroovyManager getGroovyManager(URL classpath) throws Exception { 34 String id = classpath.toString() ; 35 GroovyManager manager = (GroovyManager) managers_.get(id) ; 36 if(manager == null) { 37 synchronized(managers_) { 38 manager = new GroovyManager(classpath) ; 39 managers_.put(id, manager) ; 40 } 41 } 42 return manager ; 43 } 44 45 public GroovyManager removeGroovyManager(URL classpath) throws Exception { 46 synchronized (managers_) { 47 GroovyManager manager = (GroovyManager) managers_.remove(classpath.toString()) ; 48 return manager ; 49 } 50 } 51 52 public void start() {} 53 public void stop() { 54 if(scanner_ != null) scanner_.interrupt(); 55 } 56 57 public class ScannerThread extends Thread { 58 private long period_ ; 59 60 public ScannerThread(long period) { 61 setPriority(MIN_PRIORITY) ; 62 period_ = period ; 63 } 64 65 public void run() { 66 long checkPeriod = 20000 ; 67 while (true) { 68 try { 69 sleep(checkPeriod) ; 70 if(managers_.size() == 0) return ; 71 checkPeriod = period_ ; 72 Iterator i = managers_.values().iterator() ; 73 while(i.hasNext()) { 74 GroovyManager gmanager = (GroovyManager) i.next() ; 75 if(gmanager.isDispose()) { 76 i.remove() ; 77 } else { 78 gmanager.checkModifiedObjects() ; 79 } 80 } 81 } catch (InterruptedException ex) { 82 return ; 83 } catch (Throwable ex) { 84 ex.printStackTrace() ; 85 } 86 } 87 } 88 } 89 } | Popular Tags |