KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.sape.carbon.core.config.InvalidConfigurationException;
25 import org.sape.carbon.core.config.format.ConfigurationFormatException;
26 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService;
27 import org.sape.carbon.core.config.node.AbstractFolder;
28 import org.sape.carbon.core.config.node.ConfigurationDocument;
29 import org.sape.carbon.core.config.node.Node;
30 import org.sape.carbon.core.config.node.NodeCreationException;
31 import org.sape.carbon.core.config.node.NodeFactory;
32 import org.sape.carbon.core.config.node.NodeIOException;
33 import org.sape.carbon.core.config.node.NodeNotFoundException;
34 import org.sape.carbon.core.config.node.NodeRemovalException;
35 import org.sape.carbon.core.config.node.link.LinkNodeConfiguration;
36 import org.sape.carbon.core.config.node.link.LinkNodeFactory;
37
38 /**
39  * A node that represents a physical directory within a file system.
40  * These nodes
41  * cache child nodes for quick reference, and thus synchronize removal
42  * and retrieval of child nodes.
43  *
44  * Copyright 2002 Sapient
45  * @see org.sape.carbon.core.config.node.file.FileConfigurationDocument
46  * @since carbon 1.0
47  * @author Mike Redd, February 2001
48  * @version $Revision: 1.27 $($Author: dvoet $ / $Date: 2003/07/29 18:52:33 $)
49  */

