KickJava   Java API By Example, From Geeks To Geeks.

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


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 groovy.lang.GroovyClassLoader;
8 import java.io.File JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.net.URLClassLoader JavaDoc;
12 import java.util.* ;
13 import org.exoplatform.commons.utils.IOUtil;
14 /**
15  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
16  * @since Nov 8, 2004
17  * @version $Id$
18  */

19 public class GroovyManager {
20   private URL JavaDoc classpath_ ;
21   private Map cacheObject_ ;
22   private GroovyClassLoader gcl_ ;
23   private List listeners_ ;
24   private boolean dispose_ ;
25   
26   public GroovyManager(URL JavaDoc classpath) throws Exception JavaDoc {
27     classpath_ = classpath ;
28     listeners_ = new ArrayList(5) ;
29     listeners_.add(new GroovyManagerListener()) ;
30     cacheObject_ = new HashMap() ;
31     gcl_ = createGroovyClassLoader() ;
32     dispose_ = false ;
33   }
34   
35   public boolean isDispose() { return dispose_ ; }
36   public void setDispose(boolean b) { dispose_ = b ; }
37   
38   public String JavaDoc getGroovyClassPath() { return classpath_.toString() ; }
39  
40   synchronized public void addListener(GroovyManagerListener listener) {
41     listeners_.add(listener) ;
42   }
43   
44   synchronized public void removeListener(GroovyManagerListener listener) {
45     listeners_.remove(listener) ;
46   }
47   
48   synchronized public void removeAllListener() { listeners_.clear() ; }
49   
50   public Object JavaDoc getObject(String JavaDoc resource) throws Exception JavaDoc {
51     GroovyObject gobject= getGroovyObject(resource) ;
52     if(gobject.getType() == null) {
53       gobject.setType(gcl_ );
54       GroovyManagerListener.reload(listeners_, gobject) ;
55     }
56     return gobject.getObject() ;
57   }
58   
59   public GroovyClassLoader getGroovyClassLoader() { return gcl_ ; }
60   
61   public GroovyObject getGroovyObject(String JavaDoc resource) throws Exception JavaDoc {
62     GroovyObject gobject = (GroovyObject) cacheObject_.get(resource) ;
63     if(gobject == null) {
64       synchronized (cacheObject_){
65         gobject = new GroovyObject(resource) ;
66         gobject.setType(gcl_ );
67         GroovyManagerListener.load(listeners_, gobject) ;
68         cacheObject_.put(resource, gobject) ;
69       }
70     }
71     return gobject ;
72   }
73   
74   public String JavaDoc getGroovyObjectAsText(String JavaDoc resource) throws Exception JavaDoc {
75     return IOUtil.getStreamContentAsString(gcl_.getResourceAsStream(resource)) ;
76   }
77   
78   public void saveGroovyObject(String JavaDoc resource, String JavaDoc text) throws Exception JavaDoc {
79     String JavaDoc url = gcl_.getResource(resource).toString() ;
80     if(!url.startsWith("file:")) {
81       throw new Exception JavaDoc("The resources is in a jar file........") ;
82     }
83     String JavaDoc filePath = url.substring("file:".length() + 1, url.length()) ;
84     FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(filePath) ;
85     os.write(text.getBytes()) ;
86     os.close() ;
87   }
88   
89   private GroovyClassLoader createGroovyClassLoader() throws Exception JavaDoc {
90     ClassLoader JavaDoc parentLoader = Thread.currentThread().getContextClassLoader() ;
91     if(classpath_ != null) {
92       URL JavaDoc[] url = { classpath_ } ;
93       parentLoader = new URLClassLoader JavaDoc(url, Thread.currentThread().getContextClassLoader()) ;
94       String JavaDoc cpath = System.getProperty("java.class.path") ;
95       String JavaDoc newpath = classpath_.toString() ;
96       newpath = newpath.substring("file:".length(), newpath.length()) ;
97       if(cpath.indexOf(newpath) < 0) {
98         cpath = cpath + File.pathSeparator + newpath ;
99         System.setProperty("java.class.path", cpath) ;
100       }
101     }
102     return new GroovyClassLoader(parentLoader) ;
103   }
104   
105   public void checkModifiedObjects() throws Exception JavaDoc {
106     Iterator i = cacheObject_.values().iterator() ;
107     while(i.hasNext()) {
108       GroovyObject gobject = (GroovyObject) i.next() ;
109       if(gobject.isReloadable() && isObjectModified(gobject)) {
110         reloadGroovyObjects();
111         return ;
112       }
113     }
114   }
115   
116   public void reloadGroovyObjects() throws Exception JavaDoc {
117     synchronized(cacheObject_) {
118       Iterator i = cacheObject_.values().iterator() ;
119       gcl_ = createGroovyClassLoader() ;
120       while(i.hasNext()) {
121         GroovyObject gobject = (GroovyObject) i.next() ;
122         GroovyManagerListener.unload(listeners_, gobject) ;
123         gobject.setObject(null) ;
124       }
125     }
126   }
127   
128   private boolean isObjectModified(GroovyObject gobject) throws Exception JavaDoc {
129     URL JavaDoc resourceURL = gcl_.getResource(gobject.getGroovyResource()) ;
130     //in case of redeploying the resource , the resource url will be not found
131
//for a period , until the server explode the war file.
132
if(resourceURL == null) return false ;
133     String JavaDoc url = resourceURL.toString();
134     String JavaDoc filePath = url.substring("file:".length(), url.length()) ;
135     File JavaDoc file = new File JavaDoc(filePath) ;
136     long lastModifiedTime = file.lastModified() ;
137     if(lastModifiedTime > gobject.getLoadTime()) {
138       return true ;
139     }
140     return false ;
141   }
142 }
Popular Tags