001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso;
006:
007: import org.osgi.framework.Bundle;
008:
009: import com.terracottatech.config.AdditionalBootJarClasses;
010: import com.terracottatech.config.Autolock;
011: import com.terracottatech.config.DistributedMethods;
012: import com.terracottatech.config.DsoApplication;
013: import com.terracottatech.config.Include;
014: import com.terracottatech.config.InstrumentedClasses;
015: import com.terracottatech.config.Locks;
016: import com.terracottatech.config.Module;
017: import com.terracottatech.config.NamedLock;
018: import com.terracottatech.config.Root;
019: import com.terracottatech.config.Roots;
020: import com.terracottatech.config.TransientFields;
021: import com.terracottatech.config.WebApplication;
022: import com.terracottatech.config.WebApplications;
023: import com.terracottatech.config.DistributedMethods.MethodExpression;
024:
025: import java.net.URL;
026: import java.util.ArrayList;
027: import java.util.Arrays;
028: import java.util.List;
029:
030: public class ModulesConfiguration {
031: private List<ModuleInfo> fModuleInfoList;
032: private DsoApplication fApplication;
033:
034: public ModulesConfiguration() {
035: fModuleInfoList = new ArrayList();
036: fApplication = DsoApplication.Factory.newInstance();
037: }
038:
039: public ModuleInfo add(Module module) {
040: ModuleInfo moduleInfo = new ModuleInfo(module);
041: fModuleInfoList.add(moduleInfo);
042: return moduleInfo;
043: }
044:
045: public ModuleInfo getOrAdd(Module module) {
046: ModuleInfo moduleInfo = getModuleInfo(module);
047: if (moduleInfo == null) {
048: moduleInfo = add(module);
049: }
050: return moduleInfo;
051: }
052:
053: public static boolean sameModule(Module m1, Module m2) {
054: return m1 != null && m2 != null
055: && m1.getName().equals(m2.getName())
056: && m1.getGroupId().equals(m2.getGroupId())
057: && m1.getVersion().equals(m2.getVersion());
058: }
059:
060: public ModuleInfo getModuleInfo(Module module) {
061: for (ModuleInfo moduleInfo : fModuleInfoList) {
062: if (sameModule(module, moduleInfo.getModule())) {
063: return moduleInfo;
064: }
065: }
066: return null;
067: }
068:
069: public ModuleInfo associateBundle(Bundle bundle) {
070: for (ModuleInfo moduleInfo : fModuleInfoList) {
071: URL location = moduleInfo.getLocation();
072: String bundleLocation = bundle.getLocation();
073:
074: if (location != null
075: && location.toString().equals(bundleLocation)) {
076: moduleInfo.setBundle(bundle);
077: return moduleInfo;
078: }
079: }
080: return null;
081: }
082:
083: public void setModuleApplication(ModuleInfo moduleInfo,
084: DsoApplication application) {
085: moduleInfo.setApplication(application);
086: merge(application);
087: }
088:
089: public DsoApplication getApplication() {
090: return fApplication;
091: }
092:
093: public void merge(DsoApplication application) {
094: merge(application, fApplication);
095: }
096:
097: public static void merge(DsoApplication src, DsoApplication dest) {
098: if (src.isSetInstrumentedClasses()) {
099: InstrumentedClasses srcInstrumentedClasses = src
100: .getInstrumentedClasses();
101: InstrumentedClasses destInstrumentedClasses = dest
102: .isSetInstrumentedClasses() ? dest
103: .getInstrumentedClasses() : dest
104: .addNewInstrumentedClasses();
105: merge(srcInstrumentedClasses, destInstrumentedClasses);
106: }
107:
108: if (src.isSetTransientFields()) {
109: TransientFields srcTransientFields = src
110: .getTransientFields();
111: TransientFields destTransientFields = dest
112: .isSetTransientFields() ? dest.getTransientFields()
113: : dest.addNewTransientFields();
114: merge(srcTransientFields, destTransientFields);
115: }
116:
117: if (src.isSetLocks()) {
118: Locks srcLocks = src.getLocks();
119: Locks destLocks = dest.isSetLocks() ? dest.getLocks()
120: : dest.addNewLocks();
121: merge(srcLocks, destLocks);
122: }
123:
124: if (src.isSetRoots()) {
125: Roots srcRoots = src.getRoots();
126: Roots destRoots = dest.isSetRoots() ? dest.getRoots()
127: : dest.addNewRoots();
128: merge(srcRoots, destRoots);
129: }
130:
131: if (src.isSetDistributedMethods()) {
132: DistributedMethods srcDistributedMethods = src
133: .getDistributedMethods();
134: DistributedMethods destDistributedMethods = dest
135: .isSetDistributedMethods() ? dest
136: .getDistributedMethods() : dest
137: .addNewDistributedMethods();
138: merge(srcDistributedMethods, destDistributedMethods);
139: }
140:
141: if (src.isSetAdditionalBootJarClasses()) {
142: AdditionalBootJarClasses srcBootClasses = src
143: .getAdditionalBootJarClasses();
144: AdditionalBootJarClasses destBootClasses = dest
145: .isSetAdditionalBootJarClasses() ? dest
146: .getAdditionalBootJarClasses() : dest
147: .addNewAdditionalBootJarClasses();
148: merge(srcBootClasses, destBootClasses);
149: }
150:
151: if (src.isSetWebApplications()) {
152: WebApplications srcWebApps = src.getWebApplications();
153: WebApplications destWebApps = dest.isSetWebApplications() ? dest
154: .getWebApplications()
155: : dest.addNewWebApplications();
156: merge(srcWebApps, destWebApps);
157: }
158:
159: if (src.isSetDsoReflectionEnabled()) {
160: dest.setDsoReflectionEnabled(src.getDsoReflectionEnabled());
161: }
162: }
163:
164: public static void merge(InstrumentedClasses src,
165: InstrumentedClasses dest) {
166: Include[] includes = src.getIncludeArray();
167: List<Include> includeList = new ArrayList(Arrays
168: .asList(includes));
169:
170: for (Include include : dest.getIncludeArray()) {
171: includeList.add((Include) include.copy());
172: }
173: dest.setIncludeArray(includeList.toArray(new Include[0]));
174: }
175:
176: public static void merge(TransientFields src, TransientFields dest) {
177: String[] fields = src.getFieldNameArray();
178: List<String> fieldList = new ArrayList(Arrays.asList(fields));
179:
180: fieldList.addAll(Arrays.asList(dest.getFieldNameArray()));
181: dest.setFieldNameArray(fieldList.toArray(new String[0]));
182: }
183:
184: public static void merge(Locks src, Locks dest) {
185: Autolock[] autolocks = src.getAutolockArray();
186: List<Autolock> autolockList = new ArrayList(Arrays
187: .asList(autolocks));
188:
189: for (Autolock autolock : dest.getAutolockArray()) {
190: autolockList.add((Autolock) autolock.copy());
191: }
192: dest.setAutolockArray(autolockList.toArray(new Autolock[0]));
193:
194: NamedLock[] namedLocks = src.getNamedLockArray();
195: List<NamedLock> namedLockList = new ArrayList(Arrays
196: .asList(namedLocks));
197:
198: for (NamedLock namedLock : dest.getNamedLockArray()) {
199: namedLockList.add((NamedLock) namedLock.copy());
200: }
201: dest.setNamedLockArray(namedLockList.toArray(new NamedLock[0]));
202: }
203:
204: public static void merge(Roots src, Roots dest) {
205: Root[] roots = src.getRootArray();
206: List<Root> rootList = new ArrayList(Arrays.asList(roots));
207:
208: for (Root root : dest.getRootArray()) {
209: rootList.add((Root) root.copy());
210: }
211: dest.setRootArray(rootList.toArray(new Root[0]));
212: }
213:
214: public static void merge(DistributedMethods src,
215: DistributedMethods dest) {
216: MethodExpression[] methodExprs = src.getMethodExpressionArray();
217: List<MethodExpression> exprList = new ArrayList(Arrays
218: .asList(methodExprs));
219:
220: for (MethodExpression expr : dest.getMethodExpressionArray()) {
221: exprList.add((MethodExpression) expr.copy());
222: }
223: dest.setMethodExpressionArray(exprList
224: .toArray(new MethodExpression[0]));
225: }
226:
227: public static void merge(AdditionalBootJarClasses src,
228: AdditionalBootJarClasses dest) {
229: String[] includes = src.getIncludeArray();
230: List<String> includeList = new ArrayList(Arrays
231: .asList(includes));
232:
233: includeList.addAll(Arrays.asList(dest.getIncludeArray()));
234: dest.setIncludeArray(includeList.toArray(new String[0]));
235: }
236:
237: public static void merge(WebApplications src, WebApplications dest) {
238: WebApplication[] webApps = src.getWebApplicationArray();
239: List<WebApplication> webAppList = new ArrayList(Arrays
240: .asList(webApps));
241:
242: for (WebApplication webApp : dest.getWebApplicationArray()) {
243: webAppList.add((WebApplication) webApp.copy());
244: }
245: dest.setWebApplicationArray(webAppList
246: .toArray(new WebApplication[0]));
247: }
248:
249: }
|