KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.nanocontainer.script;
10
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import org.nanocontainer.integrationkit.LifecycleContainerBuilder;
18 import org.picocontainer.MutablePicoContainer;
19 import org.picocontainer.PicoContainer;
20
21 /**
22  * Base abstract class for script-based container builders based.
23  *
24  * @author Aslak Hellesøy
25  * @author Obie Fernandez
26  * @author Mauro Talevi
27  * @version $Revision: 2164 $
28  */

29 public abstract class ScriptedContainerBuilder extends LifecycleContainerBuilder {
30     private final Reader JavaDoc scriptReader;
31     private final URL JavaDoc scriptURL;
32     private final ClassLoader JavaDoc classLoader;
33
34     public ScriptedContainerBuilder(Reader JavaDoc script, ClassLoader JavaDoc classLoader) {
35         this.scriptReader = script;
36         if (script == null) {
37             throw new NullPointerException JavaDoc("script");
38         }
39         this.scriptURL = null;
40         this.classLoader = classLoader;
41         if ( classLoader == null) {
42             throw new NullPointerException JavaDoc("classLoader");
43         }
44     }
45
46     public ScriptedContainerBuilder(URL JavaDoc script, ClassLoader JavaDoc classLoader) {
47         this.scriptReader = null;
48         this.scriptURL = script;
49         if (script == null) {
50             throw new NullPointerException JavaDoc("script");
51         }
52         this.classLoader = classLoader;
53         if ( classLoader == null) {
54             throw new NullPointerException JavaDoc("classLoader");
55         }
56     }
57
58     protected final PicoContainer createContainer(PicoContainer parentContainer, Object JavaDoc assemblyScope) {
59         try {
60             return createContainerFromScript(parentContainer, assemblyScope);
61         } finally {
62             try {
63                 Reader JavaDoc reader = getScriptReader();
64                 if (reader != null) {
65                     reader.close();
66                 }
67             } catch (IOException JavaDoc e) {
68                 // do nothing. we've given it our best try, now get on with it
69
}
70         }
71     }
72
73     protected final ClassLoader JavaDoc getClassLoader() {
74         return classLoader;
75     }
76     
77     protected final InputStream JavaDoc getScriptInputStream() throws IOException JavaDoc{
78         if ( scriptReader != null ){
79             return new InputStream JavaDoc() {
80                 public int read() throws IOException JavaDoc {
81                     return scriptReader.read();
82                 }
83             };
84         }
85         return scriptURL.openStream();
86     }
87
88     protected final Reader JavaDoc getScriptReader() throws IOException JavaDoc{
89         if ( scriptReader != null ){
90             return scriptReader;
91         }
92         return new InputStreamReader JavaDoc(scriptURL.openStream());
93     }
94     
95     // TODO: This should really return NanoContainer using a nano variable in the script. --Aslak
96
protected abstract PicoContainer createContainerFromScript(PicoContainer parentContainer, Object JavaDoc assemblyScope);
97
98     protected void composeContainer(MutablePicoContainer container, Object JavaDoc assemblyScope) {
99         // do nothing. assume that this is done in createContainer().
100
}
101 }
Popular Tags