KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > config > builders > ScriptConfigurationBuilder


1 /*
2  * $Id: ScriptConfigurationBuilder.java 4259 2006-12-14 03:12:07Z aperepel $
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.config.builders;
12
13 import java.io.IOException JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 import javax.script.CompiledScript;
17 import javax.script.Namespace;
18
19 import org.mule.MuleManager;
20 import org.mule.components.script.jsr223.Scriptable;
21 import org.mule.config.ConfigurationBuilder;
22 import org.mule.config.ConfigurationException;
23 import org.mule.config.MuleProperties;
24 import org.mule.config.ReaderResource;
25 import org.mule.config.i18n.Message;
26 import org.mule.config.i18n.Messages;
27 import org.mule.umo.manager.UMOManager;
28 import org.mule.util.PropertiesUtils;
29
30 /**
31  * Configures a MuleManager from one or more script files.
32  */

33 public class ScriptConfigurationBuilder extends Scriptable implements ConfigurationBuilder
34 {
35
36     public static final String JavaDoc SCRIPT_ENGINE_NAME_PROPERTY = "org.mule.script.engine";
37
38     protected UMOManager manager = null;
39     protected QuickConfigurationBuilder builder = null;
40     protected boolean initialised = false;
41
42     public ScriptConfigurationBuilder()
43     {
44         builder = new QuickConfigurationBuilder(false);
45         manager = MuleManager.getInstance();
46         String JavaDoc scriptName = System.getProperty(SCRIPT_ENGINE_NAME_PROPERTY);
47         if (scriptName == null)
48         {
49             throw new NullPointerException JavaDoc(new Message(Messages.SYSTEM_PROPERTY_X_NOT_SET,
50                 SCRIPT_ENGINE_NAME_PROPERTY).getMessage());
51         }
52         else
53         {
54             this.setScriptEngineName(scriptName);
55         }
56     }
57
58     public ScriptConfigurationBuilder(String JavaDoc scriptEngineName)
59     {
60         builder = new QuickConfigurationBuilder(false);
61         manager = MuleManager.getInstance();
62         this.setScriptEngineName(scriptEngineName);
63     }
64
65     public UMOManager configure(String JavaDoc configResources) throws ConfigurationException
66     {
67         return configure(configResources, null);
68     }
69
70     /**
71      * Will configure a UMOManager based on the configuration file(s) provided.
72      *
73      * @param configResources a comma separated list of configuration files to load,
74      * this should be accessible on the classpath or filesystem
75      * @return A configured UMOManager
76      * @throws org.mule.config.ConfigurationException
77      */

78     public UMOManager configure(String JavaDoc configResources, String JavaDoc startupPropertiesFile)
79         throws ConfigurationException
80     {
81         // if(!initialised) {
82
// try {
83
// initialise();
84
// initialised = true;
85
// } catch (InitialisationException e) {
86
// throw new ConfigurationException(e);
87
// }
88
// }
89
try
90         {
91             ReaderResource[] readers = ReaderResource.parseResources(configResources);
92
93             // Load startup properties if any.
94
if (startupPropertiesFile != null)
95             {
96                 return configure(readers, PropertiesUtils.loadProperties(startupPropertiesFile, getClass()));
97             }
98             else
99             {
100                 return configure(readers, null);
101             }
102         }
103         catch (IOException JavaDoc e)
104         {
105             throw new ConfigurationException(e);
106         }
107     }
108
109     /**
110      * @deprecated Please use configure(ReaderResource[] configResources, Properties
111      * startupProperties) instead.
112      */

113     public UMOManager configure(ReaderResource[] configResources) throws ConfigurationException
114     {
115         return configure(configResources, null);
116     }
117
118     /**
119      * Will configure a UMOManager based on the configurations made available through
120      * Readers
121      *
122      * @param configResources an array of Readers
123      * @return A configured UMOManager
124      * @throws org.mule.config.ConfigurationException
125      */

126     public UMOManager configure(ReaderResource[] configResources, Properties JavaDoc startupProperties)
127         throws ConfigurationException
128     {
129         if (startupProperties != null)
130         {
131             ((MuleManager)MuleManager.getInstance()).addProperties(startupProperties);
132         }
133
134         try
135         {
136             for (int i = 0; i < configResources.length; i++)
137             {
138                 ReaderResource configResource = configResources[i];
139                 setScriptFile(configResource.getDescription());
140                 initialise();
141                 Namespace ns = getScriptEngine().createNamespace();
142                 populateNamespace(ns);
143                 CompiledScript script = compileScript(configResource.getReader());
144                 script.eval(ns);
145             }
146
147             if (System.getProperty(MuleProperties.MULE_START_AFTER_CONFIG_SYSTEM_PROPERTY, "true")
148                 .equalsIgnoreCase("true"))
149             {
150                 if (!manager.isStarted())
151                 {
152                     manager.start();
153                 }
154             }
155
156         }
157         catch (Exception JavaDoc e)
158         {
159             throw new ConfigurationException(e);
160         }
161         return manager;
162     }
163
164     protected void populateNamespace(Namespace namespace)
165     {
166         namespace.put("manager", manager);
167         namespace.put("builder", builder);
168
169     }
170
171     public boolean isConfigured()
172     {
173         return manager != null;
174     }
175 }
176
Popular Tags