1 17 18 package org.sape.carbon.services.config.classloader; 19 20 import java.net.URL ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 26 import org.sape.carbon.core.config.InvalidConfigurationException; 27 import org.sape.carbon.core.config.format.ConfigurationFormatException; 28 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService; 29 import org.sape.carbon.core.config.node.AbstractFolder; 30 import org.sape.carbon.core.config.node.ConfigurationDocument; 31 import org.sape.carbon.core.config.node.Node; 32 import org.sape.carbon.core.config.node.NodeCreationException; 33 import org.sape.carbon.core.config.node.NodeFactory; 34 import org.sape.carbon.core.config.node.NodeIOException; 35 import org.sape.carbon.core.config.node.NodeNotFoundException; 36 import org.sape.carbon.core.config.node.NodeRemovalException; 37 import org.sape.carbon.core.config.node.file.FileNodeFilter; 38 import org.sape.carbon.core.config.node.link.LinkNodeConfiguration; 39 import org.sape.carbon.core.config.node.link.LinkNodeFactory; 40 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 44 54 public class ClassloaderFolder extends AbstractFolder { 55 56 59 private Log log = LogFactory.getLog(this.getClass()); 60 61 62 private String resourcePath; 63 64 68 private FileNodeFilter fileFilter = new FileNodeFilter(); 69 70 private ConfigurationSource configSource; 71 72 73 public static final String CLASSLOADER_DELIMETER = "/"; 74 75 private static final Collection EXCLUDE_NODE_NAMES; 76 private static final String META_INF_DIR = "META-INF"; 77 private static final String CVS_DIR = "CVS"; 78 79 static { 80 EXCLUDE_NODE_NAMES = new ArrayList (); 81 EXCLUDE_NODE_NAMES.add(META_INF_DIR); 82 EXCLUDE_NODE_NAMES.add(CVS_DIR); 83 } 84 85 94 public ClassloaderFolder( 95 Node parent, 96 String name, 97 NodeFactory subFolderFactory, 98 NodeFactory configurationDocumentFactory, 99 NodeFactory linkNodeFactory, 100 String resourcePath, 101 ConfigurationSource configSource) { 102 103 super( 104 parent, 105 name, 106 subFolderFactory, 107 configurationDocumentFactory, 108 linkNodeFactory); 109 110 this.resourcePath = resourcePath; 111 this.configSource = configSource; 112 } 113 114 120 protected Node loadChild(String nodeName) 121 throws NodeNotFoundException { 122 123 String childResourcePath = this.resourcePath + nodeName; 124 125 try { 126 Node child = null; 127 128 if (!ClassloaderFolder.EXCLUDE_NODE_NAMES.contains(nodeName.toUpperCase())) { 130 131 log.trace( 132 "Classloader retrieving child [" + 133 childResourcePath + this.fileFilter.CONFIG_DOC_NODE_EXTENSION + "]"); 134 135 URL childDoc = 136 this.configSource.getResource( 137 childResourcePath + this.fileFilter.CONFIG_DOC_NODE_EXTENSION); 138 139 if (childDoc != null) { 141 child = loadConfigurationDocument(nodeName); 142 } 143 144 145 URL childLink = 146 this.configSource.getResource( 147 childResourcePath + this.fileFilter.LINK_NODE_EXTENSION); 148 149 if (childLink != null) { 151 child = loadChildLinkNode(nodeName); 153 } 154 } 155 156 if (child == null) { 157 child = new ClassloaderFolder( 158 this, 159 nodeName, 160 getSubFolderFactory(), 161 getConfigurationDocumentFactory(), 162 getLinkNodeFactory(), 163 childResourcePath + "/", 164 this.configSource); 165 } 166 167 return child; 168 169 } catch (NodeCreationException nce) { 170 throw new NodeNotFoundException( 171 this.getClass(), 172 nodeName, 173 nce); 174 } 175 } 176 177 183 protected Set getAllChildNames() { 184 return new HashSet (); 185 } 186 187 193 protected void destroyBackingData() throws NodeRemovalException { 194 } 195 196 202 protected boolean backingDataExists() { 203 return true; 204 } 205 206 212 String getResourcePath() { 213 return this.resourcePath; 214 } 215 216 ConfigurationSource getConfigSource() { 217 return this.configSource; 218 } 219 220 227 private Node loadSubFolder(String nodeName) 228 throws NodeCreationException { 229 return getSubFolderFactory().getInstance(this, nodeName); 230 } 231 232 239 private Node loadConfigurationDocument(String nodeName) 240 throws NodeCreationException { 241 242 return getConfigurationDocumentFactory().getInstance(this, nodeName); 243 } 244 245 252 private Node loadChildLinkNode(String nodeName) 253 throws NodeCreationException { 254 255 try { 256 257 ConfigurationDocument linkConfigurationDoc = 258 new ClassloaderConfigurationDocument( 259 this, 260 nodeName, 261 new DefaultConfigurationFormatService(), 262 this.resourcePath + nodeName + this.fileFilter.LINK_NODE_EXTENSION, 263 this.configSource); 264 265 LinkNodeConfiguration config = 266 (LinkNodeConfiguration) linkConfigurationDoc.readConfiguration(); 267 268 if (config.getLinkNodeFactoryClass() == null) { 269 throw new InvalidConfigurationException( 270 this.getClass(), 271 config.getConfigurationName(), 272 "LinkNodeFactoryClass", 273 "Cannot be null"); 274 } 275 276 LinkNodeFactory factory = 277 (LinkNodeFactory) config.getLinkNodeFactoryClass().newInstance(); 278 279 return factory.getInstance(this, nodeName, linkConfigurationDoc); 280 281 } catch (ConfigurationFormatException e) { 282 throw new NodeCreationException( 283 this.getClass(), 284 this, 285 nodeName, 286 "exception occured", 287 e); 288 } catch (NodeIOException e) { 289 throw new NodeCreationException( 290 this.getClass(), 291 this, 292 nodeName, 293 "exception occured", 294 e); 295 } catch (InstantiationException e) { 296 throw new NodeCreationException( 297 this.getClass(), 298 this, 299 nodeName, 300 "exception occured", 301 e); 302 } catch (IllegalAccessException e) { 303 throw new NodeCreationException( 304 this.getClass(), 305 this, 306 nodeName, 307 "exception occured", 308 e); 309 } 310 } 311 } | Popular Tags |