001: /**
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */package com.tc.config.schema.setup;
005:
006: import org.apache.xmlbeans.XmlObject;
007: import org.apache.xmlbeans.XmlOptions;
008:
009: import com.tc.util.Assert;
010: import com.terracottatech.config.Application;
011: import com.terracottatech.config.Client;
012: import com.terracottatech.config.Server;
013: import com.terracottatech.config.Servers;
014: import com.terracottatech.config.System;
015:
016: import java.util.ArrayList;
017: import java.util.HashMap;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Map;
021:
022: /**
023: * Holds on to a set of beans that can be modified; this is used by the
024: * {@link com.tc.config.schema.setup.TestTVSConfigurationSetupManagerFactory}.
025: */
026: public class TestConfigBeanSet {
027:
028: public static final String DEFAULT_HOST = "localhost";
029: public static final String DEFAULT_SERVER_NAME = "default";
030:
031: private final Client rootClientBean;
032: private final Servers rootServersBean;
033: private final System rootSystemBean;
034: private final Map rootApplicationBeans;
035:
036: public TestConfigBeanSet() {
037: this .rootClientBean = Client.Factory.newInstance();
038:
039: this .rootServersBean = Servers.Factory.newInstance();
040: Server initialServer = this .rootServersBean.addNewServer();
041: initialServer.setHost(DEFAULT_HOST);
042: initialServer.setName(DEFAULT_SERVER_NAME);
043:
044: this .rootSystemBean = System.Factory.newInstance();
045:
046: this .rootApplicationBeans = new HashMap();
047: this .rootApplicationBeans
048: .put(
049: TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME,
050: createNewApplication());
051:
052: checkValidates(this .rootClientBean);
053: checkValidates(this .rootServersBean);
054: checkValidates(this .rootSystemBean);
055:
056: Iterator iter = this .rootApplicationBeans.values().iterator();
057: while (iter.hasNext()) {
058: checkValidates((XmlObject) iter.next());
059: }
060: }
061:
062: private void checkValidates(XmlObject object) {
063: List errors = new ArrayList();
064: XmlOptions options = new XmlOptions().setErrorListener(errors);
065:
066: boolean result = object.validate(options);
067: if ((!result) || (errors.size() > 0)) {
068: // formatting
069: throw Assert.failure("Object " + object + " of "
070: + object.getClass()
071: + " didn't validate; errors were: " + errors);
072: }
073: }
074:
075: public Client clientBean() {
076: return this .rootClientBean;
077: }
078:
079: public Servers serversBean() {
080: return this .rootServersBean;
081: }
082:
083: public System systemBean() {
084: return this .rootSystemBean;
085: }
086:
087: public String[] applicationNames() {
088: return (String[]) this .rootApplicationBeans.keySet().toArray(
089: new String[this .rootApplicationBeans.size()]);
090: }
091:
092: public Application applicationBeanFor(String applicationName) {
093: Application out = (Application) this .rootApplicationBeans
094: .get(applicationName);
095: if (out == null) {
096: out = createNewApplication();
097: this .rootApplicationBeans.put(applicationName, out);
098: }
099: return out;
100: }
101:
102: private Application createNewApplication() {
103: Application out = Application.Factory.newInstance();
104: out.addNewDso().addNewInstrumentedClasses();
105: return out;
106: }
107:
108: }
|