001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: UpocInstance.java 11995 2008-02-18 08:39:52Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.lib;
025:
026: import java.io.File;
027: import java.io.FileNotFoundException;
028: import java.io.FileReader;
029: import java.util.concurrent.ConcurrentHashMap;
030:
031: import javax.script.Compilable;
032: import javax.script.CompiledScript;
033: import javax.script.Namespace;
034: import javax.script.ScriptEngine;
035: import javax.script.ScriptEngineManager;
036: import javax.script.ScriptException;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: import com.bostechcorp.cbesb.common.util.RuntimeClassLoader;
042:
043: public class UpocInstance {
044: protected static Log log = LogFactory.getLog(UpocInstance.class);
045:
046: private static ConcurrentHashMap<UpocInstanceKey, UpocInstance> upocInstanceMap = new ConcurrentHashMap<UpocInstanceKey, UpocInstance>();
047: private static ScriptEngineManager manager = new ScriptEngineManager();
048:
049: private CompiledScript this Compiled;
050: private Namespace this Namespace;
051: private ScriptEngine this Engine;
052: private Class this Class;
053: private Object this ClassInstance;
054:
055: public UpocInstance(CompiledScript compiled, Namespace namespace,
056: ScriptEngine engine, Class pojoClass,
057: Object pojoClassInstance) {
058: this Compiled = compiled;
059: this Namespace = namespace;
060: this Engine = engine;
061: this Class = pojoClass;
062: this ClassInstance = pojoClassInstance;
063: }
064:
065: public static synchronized void removeInstances(String upocRootDir) {
066: log.debug("removing upoc instances for " + upocRootDir);
067: log.debug("upocInstanceMap's length:" + upocInstanceMap.size());
068: log.debug("upocInstanceMap is" + upocInstanceMap);
069:
070: for (UpocInstanceKey key : upocInstanceMap.keySet()) {
071: log.debug("checking key's rootdir:" + key.getRootDir());
072: if (key.getRootDir().equals(upocRootDir)) {
073: log.debug("remove upoc instance for key's rootdir:"
074: + key.getRootDir());
075: upocInstanceMap.remove(key);
076: }
077: }
078:
079: }
080:
081: public static synchronized UpocInstance getUpocInstance(
082: String upocType, String upocClass, String upocRootDir)
083: throws ScriptException, FileNotFoundException,
084: ClassNotFoundException, InstantiationException,
085: IllegalAccessException {
086: return getUpocInstance(upocType, upocClass, upocRootDir, null);
087: }
088:
089: public static synchronized UpocInstance getUpocInstance(
090: String upocType, String upocClass, String upocRootDir,
091: Object parent) throws ScriptException,
092: FileNotFoundException, ClassNotFoundException,
093: InstantiationException, IllegalAccessException {
094: UpocInstanceKey this Key = new UpocInstanceKey(upocType,
095: upocClass, upocRootDir);
096: UpocInstance this Instance = (UpocInstance) upocInstanceMap
097: .get(this Key);
098: log.debug("thisKey=" + this Key);
099: log.debug("getUpocInstance()\n type=" + upocType
100: + "\n class=" + upocClass + "\n root="
101: + upocRootDir + "\n parent=" + parent
102: + "\n thisInstance=" + this Instance + "\n\n");
103: log.debug("upocInstanceMap is" + upocInstanceMap);
104:
105: if (this Instance == null) {
106: // instantiate a new UpocInstance
107: if (upocType.equalsIgnoreCase("Groovy")) {
108: ScriptEngine se = manager.getEngineByName(upocType);
109: CompiledScript compiled = (((Compilable) se)
110: .compile(new FileReader(new File(upocRootDir,
111: upocClass))));
112: Namespace namespace = se.createNamespace();
113: compiled.eval(namespace);
114: this Instance = new UpocInstance(compiled, namespace,
115: se, null, null);
116: log.debug("put upoc key to map" + this Key.getRootDir());
117:
118: upocInstanceMap.put(this Key, this Instance);
119: } else if (upocType.equalsIgnoreCase("Pojo")
120: || upocType.equalsIgnoreCase("Java")) {
121: String saName = CcslUtil
122: .getAssemblyNameFromWorkspaceDir(upocRootDir);
123: log.debug("loading "
124: + upocClass
125: + " loader="
126: + RuntimeClassLoader.getClassLoader(saName,
127: parent));
128: Class pojoClass = Class.forName(upocClass, true,
129: RuntimeClassLoader.getClassLoader(saName,
130: parent));
131: this Instance = new UpocInstance(null, null, null,
132: pojoClass, pojoClass.newInstance());
133: log.debug("put upoc key to map" + this Key.getRootDir());
134: upocInstanceMap.put(this Key, this Instance);
135:
136: }
137: }
138: return this Instance;
139: }
140:
141: public ScriptEngine getEngine() {
142: return this Engine;
143: }
144:
145: public Namespace getNamespace() {
146: return this Namespace;
147: }
148:
149: public CompiledScript getCompiledScript() {
150: return this Compiled;
151: }
152:
153: public Class getPojoClass() {
154: return this Class;
155: }
156:
157: public Object getPojoClassInstance() {
158: return thisClassInstance;
159: }
160:
161: }
|