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.simulator.app;
006:
007: import com.tc.logging.TCLogger;
008: import com.tc.logging.TCLogging;
009: import com.tc.object.bytecode.hook.impl.PreparedComponentsFromL2Connection;
010: import com.tc.object.config.DSOClientConfigHelper;
011: import com.tc.object.loaders.IsolationClassLoader;
012: import com.tc.simulator.listener.ListenerProvider;
013:
014: import java.lang.reflect.Constructor;
015: import java.util.Iterator;
016: import java.util.Map;
017:
018: public class DSOApplicationBuilder implements ApplicationBuilder {
019:
020: private final ApplicationConfig applicationConfig;
021: private final ClassLoader classloader;
022:
023: private Class applicationClass;
024: private Constructor applicationConstructor;
025:
026: private boolean usesAdapters = false;
027:
028: public DSOApplicationBuilder(DSOClientConfigHelper config,
029: ApplicationConfig applicationConfig,
030: PreparedComponentsFromL2Connection components) {
031:
032: this (applicationConfig, new IsolationClassLoader(config,
033: components));
034: }
035:
036: public DSOApplicationBuilder(DSOClientConfigHelper config,
037: ApplicationConfig applicationConfig,
038: PreparedComponentsFromL2Connection components,
039: Map adapterMap) {
040:
041: this (applicationConfig, new IsolationClassLoader(config,
042: components));
043: if (adapterMap != null) {
044: if (adapterMap.size() > 0) {
045: usesAdapters = true;
046: }
047: for (Iterator iter = adapterMap.keySet().iterator(); iter
048: .hasNext();) {
049: String adapteeName = (String) iter.next();
050: Class adapterClass = (Class) adapterMap
051: .get(adapteeName);
052: ((IsolationClassLoader) classloader).addAdapter(
053: adapteeName, adapterClass);
054: }
055: }
056: }
057:
058: public DSOApplicationBuilder(ApplicationConfig applicationConfig,
059: ClassLoader classloader) {
060: this .applicationConfig = applicationConfig;
061: this .classloader = classloader;
062: }
063:
064: public void setAppConfigAttribute(String key, String value) {
065: applicationConfig.setAttribute(key, value);
066: }
067:
068: // XXX:: Adding more debugs to figure out the OOME in Primitive ArrayTest.
069: TCLogger logger = TCLogging.getLogger(DSOApplicationBuilder.class);
070:
071: public Application newApplication(String applicationId,
072: ListenerProvider listenerProvider)
073: throws ApplicationInstantiationException {
074:
075: // so the app instance can tell whether it has been adapted
076: applicationConfig.setAttribute(applicationId
077: + ApplicationConfig.ADAPTED_KEY, usesAdapters + "");
078:
079: try {
080: logger.info("Before initializing Class Loader...");
081: initializeClassLoader();
082: logger.info("After initializing Class Loader...");
083: Class applicationConfigClass = classloader
084: .loadClass(ApplicationConfig.class.getName());
085: Class listenerProviderClass = classloader
086: .loadClass(ListenerProvider.class.getName());
087: this .applicationClass = this .classloader
088: .loadClass(this .applicationConfig
089: .getApplicationClassname());
090: this .applicationConstructor = this .applicationClass
091: .getConstructor(new Class[] { String.class,
092: applicationConfigClass,
093: listenerProviderClass });
094: logger.info("Before new Instance is created...");
095:
096: return (Application) this .applicationConstructor
097: .newInstance(new Object[] { applicationId,
098: this .applicationConfig, listenerProvider });
099: } catch (Throwable t) {
100: t.printStackTrace();
101: throw new ApplicationInstantiationException(t);
102: }
103:
104: }
105:
106: private void initializeClassLoader() {
107: if (this .classloader instanceof IsolationClassLoader) {
108: ((IsolationClassLoader) this .classloader).init();
109: }
110: }
111:
112: public ClassLoader getContextClassLoader() {
113: return classloader;
114: }
115:
116: }
|