001: package com.tc.test.server.util;
002:
003: import org.apache.commons.io.IOUtils;
004: import org.apache.xmlbeans.XmlOptions;
005:
006: import com.tc.config.Loader;
007: import com.terracottatech.config.Autolock;
008: import com.terracottatech.config.Include;
009: import com.terracottatech.config.LockLevel;
010: import com.terracottatech.config.Module;
011: import com.terracottatech.config.QualifiedClassName;
012: import com.terracottatech.config.Root;
013: import com.terracottatech.config.TcConfigDocument;
014: import com.terracottatech.config.WebApplication;
015: import com.terracottatech.config.TcConfigDocument.TcConfig;
016:
017: import java.io.File;
018: import java.io.FileOutputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021:
022: public class TcConfigBuilder {
023: private TcConfigDocument tcConfigDocument;
024: private TcConfig tcConfig;
025: private XmlOptions xmlOptions;
026:
027: public TcConfigBuilder() {
028: this ("default-tc-config.xml");
029: }
030:
031: public TcConfigBuilder(String resourcePath) {
032: try {
033: tcConfigDocument = new Loader().parse(getClass()
034: .getResourceAsStream(resourcePath));
035: tcConfig = tcConfigDocument.getTcConfig();
036: } catch (Exception e) {
037: throw new RuntimeException(e);
038: }
039: }
040:
041: public TcConfigBuilder(File file) {
042: try {
043: tcConfigDocument = new Loader().parse(file);
044: tcConfig = tcConfigDocument.getTcConfig();
045: } catch (Exception e) {
046: throw new RuntimeException(e);
047: }
048: }
049:
050: private TcConfigBuilder(TcConfigDocument tcd) {
051: tcConfigDocument = tcd;
052: tcConfig = tcConfigDocument.getTcConfig();
053: }
054:
055: public void setDsoPort(int portNo) {
056: ensureServers();
057: tcConfig.getServers().getServerArray(0).setDsoPort(portNo);
058: }
059:
060: public int getDsoPort() {
061: ensureServers();
062: return tcConfig.getServers().getServerArray(0).getDsoPort();
063: }
064:
065: public void setJmxPort(int portNo) {
066: ensureServers();
067: tcConfig.getServers().getServerArray(0).setJmxPort(portNo);
068: }
069:
070: public int getJmxPort() {
071: ensureServers();
072: return tcConfig.getServers().getServerArray(0).getJmxPort();
073: }
074:
075: public void setServerLogs(String path) {
076: ensureServers();
077: tcConfig.getServers().getServerArray(0).setLogs(path);
078: }
079:
080: public void setServerData(String path) {
081: ensureServers();
082: tcConfig.getServers().getServerArray(0).setData(path);
083: }
084:
085: public void setClientLogs(String path) {
086: ensureClients();
087: tcConfig.getClients().setLogs(path);
088: }
089:
090: public void addAutoLock(String pattern, String lockLevel) {
091: addAutoLock(pattern, lockLevel, false);
092: }
093:
094: public void addAutoLock(String pattern, String lockLevel,
095: boolean autoSynch) {
096: ensureLocks();
097: Autolock autoLock = tcConfig.getApplication().getDso()
098: .getLocks().insertNewAutolock(0);
099: autoLock.setMethodExpression(pattern);
100: autoLock.setLockLevel(LockLevel.Enum.forString(lockLevel));
101: if (autoSynch) {
102: autoLock.setAutoSynchronized(autoSynch);
103: }
104: }
105:
106: public void addRoot(String fieldName, String rootName) {
107: ensureRoots();
108: Root root = tcConfig.getApplication().getDso().getRoots()
109: .addNewRoot();
110: root.setFieldName(fieldName);
111: root.setRootName(rootName);
112: }
113:
114: public void addInstrumentedClass(String pattern) {
115: addInstrumentedClass(pattern, false);
116: }
117:
118: public void addInstrumentedClass(String pattern,
119: boolean honorTransient) {
120: ensureInstrumentedClasses();
121: Include include = tcConfig.getApplication().getDso()
122: .getInstrumentedClasses().insertNewInclude(0);
123: include.setClassExpression(pattern);
124: if (honorTransient) {
125: include.setHonorTransient(honorTransient);
126: }
127: }
128:
129: public void addBootJarClass(String classname) {
130: ensureBootJarClasses();
131: QualifiedClassName qcn = tcConfig.getApplication().getDso()
132: .getAdditionalBootJarClasses().insertNewInclude(0);
133: qcn.setStringValue(classname);
134: }
135:
136: public void addExclude(String pattern) {
137: ensureInstrumentedClasses();
138: tcConfig.getApplication().getDso().getInstrumentedClasses()
139: .addExclude(pattern);
140: }
141:
142: public void addModule(String name, String version) {
143: ensureModules();
144: Module newModule = tcConfig.getClients().getModules()
145: .insertNewModule(0);
146: newModule.setName(name);
147: newModule.setVersion(version);
148: }
149:
150: public void addRepository(String location) {
151: ensureModules();
152: tcConfig.getClients().getModules().addRepository(location);
153: }
154:
155: public void addWebApplication(String appName) {
156: addWebApplication(appName, false);
157: }
158:
159: public void addWebApplication(String appName, boolean synchWrite) {
160: ensureWebApplications();
161: WebApplication wa = tcConfig.getApplication().getDso()
162: .getWebApplications().insertNewWebApplication(0);
163: wa.setStringValue(appName);
164: if (synchWrite) {
165: wa.setSynchronousWrite(synchWrite);
166: }
167: }
168:
169: public String toString() {
170: return tcConfigDocument.toString();
171: }
172:
173: public void saveToFile(File filename) throws IOException {
174: InputStream is = null;
175: FileOutputStream fos = null;
176: try {
177: is = tcConfigDocument.newInputStream(getXmlOptions());
178: fos = new FileOutputStream(filename);
179: IOUtils.copy(tcConfigDocument
180: .newInputStream(getXmlOptions()), fos);
181: } finally {
182: IOUtils.closeQuietly(fos);
183: IOUtils.closeQuietly(is);
184: }
185: }
186:
187: private XmlOptions getXmlOptions() {
188: if (xmlOptions == null) {
189: xmlOptions = new XmlOptions();
190: xmlOptions.setLoadLineNumbers();
191: xmlOptions.setSavePrettyPrint();
192: xmlOptions.setSavePrettyPrintIndent(2);
193: }
194: return xmlOptions;
195: }
196:
197: private void ensureServers() {
198: if (!tcConfig.isSetServers()) {
199: tcConfig.addNewServers();
200: }
201: }
202:
203: private void ensureClients() {
204: if (!tcConfig.isSetClients()) {
205: tcConfig.addNewClients();
206: }
207: }
208:
209: private void ensureModules() {
210: ensureClients();
211: if (!tcConfig.getClients().isSetModules()) {
212: tcConfig.getClients().addNewModules();
213: }
214: }
215:
216: private void ensureApplication() {
217: if (!tcConfig.isSetApplication()) {
218: tcConfig.addNewApplication();
219: }
220: }
221:
222: private void ensureDso() {
223: ensureApplication();
224: if (!tcConfig.getApplication().isSetDso()) {
225: tcConfig.getApplication().addNewDso();
226: }
227: }
228:
229: private void ensureLocks() {
230: ensureDso();
231: if (!tcConfig.getApplication().getDso().isSetLocks()) {
232: tcConfig.getApplication().getDso().addNewLocks();
233: }
234: }
235:
236: private void ensureRoots() {
237: ensureDso();
238: if (!tcConfig.getApplication().getDso().isSetRoots()) {
239: tcConfig.getApplication().getDso().addNewRoots();
240: }
241: }
242:
243: private void ensureInstrumentedClasses() {
244: ensureDso();
245: if (!tcConfig.getApplication().getDso()
246: .isSetInstrumentedClasses()) {
247: tcConfig.getApplication().getDso()
248: .addNewInstrumentedClasses();
249: }
250: }
251:
252: private void ensureBootJarClasses() {
253: ensureDso();
254: if (!tcConfig.getApplication().getDso()
255: .isSetAdditionalBootJarClasses()) {
256: tcConfig.getApplication().getDso()
257: .addNewAdditionalBootJarClasses();
258: }
259: }
260:
261: private void ensureWebApplications() {
262: ensureDso();
263: if (!tcConfig.getApplication().getDso().isSetWebApplications()) {
264: tcConfig.getApplication().getDso().addNewWebApplications();
265: }
266: }
267:
268: public TcConfigBuilder copy() {
269: try {
270: TcConfigBuilder aCopy = new TcConfigBuilder(new Loader()
271: .parse(this .toString()));
272: return aCopy;
273: } catch (Exception e) {
274: throw new RuntimeException(e);
275: }
276: }
277:
278: public static void main(String[] args) {
279: TcConfigBuilder tc = new TcConfigBuilder();
280: tc.setDsoPort(3232);
281: tc.addModule("tc", "1.2");
282: tc.addModule("asdfa", "23432");
283: tc.setServerData("c:/temp");
284: tc.setClientLogs("c:/temp/logs");
285: tc.addAutoLock("* com.tctest.*.*(..)", "write", true);
286: tc.addAutoLock("* adfad..*()", "read");
287: tc.addRoot("com.tc.Test.field", "myField");
288: tc.addWebApplication("events", false);
289: tc.addBootJarClass("java.lang.Local");
290: TcConfigBuilder aCopy = tc.copy();
291: aCopy.addModule("hung", "huynh");
292: System.out.println(aCopy.toString());
293: }
294:
295: }
|