KickJava   Java API By Example, From Geeks To Geeks.

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


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.URLClassLoader JavaDoc;
21
22 import org.sape.carbon.core.config.InvalidConfigurationException;
23 import org.sape.carbon.core.config.format.ConfigurationFormatException;
24 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService;
25 import org.sape.carbon.core.config.node.ConfigurationDocument;
26 import org.sape.carbon.core.config.node.Node;
27 import org.sape.carbon.core.config.node.NodeCreationException;
28 import org.sape.carbon.core.config.node.NodeFactory;
29 import org.sape.carbon.core.config.node.NodeIOException;
30 import org.sape.carbon.core.config.node.file.FileNodeFilter;
31 import org.sape.carbon.core.config.node.link.ConfigurationDocumentLinkImpl;
32 import org.sape.carbon.core.config.node.link.FolderLinkImpl;
33 import org.sape.carbon.core.config.node.link.LinkNode;
34 import org.sape.carbon.core.config.node.link.LinkNodeFactory;
35
36 /**
37  * Factory that creates links to ClassLoader based configuration node
38  * hierarchies.
39  *
40  * Copyright 2003 Sapient
41  * @since carbon 2.0
42  * @author Greg Hinkle, April 2003
43  * @version $Revision: 1.5 $($Author: dvoet $ / $Date: 2003/11/11 21:19:58 $)
44  */

45 public class ClassloaderLinkNodeFactory implements LinkNodeFactory {
46
47     private static final NodeFactory subFolderFactory =
48         new ClassloaderFolderFactory();
49     private static final NodeFactory configurationDocumentFactory =
50         new ClassloaderConfigurationDocumentFactory();
51     private static final NodeFactory linkNodeFactory =
52         new ClassloaderLinkNodeConfigurationDocumentFactory();
53
54     /**
55      * @inherit
56      */

57     public LinkNode getInstance(Node parent, String JavaDoc name,
58         ConfigurationDocument linkConfigurationDoc)
59         throws NodeCreationException {
60
61         try {
62             ClassloaderLinkNodeConfiguration linkConfiguration =
63                 (ClassloaderLinkNodeConfiguration)
64                 linkConfigurationDoc.readConfiguration();
65
66             String JavaDoc prefix = linkConfiguration.getPathPrefix();
67             ConfigurationSource configSource;
68             if (linkConfiguration.getClassloaderURLs() == null ||
69                 linkConfiguration.getClassloaderURLs().length == 0) {
70                     
71                 configSource = new ConfigurationSource();
72                 
73             } else {
74                 
75                 configSource =
76                     new ConfigurationSource(linkConfiguration.getClassloaderURLs());
77             }
78
79             if (prefix.endsWith(FileNodeFilter.CONFIG_DOC_NODE_EXTENSION) ||
80                 prefix.endsWith(FileNodeFilter.LINK_NODE_EXTENSION)) {
81
82                 if (configSource.getResource(prefix) == null) {
83                     throw new InvalidConfigurationException(
84                         this.getClass(),
85                         linkConfigurationDoc.getName(),
86                         "PathPrefix",
87                         "PathPrefix denotes a document or link, but did not exist in the classloader");
88                 }
89
90                 ClassloaderConfigurationDocument target =
91                     new ClassloaderConfigurationDocument(
92                         parent,
93                         name,
94                         new DefaultConfigurationFormatService(),
95                         prefix,
96                         configSource);
97
98                 return new ClassloaderDocumentLinkImpl(
99                     linkConfigurationDoc,
100                     target,
101                     configSource);
102
103             } else {
104                 if (!prefix.endsWith(ClassloaderFolder.CLASSLOADER_DELIMETER)) {
105                     prefix = prefix + ClassloaderFolder.CLASSLOADER_DELIMETER;
106                 }
107
108                 ClassloaderFolder target =
109                     new ClassloaderFolder(
110                         parent,
111                         name,
112                         getSubFolderFactory(),
113                         getConfigurationDocumentFactory(),
114                         getLinkNodeFactory(),
115                         prefix,
116                         configSource);
117
118                 return new ClassloaderFolderLinkImpl(
119                     linkConfigurationDoc,
120                     target,
121                     configSource);
122             }
123         } catch(ClassCastException JavaDoc cce) {
124             throw new InvalidConfigurationException(
125                 this.getClass(),
126                 linkConfigurationDoc.getName(),
127                 "LinkNodeFactory",
128                 "LinkNodeFactory is not an instance of " +
129                 ClassloaderLinkNodeConfiguration.class.getName(), cce);
130
131         } catch(NodeIOException nioe) {
132             throw new InvalidConfigurationException(
133                 this.getClass(),
134                 linkConfigurationDoc.getName(), null,
135                 "Could not read link configuration document", nioe);
136
137         } catch(ConfigurationFormatException cfe) {
138             throw new InvalidConfigurationException(
139                 this.getClass(),
140                 linkConfigurationDoc.getName(), null,
141                 "Could not read link configuration document", cfe);
142         }
143     }
144
145     /**
146      * Gets the factory used within this package to create folders
147      *
148      * @return NodeFactory
149      */

150     protected NodeFactory getSubFolderFactory() {
151         return ClassloaderLinkNodeFactory.subFolderFactory;
152     }
153
154     /**
155      * Gets the factory used within this package to create
156      * configuration documents
157      *
158      * @return NodeFactory
159      */

160     protected NodeFactory getConfigurationDocumentFactory() {
161         return ClassloaderLinkNodeFactory.configurationDocumentFactory;
162     }
163
164     /**
165      * Gets the factory used within this package to create links
166      *
167      * @return NodeFactory
168      */

169     protected NodeFactory getLinkNodeFactory() {
170         return ClassloaderLinkNodeFactory.linkNodeFactory;
171     }
172
173 }
Popular Tags