1 4 package com.tc.config.schema.setup; 5 6 import org.apache.xmlbeans.XmlObject; 7 import org.apache.xmlbeans.XmlOptions; 8 import org.apache.xmlbeans.impl.common.XPath; 9 10 import com.tc.config.schema.IllegalConfigurationChangeHandler; 11 import com.tc.config.schema.NewCommonL1Config; 12 import com.tc.config.schema.NewCommonL2Config; 13 import com.tc.config.schema.NewSystemConfig; 14 import com.tc.config.schema.SettableConfigItem; 15 import com.tc.config.schema.TestConfigObjectInvocationHandler; 16 import com.tc.config.schema.dynamic.ConfigItem; 17 import com.tc.config.schema.dynamic.XPathBasedConfigItem; 18 import com.tc.config.schema.repository.MutableBeanRepository; 19 import com.tc.object.config.schema.NewDSOApplicationConfig; 20 import com.tc.object.config.schema.NewL1DSOConfig; 21 import com.tc.object.config.schema.NewL2DSOConfig; 22 import com.tc.util.Assert; 23 import com.terracottatech.config.Application; 24 import com.terracottatech.config.Server; 25 26 import java.lang.reflect.Proxy ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Set ; 33 34 135 public class TestTVSConfigurationSetupManagerFactory extends BaseTVSConfigurationSetupManagerFactory { 136 137 public static final int MODE_CENTRALIZED_CONFIG = 0; 138 public static final int MODE_DISTRIBUTED_CONFIG = 1; 139 140 private final TestConfigBeanSet beanSet; 141 142 private final TestConfigurationCreator l1ConfigurationCreator; 143 private final TestConfigurationCreator l2ConfigurationCreator; 144 145 private final NewSystemConfig sampleSystem; 146 private final NewCommonL1Config sampleL1Common; 147 private final NewL1DSOConfig sampleL1DSO; 148 private final NewCommonL2Config sampleL2Common; 149 private final NewL2DSOConfig sampleL2DSO; 150 private final NewDSOApplicationConfig sampleDSOApplication; 151 152 private final String defaultL2Identifier; 153 154 private final int mode; 155 156 public TestTVSConfigurationSetupManagerFactory(int mode, String l2Identifier, 157 IllegalConfigurationChangeHandler illegalConfigurationChangeHandler) { 158 super(illegalConfigurationChangeHandler); 159 160 this.beanSet = new TestConfigBeanSet(); 161 162 this.l2ConfigurationCreator = new TestConfigurationCreator(this.beanSet, true); 163 164 this.mode = mode; 165 if (mode == MODE_CENTRALIZED_CONFIG) { 166 this.l1ConfigurationCreator = new TestConfigurationCreator(this.beanSet, true); 167 } else if (mode == MODE_DISTRIBUTED_CONFIG) { 168 this.l1ConfigurationCreator = new TestConfigurationCreator(this.beanSet, false); 169 } else { 170 throw Assert.failure("Unknown mode: " + mode); 171 } 172 173 if (l2Identifier != null) { 174 Assert.assertNotBlank(l2Identifier); 175 this.defaultL2Identifier = l2Identifier; 176 } else { 177 this.defaultL2Identifier = this.beanSet.serversBean().getServerArray()[0].getName(); 178 } 179 180 Assert.assertNotNull(this.defaultL2Identifier); 181 182 L1TVSConfigurationSetupManager sampleL1Manager; 185 L2TVSConfigurationSetupManager sampleL2Manager; 186 187 try { 188 sampleL1Manager = this.createL1TVSConfigurationSetupManager(new TestConfigurationCreator(this.beanSet, true)); 189 sampleL2Manager = this.createL2TVSConfigurationSetupManager(null); 190 } catch (ConfigurationSetupException cse) { 191 throw Assert.failure("Huh?", cse); 192 } 193 194 this.sampleSystem = sampleL2Manager.systemConfig(); 195 this.sampleL1Common = sampleL1Manager.commonL1Config(); 196 this.sampleL1DSO = sampleL1Manager.dsoL1Config(); 197 this.sampleL2Common = sampleL2Manager.commonl2Config(); 198 this.sampleL2DSO = sampleL2Manager.dsoL2Config(); 199 this.sampleDSOApplication = sampleL1Manager 200 .dsoApplicationConfigFor(TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME); 201 202 applyDefaultTestConfig(); 203 } 204 205 public TestConfigBeanSet beanSet() { 206 return this.beanSet; 207 } 208 209 private static final String BOGUS_FILENAME = "nonexistent-directory-SHOULD-NEVER-EXIST/../"; 210 211 private void applyDefaultTestConfig() { 212 ((SettableConfigItem) l2CommonConfig().jmxPort()).setValue(0); 225 226 ((SettableConfigItem) l1CommonConfig().logsPath()).setValue(BOGUS_FILENAME); 230 ((SettableConfigItem) l2CommonConfig().dataPath()).setValue(BOGUS_FILENAME); 231 ((SettableConfigItem) l2CommonConfig().logsPath()).setValue(BOGUS_FILENAME); 232 } 233 234 public void activateConfigurationChange() throws ConfigurationSetupException { 235 Set allRepositories = collectAllRepositories(); 236 237 Iterator iter = allRepositories.iterator(); 238 while (iter.hasNext()) { 239 MutableBeanRepository repository = (MutableBeanRepository) iter.next(); 240 checkValidates(repository.bean()); 241 repository.didMutateBean(); 242 } 243 244 iter = allRepositories.iterator(); 245 while (iter.hasNext()) { 246 MutableBeanRepository repository = (MutableBeanRepository) iter.next(); 247 repository.saveCopyOfBeanInAnticipationOfFutureMutation(); 248 } 249 } 250 251 private void checkValidates(XmlObject bean) throws ConfigurationSetupException { 252 List errors = new ArrayList (); 253 XmlOptions options = new XmlOptions().setErrorListener(errors); 254 boolean valid = bean.validate(options); 255 256 if ((!valid) || (errors.size() > 0)) { 257 throw new ConfigurationSetupException("You have errors in your config: " + errors); 259 } 260 } 261 262 private Set collectAllRepositories() { 263 Set allRepositories = new HashSet (); 264 265 allRepositories.addAll(Arrays.asList(this.l1ConfigurationCreator.allRepositoriesStoredInto())); 266 allRepositories.addAll(Arrays.asList(this.l2ConfigurationCreator.allRepositoriesStoredInto())); 267 return allRepositories; 268 } 269 270 private Object proxify(Class theClass, XmlObject[] destObjects, Object realImplementation, String xpathPrefix) { 271 return Proxy.newProxyInstance(getClass().getClassLoader(), new Class [] { theClass }, 272 new TestConfigObjectInvocationHandler(theClass, destObjects, realImplementation, 273 xpathPrefix)); 274 } 275 276 private Object proxify(Class theClass, XmlObject destObject, Object realImplementation, String xpathPrefix) { 277 return proxify(theClass, new XmlObject[] { destObject }, realImplementation, xpathPrefix); 278 } 279 280 private XmlObject[] allServerBeans() { 281 return this.beanSet.serversBean().getServerArray(); 282 } 283 284 public NewSystemConfig systemConfig() { 285 return (NewSystemConfig) proxify(NewSystemConfig.class, this.beanSet.systemBean(), this.sampleSystem, null); 286 } 287 288 public NewCommonL1Config l1CommonConfig() { 289 return (NewCommonL1Config) proxify(NewCommonL1Config.class, this.beanSet.clientBean(), this.sampleL1Common, null); 290 } 291 292 public NewL1DSOConfig l1DSOConfig() { 293 return (NewL1DSOConfig) proxify(NewL1DSOConfig.class, this.beanSet.clientBean(), this.sampleL1DSO, "dso"); 294 } 295 296 public void addServerConfig(String name) { 297 Server newL2 = this.beanSet.serversBean().addNewServer(); 298 299 newL2.setName(name); 300 newL2.setHost("localhost"); 301 302 newL2.setDsoPort(0); 303 newL2.setJmxPort(0); 304 305 newL2.setData(BOGUS_FILENAME); 306 newL2.setLogs(BOGUS_FILENAME); 307 } 308 309 private Server findL2Bean(String name) { 310 Server[] allServers = this.beanSet.serversBean().getServerArray(); 311 312 if (allServers == null || allServers.length == 0) throw Assert.failure("No L2s are defined."); 313 314 if (name == null) { 315 if (allServers.length == 1) return allServers[0]; 316 else throw Assert 317 .failure("You passed in null for the L2 name, but there's more than one L2. Please specify which one you want."); 318 } 319 320 for (int i = 0; i < allServers.length; ++i) { 321 if (allServers[i].getName().equals(name)) return allServers[i]; 322 } 323 throw Assert.failure("There is no L2 defined named '" + name + "'."); 324 } 325 326 public NewCommonL2Config l2CommonConfig(String l2Name) { 327 return (NewCommonL2Config) proxify(NewCommonL2Config.class, findL2Bean(l2Name), this.sampleL2Common, null); 328 } 329 330 public NewL2DSOConfig l2DSOConfig(String l2Name) { 331 return (NewL2DSOConfig) proxify(NewL2DSOConfig.class, findL2Bean(l2Name), this.sampleL2DSO, null); 332 } 333 334 public NewCommonL2Config l2CommonConfig() { 335 return (NewCommonL2Config) proxify(NewCommonL2Config.class, allServerBeans(), this.sampleL2Common, null); 336 } 337 338 public NewL2DSOConfig l2DSOConfig() { 339 return (NewL2DSOConfig) proxify(NewL2DSOConfig.class, allServerBeans(), this.sampleL2DSO, null); 340 } 341 342 public NewDSOApplicationConfig dsoApplicationConfig(String applicationName) { 343 return (NewDSOApplicationConfig) proxify(NewDSOApplicationConfig.class, this.beanSet 344 .applicationBeanFor(applicationName), this.sampleDSOApplication, "dso"); 345 } 346 347 public NewDSOApplicationConfig dsoApplicationConfig() { 348 return dsoApplicationConfig(TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME); 349 } 350 351 public TestTVSConfigurationSetupManagerFactory(String l2Identifier, 352 IllegalConfigurationChangeHandler illegalConfigurationChangeHandler) { 353 this(MODE_CENTRALIZED_CONFIG, l2Identifier, illegalConfigurationChangeHandler); 354 } 355 356 public TestTVSConfigurationSetupManagerFactory(IllegalConfigurationChangeHandler illegalConfigurationChangeHandler) { 357 this(null, illegalConfigurationChangeHandler); 358 } 359 360 public L1TVSConfigurationSetupManager createL1TVSConfigurationSetupManager() throws ConfigurationSetupException { 361 return createL1TVSConfigurationSetupManager(this.l1ConfigurationCreator); 362 } 363 364 public L1TVSConfigurationSetupManager createL1TVSConfigurationSetupManager(TestConfigurationCreator configCreator) 365 throws ConfigurationSetupException { 366 if (mode == MODE_CENTRALIZED_CONFIG) { 367 StringBuffer l2sSpec = new StringBuffer (); 368 369 Server[] allServers = (Server[]) this.allServerBeans(); 370 for (int i = 0; i < allServers.length; ++i) { 371 Server thisServer = allServers[i]; 372 373 if (i > 0) l2sSpec.append(","); 374 375 String hostname = thisServer.getHost(); 376 if (hostname == null) hostname = thisServer.getName(); 377 Assert.assertNotBlank(hostname); 378 379 l2sSpec.append(hostname + ":" + thisServer.getDsoPort()); 380 } 381 382 System.setProperty(TVSConfigurationSetupManagerFactory.CONFIG_FILE_PROPERTY_NAME, l2sSpec.toString()); 383 } 384 385 return new StandardL1TVSConfigurationSetupManager(configCreator, this.defaultValueProvider, 386 this.xmlObjectComparator, this.illegalChangeHandler); 387 } 388 389 public L2TVSConfigurationSetupManager createL2TVSConfigurationSetupManager(String l2Identifier) 390 throws ConfigurationSetupException { 391 String effectiveL2Identifier = l2Identifier == null ? this.defaultL2Identifier : l2Identifier; 392 return new StandardL2TVSConfigurationSetupManager(this.l2ConfigurationCreator, effectiveL2Identifier, 393 this.defaultValueProvider, this.xmlObjectComparator, 394 this.illegalChangeHandler); 395 } 396 397 } 398 | Popular Tags |