KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > node > file > FileLinkNodeFactory


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.config.node.file;
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
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38
39 /**
40  * Factory used to create links to nodes on a file system
41  *
42  * Copyright 2002 Sapient
43  * @since carbon 1.0
44  * @author Douglas Voet, April 2002
45  * @version $Revision: 1.12 $($Author: dvoet $ / $Date: 2003/06/27 20:46:20 $)
46  */

47 public class FileLinkNodeFactory implements LinkNodeFactory {
48     /** The handle to Apache-commons logger */
49     private Log log = LogFactory.getLog(this.getClass());
50
51     /**
52      * Holds a reference to the subFolderFactory used.
53      * Since this is a FileLinkNodeFactory, a FileFolderFactory is used.
54      */

55     private static final NodeFactory subFolderFactory =
56         new FileFolderFactory();
57
58     /**
59      * Holds a reference to the configurationNodeFactory used.
60      * Since this is a FileLinkNodeFactory, a
61      * FileConfigurationDocumentFactory is used.
62      */

63     private static final NodeFactory configurationDocumentFactory =
64         new FileConfigurationDocumentFactory();
65
66     /**
67      * Holds a reference to the linkNodeFactory used.
68      * Since this is a FileLinkNodeFactory, a
69      * FileLinkNodeConfigurationDocumentFactory is used.
70      */

71     private static final NodeFactory linkNodeFactory =
72         new FileLinkNodeConfigurationDocumentFactory();
73
74     /**
75      * @see LinkNodeFactory#getInstance
76      */

77     public LinkNode getInstance(Node parent, String JavaDoc name,
78         ConfigurationDocument linkConfigurationDoc)
79         throws NodeCreationException {
80
81         File JavaDoc targetFile = new File JavaDoc(getTargetPath(linkConfigurationDoc));
82
83         if (!targetFile.isAbsolute()) {
84             if (parent != null) {
85                 try {
86                     targetFile = new File JavaDoc(
87                         ((FileFolder) parent).getInternalFileObject(),
88                         targetFile.getPath());
89                 } catch (ClassCastException JavaDoc cce) {
90                     // this means that the parent was not a FileFolder, but
91
// linkFile was not absolute so the file cannot be valid
92
// throw an exception to signify our displeasure
93
throw new NodeCreationException(
94                         this.getClass(),
95                         parent, name,
96                         "LinkToPath is a relative path but parent is not a "
97                             + "FileFolder: [" + targetFile.getPath() + "]");
98                 }
99             } else {
100                 if (this.log.isDebugEnabled()) {
101                     this.log.debug("LinkToPath is a relative path for root " +
102                         "node, current working directory is [" +
103                         System.getProperty("user.dir") +
104                         "]");
105                 }
106             }
107         }
108
109         if (!targetFile.exists()) {
110             throw new NodeCreationException(
111                 this.getClass(),
112                 parent,
113                 name,
114                 "Target of link does not exist [" + targetFile.getAbsolutePath() + "]");
115         }
116
117         if (targetFile.isDirectory()) {
118             return new FolderLinkImpl(
119                 linkConfigurationDoc,
120                 new FileFolder(parent, name,
121                     getSubFolderFactory(),
122                     getConfigurationDocumentFactory(),
123                     getLinkNodeFactory(),
124                     targetFile));
125         } else {
126             return new ConfigurationDocumentLinkImpl(
127                 linkConfigurationDoc,
128                 new FileConfigurationDocument(parent, name,
129                     new DefaultConfigurationFormatService(),
130                     targetFile));
131         }
132     }
133
134     /**
135      * Gets the factory used within this package to create folders
136      *
137      * @return NodeFactory
138      */

139     protected NodeFactory getSubFolderFactory() {
140         return FileLinkNodeFactory.subFolderFactory;
141     }
142
143     /**
144      * Gets the factory used within this package to create
145      * configuration documents
146      *
147      * @return NodeFactory
148      */

149     protected NodeFactory getConfigurationDocumentFactory() {
150         return FileLinkNodeFactory.configurationDocumentFactory;
151     }
152
153     /**
154      * Gets the factory used within this package to create links
155      *
156      * @return NodeFactory
157      */

158     protected NodeFactory getLinkNodeFactory() {
159         return FileLinkNodeFactory.linkNodeFactory;
160     }
161
162     /**
163      * Gets and validates the target path in config
164      *
165      * @param linkConfigurationDoc configuration document containing the link
166      * @return the target path contained in the link document
167      */

168     private String JavaDoc getTargetPath(ConfigurationDocument linkConfigurationDoc) {
169         FileLinkNodeConfiguration fileLinkConfiguration;
170
171         try {
172
173             fileLinkConfiguration = (FileLinkNodeConfiguration)
174                 linkConfigurationDoc.readConfiguration();
175
176             String JavaDoc targetPath = fileLinkConfiguration.getLinkToPath();
177
178             if (targetPath == null) {
179                 throw new InvalidConfigurationException(
180                     this.getClass(),
181                     linkConfigurationDoc.getName(),
182                     "LinkToPath");
183             }
184
185             return targetPath;
186
187         } catch (ClassCastException JavaDoc cce) {
188             throw new InvalidConfigurationException(
189                 this.getClass(),
190                 linkConfigurationDoc.getName(),
191                 "LinkNodeFactory",
192                 "LinkNodeFactory is not an instance of "
193                     + FileLinkNodeConfiguration.class.getName(),
194                 cce);
195         } catch (NodeIOException nioe) {
196             throw new InvalidConfigurationException(
197                 this.getClass(),
198                 linkConfigurationDoc.getName(), null,
199                 "Could not read link configuration document", nioe);
200         } catch (ConfigurationFormatException cfe) {
201             throw new InvalidConfigurationException(
202                 this.getClass(),
203                 linkConfigurationDoc.getName(), null,
204                 "Could not read link configuration document", cfe);
205         }
206     }
207 }
Popular Tags