View Javadoc

1   /*
2    * $Id: AbstractScriptComponent.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.components.script;
12  
13  import org.mule.impl.UMODescriptorAware;
14  import org.mule.umo.UMODescriptor;
15  import org.mule.umo.UMOException;
16  import org.mule.umo.lifecycle.Callable;
17  import org.mule.umo.lifecycle.Initialisable;
18  import org.mule.umo.lifecycle.InitialisationException;
19  import org.mule.umo.lifecycle.Lifecycle;
20  import org.mule.util.ClassUtils;
21  import org.mule.util.FileUtils;
22  import org.mule.util.monitor.FileListener;
23  import org.mule.util.monitor.FileMonitor;
24  
25  import java.io.File;
26  import java.net.MalformedURLException;
27  import java.net.URL;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * <code>AbstractScriptComponent</code> is a component that can execute scripts as
34   * components in Mule. This component also supports reloading if the script file
35   * changes (providing the file is on the file system)
36   * 
37   */
38  public abstract class AbstractScriptComponent
39      implements Initialisable, Lifecycle, UMODescriptorAware, FileListener, Callable
40  {
41      public static final int DEFAULT_RELOAD_INTERVAL_MS = 60000;
42  
43      /**
44       * logger used by this class
45       */
46      protected transient Log logger = LogFactory.getLog(getClass());
47      private String script = null;
48  
49      private String scriptText = null;
50      private boolean autoReload = true;
51      protected UMODescriptor descriptor;
52      private FileMonitor monitor;
53      private long reloadInterval = DEFAULT_RELOAD_INTERVAL_MS;
54  
55      public void setDescriptor(UMODescriptor descriptor)
56      {
57          this.descriptor = descriptor;
58      }
59  
60      public void initialise() throws InitialisationException
61      {
62          if (getScript() == null && getScriptText() == null)
63          {
64              String extension = getDefaultFileExtension();
65              if (!extension.startsWith("."))
66              {
67                  extension = "." + extension;
68              }
69              setScript(descriptor.getName() + extension);
70              logger.info("script name is not set, using default: " + descriptor.getName() + extension);
71          }
72  
73          if (getScriptText() != null)
74          {
75              loadInterpreter(getScriptText());
76          }
77          else
78          {
79              // load script before creating a file monitor so that the script name
80              // can be monified
81              loadInterpreter(getScriptUrl(getScript()));
82          }
83          if (autoReload)
84          {
85              File f = FileUtils.newFile(getScript());
86              if (f.exists())
87              {
88                  monitor = new FileMonitor(reloadInterval);
89                  monitor.addFile(f);
90                  monitor.addListener(this);
91                  logger.debug("Component script is reloadable");
92              }
93              else
94              {
95                  logger.warn("Cannot setup autoreload as the script fie is not on the local file system");
96              }
97          }
98      }
99  
100     protected URL getScriptUrl(String scriptLocation)
101     {
102         File f = FileUtils.newFile(scriptLocation);
103         if (f.exists())
104         {
105             try
106             {
107                 return f.toURL();
108             }
109             catch (MalformedURLException e)
110             {
111                 logger.error("Failed to create URL from file: " + f.getAbsolutePath(), e);
112                 return null;
113             }
114         }
115         else
116         {
117             return ClassUtils.getResource(scriptLocation, getClass());
118         }
119     }
120 
121     public String getScript()
122     {
123         return script;
124     }
125 
126     public void setScript(String script)
127     {
128         this.script = script;
129     }
130 
131     public boolean isAutoReload()
132     {
133         return autoReload;
134     }
135 
136     public void setAutoReload(boolean autoReload)
137     {
138         this.autoReload = autoReload;
139     }
140 
141     public void start() throws UMOException
142     {
143         if (monitor != null)
144         {
145             monitor.start();
146         }
147     }
148 
149     public void stop() throws UMOException
150     {
151         if (monitor != null)
152         {
153             monitor.stop();
154         }
155     }
156 
157     public void dispose()
158     {
159         try
160         {
161             stop();
162         }
163         catch (UMOException e)
164         {
165             logger.error(e.getMessage(), e);
166         }
167     }
168 
169     public String getScriptText()
170     {
171         return scriptText;
172     }
173 
174     public void setScriptText(String scriptText)
175     {
176         this.scriptText = scriptText;
177     }
178 
179     protected abstract void loadInterpreter(URL script) throws InitialisationException;
180 
181     protected abstract void loadInterpreter(String scriptText) throws InitialisationException;
182 
183     protected abstract String getDefaultFileExtension();
184 }