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: CcslUpoc.java 9472 2007-10-09 15:24:17Z elu $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.lib;
025:
026: import java.io.FileNotFoundException;
027: import java.lang.reflect.InvocationTargetException;
028: import java.lang.reflect.Method;
029: import java.util.LinkedList;
030: import java.util.Map;
031:
032: import javax.jbi.component.ComponentContext;
033: import javax.jbi.messaging.DeliveryChannel;
034: import javax.jbi.messaging.MessageExchange;
035: import javax.script.Invocable;
036: import javax.script.ScriptEngine;
037: import javax.script.ScriptException;
038:
039: import org.apache.commons.logging.Log;
040:
041: import com.bostechcorp.cbesb.common.runtime.*;
042:
043: public class CcslUpoc {
044:
045: public static Object runScript(Log log, String rootDir,
046: String upocContext, String upocType, String upocClass,
047: String upocMethod, ComponentContext componentContext,
048: DeliveryChannel channel, MessageExchange exchange,
049: Map<String, String> params, Object parent)
050: throws CbesbException {
051: LinkedList sendList = new LinkedList();
052:
053: // run the script
054: Object[] scriptArgs = { log, upocContext, componentContext,
055: channel, exchange, params };
056: Object rtn = runScript(rootDir, upocType, upocClass,
057: upocMethod, scriptArgs, parent);
058:
059: if (rtn instanceof LinkedList)
060: sendList = (LinkedList) rtn;
061: return sendList;
062: }
063:
064: public static Object runScript(Log log, String rootDir,
065: String upocType, String upocClass, String upocMethod,
066: ComponentContext componentContext, DeliveryChannel channel,
067: MessageExchange exchange, Map<String, String> params)
068: throws CbesbException {
069: LinkedList sendList = new LinkedList();
070:
071: // run the script
072: Object[] scriptArgs = { log, componentContext, channel,
073: exchange, params };
074: Object rtn = runScript(rootDir, upocType, upocClass,
075: upocMethod, scriptArgs);
076:
077: if (rtn instanceof LinkedList)
078: sendList = (LinkedList) rtn;
079: return sendList;
080: }
081:
082: public static Object runScript(Log log, String rootDir,
083: String upocType, String upocClass, String upocMethod,
084: ComponentContext componentContext, DeliveryChannel channel,
085: Map<String, String> params) throws CbesbException {
086: LinkedList sendList = new LinkedList();
087:
088: // run the script
089: Object[] scriptArgs = { log, componentContext, channel, params };
090: Object rtn = runScript(rootDir, upocType, upocClass,
091: upocMethod, scriptArgs);
092:
093: if (rtn instanceof LinkedList)
094: sendList = (LinkedList) rtn;
095: return sendList;
096: }
097:
098: public static Object runScript(String rootDir, String upocType,
099: String upocClass, String upocMethod, Object[] scriptArgs)
100: throws CbesbException {
101: return runScript(rootDir, upocType, upocClass, upocMethod,
102: scriptArgs, null);
103: }
104:
105: public static Object runScript(String rootDir, String upocType,
106: String upocClass, String upocMethod, Object[] scriptArgs,
107: Object parent) throws CbesbException {
108: // get the UpocInstance
109: UpocInstance inst;
110: try {
111: inst = UpocInstance.getUpocInstance(upocType, upocClass,
112: rootDir, parent);
113: } catch (Exception e) {
114: throw new ConfigurationException(
115: "Fail to instantiate the Upoc class: '" + upocClass
116: + "'.",
117: "Make sure the class name and type is correct.", e);
118: }
119: if (upocType.equalsIgnoreCase("Groovy")) {
120: ScriptEngine se = inst.getEngine();
121:
122: // run the script
123:
124: Object rtn;
125: try {
126: rtn = ((Invocable) se).call(upocMethod, scriptArgs);
127: } catch (ScriptException e) {
128: throw new ConfigurationException(
129: "Script exception in executing the Groovy method '"
130: + upocMethod + "' in class '"
131: + upocClass + "'.",
132: "Check the Groovy script.", e);
133: } catch (NoSuchMethodException e) {
134: throw new ConfigurationException("Groovy method '"
135: + upocMethod + "' in class '" + upocClass
136: + "' does not exist.",
137: "Check the Groovy script.", e);
138: }
139: return rtn;
140: } else if (upocType.equalsIgnoreCase("Pojo")
141: || upocType.equalsIgnoreCase("Java")) {
142: Class pojoClass = inst.getPojoClass();
143: Method[] methods = pojoClass.getDeclaredMethods();
144: for (int i = 0; i < methods.length; i++) {
145: if (methods[i].getName().equals(upocMethod)) {
146:
147: Object rtn;
148: try {
149: rtn = methods[i].invoke(inst
150: .getPojoClassInstance(), scriptArgs);
151: } catch (Exception e) {
152: throw new ConfigurationException(
153: "Exception in executing the method '"
154: + upocMethod + "' in class '"
155: + upocClass + "'.",
156: "Check the Java Upoc class.", e);
157: }
158: return rtn;
159: }
160: }
161: }
162: return null;
163: }
164: }
|