KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > container > groovy > GroovyManagerContainer


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.container.groovy;
6
7 import java.net.URL JavaDoc;
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 /**
14  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
15  * @since Nov 8, 2004
16  * @version $Id$
17  */

18 public class GroovyManagerContainer implements Startable {
19   private Map managers_ ;
20   private Thread JavaDoc scanner_ ;
21   
22   public GroovyManagerContainer(ConfigurationManager cService) throws Exception JavaDoc {
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 JavaDoc classpath) throws Exception JavaDoc {
34     String JavaDoc 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 JavaDoc classpath) throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc ex) {
82           return ;
83         } catch (Throwable JavaDoc ex) {
84           ex.printStackTrace() ;
85         }
86       }
87     }
88   }
89 }
Popular Tags