KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > bootstrap > BootConfigurationDocument


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.bootstrap;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import org.sape.carbon.core.config.Configuration;
27 import org.sape.carbon.core.config.format.ConfigurationFormatException;
28 import org.sape.carbon.core.config.format.ConfigurationFormatService;
29 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService;
30 import org.sape.carbon.core.config.node.ConfigurationDocument;
31 import org.sape.carbon.core.config.node.Node;
32 import org.sape.carbon.core.config.node.NodeCreationException;
33 import org.sape.carbon.core.config.node.NodeFactory;
34 import org.sape.carbon.core.config.node.NodeIOException;
35 import org.sape.carbon.core.config.node.NodeNotFoundException;
36 import org.sape.carbon.core.config.node.NodeRemovalException;
37 import org.sape.carbon.core.config.node.event.NodeEventListener;
38 import org.sape.carbon.core.config.node.link.LinkNodeConfiguration;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43
44 /**
45  * <p>An implementation of <code>ConfigurationDocument</code> that provides
46  * functionality specific to retrieving the configuration required to boot
47  * the Carbon Core. This <code>ConfigurationDocument</code> is read-only.
48  * Calls to writeConfiguration or remove will throw an
49  * <code>UnsupportedOperationException</code>.</p>
50  *
51  * <p>
52  * package protected because this implementation need not be visible outside
53  * of bootstrap, it can be referenced by its interface.</p>
54  *
55  * Copyright 2002 Sapient
56  * @since carbon 1.0
57  * @author Douglas Voet, March 2002
58  * @version $Revision: 1.29 $($Author: dvoet $ / $Date: 2003/10/17 14:40:55 $)
59  */

