1 45 package org.exolab.jms.config; 46 47 import java.io.FileReader ; 48 import java.io.IOException ; 49 import java.io.InputStream ; 50 import java.io.InputStreamReader ; 51 52 import org.exolab.castor.xml.MarshalException; 53 import org.exolab.castor.xml.Unmarshaller; 54 import org.exolab.castor.xml.ValidationException; 55 56 57 65 public class ConfigurationLoader { 66 67 70 private static final String DEFAULT_CONFIG = "openjms_defaults.xml"; 71 72 73 76 public ConfigurationLoader() { 77 } 78 79 90 public Configuration load(String path) 91 throws IOException , MarshalException, ValidationException { 92 93 Configuration result = null; 94 FileReader reader = new FileReader (path); 95 try { 96 Unmarshaller stream = new Unmarshaller(Configuration.class); 97 AttributeExpander handler = new AttributeExpander(reader); 98 result = (Configuration) stream.unmarshal(handler); 99 } finally { 100 reader.close(); 101 } 102 return load(result); 103 } 104 105 115 public Configuration load(Configuration config) 116 throws IOException , MarshalException, ValidationException { 117 DefaultConfiguration defaults = getDefaults(); 118 119 if (config.getServerConfiguration() == null) { 120 config.setServerConfiguration(defaults.getServerConfiguration()); 121 } 122 if (config.getConnectors() == null) { 123 config.setConnectors(defaults.getConnectors()); 124 } 125 if (config.getLoggerConfiguration() == null) { 126 config.setLoggerConfiguration(defaults.getLoggerConfiguration()); 127 } 128 if (config.getTcpConfiguration() == null) { 129 config.setTcpConfiguration(defaults.getTcpConfiguration()); 130 } 131 if (config.getTcpsConfiguration() == null) { 132 config.setTcpsConfiguration(defaults.getTcpsConfiguration()); 133 } 134 if (config.getRmiConfiguration() == null) { 135 config.setRmiConfiguration(defaults.getRmiConfiguration()); 136 } 137 if (config.getHttpConfiguration() == null) { 138 config.setHttpConfiguration(defaults.getHttpConfiguration()); 139 } 140 if (config.getHttpsConfiguration() == null) { 141 config.setHttpsConfiguration(defaults.getHttpsConfiguration()); 142 } 143 if (config.getMessageManagerConfiguration() == null) { 144 config.setMessageManagerConfiguration( 145 defaults.getMessageManagerConfiguration()); 146 } 147 if (config.getSchedulerConfiguration() == null) { 148 config.setSchedulerConfiguration( 149 defaults.getSchedulerConfiguration()); 150 } 151 if (config.getGarbageCollectionConfiguration() == null) { 152 config.setGarbageCollectionConfiguration( 153 defaults.getGarbageCollectionConfiguration()); 154 } 155 if (config.getSecurityConfiguration() == null) { 156 config.setSecurityConfiguration( 157 defaults.getSecurityConfiguration()); 158 } 159 if (config.getServerConfiguration().getEmbeddedJNDI()) { 160 config.setJndiConfiguration( 163 JndiConfigurationFactory.create(config)); 164 } else if (config.getJndiConfiguration() == null) { 165 throw new ValidationException( 166 "JndiConfiguration must be provided when " 167 + "ServerConfiguration/embeddedJNDI is false"); 168 } 169 return config; 170 } 171 172 180 private DefaultConfiguration getDefaults() 181 throws IOException , MarshalException, ValidationException { 182 183 DefaultConfiguration result = null; 184 InputStream source = getClass().getResourceAsStream(DEFAULT_CONFIG); 185 if (source == null) { 186 throw new IOException ( 187 "Failed to find default configuration: " + DEFAULT_CONFIG); 188 } 189 try { 190 Unmarshaller stream = new Unmarshaller(DefaultConfiguration.class); 191 AttributeExpander handler = new AttributeExpander( 192 new InputStreamReader (source)); 193 result = (DefaultConfiguration) stream.unmarshal(handler); 194 } finally { 195 try { 196 source.close(); 197 } catch (IOException ignore) { 198 } 199 } 200 return result; 201 } 202 203 } 204 | Popular Tags |