KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > config > ConfigLoader


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

5 package com.tc.object.config;
6
7 import org.apache.commons.lang.ArrayUtils;
8
9 import com.tc.config.schema.setup.ConfigurationSetupException;
10 import com.tc.logging.TCLogger;
11 import com.tc.object.config.schema.ExcludedInstrumentedClass;
12 import com.tc.object.config.schema.IncludeOnLoad;
13 import com.tc.object.config.schema.IncludedInstrumentedClass;
14 import com.tc.util.Assert;
15 import com.tc.util.ClassUtils;
16 import com.tc.util.ClassUtils.ClassSpec;
17 import com.terracottatech.config.AdditionalBootJarClasses;
18 import com.terracottatech.config.Autolock;
19 import com.terracottatech.config.DistributedMethods;
20 import com.terracottatech.config.DsoApplication;
21 import com.terracottatech.config.Include;
22 import com.terracottatech.config.InstrumentedClasses;
23 import com.terracottatech.config.LockLevel;
24 import com.terracottatech.config.Locks;
25 import com.terracottatech.config.NamedLock;
26 import com.terracottatech.config.NonDistributedFields;
27 import com.terracottatech.config.OnLoad;
28 import com.terracottatech.config.Root;
29 import com.terracottatech.config.Roots;
30 import com.terracottatech.config.SpringAppContext;
31 import com.terracottatech.config.SpringApplication;
32 import com.terracottatech.config.SpringApps;
33 import com.terracottatech.config.SpringBean;
34 import com.terracottatech.config.SpringDistributedEvent;
35 import com.terracottatech.config.SpringPath;
36 import com.terracottatech.config.TransientFields;
37 import com.terracottatech.config.WebApplication;
38 import com.terracottatech.config.WebApplications;
39 import com.terracottatech.config.DistributedMethods.MethodExpression;
40
41 import java.text.ParseException JavaDoc;
42 import java.util.Arrays JavaDoc;
43 import java.util.HashSet JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.Set JavaDoc;
46
47 public class ConfigLoader {
48   private final DSOClientConfigHelper config;
49   private final TCLogger logger;
50
51   public ConfigLoader(DSOClientConfigHelper config, TCLogger logger) {
52     this.config = config;
53     this.logger = logger;
54   }
55
56   public void loadDsoConfig(DsoApplication dsoApplication) throws ConfigurationSetupException {
57     if (dsoApplication != null) {
58       Roots rootsList = dsoApplication.getRoots();
59       if (rootsList != null && rootsList.getRootArray() != null) {
60         Root[] roots = rootsList.getRootArray();
61         for (int i = 0; i < roots.length; ++i) {
62           Root root = roots[i];
63           try {
64             ClassSpec classSpec = ClassUtils.parseFullyQualifiedFieldName(root.getFieldName());
65             String JavaDoc className = classSpec.getFullyQualifiedClassName();
66             String JavaDoc fieldName = classSpec.getShortFieldName();
67             String JavaDoc rootName = root.getRootName();
68             config.addRoot(className, fieldName, rootName, false);
69           } catch (ParseException JavaDoc pe) {
70             throw new ConfigurationSetupException("Root '" + root.getFieldName() + "' is invalid", pe);
71           }
72         }
73       }
74
75       WebApplications webApplicationsList = dsoApplication.getWebApplications();
76       if (webApplicationsList != null && webApplicationsList.getWebApplicationArray() != null) {
77         WebApplication[] webApplications = webApplicationsList.getWebApplicationArray();
78         for (int i = 0; i < webApplications.length; i++) {
79           config.addApplicationName(webApplications[i].getStringValue());
80           if (webApplications[i].getSynchronousWrite()) {
81             config.addSynchronousWriteApplication(webApplications[i].getStringValue());
82           }
83         }
84       }
85
86       loadLocks(dsoApplication.getLocks());
87       loadTransientFields(dsoApplication.getTransientFields());
88       loadInstrumentedClasses(dsoApplication.getInstrumentedClasses());
89       loadDistributedMethods(dsoApplication.getDistributedMethods());
90
91       AdditionalBootJarClasses additionalBootJarClassesList = dsoApplication.getAdditionalBootJarClasses();
92       // XXX
93
if (additionalBootJarClassesList != null) {
94         Set JavaDoc userDefinedBootClassNames = new HashSet JavaDoc();
95         userDefinedBootClassNames.addAll(Arrays.asList(additionalBootJarClassesList.getIncludeArray()));
96         logger.debug("Additional boot-jar classes: " + ArrayUtils.toString(userDefinedBootClassNames));
97
98         for (Iterator JavaDoc i = userDefinedBootClassNames.iterator(); i.hasNext();) {
99           String JavaDoc className = (String JavaDoc) i.next();
100           TransparencyClassSpec spec = config.getSpec(className);
101           if (spec == null) {
102             spec = new TransparencyClassSpec(className, config);
103             spec.markPreInstrumented();
104             config.addUserDefinedBootSpec(spec.getClassName(), spec);
105           } else if (!spec.isPreInstrumented()) {
106             // DEV-458: if the class being added to the boot jar defines locks/distributed methods/etc. it creates a
107
// spec but does not pre-instrument it. This makes sure that the adapted code gets into the boot jar in
108
// this case.
109
spec.markPreInstrumented();
110           }
111         }
112       }
113     }
114   }
115
116   public void loadSpringConfig(SpringApplication springApplication) throws ConfigurationSetupException {
117     if (springApplication != null) {
118       SpringApps[] springApps = springApplication.getJeeApplicationArray();
119       for (int i = 0; springApps != null && i < springApps.length; i++) {
120         SpringApps springApp = springApps[i];
121         if (springApp != null) {
122           loadSpringApp(springApp);
123         }
124       }
125     }
126   }
127
128   private void loadSpringApp(SpringApps springApp) throws ConfigurationSetupException {
129     // TODO scope the following by app namespace https://jira.terracotta.lan/jira/browse/LKC-2284
130
loadLocks(springApp.getLocks());
131     loadTransientFields(springApp.getTransientFields());
132     loadInstrumentedClasses(springApp.getInstrumentedClasses());
133
134     if (springApp.getSessionSupport()) {
135       config.addApplicationName(springApp.getName()); // enable session support
136
}
137
138     if (springApp.getApplicationContexts() != null) {
139       loadSpringAppContexts(springApp);
140     }
141   }
142
143   private void loadSpringAppContexts(SpringApps springApp) {
144     String JavaDoc appName = springApp.getName();
145     boolean fastProxy = springApp.getFastProxy();
146     SpringAppContext[] applicationContexts = springApp.getApplicationContexts().getApplicationContextArray();
147     for (int i = 0; applicationContexts != null && i < applicationContexts.length; i++) {
148       SpringAppContext appContext = applicationContexts[i];
149       if (appContext == null) continue;
150
151       DSOSpringConfigHelper springConfigHelper = new StandardDSOSpringConfigHelper();
152       springConfigHelper.addApplicationNamePattern(appName);
153       springConfigHelper.setFastProxyEnabled(fastProxy); // copy flag to all subcontexts
154
springConfigHelper.setRootName(appContext.getRootName());
155       springConfigHelper.setLocationInfoEnabled(appContext.getEnableLocationInfo());
156
157       SpringDistributedEvent distributedEventList = appContext.getDistributedEvents();
158       if (distributedEventList != null) {
159         String JavaDoc[] distributedEvents = distributedEventList.getDistributedEventArray();
160         for (int k = 0; distributedEvents != null && k < distributedEvents.length; k++) {
161           springConfigHelper.addDistributedEvent(distributedEvents[k]);
162         }
163       }
164
165       SpringPath pathList = appContext.getPaths();
166       if (pathList != null) {
167         String JavaDoc[] paths = pathList.getPathArray();
168         for (int j = 0; paths != null && j < paths.length; j++) {
169           springConfigHelper.addConfigPattern(paths[j]);
170         }
171       }
172
173       SpringBean springBean = appContext.getBeans();
174       if (springBean != null) {
175         NonDistributedFields[] nonDistributedFields = springBean.getBeanArray();
176         for (int j = 0; nonDistributedFields != null && j < nonDistributedFields.length; j++) {
177           NonDistributedFields nonDistributedField = nonDistributedFields[j];
178
179           String JavaDoc beanName = nonDistributedField.getName();
180           springConfigHelper.addBean(beanName);
181
182           String JavaDoc[] fields = nonDistributedField.getNonDistributedFieldArray();
183           for (int k = 0; fields != null && k < fields.length; k++) {
184             springConfigHelper.excludeField(beanName, fields[k]);
185           }
186         }
187       }
188
189       config.addDSOSpringConfig(springConfigHelper);
190     }
191   }
192
193   private ConfigLockLevel getLockLevel(LockLevel.Enum lockLevel) {
194     if (lockLevel == null || LockLevel.WRITE.equals(lockLevel)) {
195       return ConfigLockLevel.WRITE;
196     } else if (LockLevel.CONCURRENT.equals(lockLevel)) {
197       return ConfigLockLevel.CONCURRENT;
198     } else if (LockLevel.READ.equals(lockLevel)) {
199       return ConfigLockLevel.READ;
200     } else if (LockLevel.SYNCHRONOUS_WRITE.equals(lockLevel)) { return ConfigLockLevel.SYNCHRONOUS_WRITE; }
201     throw Assert.failure("Unknown lock level " + lockLevel);
202   }
203
204   private void loadLocks(Locks lockList) {
205     if (lockList == null) return;
206
207     Autolock[] autolocks = lockList.getAutolockArray();
208     for (int i = 0; autolocks != null && i < autolocks.length; i++) {
209       config.addAutolock(autolocks[i].getMethodExpression(), getLockLevel(autolocks[i].getLockLevel()));
210     }
211
212     NamedLock[] namedLocks = lockList.getNamedLockArray();
213     for (int i = 0; namedLocks != null && i < namedLocks.length; i++) {
214       NamedLock namedLock = namedLocks[i];
215       LockDefinition lockDefinition = new LockDefinition(namedLock.getLockName(),
216                                                          getLockLevel(namedLock.getLockLevel()));
217       lockDefinition.commit();
218       config.addLock(namedLock.getMethodExpression(), lockDefinition);
219     }
220   }
221
222   private void loadTransientFields(TransientFields transientFieldsList) throws ConfigurationSetupException {
223     if (transientFieldsList != null) {
224       String JavaDoc[] transientFields = transientFieldsList.getFieldNameArray();
225       try {
226         for (int i = 0; transientFields != null && i < transientFields.length; i++) {
227           ClassSpec spec = ClassUtils.parseFullyQualifiedFieldName(transientFields[i]);
228           config.addTransient(spec.getFullyQualifiedClassName(), spec.getShortFieldName());
229         }
230       } catch (ParseException JavaDoc e) {
231         throw new ConfigurationSetupException(e.getLocalizedMessage(), e);
232       }
233     }
234   }
235
236   private void loadInstrumentedClasses(InstrumentedClasses instrumentedClasses) {
237     if (instrumentedClasses != null) {
238       Include[] includes = instrumentedClasses.getIncludeArray();
239       for (int i = 0; includes != null && i < includes.length; i++) {
240         Include include = includes[i];
241         IncludeOnLoad includeOnLoad = new IncludeOnLoad();
242         OnLoad onLoad = include.getOnLoad();
243         if (onLoad != null) {
244           if (onLoad.getExecute() != null) {
245             includeOnLoad = new IncludeOnLoad(IncludeOnLoad.EXECUTE, onLoad.getExecute());
246           } else if (onLoad.getMethod() != null) {
247             includeOnLoad = new IncludeOnLoad(IncludeOnLoad.METHOD, onLoad.getMethod());
248           }
249         }
250         config.addInstrumentationDescriptor(new IncludedInstrumentedClass(include.getClassExpression(), include
251             .getHonorTransient(), false, includeOnLoad));
252       }
253
254       String JavaDoc[] excludeArray = instrumentedClasses.getExcludeArray();
255       for (int i = 0; excludeArray != null && i < excludeArray.length; i++) {
256         config.addInstrumentationDescriptor(new ExcludedInstrumentedClass(excludeArray[i]));
257       }
258     }
259   }
260
261   private void loadDistributedMethods(DistributedMethods distributedMethods) {
262     if (distributedMethods != null) {
263       MethodExpression[] methodExpressions = distributedMethods.getMethodExpressionArray();
264       for (int i = 0; methodExpressions != null && i < methodExpressions.length; i++) {
265         final MethodExpression me = methodExpressions[i];
266         final DistributedMethodSpec dms = new DistributedMethodSpec(me.getStringValue(), me.getRunOnAllNodes());
267         config.addDistributedMethodCall(dms);
268       }
269     }
270   }
271
272 }
273
Popular Tags