1 17 18 package org.sape.carbon.core.config.node.file; 19 20 import java.io.File ; 21 import java.util.HashSet ; 22 import java.util.Set ; 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 50 public class FileFolder extends AbstractFolder { 51 52 53 57 private FileNodeFilter fileFilter = new FileNodeFilter(); 58 59 60 private File directory; 61 62 63 74 protected FileFolder( 75 Node parent, 76 String name, 77 NodeFactory subFolderFactory, 78 NodeFactory configurationDocumentFactory, 79 NodeFactory linkNodeFactory, 80 File directory) { 81 82 super(parent, name, subFolderFactory, 83 configurationDocumentFactory, linkNodeFactory); 84 this.directory = directory; 85 } 86 87 91 92 93 96 protected Set getAllChildNames() { 97 String [] childNames = this.directory.list(this.fileFilter); 98 Set childNameSet = new HashSet (); 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 125 protected Node loadChild(String nodeName) throws NodeNotFoundException { 126 127 try { 128 File childFile = new File (this.directory, nodeName); 129 130 if (childFile.exists() && childFile.isDirectory()) { 131 return loadSubFolder(nodeName); 132 } 133 134 childFile = new File (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 (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 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 186 File getInternalFileObject() { 187 return this.directory; 188 } 189 190 197 private Node loadSubFolder(String nodeName) throws NodeCreationException { 198 return getSubFolderFactory().getInstance(this, nodeName); 199 } 200 201 208 private Node loadConfigurationDocument(String nodeName) 209 throws NodeCreationException { 210 211 return getConfigurationDocumentFactory().getInstance(this, nodeName); 212 } 213 214 221 private Node loadChildLinkNode(String nodeName) 222 throws NodeCreationException { 223 224 try { 225 226 ConfigurationDocument linkConfiguraitonDoc = 227 new FileConfigurationDocument( 228 this, 229 nodeName, 230 new DefaultConfigurationFormatService(), 231 new File (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 e) { 259 throw new NodeCreationException( 260 this.getClass(), 261 this, nodeName, "exception occured", e); 262 } catch (IllegalAccessException e) { 263 throw new NodeCreationException( 264 this.getClass(), 265 this, nodeName, "exception occured", e); 266 } 267 } 268 269 272 protected boolean backingDataExists() { 273 return (this.directory.exists() && this.directory.isDirectory()); 274 } 275 276 } | Popular Tags |