1 5 package org.exoplatform.container.groovy; 6 7 import groovy.lang.GroovyClassLoader; 8 import java.io.File ; 9 import java.io.FileOutputStream ; 10 import java.net.URL ; 11 import java.net.URLClassLoader ; 12 import java.util.* ; 13 import org.exoplatform.commons.utils.IOUtil; 14 19 public class GroovyManager { 20 private URL classpath_ ; 21 private Map cacheObject_ ; 22 private GroovyClassLoader gcl_ ; 23 private List listeners_ ; 24 private boolean dispose_ ; 25 26 public GroovyManager(URL classpath) throws Exception { 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 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 getObject(String resource) throws Exception { 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 resource) throws Exception { 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 getGroovyObjectAsText(String resource) throws Exception { 75 return IOUtil.getStreamContentAsString(gcl_.getResourceAsStream(resource)) ; 76 } 77 78 public void saveGroovyObject(String resource, String text) throws Exception { 79 String url = gcl_.getResource(resource).toString() ; 80 if(!url.startsWith("file:")) { 81 throw new Exception ("The resources is in a jar file........") ; 82 } 83 String filePath = url.substring("file:".length() + 1, url.length()) ; 84 FileOutputStream os = new FileOutputStream (filePath) ; 85 os.write(text.getBytes()) ; 86 os.close() ; 87 } 88 89 private GroovyClassLoader createGroovyClassLoader() throws Exception { 90 ClassLoader parentLoader = Thread.currentThread().getContextClassLoader() ; 91 if(classpath_ != null) { 92 URL [] url = { classpath_ } ; 93 parentLoader = new URLClassLoader (url, Thread.currentThread().getContextClassLoader()) ; 94 String cpath = System.getProperty("java.class.path") ; 95 String 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 { 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 { 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 { 129 URL resourceURL = gcl_.getResource(gobject.getGroovyResource()) ; 130 if(resourceURL == null) return false ; 133 String url = resourceURL.toString(); 134 String filePath = url.substring("file:".length(), url.length()) ; 135 File file = new File (filePath) ; 136 long lastModifiedTime = file.lastModified() ; 137 if(lastModifiedTime > gobject.getLoadTime()) { 138 return true ; 139 } 140 return false ; 141 } 142 } | Popular Tags |