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: */
005: package com.tc.object.config.schema;
006:
007: import org.apache.xmlbeans.XmlObject;
008:
009: import com.tc.util.Assert;
010: import com.terracottatech.config.Autolock;
011: import com.terracottatech.config.ClassExpression;
012: import com.terracottatech.config.Include;
013: import com.terracottatech.config.LockLevel;
014: import com.terracottatech.config.Locks;
015: import com.terracottatech.config.NamedLock;
016: import com.terracottatech.config.OnLoad;
017:
018: /**
019: * Knows how to translate various chunks of config between their XMLBeans representations and the internal
020: * representations we actually use.
021: */
022: public class ConfigTranslationHelper {
023:
024: static Object translateIncludes(XmlObject xmlObject) {
025: if (xmlObject == null)
026: return null;
027:
028: XmlObject[] objects = xmlObject.selectPath("*");
029: InstrumentedClass[] classes = new InstrumentedClass[objects.length];
030:
031: Assert.eval(classes.length == objects.length);
032: for (int i = 0; i < objects.length; ++i) {
033: if (objects[i] instanceof Include) {
034: Include theInclude = (Include) objects[i];
035:
036: // XXX: "honor volatile" should probably be exposed in config
037: boolean honorVolatile = false;
038:
039: classes[i] = new IncludedInstrumentedClass(
040: theInclude.getClassExpression(),
041: theInclude.getHonorTransient(),
042: honorVolatile,
043: ConfigTranslationHelper
044: .createOnLoadObj(theInclude.getOnLoad()));
045: } else if (objects[i] instanceof ClassExpression) {
046: ClassExpression theExpression = (ClassExpression) objects[i];
047: classes[i] = new ExcludedInstrumentedClass(
048: theExpression.getStringValue());
049: } else {
050: throw Assert
051: .failure("Child #"
052: + i
053: + " of the <instrumented-classes> element appears to be "
054: + "neither an <include> nor an <exclude> element. This is a programming error in "
055: + "Terracotta software.");
056: }
057: }
058:
059: return classes;
060: }
061:
062: static Object translateLocks(XmlObject xmlObject) {
063: if (xmlObject == null)
064: return null;
065:
066: NamedLock[] namedLocks = ((Locks) xmlObject)
067: .getNamedLockArray();
068: Autolock[] autoLocks = ((Locks) xmlObject).getAutolockArray();
069:
070: int namedLength = namedLocks == null ? 0 : namedLocks.length;
071: int autoLength = autoLocks == null ? 0 : autoLocks.length;
072:
073: Lock[] out = new Lock[namedLength + autoLength];
074: for (int i = 0; i < namedLength; ++i) {
075: com.tc.object.config.schema.LockLevel level;
076:
077: if (namedLocks[i].getLockLevel() != null) {
078: if (namedLocks[i].getLockLevel().equals(
079: LockLevel.CONCURRENT))
080: level = com.tc.object.config.schema.LockLevel.CONCURRENT;
081: else if (namedLocks[i].getLockLevel().equals(
082: LockLevel.READ))
083: level = com.tc.object.config.schema.LockLevel.READ;
084: else if (namedLocks[i].getLockLevel().equals(
085: LockLevel.WRITE))
086: level = com.tc.object.config.schema.LockLevel.WRITE;
087: else
088: throw Assert.failure("Unknown lock level "
089: + namedLocks[i].getLockLevel());
090: } else {
091: level = com.tc.object.config.schema.LockLevel.WRITE;
092: }
093:
094: out[i] = new com.tc.object.config.schema.NamedLock(
095: namedLocks[i].getLockName(), namedLocks[i]
096: .getMethodExpression(), level);
097: }
098:
099: for (int i = 0; i < autoLength; ++i) {
100: com.tc.object.config.schema.LockLevel level;
101:
102: if (autoLocks[i].getLockLevel() != null) {
103: if (autoLocks[i].getLockLevel().equals(
104: LockLevel.CONCURRENT))
105: level = com.tc.object.config.schema.LockLevel.CONCURRENT;
106: else if (autoLocks[i].getLockLevel().equals(
107: LockLevel.READ))
108: level = com.tc.object.config.schema.LockLevel.READ;
109: else if (autoLocks[i].getLockLevel().equals(
110: LockLevel.WRITE))
111: level = com.tc.object.config.schema.LockLevel.WRITE;
112: else if (autoLocks[i].getLockLevel().equals(
113: LockLevel.SYNCHRONOUS_WRITE))
114: level = com.tc.object.config.schema.LockLevel.SYNCHRONOUS_WRITE;
115: else
116: throw Assert.failure("Unknown lock level "
117: + namedLocks[i].getLockLevel());
118: } else {
119: level = com.tc.object.config.schema.LockLevel.WRITE;
120: }
121:
122: out[namedLength + i] = new com.tc.object.config.schema.AutoLock(
123: autoLocks[i].getMethodExpression(), level);
124: }
125:
126: return out;
127: }
128:
129: private static IncludeOnLoad createOnLoadObj(OnLoad xmlOnLoad) {
130: if (xmlOnLoad == null)
131: return new IncludeOnLoad();
132:
133: Object value;
134: if ((value = xmlOnLoad.getExecute()) != null)
135: return new IncludeOnLoad(IncludeOnLoad.EXECUTE, value);
136: if ((value = xmlOnLoad.getMethod()) != null)
137: return new IncludeOnLoad(IncludeOnLoad.METHOD, value);
138:
139: return new IncludeOnLoad();
140: }
141:
142: }
|