KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > ScriptBuilderResolver


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  *****************************************************************************/

9
10 package org.nanocontainer.script;
11
12 import java.io.File JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 /**
18  * ScriptBuilderResolver handles the task of resolving a file name to a builder
19  * name. Typical default resolution is for Groovy, BeanShell, JavaScript,
20  * Jython, and XML script names. However, you can register/replace your
21  * own builder implementations by using the registerBuilder() function.
22  * @author Michael Rimov
23  */

24 public class ScriptBuilderResolver {
25
26     public static final String JavaDoc GROOVY = ".groovy";
27     public static final String JavaDoc BEANSHELL = ".bsh";
28     public static final String JavaDoc JAVASCRIPT = ".js";
29     public static final String JavaDoc JYTHON = ".py";
30     public static final String JavaDoc XML = ".xml";
31
32     public static final String JavaDoc DEFAULT_GROOVY_BUILDER = "org.nanocontainer.script.groovy.GroovyContainerBuilder";
33     public static final String JavaDoc DEFAULT_BEANSHELL_BUILDER = "org.nanocontainer.script.bsh.BeanShellContainerBuilder";
34     public static final String JavaDoc DEFAULT_JAVASCRIPT_BUILDER = "org.nanocontainer.script.rhino.JavascriptContainerBuilder";
35     public static final String JavaDoc DEFAULT_XML_BUILDER = "org.nanocontainer.script.xml.XMLContainerBuilder";
36     public static final String JavaDoc DEFAULT_JYTHON_BUILDER = "org.nanocontainer.script.jython.JythonContainerBuilder";
37
38     private final Map JavaDoc extensionToBuilders = new HashMap JavaDoc();
39
40
41
42     public ScriptBuilderResolver() {
43         resetBuilders();
44     }
45
46
47     /**
48      * Retrieve the classname of the appropriate ScriptedContainerBuilder given the file.
49      * @param compositionFile File
50      * @return String
51      */

52     public String JavaDoc getBuilderClassName(File JavaDoc compositionFile) {
53         String JavaDoc language = getExtension(compositionFile.getAbsolutePath());
54         return getBuilderClassName(language);
55     }
56
57
58
59
60     /**
61      * Retrieve the classname of the appropriate ScriptedContainerBuilder given the file.
62      * @param compositionFile File
63      * @return String
64      */

65     public String JavaDoc getBuilderClassName(URL JavaDoc compositionURL) {
66         String JavaDoc language = getExtension(compositionURL.getFile());
67         return getBuilderClassName(language);
68     }
69
70     /**
71      * Retrieve the classname of the builder to use given the provided
72      * extension. Example:
73      * <code><pre>
74      * ScriptedContainerBuilderFactory factory = new ScriptedContainerBuilderFactory(.....);
75      * String groovyBuilderName = factory.getBuilderClassName(&quot;.groovy&quot;);
76      * assert &quot;org.nanocontainer.script.groovy.GroovyContainerBuilder&quot;.equals(groovyBuilderName);
77      * </pre></code>
78      * @param extension String
79      * @return String
80      */

81     public synchronized String JavaDoc getBuilderClassName(final String JavaDoc extension) throws UnsupportedScriptTypeException{
82         String JavaDoc resultingBuilderClassName = null;
83         resultingBuilderClassName = (String JavaDoc) extensionToBuilders.get(extension);
84         if (resultingBuilderClassName == null) {
85             throw new UnsupportedScriptTypeException(extension, this.getAllSupportedExtensions());
86         }
87         return resultingBuilderClassName;
88     }
89
90     /**
91      * Function to allow the resetting of the builder map to defaults. Allows
92      * testing of the static resource a bit better.
93      */

94     public synchronized void resetBuilders() {
95         extensionToBuilders.clear();
96
97         //This is a bit clunky compared to just registering the items
98
//directly into the map, but this way IMO it provides a single access
99
//point into the extensionToBuilders map.
100
registerBuilder(GROOVY, DEFAULT_GROOVY_BUILDER);
101         registerBuilder(BEANSHELL, DEFAULT_BEANSHELL_BUILDER);
102         registerBuilder(JAVASCRIPT, DEFAULT_JAVASCRIPT_BUILDER);
103         registerBuilder(XML, DEFAULT_XML_BUILDER);
104         registerBuilder(JYTHON, DEFAULT_JYTHON_BUILDER);
105
106     }
107
108     /**
109      * Registers/replaces a new handler for a given extension. Allows for customizable
110      * behavior in the various builders or the possibility to dynamically add
111      * handlers for new file types. Example:
112      * <code><pre>
113      * ScriptedContainerBuilderFactory factory = new ScriptedContainerBuilderFactory(...)
114      * factory.registerBuilder(&quot;.groovy&quot;, &quot;org.nanocontainer.script.groovy.GroovyContainerBuilder&quot;);
115      * ScriptedContainerBuilder builder = factory.getContainerBuilder();
116      * assertNotNull(builder);
117      * </pre></code>
118      * <p>The internal code now requires synchronization of the builder extension map since
119      * who knows what is using it when a new builder is registered.</p>
120      * @param extension String the extension to register under.
121      * @param className String the classname to use for the given extension.
122      */

123     public synchronized void registerBuilder(final String JavaDoc extension, final String JavaDoc className) {
124         extensionToBuilders.put(extension, className);
125     }
126
127     /**
128      * Retrieve a list of all supported extensions.
129      * @return String[] of extensions including the period in the name.
130      */

131     public synchronized String JavaDoc[] getAllSupportedExtensions() {
132          return (String JavaDoc[]) extensionToBuilders.keySet().toArray(new String JavaDoc[extensionToBuilders.size()]);
133     }
134
135
136     /**
137      * Retrieve the extension of the file name.
138      * @param fileName String
139      * @return String
140      */

141     private String JavaDoc getExtension(String JavaDoc fileName) {
142         return fileName.substring(fileName.lastIndexOf("."));
143     }
144
145
146 }
147
Popular Tags