60 class BootConfigurationDocument implements ConfigurationDocument {
61
62     /** The handle to Apache-commons logger */
63     private Log log = LogFactory.getLog(this.getClass());
64
65     /**
66      * Config property for the root link.
67      */

68     private static final String JavaDoc ROOT_LINK_PROPERTY_NAME =
69         "carbon.config.RootLink";
70
71     /**
72      * Contains a complete Xml Configuration file. Used as the default root
73      * link if none can be found.
74      */

75     private static final String JavaDoc DEFAULT_ROOT_LINK =
76         "<Configuration ConfigurationInterface=\"org.sape.carbon.core.config.node.file.FileLinkNodeConfiguration\"><LinkNodeFactoryClass>org.sape.carbon.core.config.node.file.FileLinkNodeFactory</LinkNodeFactoryClass><LinkToPath>{carbon.config.Path}</LinkToPath></Configuration>";
77
78     /**
79      * The name of the resource that contains the information for the link
80      * to the root of the configuration servie
81      */

82     public static final String JavaDoc ROOT_LINK_RESOURCE_NAME =
83         "CarbonConfigRoot.link";
84     /** The system property key used to define the name of the
85
86     /** internal configuration object */

87     private LinkNodeConfiguration config;
88
89     /** the default configuration formatter service. */
90     private ConfigurationFormatService formatter =
91         new DefaultConfigurationFormatService();
92
93     /**
94      * This constructor loads the <code>BootStrapperConfiguration</code>
95      * for the system. If the system property defined by
96      * <code>BOOT_CONFIG_PROPERTY</code> is specified on the command line,
97      * its value is used as a path to a file that contains the configuration
98      * information. If the system property is not specified, the first
99      * instance of the file named by <code>BOOT_CONFIG_FILE_NAME</code> found
100      * in the classpath is used.
101      * <p>
102      * package protected because only the bootstrap subsystem
103      * should instantiate it
104      */

105     BootConfigurationDocument() {
106
107         InputStream JavaDoc rootLinkConfigInputStream = null;
108
109         try {
110             rootLinkConfigInputStream = getRootLinkInputStream();
111
112             this.config =
113                 (LinkNodeConfiguration) this.formatter.readConfigurationStream(
114                     "",
115                     rootLinkConfigInputStream);
116
117         } catch (ConfigurationFormatException cfe) {
118             throw new BootStrapException(this.getClass(),
119                 "Exception reading configuration root link document",
120                 cfe);
121
122         } catch (ClassCastException JavaDoc cce) {
123             throw new BootStrapException(
124                 this.getClass(),
125                 "Configuration root link object must be of type "
126                     + LinkNodeConfiguration.class.getName(),
127                 cce);
128         } finally {
129             // close configRootLinkInputStream if it was openned
130
if (rootLinkConfigInputStream != null) {
131                 try {
132                     rootLinkConfigInputStream.close();
133                 } catch (IOException JavaDoc ioe) {
134                     // eat it, just wanted to make sure the stream is closed
135
}
136             }
137         }
138     }
139
140     /**
141      * Gets the configuration object loaded in the constructor
142      *
143      * @return the <code>Configuration</code> object representing the
144      * configurable information required by the bootstrap subsystem.
145      * @throws NodeIOException when there is an exception in dealing
146      * with the configuration backing store
147      * @throws ConfigurationFormatException when the configuration document
148      * to be read has an invalid format or inproper data
149      */

150     public Configuration readConfiguration()
151         throws NodeIOException, ConfigurationFormatException {
152
153         return this.config;
154     }
155
156     /**
157      * Not supported by this implementation
158      *
159      * @param config not supported
160      * @throws NodeIOException not supported
161      * @throws ConfigurationFormatException not supported
162      */

163     public void writeConfiguration(Configuration config)
164         throws NodeIOException, ConfigurationFormatException {
165
166         throw new UnsupportedOperationException JavaDoc(this.getClass().getName());
167     }
168
169     /**
170      * This implementation does not support children
171      *
172      * @return false always
173      */

174     public boolean getAllowsChildren() {
175         return false;
176     }
177
178     /**
179      * Not supported by this implementation
180      *
181      * @throws NodeRemovalException not supported
182      * @return not supported
183      */

184     public int remove() throws NodeRemovalException {
185         throw new UnsupportedOperationException JavaDoc(this.getClass().getName());
186     }
187
188     /**
189      * This implementation cannot be refreshed
190      *
191      * @see Node#refresh()
192      */

193     public void refresh() {
194         throw new UnsupportedOperationException JavaDoc(this.getClass().getName());
195     }
196
197     /**
198      * @see org.sape.carbon.core.config.node.ConfigurationDocument#addNestedConfigurationDocument
199      */

200     public ConfigurationDocument addNestedConfigurationDocument(
201         String JavaDoc name,
202         Configuration config)
203         throws NodeCreationException {
204         throw new UnsupportedOperationException JavaDoc(this.getClass().getName());
205     }
206
207     /**
208      * @see org.sape.carbon.core.config.node.Node#containsChild(String)
209      */

210     public boolean containsChild(String JavaDoc childName) {
211         return false;
212     }
213
214     /**
215      * @see org.sape.carbon.core.config.node.Node#fetchChild(String)
216      */

217     public Node fetchChild(String JavaDoc childName) throws NodeNotFoundException {
218         throw new NodeNotFoundException(this.getClass(), childName);
219     }
220
221     /**
222      * @see org.sape.carbon.core.config.node.Node#fetchChildren()
223      */

224     public Node[] fetchChildren() {
225         return new Node[0];
226     }
227
228     /**
229      * @see org.sape.carbon.core.config.node.Node#getAbsoluteName()
230      */

231     public String JavaDoc getAbsoluteName() {
232         return "";
233     }
234
235     /**
236      * @see org.sape.carbon.core.config.node.Node#getName()
237      */

238     public String JavaDoc getName() {
239         return "";
240     }
241
242     /**
243      * @see org.sape.carbon.core.config.node.Node#getParent()
244      */

245     public Node getParent() {
246         return null;
247     }
248
249     /**
250      * @see org.sape.carbon.core.config.node.Node#isRemoved()
251      */

252     public boolean isRemoved() {
253         return false;
254     }
255
256     /**
257      * @see org.sape.carbon.core.config.node.ConfigurationDocument#getFormatService()
258      */

259     public ConfigurationFormatService getFormatService() {
260         return this.formatter;
261     }
262
263     /**
264      * @see org.sape.carbon.core.config.node.Node#addNodeListener(NodeEventListener)
265      */

266     public void addNodeListener(NodeEventListener listener) {
267         // this node cannot change, so listeners will never be notified
268
}
269
270     /**
271      * Loads the configuration root link. The configuration root link
272      * is located in the following order
273      * <ol>
274      * <ul>
275      * Load the config root link specifed in the deployment property
276      * carbon.config.RootLink
277      * </ul>
278      * <ul>
279      * Load the config root link CarbonConfigRoot.link from the classloader
280      * </ul>
281      * <ul>
282      * if everything fails load the deault config root link specified
283      * in this document
284      * </ul>
285      * </ol>
286      *
287      * @return input stream to the root link document
288      */

289     private InputStream JavaDoc getRootLinkInputStream() {
290
291         InputStream JavaDoc rootLinkInputStream = null;
292
293         //1. get config root link from deployment property carbon.
294
// config.RootLink
295
try {
296             if (log.isDebugEnabled()) {
297                 log.debug("Attempting to load configuration root link "
298                     + "by using deployment property "
299                     + ROOT_LINK_PROPERTY_NAME);
300             }
301
302             String JavaDoc rootLinkFileName =
303                 BootStrapper.getInstance().getDeploymentProperty(
304                     ROOT_LINK_PROPERTY_NAME);
305
306             if (rootLinkFileName != null) {
307                 // file name exists in deployment properties,
308
// get link from there
309
if (log.isInfoEnabled()) {
310                     log.info("Using file to load configuration root link"
311                         + ": file name ["
312                         + rootLinkFileName
313                         + "]");
314                 }
315
316                 rootLinkInputStream = new FileInputStream JavaDoc(rootLinkFileName);
317             }
318         } catch (IOException JavaDoc ioe) {
319             if (log.isInfoEnabled()) {
320                 log.info("Attempted to get configuration root link from file "
321                     + "but caught IOException", ioe);
322             }
323
324             // ensure that rootLinkInputStream is null and
325
// continue to other options
326
rootLinkInputStream = null;
327         }
328
329         //2. get config root link by loading ROOT_LINK_RESOURCE_NAME
330
// from classloader
331
try {
332             if (rootLinkInputStream == null) {
333                 if (log.isDebugEnabled()) {
334                     log.debug("Attempting to load configuration root "
335                         + "link resource ["
336                         + ROOT_LINK_RESOURCE_NAME
337                         + "] from the class loader ");
338                 }
339
340                 // get from ClassLoader
341
// This will always load the file from the same Classpath,
342
// using the same ClassLoader as was used to load this class.
343
ClassLoader JavaDoc classLoader = this.getClass().getClassLoader();
344                 URL JavaDoc rootLinkURL =
345                     classLoader.getResource(ROOT_LINK_RESOURCE_NAME);
346
347                 if (rootLinkURL != null) {
348                     if (log.isInfoEnabled()) {
349                        log.info("Using ClassLoader to load configuration "
350                             + "root link: resource ["
351                             + rootLinkURL.getPath()
352                             + "]");
353                     }
354
355                     rootLinkInputStream = rootLinkURL.openStream();
356                 }
357             }
358         } catch (IOException JavaDoc ioe) {
359             if (log.isInfoEnabled()) {
360                 log.info("Attempted to get configuration root link "
361                     + "from ClassLoader but caught IOException", ioe);
362             }
363
364             // ensure that rootLinkInputStream is null and
365
// continue to other options
366
rootLinkInputStream = null;
367         }
368
369         //3. finally if nothing works, load the default config
370
// root link specified in the boot configuration document
371
if (rootLinkInputStream == null) {
372             if (log.isInfoEnabled()) {
373                 log.info("Configuration root link wasnt located, "
374                     + "Using default config root link");
375             }
376
377             // ensure that rootLinkInputStream is null and
378
// continue to other options
379
rootLinkInputStream =
380                 new ByteArrayInputStream JavaDoc(DEFAULT_ROOT_LINK.getBytes());
381         }
382
383         return rootLinkInputStream;
384     }
385
386     /**
387      * @see org.sape.carbon.core.config.node.ConfigurationDocument#getNestedNodeFactory()
388      */

389     public NodeFactory getNestedNodeFactory() {
390         return null;
391     }
392
393 }
Popular Tags