KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > setup > TestConfigBeanSet


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config.schema.setup;
5
6 import org.apache.xmlbeans.XmlObject;
7 import org.apache.xmlbeans.XmlOptions;
8
9 import com.tc.util.Assert;
10 import com.terracottatech.config.Application;
11 import com.terracottatech.config.Client;
12 import com.terracottatech.config.Server;
13 import com.terracottatech.config.Servers;
14 import com.terracottatech.config.System;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * Holds on to a set of beans that can be modified; this is used by the
24  * {@link com.tc.config.schema.setup.TestTVSConfigurationSetupManagerFactory}.
25  */

26 public class TestConfigBeanSet {
27
28   private final Client rootClientBean;
29   private final Servers rootServersBean;
30   private final System JavaDoc rootSystemBean;
31   private final Map JavaDoc rootApplicationBeans;
32
33   public TestConfigBeanSet() {
34     this.rootClientBean = Client.Factory.newInstance();
35
36     this.rootServersBean = Servers.Factory.newInstance();
37     Server initialServer = this.rootServersBean.addNewServer();
38     initialServer.setName("localhost");
39
40     this.rootSystemBean = System.Factory.newInstance();
41
42     this.rootApplicationBeans = new HashMap JavaDoc();
43     this.rootApplicationBeans.put(TVSConfigurationSetupManagerFactory.DEFAULT_APPLICATION_NAME, createNewApplication());
44
45     checkValidates(this.rootClientBean);
46     checkValidates(this.rootServersBean);
47     checkValidates(this.rootSystemBean);
48
49     Iterator JavaDoc iter = this.rootApplicationBeans.values().iterator();
50     while (iter.hasNext()) {
51       checkValidates((XmlObject) iter.next());
52     }
53   }
54
55   private void checkValidates(XmlObject object) {
56     List JavaDoc errors = new ArrayList JavaDoc();
57     XmlOptions options = new XmlOptions().setErrorListener(errors);
58
59     boolean result = object.validate(options);
60     if ((!result) || (errors.size() > 0)) {
61       // formatting
62
throw Assert
63           .failure("Object " + object + " of " + object.getClass() + " didn't validate; errors were: " + errors);
64     }
65   }
66
67   public Client clientBean() {
68     return this.rootClientBean;
69   }
70
71   public Servers serversBean() {
72     return this.rootServersBean;
73   }
74
75   public System JavaDoc systemBean() {
76     return this.rootSystemBean;
77   }
78
79   public String JavaDoc[] applicationNames() {
80     return (String JavaDoc[]) this.rootApplicationBeans.keySet().toArray(new String JavaDoc[this.rootApplicationBeans.size()]);
81   }
82
83   public Application applicationBeanFor(String JavaDoc applicationName) {
84     Application out = (Application) this.rootApplicationBeans.get(applicationName);
85     if (out == null) {
86       out = createNewApplication();
87       this.rootApplicationBeans.put(applicationName, out);
88     }
89     return out;
90   }
91
92   private Application createNewApplication() {
93     Application out = Application.Factory.newInstance();
94     out.addNewDso().addNewInstrumentedClasses();
95     return out;
96   }
97
98 }
99
Popular Tags