001: /*
002: * $Id: StandardJavaEngine.java,v 1.2 2003/09/03 20:47:59 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.service.engine;
026:
027: import java.lang.reflect.InvocationTargetException;
028: import java.lang.reflect.Method;
029: import java.util.Map;
030:
031: import org.ofbiz.service.DispatchContext;
032: import org.ofbiz.service.GenericServiceException;
033: import org.ofbiz.service.ModelService;
034: import org.ofbiz.service.ServiceDispatcher;
035: import org.ofbiz.base.util.Debug;
036:
037: /**
038: * Standard Java Static Method Service Engine
039: *
040: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
041: * @version $Revision: 1.2 $
042: * @since 2.0
043: */
044: public final class StandardJavaEngine extends GenericAsyncEngine {
045:
046: public static final String module = StandardJavaEngine.class
047: .getName();
048:
049: public StandardJavaEngine(ServiceDispatcher dispatcher) {
050: super (dispatcher);
051: }
052:
053: /**
054: * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
055: */
056: public void runSyncIgnore(String localName,
057: ModelService modelService, Map context)
058: throws GenericServiceException {
059: Map result = runSync(localName, modelService, context);
060: }
061:
062: /**
063: * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
064: */
065: public Map runSync(String localName, ModelService modelService,
066: Map context) throws GenericServiceException {
067: Object result = serviceInvoker(localName, modelService, context);
068:
069: if (result == null || !(result instanceof Map))
070: throw new GenericServiceException(
071: "Service did not return expected result");
072: return (Map) result;
073: }
074:
075: // Invoke the static java method service.
076: private Object serviceInvoker(String localName,
077: ModelService modelService, Map context)
078: throws GenericServiceException {
079: // static java service methods should be: public Map methodName(DispatchContext dctx, Map context)
080: DispatchContext dctx = dispatcher.getLocalContext(localName);
081:
082: if (modelService == null)
083: Debug.logError("ERROR: Null Model Service.", module);
084: if (dctx == null)
085: Debug.logError("ERROR: Null DispatchContext.", module);
086: if (context == null)
087: Debug.logError("ERROR: Null Service Context.", module);
088:
089: Class[] paramTypes = new Class[] { DispatchContext.class,
090: Map.class };
091: Object[] params = new Object[] { dctx, context };
092: Object result = null;
093:
094: // check the package and method names
095: if (modelService.location == null
096: || modelService.invoke == null)
097: throw new GenericServiceException(
098: "Cannot locate service to invoke (location or invoke name missing)");
099:
100: // get the classloader to use
101: ClassLoader cl = null;
102:
103: if (dctx == null)
104: cl = this .getClass().getClassLoader();
105: else
106: cl = dctx.getClassLoader();
107:
108: try {
109: Class c = cl.loadClass(modelService.location);
110: Method m = c.getMethod(modelService.invoke, paramTypes);
111: result = m.invoke(null, params);
112: } catch (ClassNotFoundException cnfe) {
113: throw new GenericServiceException(
114: "Cannot find service location", cnfe);
115: } catch (NoSuchMethodException nsme) {
116: throw new GenericServiceException(
117: "Service method does not exist", nsme);
118: } catch (SecurityException se) {
119: throw new GenericServiceException("Access denied", se);
120: } catch (IllegalAccessException iae) {
121: throw new GenericServiceException("Method not accessible",
122: iae);
123: } catch (IllegalArgumentException iarge) {
124: throw new GenericServiceException(
125: "Invalid parameter match", iarge);
126: } catch (InvocationTargetException ite) {
127: throw new GenericServiceException(
128: "Service target threw an unexpected exception", ite
129: .getTargetException());
130: } catch (NullPointerException npe) {
131: throw new GenericServiceException(
132: "Specified object is null", npe);
133: } catch (ExceptionInInitializerError eie) {
134: throw new GenericServiceException("Initialization failed",
135: eie);
136: } catch (Throwable th) {
137: throw new GenericServiceException(
138: "Error or nknown exception", th);
139: }
140:
141: return result;
142: }
143: }
|