1 17 18 package org.sape.carbon.core.config.node.file; 19 20 import java.io.File ; 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 47 public class FileLinkNodeFactory implements LinkNodeFactory { 48 49 private Log log = LogFactory.getLog(this.getClass()); 50 51 55 private static final NodeFactory subFolderFactory = 56 new FileFolderFactory(); 57 58 63 private static final NodeFactory configurationDocumentFactory = 64 new FileConfigurationDocumentFactory(); 65 66 71 private static final NodeFactory linkNodeFactory = 72 new FileLinkNodeConfigurationDocumentFactory(); 73 74 77 public LinkNode getInstance(Node parent, String name, 78 ConfigurationDocument linkConfigurationDoc) 79 throws NodeCreationException { 80 81 File targetFile = new File (getTargetPath(linkConfigurationDoc)); 82 83 if (!targetFile.isAbsolute()) { 84 if (parent != null) { 85 try { 86 targetFile = new File ( 87 ((FileFolder) parent).getInternalFileObject(), 88 targetFile.getPath()); 89 } catch (ClassCastException cce) { 90 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 139 protected NodeFactory getSubFolderFactory() { 140 return FileLinkNodeFactory.subFolderFactory; 141 } 142 143 149 protected NodeFactory getConfigurationDocumentFactory() { 150 return FileLinkNodeFactory.configurationDocumentFactory; 151 } 152 153 158 protected NodeFactory getLinkNodeFactory() { 159 return FileLinkNodeFactory.linkNodeFactory; 160 } 161 162 168 private String getTargetPath(ConfigurationDocument linkConfigurationDoc) { 169 FileLinkNodeConfiguration fileLinkConfiguration; 170 171 try { 172 173 fileLinkConfiguration = (FileLinkNodeConfiguration) 174 linkConfigurationDoc.readConfiguration(); 175 176 String 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 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 |