50 public class FileFolder extends AbstractFolder {
51
52
53     /**
54      * The filter used to obtain valid child files that may be represented
55      * by <code>FileNodes</code> from the file system.
56      */

57     private FileNodeFilter fileFilter = new FileNodeFilter();
58
59     /** Internal file object */
60     private File JavaDoc directory;
61
62
63     /**
64      * Constructor for FileFolder.
65      *
66      * @param parent parent node that contains this folder
67      * @param name name of this folder
68      * @param subFolderFactory factory to create sub-folders with
69      * @param configurationDocumentFactory factory to create
70      * configuration documents with
71      * @param linkNodeFactory factory to create link nodes with
72      * @param directory directory being represented by this folder
73      */

74     protected FileFolder(
75         Node parent,
76         String JavaDoc name,
77         NodeFactory subFolderFactory,
78         NodeFactory configurationDocumentFactory,
79         NodeFactory linkNodeFactory,
80         File JavaDoc directory) {
81
82         super(parent, name, subFolderFactory,
83             configurationDocumentFactory, linkNodeFactory);
84         this.directory = directory;
85     }
86
87     //////////////////////////////////////////////////////////////////
88
// Child Node Creation
89
//////////////////////////////////////////////////////////////////
90

91
92
93     /**
94      * @see AbstractFolder#getAllChildNames()
95      */

96     protected Set JavaDoc getAllChildNames() {
97         String JavaDoc[] childNames = this.directory.list(this.fileFilter);
98         Set JavaDoc childNameSet = new HashSet JavaDoc();
99         if (childNames != null) {
100             for (int i = 0; i < childNames.length; i++) {
101                 if (childNames[i].endsWith(
102                     this.fileFilter.CONFIG_DOC_NODE_EXTENSION)) {
103
104                     childNameSet.add(childNames[i].substring(0, childNames[i].
105                         lastIndexOf(
106                             this.fileFilter.CONFIG_DOC_NODE_EXTENSION)));
107
108                 } else if (childNames[i].endsWith(
109                     this.fileFilter.LINK_NODE_EXTENSION)) {
110
111                     childNameSet.add(childNames[i].substring(0, childNames[i].
112                         lastIndexOf(this.fileFilter.LINK_NODE_EXTENSION)));
113
114                 } else {
115                     childNameSet.add(childNames[i]);
116                 }
117             }
118         }
119         return childNameSet;
120     }
121
122     /**
123      * @see AbstractFolder#loadChild
124      */

125     protected Node loadChild(String JavaDoc nodeName) throws NodeNotFoundException {
126
127         try {
128             File JavaDoc childFile = new File JavaDoc(this.directory, nodeName);
129
130             if (childFile.exists() && childFile.isDirectory()) {
131                 return loadSubFolder(nodeName);
132             }
133
134             childFile = new File JavaDoc(this.directory, nodeName
135                 + this.fileFilter.CONFIG_DOC_NODE_EXTENSION);
136
137             if (childFile.exists() && !childFile.isDirectory()) {
138                 return loadConfigurationDocument(nodeName);
139             }
140
141             childFile = new File JavaDoc(this.directory, nodeName
142                 + this.fileFilter.LINK_NODE_EXTENSION);
143
144             if (childFile.exists() && !childFile.isDirectory()) {
145                 return loadChildLinkNode(nodeName);
146             }
147
148             throw new NodeNotFoundException(
149                 this.getClass(),
150                 this.getAbsoluteName() + Node.DELIMITER + nodeName);
151
152         } catch (NodeCreationException nce) {
153             throw new NodeNotFoundException(
154                 this.getClass(),
155                 nodeName,
156                 nce);
157         }
158     }
159
160     /**
161      * @see org.sape.carbon.core.config.node.AbstractNode#destroyBackingData()
162      */

163     protected void destroyBackingData() throws NodeRemovalException {
164         if (this.directory.exists()) {
165             if (!this.directory.delete()) {
166                 throw new NodeRemovalException(
167                     this.getClass(),
168                     this,
169                     "File.delete returned false");
170             }
171         } else {
172             throw new NodeRemovalException(
173                 this.getClass(),
174                 this,
175                 "The directory does not exist");
176         }
177     }
178
179     /**
180      * Accessor method for this.directory. Package private, for use only
181      * by FileFolderFactory.
182      *
183      * @return File internal File object representing the directory on the
184      * file system.
185      */

186     File JavaDoc getInternalFileObject() {
187         return this.directory;
188     }
189
190     /**
191      * Helper method to create a sub folder
192      *
193      * @param nodeName name of node containing the sub folder
194      * @return Node node element representing the sub-folder
195      * @throws NodeCreationException indicates an error loading the sub folder
196      */

197     private Node loadSubFolder(String JavaDoc nodeName) throws NodeCreationException {
198         return getSubFolderFactory().getInstance(this, nodeName);
199     }
200
201     /**
202      * Helper method to create a configuration document
203      *
204      * @param nodeName name of node to load the document for
205      * @return Node the instance of the loaded node
206      * @throws NodeCreationException indicates an error loading the node
207      */

208     private Node loadConfigurationDocument(String JavaDoc nodeName)
209         throws NodeCreationException {
210
211         return getConfigurationDocumentFactory().getInstance(this, nodeName);
212     }
213
214     /**
215      * Helper method to create a sub link
216      *
217      * @param nodeName name of sub link to create
218      * @return Node the node for the sublink
219      * @throws NodeCreationException indicates an error loading the child
220      */

221     private Node loadChildLinkNode(String JavaDoc nodeName)
222         throws NodeCreationException {
223
224         try {
225
226             ConfigurationDocument linkConfiguraitonDoc =
227                 new FileConfigurationDocument(
228                     this,
229                     nodeName,
230                     new DefaultConfigurationFormatService(),
231                     new File JavaDoc(this.directory, nodeName
232                         + this.fileFilter.LINK_NODE_EXTENSION));
233
234             LinkNodeConfiguration config = (LinkNodeConfiguration)
235                 linkConfiguraitonDoc.readConfiguration();
236                 
237             if (config.getLinkNodeFactoryClass() == null) {
238                 throw new InvalidConfigurationException(
239                     this.getClass(),
240                     config.getConfigurationName(),
241                     "LinkNodeFactoryClass",
242                     "Cannot be null");
243             }
244
245             LinkNodeFactory factory = (LinkNodeFactory)
246                 config.getLinkNodeFactoryClass().newInstance();
247
248             return factory.getInstance(this, nodeName, linkConfiguraitonDoc);
249
250         } catch (ConfigurationFormatException e) {
251             throw new NodeCreationException(
252                 this.getClass(),
253                 this, nodeName, "exception occured", e);
254         } catch (NodeIOException e) {
255             throw new NodeCreationException(
256                 this.getClass(),
257                 this, nodeName, "exception occured", e);
258         } catch (InstantiationException JavaDoc e) {
259             throw new NodeCreationException(
260                 this.getClass(),
261                 this, nodeName, "exception occured", e);
262         } catch (IllegalAccessException JavaDoc e) {
263             throw new NodeCreationException(
264                 this.getClass(),
265                 this, nodeName, "exception occured", e);
266         }
267     }
268
269     /**
270      * @see org.sape.carbon.core.config.node.AbstractFolder#backingDataExists()
271      */

272     protected boolean backingDataExists() {
273         return (this.directory.exists() && this.directory.isDirectory());
274     }
275
276 }
Popular Tags