1 10 11 package org.mule.components.script; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.mule.impl.UMODescriptorAware; 16 import org.mule.umo.UMODescriptor; 17 import org.mule.umo.UMOException; 18 import org.mule.umo.lifecycle.Callable; 19 import org.mule.umo.lifecycle.Initialisable; 20 import org.mule.umo.lifecycle.InitialisationException; 21 import org.mule.umo.lifecycle.Lifecycle; 22 import org.mule.util.ClassUtils; 23 import org.mule.util.FileUtils; 24 import org.mule.util.monitor.FileListener; 25 import org.mule.util.monitor.FileMonitor; 26 27 import java.io.File ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 31 37 public abstract class AbstractScriptComponent 38 implements Initialisable, Lifecycle, UMODescriptorAware, FileListener, Callable 39 { 40 43 protected transient Log logger = LogFactory.getLog(getClass()); 44 45 private String script = null; 46 private String scriptText = null; 47 48 private boolean autoReload = true; 49 protected UMODescriptor descriptor; 50 private FileMonitor monitor; 51 private long reloadInterval = 60000; 52 53 public void setDescriptor(UMODescriptor descriptor) 54 { 55 this.descriptor = descriptor; 56 } 57 58 public void initialise() throws InitialisationException 59 { 60 if (getScript() == null && getScriptText() == null) 61 { 62 String extension = getDefaultFileExtension(); 63 if (!extension.startsWith(".")) 64 { 65 extension = "." + extension; 66 } 67 setScript(descriptor.getName() + extension); 68 logger.info("script name is not set, using default: " + descriptor.getName() + extension); 69 } 70 71 if (getScriptText() != null) 72 { 73 loadInterpreter(getScriptText()); 74 } 75 else 76 { 77 loadInterpreter(getScriptUrl(getScript())); 80 } 81 if (autoReload) 82 { 83 File f = FileUtils.newFile(getScript()); 84 if (f.exists()) 85 { 86 monitor = new FileMonitor(reloadInterval); 87 monitor.addFile(f); 88 monitor.addListener(this); 89 logger.debug("Component script is reloadable"); 90 } 91 else 92 { 93 logger.warn("Cannot setup autoreload as the script fie is not on the local file system"); 94 } 95 } 96 } 97 98 protected URL getScriptUrl(String scriptLocation) 99 { 100 File f = FileUtils.newFile(scriptLocation); 101 if (f.exists()) 102 { 103 try 104 { 105 return f.toURL(); 106 } 107 catch (MalformedURLException e) 108 { 109 logger.error("Failed to create URL from file: " + f.getAbsolutePath(), e); 110 return null; 111 } 112 } 113 else 114 { 115 return ClassUtils.getResource(scriptLocation, getClass()); 116 } 117 } 118 119 public String getScript() 120 { 121 return script; 122 } 123 124 public void setScript(String script) 125 { 126 this.script = script; 127 } 128 129 public boolean isAutoReload() 130 { 131 return autoReload; 132 } 133 134 public void setAutoReload(boolean autoReload) 135 { 136 this.autoReload = autoReload; 137 } 138 139 public void start() throws UMOException 140 { 141 if (monitor != null) 142 { 143 monitor.start(); 144 } 145 } 146 147 public void stop() throws UMOException 148 { 149 if (monitor != null) 150 { 151 monitor.stop(); 152 } 153 } 154 155 public void dispose() 156 { 157 try 158 { 159 stop(); 160 } 161 catch (UMOException e) 162 { 163 logger.error(e.getMessage(), e); 164 } 165 } 166 167 public String getScriptText() 168 { 169 return scriptText; 170 } 171 172 public void setScriptText(String scriptText) 173 { 174 this.scriptText = scriptText; 175 } 176 177 protected abstract void loadInterpreter(URL script) throws InitialisationException; 178 179 protected abstract void loadInterpreter(String scriptText) throws InitialisationException; 180 181 protected abstract String getDefaultFileExtension(); 182 } 183 | Popular Tags |