KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.sape.carbon.core.config.InvalidConfigurationException;
27 import org.sape.carbon.core.config.format.ConfigurationFormatException;
28 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService;
29 import org.sape.carbon.core.config.node.AbstractFolder;
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.file.FileNodeFilter;
38 import org.sape.carbon.core.config.node.link.LinkNodeConfiguration;
39 import org.sape.carbon.core.config.node.link.LinkNodeFactory;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 /**
45  * Implementation of folder node for folders residing in classloader. As
46  * classloaders have no notion of folders, these nodes are not actually
47  * backed by information contained in the classloader.
48  *
49  * Copyright 2003 Sapient
50  * @since carbon 2.0
51  * @author Greg Hinkle, April 2003
52  * @version $Revision: 1.7 $($Author: dvoet $ / $Date: 2003/11/11 21:19:58 $)
53  */

54 public class ClassloaderFolder extends AbstractFolder {
55
56     /**
57      * Provides a handle to Apache-commons logger
58      */

59     private Log log = LogFactory.getLog(this.getClass());
60
61     /** the full resource path of this folder */
62     private String JavaDoc resourcePath;
63
64     /**
65      * The filter used to obtain valid child entries that may be represented
66      * by <code>JarNodes</code> from the jarFile.
67      */

68     private FileNodeFilter fileFilter = new FileNodeFilter();
69     
70     private ConfigurationSource configSource;
71
72     /** string that delimits entry names in the jar */
73     public static final String JavaDoc CLASSLOADER_DELIMETER = "/";
74
75     private static final Collection JavaDoc EXCLUDE_NODE_NAMES;
76     private static final String JavaDoc META_INF_DIR = "META-INF";
77     private static final String JavaDoc CVS_DIR = "CVS";
78
79     static {
80         EXCLUDE_NODE_NAMES = new ArrayList JavaDoc();
81         EXCLUDE_NODE_NAMES.add(META_INF_DIR);
82         EXCLUDE_NODE_NAMES.add(CVS_DIR);
83     }
84
85     /**
86      * Constructor for JarFolder.
87      * @param parent the parent of this folder
88      * @param name the name of this folder
89      * @param subFolderFactory the factory for creating sub folders
90      * @param configurationDocumentFactory the factory for creating documents
91      * @param linkNodeFactory the factory for creating sub links
92      * @param resourcePath
93      */

94     public ClassloaderFolder(
95         Node parent,
96         String JavaDoc name,
97         NodeFactory subFolderFactory,
98         NodeFactory configurationDocumentFactory,
99         NodeFactory linkNodeFactory,
100         String JavaDoc resourcePath,
101         ConfigurationSource configSource) {
102
103         super(
104             parent,
105             name,
106             subFolderFactory,
107             configurationDocumentFactory,
108             linkNodeFactory);
109
110         this.resourcePath = resourcePath;
111         this.configSource = configSource;
112     }
113
114     /**
115      * This is not synchronized because it is called from
116      * AbstractFolder.fetchChild which is synchronized
117      *
118      * @see AbstractFolder#loadChild(String)
119      */

120     protected Node loadChild(String JavaDoc nodeName)
121         throws NodeNotFoundException {
122
123         String JavaDoc childResourcePath = this.resourcePath + nodeName;
124
125         try {
126             Node child = null;
127
128             // skip excluded node names
129
if (!ClassloaderFolder.EXCLUDE_NODE_NAMES.contains(nodeName.toUpperCase())) {
130
131                 log.trace(
132                     "Classloader retrieving child [" +
133                     childResourcePath + this.fileFilter.CONFIG_DOC_NODE_EXTENSION + "]");
134
135                 URL JavaDoc childDoc =
136                     this.configSource.getResource(
137                         childResourcePath + this.fileFilter.CONFIG_DOC_NODE_EXTENSION);
138
139                 // if the child exists, has the config doc ext, it is a config doc node
140
if (childDoc != null) {
141                     child = loadConfigurationDocument(nodeName);
142                 }
143
144
145                 URL JavaDoc childLink =
146                     this.configSource.getResource(
147                         childResourcePath + this.fileFilter.LINK_NODE_EXTENSION);
148
149                 // if the child exists, has the link ext , it is a link node
150
if (childLink != null) {
151                     // the child is a link
152
child = loadChildLinkNode(nodeName);
153                 }
154             }
155
156             if (child == null) {
157                 child = new ClassloaderFolder(
158                     this,
159                     nodeName,
160                     getSubFolderFactory(),
161                     getConfigurationDocumentFactory(),
162                     getLinkNodeFactory(),
163                     childResourcePath + "/",
164                     this.configSource);
165             }
166
167             return child;
168
169         } catch (NodeCreationException nce) {
170             throw new NodeNotFoundException(
171                 this.getClass(),
172                 nodeName,
173                 nce);
174         }
175     }
176
177     /**
178      * There is no way to traverse the resources in the classloader to get
179      * all the children, so this implementation returns no children
180      *
181      * @see AbstractFolder#getAllChildNames()
182      */

183     protected Set JavaDoc getAllChildNames() {
184         return new HashSet JavaDoc();
185     }
186
187     /**
188      * This implementation does nothing as
189      * classloaders have no concept of folders, so there is nothing to remove
190      *
191      * @see org.sape.carbon.core.config.node.AbstractNode#destroyBackingData()
192      */

193     protected void destroyBackingData() throws NodeRemovalException {
194     }
195
196     /**
197      * As backing data never exists for classloader folders, the very existance
198      * of an instance of this class means the folder exists
199      *
200      * @return true
201      */

202     protected boolean backingDataExists() {
203         return true;
204     }
205
206     /**
207      * Returns the entry name of this node. Called by
208      * factories to preprend to resourcePaths of child nodes
209      *
210      * @return String
211      */

212     String JavaDoc getResourcePath() {
213         return this.resourcePath;
214     }
215     
216     ConfigurationSource getConfigSource() {
217         return this.configSource;
218     }
219
220     /**
221      * Helper method used to create sub folders
222      *
223      * @param nodeName
224      * @return Node
225      * @throws NodeCreationException
226      */

227     private Node loadSubFolder(String JavaDoc nodeName)
228         throws NodeCreationException {
229         return getSubFolderFactory().getInstance(this, nodeName);
230     }
231
232     /**
233      * Helper method used to create sub config docs
234      *
235      * @param nodeName
236      * @return Node
237      * @throws NodeCreationException
238      */

239     private Node loadConfigurationDocument(String JavaDoc nodeName)
240         throws NodeCreationException {
241
242         return getConfigurationDocumentFactory().getInstance(this, nodeName);
243     }
244
245     /**
246      * Helper method used to create sub links
247      *
248      * @param nodeName
249      * @return Node
250      * @throws NodeCreationException
251      */

252     private Node loadChildLinkNode(String JavaDoc nodeName)
253         throws NodeCreationException {
254
255         try {
256
257             ConfigurationDocument linkConfigurationDoc =
258                 new ClassloaderConfigurationDocument(
259                     this,
260                     nodeName,
261                     new DefaultConfigurationFormatService(),
262                     this.resourcePath + nodeName + this.fileFilter.LINK_NODE_EXTENSION,
263                     this.configSource);
264
265             LinkNodeConfiguration config =
266                 (LinkNodeConfiguration) linkConfigurationDoc.readConfiguration();
267
268             if (config.getLinkNodeFactoryClass() == null) {
269                 throw new InvalidConfigurationException(
270                     this.getClass(),
271                     config.getConfigurationName(),
272                     "LinkNodeFactoryClass",
273                     "Cannot be null");
274             }
275
276             LinkNodeFactory factory =
277                 (LinkNodeFactory) config.getLinkNodeFactoryClass().newInstance();
278
279             return factory.getInstance(this, nodeName, linkConfigurationDoc);
280
281         } catch (ConfigurationFormatException e) {
282             throw new NodeCreationException(
283                 this.getClass(),
284                 this,
285                 nodeName,
286                 "exception occured",
287                 e);
288         } catch (NodeIOException e) {
289             throw new NodeCreationException(
290                 this.getClass(),
291                 this,
292                 nodeName,
293                 "exception occured",
294                 e);
295         } catch (InstantiationException JavaDoc e) {
296             throw new NodeCreationException(
297                 this.getClass(),
298                 this,
299                 nodeName,
300                 "exception occured",
301                 e);
302         } catch (IllegalAccessException JavaDoc e) {
303             throw new NodeCreationException(
304                 this.getClass(),
305                 this,
306                 nodeName,
307                 "exception occured",
308                 e);
309         }
310     }
311 }
Popular Tags