1 17 18 package org.sape.carbon.core.bootstrap; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.URL ; 25 26 import org.sape.carbon.core.config.Configuration; 27 import org.sape.carbon.core.config.format.ConfigurationFormatException; 28 import org.sape.carbon.core.config.format.ConfigurationFormatService; 29 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService; 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.event.NodeEventListener; 38 import org.sape.carbon.core.config.node.link.LinkNodeConfiguration; 39 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 43 44 60 class BootConfigurationDocument implements ConfigurationDocument { 61 62 63 private Log log = LogFactory.getLog(this.getClass()); 64 65 68 private static final String ROOT_LINK_PROPERTY_NAME = 69 "carbon.config.RootLink"; 70 71 75 private static final String DEFAULT_ROOT_LINK = 76 "<Configuration ConfigurationInterface=\"org.sape.carbon.core.config.node.file.FileLinkNodeConfiguration\"><LinkNodeFactoryClass>org.sape.carbon.core.config.node.file.FileLinkNodeFactory</LinkNodeFactoryClass><LinkToPath>{carbon.config.Path}</LinkToPath></Configuration>"; 77 78 82 public static final String ROOT_LINK_RESOURCE_NAME = 83 "CarbonConfigRoot.link"; 84 87 private LinkNodeConfiguration config; 88 89 90 private ConfigurationFormatService formatter = 91 new DefaultConfigurationFormatService(); 92 93 105 BootConfigurationDocument() { 106 107 InputStream rootLinkConfigInputStream = null; 108 109 try { 110 rootLinkConfigInputStream = getRootLinkInputStream(); 111 112 this.config = 113 (LinkNodeConfiguration) this.formatter.readConfigurationStream( 114 "", 115 rootLinkConfigInputStream); 116 117 } catch (ConfigurationFormatException cfe) { 118 throw new BootStrapException(this.getClass(), 119 "Exception reading configuration root link document", 120 cfe); 121 122 } catch (ClassCastException cce) { 123 throw new BootStrapException( 124 this.getClass(), 125 "Configuration root link object must be of type " 126 + LinkNodeConfiguration.class.getName(), 127 cce); 128 } finally { 129 if (rootLinkConfigInputStream != null) { 131 try { 132 rootLinkConfigInputStream.close(); 133 } catch (IOException ioe) { 134 } 136 } 137 } 138 } 139 140 150 public Configuration readConfiguration() 151 throws NodeIOException, ConfigurationFormatException { 152 153 return this.config; 154 } 155 156 163 public void writeConfiguration(Configuration config) 164 throws NodeIOException, ConfigurationFormatException { 165 166 throw new UnsupportedOperationException (this.getClass().getName()); 167 } 168 169 174 public boolean getAllowsChildren() { 175 return false; 176 } 177 178 184 public int remove() throws NodeRemovalException { 185 throw new UnsupportedOperationException (this.getClass().getName()); 186 } 187 188 193 public void refresh() { 194 throw new UnsupportedOperationException (this.getClass().getName()); 195 } 196 197 200 public ConfigurationDocument addNestedConfigurationDocument( 201 String name, 202 Configuration config) 203 throws NodeCreationException { 204 throw new UnsupportedOperationException (this.getClass().getName()); 205 } 206 207 210 public boolean containsChild(String childName) { 211 return false; 212 } 213 214 217 public Node fetchChild(String childName) throws NodeNotFoundException { 218 throw new NodeNotFoundException(this.getClass(), childName); 219 } 220 221 224 public Node[] fetchChildren() { 225 return new Node[0]; 226 } 227 228 231 public String getAbsoluteName() { 232 return ""; 233 } 234 235 238 public String getName() { 239 return ""; 240 } 241 242 245 public Node getParent() { 246 return null; 247 } 248 249 252 public boolean isRemoved() { 253 return false; 254 } 255 256 259 public ConfigurationFormatService getFormatService() { 260 return this.formatter; 261 } 262 263 266 public void addNodeListener(NodeEventListener listener) { 267 } 269 270 289 private InputStream getRootLinkInputStream() { 290 291 InputStream rootLinkInputStream = null; 292 293 try { 296 if (log.isDebugEnabled()) { 297 log.debug("Attempting to load configuration root link " 298 + "by using deployment property " 299 + ROOT_LINK_PROPERTY_NAME); 300 } 301 302 String rootLinkFileName = 303 BootStrapper.getInstance().getDeploymentProperty( 304 ROOT_LINK_PROPERTY_NAME); 305 306 if (rootLinkFileName != null) { 307 if (log.isInfoEnabled()) { 310 log.info("Using file to load configuration root link" 311 + ": file name [" 312 + rootLinkFileName 313 + "]"); 314 } 315 316 rootLinkInputStream = new FileInputStream (rootLinkFileName); 317 } 318 } catch (IOException ioe) { 319 if (log.isInfoEnabled()) { 320 log.info("Attempted to get configuration root link from file " 321 + "but caught IOException", ioe); 322 } 323 324 rootLinkInputStream = null; 327 } 328 329 try { 332 if (rootLinkInputStream == null) { 333 if (log.isDebugEnabled()) { 334 log.debug("Attempting to load configuration root " 335 + "link resource [" 336 + ROOT_LINK_RESOURCE_NAME 337 + "] from the class loader "); 338 } 339 340 ClassLoader classLoader = this.getClass().getClassLoader(); 344 URL rootLinkURL = 345 classLoader.getResource(ROOT_LINK_RESOURCE_NAME); 346 347 if (rootLinkURL != null) { 348 if (log.isInfoEnabled()) { 349 log.info("Using ClassLoader to load configuration " 350 + "root link: resource [" 351 + rootLinkURL.getPath() 352 + "]"); 353 } 354 355 rootLinkInputStream = rootLinkURL.openStream(); 356 } 357 } 358 } catch (IOException ioe) { 359 if (log.isInfoEnabled()) { 360 log.info("Attempted to get configuration root link " 361 + "from ClassLoader but caught IOException", ioe); 362 } 363 364 rootLinkInputStream = null; 367 } 368 369 if (rootLinkInputStream == null) { 372 if (log.isInfoEnabled()) { 373 log.info("Configuration root link wasnt located, " 374 + "Using default config root link"); 375 } 376 377 rootLinkInputStream = 380 new ByteArrayInputStream (DEFAULT_ROOT_LINK.getBytes()); 381 } 382 383 return rootLinkInputStream; 384 } 385 386 389 public NodeFactory getNestedNodeFactory() { 390 return null; 391 } 392 393 } | Popular Tags |