KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > config > jar > JarLinkNodeFactory


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.jar;
19
20 import java.io.File 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.link.ConfigurationDocumentLinkImpl;
31 import org.sape.carbon.core.config.node.link.FolderLinkImpl;
32 import org.sape.carbon.core.config.node.link.LinkNode;
33 import org.sape.carbon.core.config.node.link.LinkNodeFactory;
34 import org.sape.carbon.core.util.jar.EnhancedJarFile;
35 import org.sape.carbon.core.util.thread.ReadWriteLock;
36 import org.sape.carbon.core.util.thread.WriterPreferenceReadWriteLock;
37
38 /**
39  *
40  * Copyright 2002 Sapient
41  * @since carbon 1.0
42  * @author Douglas Voet, April 2002
43  * @version $Revision: 1.2 $($Author: dvoet $ / $Date: 2003/05/05 21:21:09 $)
44  */

45 public class JarLinkNodeFactory implements LinkNodeFactory {
46
47     /** Holds a reference to a JarFolderFactory */
48     private static final NodeFactory subFolderFactory =
49         new JarFolderFactory();
50
51     /** Holds a reference to a JarConfigurationDocumentFactory */
52     private static final NodeFactory configurationDocumentFactory =
53         new JarConfigurationDocumentFactory();
54
55     /** Holds a reference to a JarLinkNodeConfigurationDocumentFactory */
56     private static final NodeFactory linkNodeFactory =
57         new JarLinkNodeConfigurationDocumentFactory();
58
59     /**
60      * @see LinkNodeFactory#getInstance
61      */

62     public LinkNode getInstance(Node parent, String JavaDoc name,
63         ConfigurationDocument linkConfigurationDoc)
64         throws NodeCreationException {
65
66         try {
67             JarLinkNodeConfiguration jarLinkConfiguration =
68                 (JarLinkNodeConfiguration)
69                     linkConfigurationDoc.readConfiguration();
70
71             File JavaDoc targetJarFile =
72                 new File JavaDoc(getTargetPath(jarLinkConfiguration));
73
74             if (!targetJarFile.exists() || targetJarFile.isDirectory()) {
75                 throw new NodeCreationException(
76                     this.getClass(),
77                     parent,
78                     name,
79                     "Target of link does not exist or is directory");
80             }
81
82             String JavaDoc targetWithinJar =
83                 jarLinkConfiguration.getTargetRootWithinJar();
84
85             ReadWriteLock readWriteLock;
86
87             if (parent instanceof JarFolder
88                 && ((JarFolder) parent).getInternalJarFile().
89                     equals(targetJarFile)) {
90
91                 if (targetWithinJar == null) {
92                     throw new InvalidConfigurationException(
93                         this.getClass(),
94                         linkConfigurationDoc.getName(),
95                         "TargetRootWithinJar",
96                         "Link is relative, but TargetRootWithinJar "
97                             + "is not specified");
98                 }
99
100                 JarFolder parentJar = (JarFolder) parent;
101                 readWriteLock = parentJar.getReadWriteLock();
102                 targetWithinJar =
103                     parentJar.getInternalJarEntryName() + targetWithinJar;
104
105             } else {
106                 // this is a new jar an gets its own monitor
107
readWriteLock = new WriterPreferenceReadWriteLock();
108             }
109
110             if (targetWithinJar == null
111                || targetWithinJar.equals("")
112                || targetWithinJar.endsWith(EnhancedJarFile.JAR_DELIMETER)) {
113
114                 JarFolder target = new JarFolder(parent, name,
115                     getSubFolderFactory(),
116                     getConfigurationDocumentFactory(),
117                     getLinkNodeFactory(),
118                     readWriteLock,
119                     targetJarFile,
120                     targetWithinJar);
121
122                 if (!target.backingDataExists()) {
123                     throw new NodeCreationException(
124                         this.getClass(),
125                         parent,
126                         name,
127                         "Entry name ["
128                             + targetWithinJar
129                             + "] does not exist in jar ["
130                             + targetJarFile.getAbsolutePath()
131                             + "]");
132                 }
133
134                 return new FolderLinkImpl(linkConfigurationDoc, target);
135
136             } else {
137                 JarConfigurationDocument target = new JarConfigurationDocument(
138                     parent,
139                     name,
140                     new DefaultConfigurationFormatService(),
141                     readWriteLock,
142                     targetJarFile,
143                     targetWithinJar);
144
145                 if (!target.backingDataExists()) {
146                     throw new NodeCreationException(
147                         this.getClass(),
148                         parent,
149                         name,
150                         "Entry name ["
151                             + targetWithinJar
152                             + "] does not exist in jar ["
153                             + targetJarFile.getAbsolutePath()
154                             + "]");
155                 }
156
157                 return new ConfigurationDocumentLinkImpl(
158                     linkConfigurationDoc,
159                     target);
160             }
161
162         } catch (ClassCastException JavaDoc cce) {
163             throw new InvalidConfigurationException(
164                 this.getClass(),
165                 linkConfigurationDoc.getName(),
166                 "LinkNodeFactory",
167                 "LinkNodeFactory is not an instance of "
168                     + JarLinkNodeConfiguration.class.getName(), cce);
169
170         } catch (NodeIOException nioe) {
171             throw new InvalidConfigurationException(
172                 this.getClass(),
173                 linkConfigurationDoc.getName(), null,
174                 "Could not read link configuration document", nioe);
175
176         } catch (ConfigurationFormatException cfe) {
177             throw new InvalidConfigurationException(
178                 this.getClass(),
179                 linkConfigurationDoc.getName(), null,
180                 "Could not read link configuration document", cfe);
181         }
182     }
183
184     /**
185      * Gets the factory used within this package to create folders
186      *
187      * @return NodeFactory
188      */

189     protected NodeFactory getSubFolderFactory() {
190         return JarLinkNodeFactory.subFolderFactory;
191     }
192
193     /**
194      * Gets the factory used within this package to create
195      * configuration documents
196      *
197      * @return NodeFactory
198      */

199     protected NodeFactory getConfigurationDocumentFactory() {
200         return JarLinkNodeFactory.configurationDocumentFactory;
201     }
202
203     /**
204      * Gets the factory used within this package to create links
205      *
206      * @return NodeFactory
207      */

208     protected NodeFactory getLinkNodeFactory() {
209         return JarLinkNodeFactory.linkNodeFactory;
210     }
211
212     /**
213      * Gets and validates the target jar path from configuration
214      *
215      * @param jarLinkConfiguration the link config to pull the target
216      * jar path from
217      * @return String the target jar path from configuration
218      */

219     private String JavaDoc getTargetPath(
220         JarLinkNodeConfiguration jarLinkConfiguration) {
221
222         String JavaDoc targetJarPath = jarLinkConfiguration.getTargetJarPath();
223         if (targetJarPath == null) {
224             throw new InvalidConfigurationException(
225                 this.getClass(),
226                 jarLinkConfiguration.getConfigurationName(),
227                 "TargetJarPath");
228         }
229
230         return targetJarPath;
231     }
232 }
Popular Tags