1 17 18 package org.sape.carbon.services.config.classloader; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 24 import org.sape.carbon.core.config.Configuration; 25 import org.sape.carbon.core.config.format.ConfigurationFormatService; 26 import org.sape.carbon.core.config.node.AbstractConfigurationDocument; 27 import org.sape.carbon.core.config.node.Node; 28 import org.sape.carbon.core.config.node.NodeRemovalException; 29 import org.sape.carbon.core.config.node.NodeRemovedException; 30 import org.sape.carbon.core.exception.ExceptionUtility; 31 import org.sape.carbon.core.util.string.StringUtil; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 45 public class ClassloaderConfigurationDocument 46 extends AbstractConfigurationDocument { 47 48 51 private Log log = LogFactory.getLog(this.getClass()); 52 53 58 private final String resourcePath; 59 60 65 private final ConfigurationSource configSource; 66 67 73 private final boolean isInMemoryOnly; 74 75 82 public ClassloaderConfigurationDocument( 83 Node parent, 84 String name, 85 ConfigurationFormatService formatter, 86 String resourcePath, 87 ConfigurationSource configSource) { 88 89 super(parent, name, formatter); 90 91 this.resourcePath = resourcePath; 92 this.configSource = configSource; 93 94 this.isInMemoryOnly = configSource.getResource(resourcePath) == null; 95 } 96 97 109 public void writeConfiguration(Configuration config) { 110 111 synchronized (getReadOrAlterNodeLock()) { 112 if (isRemoved()) { 113 throw new NodeRemovedException( 114 this.getClass(), 115 this); 116 } 117 118 super.configuration = (Configuration) config.clone(); 119 super.configuration.setConfigurationName(getAbsoluteName()); 120 } 121 122 issueNodeChangedEvent(); 123 notifyNestedDocuments(); 124 } 125 126 131 protected InputStream openInputStream() throws IOException { 132 133 InputStream stream = this.configSource.getResourceAsStream(this.resourcePath); 134 135 if (stream == null) { 136 try { 137 stream = this.configSource.getResource(this.resourcePath).openStream(); 138 } catch (Exception e) { 139 System.out.println(ExceptionUtility.printStackTracesToString(e)); 140 } 141 } 142 143 return stream; 144 } 145 146 147 153 protected OutputStream openOutputStream() throws IOException { 154 throw new UnsupportedOperationException ( 155 "Implemented such that this should never be called"); 156 } 157 158 164 protected void closeOutputStream(OutputStream out) 165 throws IOException { 166 throw new UnsupportedOperationException ( 167 "Implemented such that this should never be called"); 168 } 169 170 175 protected void destroyBackingData() throws NodeRemovalException { 176 } 177 178 183 protected boolean backingDataExists() { 184 if (this.isInMemoryOnly) { 185 return true; 186 } else { 187 return this.configSource.getResource(this.resourcePath) != null; 188 } 189 } 190 191 196 public int remove() throws NodeRemovalException { 197 if (this.isInMemoryOnly) { 198 return super.remove(); 200 } else { 201 throw new NodeRemovalException( 203 this.getClass(), 204 this, 205 "This node was created from a classloader resource and cannot be removed"); 206 } 207 } 208 209 212 public void refresh() { 213 if (!this.isInMemoryOnly) { 214 super.refresh(); 215 } 216 } 217 218 } 219 | Popular Tags |