KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > components > script > AbstractScriptComponent


1 /*
2  * $Id: AbstractScriptComponent.java 3937 2006-11-20 16:04:25Z lajos $
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 MuleSource MPL
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.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 JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30
31 /**
32  * <code>AbstractScriptComponent</code> is a component that can execute scripts as
33  * components in Mule. This component also supports reloading if the script file
34  * changes (providing the file is on the file system)
35  *
36  */

37 public abstract class AbstractScriptComponent
38     implements Initialisable, Lifecycle, UMODescriptorAware, FileListener, Callable
39 {
40     /**
41      * logger used by this class
42      */

43     protected transient Log logger = LogFactory.getLog(getClass());
44
45     private String JavaDoc script = null;
46     private String JavaDoc 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 JavaDoc 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             // load script before creating a file monitor so that the script name
78
// can be monified
79
loadInterpreter(getScriptUrl(getScript()));
80         }
81         if (autoReload)
82         {
83             File JavaDoc 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 JavaDoc getScriptUrl(String JavaDoc scriptLocation)
99     {
100         File JavaDoc f = FileUtils.newFile(scriptLocation);
101         if (f.exists())
102         {
103             try
104             {
105                 return f.toURL();
106             }
107             catch (MalformedURLException JavaDoc 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 JavaDoc getScript()
120     {
121         return script;
122     }
123
124     public void setScript(String JavaDoc 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 JavaDoc getScriptText()
168     {
169         return scriptText;
170     }
171
172     public void setScriptText(String JavaDoc scriptText)
173     {
174         this.scriptText = scriptText;
175     }
176
177     protected abstract void loadInterpreter(URL JavaDoc script) throws InitialisationException;
178
179     protected abstract void loadInterpreter(String JavaDoc scriptText) throws InitialisationException;
180
181     protected abstract String JavaDoc getDefaultFileExtension();
182 }
183
Popular Tags