KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > config > classloader > ClassloaderConfigurationDocument


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.services.config.classloader;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import org.sape.carbon.core.config.Configuration;
25 import org.sape.carbon.core.config.format.ConfigurationFormatService;
26 import org.sape.carbon.core.config.node.AbstractConfigurationDocument;
27 import org.sape.carbon.core.config.node.Node;
28 import org.sape.carbon.core.config.node.NodeRemovalException;
29 import org.sape.carbon.core.config.node.NodeRemovedException;
30 import org.sape.carbon.core.exception.ExceptionUtility;
31 import org.sape.carbon.core.util.string.StringUtil;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * <p>Implementation of ConfigurationDocument using streams found from a
38  * classloader.</p>
39  *
40  * Copyright 2003 Sapient
41  * @since carbon 2.0
42  * @author Greg Hinkle, April 2003
43  * @version $Revision: 1.6 $($Author: dvoet $ / $Date: 2003/11/11 21:19:58 $)
44  */

45 public class ClassloaderConfigurationDocument
46     extends AbstractConfigurationDocument {
47
48     /**
49      * Provides a handle to Apache-commons logger
50      */

51     private Log log = LogFactory.getLog(this.getClass());
52
53     /**
54      * The path of the resource that contains this configuration.
55      * final as this should never change during the life of the instance, if
56      * it does, more synchronization may be required.
57      */

58     private final String JavaDoc resourcePath;
59     
60     /**
61      * The source for configuration data.
62      * final as this should never change during the life of the instance, if
63      * it does, more synchronization may be required.
64      */

65     private final ConfigurationSource configSource;
66     
67     /**
68      * Keeps track of whether or not this node was created in memory only
69      * or it is actually on the backing data store.
70      * final as this should never change during the life of the instance, if
71      * it does, more synchronization may be required.
72      */

73     private final boolean isInMemoryOnly;
74
75     /**
76      * Constructor for ClassloaderConfigurationDocument.
77      * @param parent
78      * @param name
79      * @param formatter
80      * @param resourcePath
81      */

82     public ClassloaderConfigurationDocument(
83         Node parent,
84         String JavaDoc name,
85         ConfigurationFormatService formatter,
86         String JavaDoc resourcePath,
87         ConfigurationSource configSource) {
88
89         super(parent, name, formatter);
90
91         this.resourcePath = resourcePath;
92         this.configSource = configSource;
93         
94         this.isInMemoryOnly = configSource.getResource(resourcePath) == null;
95     }
96
97     /**
98      * This implementation does not write through to the backing datastore
99      * as Classloader resources are read-only. The in-memory copy of the
100      * configuration is replaced with a clone of the config parameter
101      * <p>
102      * This method is synchronized internally to prevent multiple writes from
103      * saving <code>Configuration</code>s concurrently and to make sure that
104      * no one is writing a new <code>Configuration</code> while someone else
105      * is reading.
106      *
107      * @param config the new <code>Configuration</code>
108      */

109     public void writeConfiguration(Configuration config) {
110
111         synchronized (getReadOrAlterNodeLock()) {
112             if (isRemoved()) {
113                 throw new NodeRemovedException(
114                     this.getClass(),
115                     this);
116             }
117
118             super.configuration = (Configuration) config.clone();
119             super.configuration.setConfigurationName(getAbsoluteName());
120         }
121
122         issueNodeChangedEvent();
123         notifyNestedDocuments();
124     }
125
126     /**
127      * This implementation opens an input stream of the resource
128      * itentified by this.resourcePath by calling
129      * classloader.getResourceAsStream(this.resourcePath)
130      */

131     protected InputStream JavaDoc openInputStream() throws IOException JavaDoc {
132
133         InputStream JavaDoc stream = this.configSource.getResourceAsStream(this.resourcePath);
134         
135         if (stream == null) {
136             try {
137                 stream = this.configSource.getResource(this.resourcePath).openStream();
138             } catch (Exception JavaDoc e) {
139                 System.out.println(ExceptionUtility.printStackTracesToString(e));
140             }
141         }
142
143         return stream;
144     }
145
146
147     /**
148      * This implementation cannot write configurations to a backing data store.
149      * writeConfiguration has been overridden in this class so as not to
150      * call this method.
151      * @throws UnsupportedOperationException
152      */

153     protected OutputStream JavaDoc openOutputStream() throws IOException JavaDoc {
154         throw new UnsupportedOperationException JavaDoc(
155             "Implemented such that this should never be called");
156     }
157
158     /**
159      * This implementation cannot write configurations to a backing data store.
160      * writeConfiguration has been overridden in this class so as not to
161      * call this method.
162      * @throws UnsupportedOperationException
163      */

164     protected void closeOutputStream(OutputStream JavaDoc out)
165         throws IOException JavaDoc {
166         throw new UnsupportedOperationException JavaDoc(
167             "Implemented such that this should never be called");
168     }
169
170     /**
171      * This is only called in the case of in-memory-only configurations.
172      * If this node is backed by a classloader resource, the remove method
173      * should throw an exception.
174      */

175     protected void destroyBackingData() throws NodeRemovalException {
176     }
177
178     /**
179      * If this node exists, then it was either loaded from a classloader
180      * resource or it is in-memory-only. In either case the backing data
181      * exists so this method always returns true.
182      */

183     protected boolean backingDataExists() {
184         if (this.isInMemoryOnly) {
185             return true;
186         } else {
187             return this.configSource.getResource(this.resourcePath) != null;
188         }
189     }
190
191     /**
192      * Only configurations that exist in-memory-only can be removed.
193      * @throws NodeRemovalException if this configuration was created from
194      * a classloader resource.
195      */

196     public int remove() throws NodeRemovalException {
197         if (this.isInMemoryOnly) {
198             // this is an in-memory only configuration
199
return super.remove();
200         } else {
201             // this configuration exists in the classloader and can't be removed
202
throw new NodeRemovalException(
203                 this.getClass(),
204                 this,
205                 "This node was created from a classloader resource and cannot be removed");
206         }
207     }
208
209     /**
210      * This implementation only refreshes the node if it is not in memory only
211      */

212     public void refresh() {
213         if (!this.isInMemoryOnly) {
214             super.refresh();
215         }
216     }
217
218 }
219
Popular Tags