1 25 package org.ofbiz.service.engine; 26 27 import java.lang.reflect.InvocationTargetException ; 28 import java.lang.reflect.Method ; 29 import java.util.Map ; 30 31 import org.ofbiz.service.DispatchContext; 32 import org.ofbiz.service.GenericServiceException; 33 import org.ofbiz.service.ModelService; 34 import org.ofbiz.service.ServiceDispatcher; 35 import org.ofbiz.base.util.Debug; 36 37 44 public final class StandardJavaEngine extends GenericAsyncEngine { 45 46 public static final String module = StandardJavaEngine.class.getName(); 47 48 public StandardJavaEngine(ServiceDispatcher dispatcher) { 49 super(dispatcher); 50 } 51 52 55 public void runSyncIgnore(String localName, ModelService modelService, Map context) throws GenericServiceException { 56 Map result = runSync(localName, modelService, context); 57 } 58 59 62 public Map runSync(String localName, ModelService modelService, Map context) throws GenericServiceException { 63 Object result = serviceInvoker(localName, modelService, context); 64 65 if (result == null || !(result instanceof Map )) 66 throw new GenericServiceException("Service did not return expected result"); 67 return (Map ) result; 68 } 69 70 private Object serviceInvoker(String localName, ModelService modelService, Map context) throws GenericServiceException { 72 DispatchContext dctx = dispatcher.getLocalContext(localName); 74 75 if (modelService == null) 76 Debug.logError("ERROR: Null Model Service.", module); 77 if (dctx == null) 78 Debug.logError("ERROR: Null DispatchContext.", module); 79 if (context == null) 80 Debug.logError("ERROR: Null Service Context.", module); 81 82 Class [] paramTypes = new Class [] {DispatchContext.class, Map .class}; 83 Object [] params = new Object [] {dctx, context}; 84 Object result = null; 85 86 if (modelService.location == null || modelService.invoke == null) 88 throw new GenericServiceException("Cannot locate service to invoke (location or invoke name missing)"); 89 90 ClassLoader cl = null; 92 93 if (dctx == null) 94 cl = this.getClass().getClassLoader(); 95 else 96 cl = dctx.getClassLoader(); 97 98 try { 99 Class c = cl.loadClass(this.getLocation(modelService)); 100 Method m = c.getMethod(modelService.invoke, paramTypes); 101 result = m.invoke(null, params); 102 } catch (ClassNotFoundException cnfe) { 103 throw new GenericServiceException("Cannot find service location", cnfe); 104 } catch (NoSuchMethodException nsme) { 105 throw new GenericServiceException("Service method does not exist", nsme); 106 } catch (SecurityException se) { 107 throw new GenericServiceException("Access denied", se); 108 } catch (IllegalAccessException iae) { 109 throw new GenericServiceException("Method not accessible", iae); 110 } catch (IllegalArgumentException iarge) { 111 throw new GenericServiceException("Invalid parameter match", iarge); 112 } catch (InvocationTargetException ite) { 113 throw new GenericServiceException("Service target threw an unexpected exception", ite.getTargetException()); 114 } catch (NullPointerException npe) { 115 throw new GenericServiceException("Specified object is null", npe); 116 } catch (ExceptionInInitializerError eie) { 117 throw new GenericServiceException("Initialization failed", eie); 118 } catch (Throwable th) { 119 throw new GenericServiceException("Error or nknown exception", th); 120 } 121 122 return result; 123 } 124 } 125 126 | Popular Tags |