KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > config > schema > ConfigTranslationHelper


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

5 package com.tc.object.config.schema;
6
7 import org.apache.xmlbeans.XmlObject;
8
9 import com.tc.util.Assert;
10 import com.terracottatech.config.Autolock;
11 import com.terracottatech.config.ClassExpression;
12 import com.terracottatech.config.Include;
13 import com.terracottatech.config.LockLevel;
14 import com.terracottatech.config.Locks;
15 import com.terracottatech.config.NamedLock;
16 import com.terracottatech.config.OnLoad;
17
18 /**
19  * Knows how to translate various chunks of config between their XMLBeans representations and the internal
20  * representations we actually use.
21  */

22 public class ConfigTranslationHelper {
23
24   static Object JavaDoc translateIncludes(XmlObject xmlObject) {
25     if (xmlObject == null) return null;
26
27     XmlObject[] objects = xmlObject.selectPath("*");
28     InstrumentedClass[] classes = new InstrumentedClass[objects.length];
29
30     Assert.eval(classes.length == objects.length);
31     for (int i = 0; i < objects.length; ++i) {
32       if (objects[i] instanceof Include) {
33         Include theInclude = (Include) objects[i];
34
35         // XXX: "honor volatile" should probably be exposed in config
36
boolean honorVolatile = false;
37
38         classes[i] = new IncludedInstrumentedClass(theInclude.getClassExpression(), theInclude.getHonorTransient(),
39             honorVolatile, ConfigTranslationHelper.createOnLoadObj(theInclude.getOnLoad()));
40       } else if (objects[i] instanceof ClassExpression) {
41         ClassExpression theExpression = (ClassExpression) objects[i];
42         classes[i] = new ExcludedInstrumentedClass(theExpression.getStringValue());
43       } else {
44         throw Assert
45             .failure("Child #" + i + " of the <instrumented-classes> element appears to be "
46                 + "neither an <include> nor an <exclude> element. This is a programming error in "
47                 + "Terracotta software.");
48       }
49     }
50
51     return classes;
52   }
53
54   static Object JavaDoc translateLocks(XmlObject xmlObject) {
55     if (xmlObject == null) return null;
56
57     NamedLock[] namedLocks = ((Locks) xmlObject).getNamedLockArray();
58     Autolock[] autoLocks = ((Locks) xmlObject).getAutolockArray();
59
60     int namedLength = namedLocks == null ? 0 : namedLocks.length;
61     int autoLength = autoLocks == null ? 0 : autoLocks.length;
62
63     Lock[] out = new Lock[namedLength + autoLength];
64     for (int i = 0; i < namedLength; ++i) {
65       com.tc.object.config.schema.LockLevel level;
66
67       if (namedLocks[i].getLockLevel() != null) {
68         if (namedLocks[i].getLockLevel().equals(LockLevel.CONCURRENT)) level = com.tc.object.config.schema.LockLevel.CONCURRENT;
69         else if (namedLocks[i].getLockLevel().equals(LockLevel.READ)) level = com.tc.object.config.schema.LockLevel.READ;
70         else if (namedLocks[i].getLockLevel().equals(LockLevel.WRITE)) level = com.tc.object.config.schema.LockLevel.WRITE;
71         else throw Assert.failure("Unknown lock level " + namedLocks[i].getLockLevel());
72       } else {
73         level = com.tc.object.config.schema.LockLevel.WRITE;
74       }
75
76       out[i] = new com.tc.object.config.schema.NamedLock(namedLocks[i].getLockName(), namedLocks[i]
77           .getMethodExpression(), level);
78     }
79
80     for (int i = 0; i < autoLength; ++i) {
81       com.tc.object.config.schema.LockLevel level;
82
83       if (autoLocks[i].getLockLevel() != null) {
84         if (autoLocks[i].getLockLevel().equals(LockLevel.CONCURRENT)) level = com.tc.object.config.schema.LockLevel.CONCURRENT;
85         else if (autoLocks[i].getLockLevel().equals(LockLevel.READ)) level = com.tc.object.config.schema.LockLevel.READ;
86         else if (autoLocks[i].getLockLevel().equals(LockLevel.WRITE)) level = com.tc.object.config.schema.LockLevel.WRITE;
87         else if (autoLocks[i].getLockLevel().equals(LockLevel.SYNCHRONOUS_WRITE)) level = com.tc.object.config.schema.LockLevel.SYNCHRONOUS_WRITE;
88         else throw Assert.failure("Unknown lock level " + namedLocks[i].getLockLevel());
89       } else {
90         level = com.tc.object.config.schema.LockLevel.WRITE;
91       }
92
93       out[namedLength + i] = new com.tc.object.config.schema.AutoLock(autoLocks[i].getMethodExpression(), level);
94     }
95
96     return out;
97   }
98
99   private static IncludeOnLoad createOnLoadObj(OnLoad xmlOnLoad) {
100     if (xmlOnLoad == null) return new IncludeOnLoad();
101
102     Object JavaDoc value;
103     if ((value = xmlOnLoad.getExecute()) != null) return new IncludeOnLoad(IncludeOnLoad.EXECUTE, value);
104     if ((value = xmlOnLoad.getMethod()) != null) return new IncludeOnLoad(IncludeOnLoad.METHOD, value);
105
106     return new IncludeOnLoad();
107   }
108
109 }
110
Popular Tags