001 package biz.hammurapi.jms.adapter; 002 003 import biz.hammurapi.config.ConfigurationException; 004 import biz.hammurapi.config.ServiceBase; 005 import biz.hammurapi.remoting.Invocation; 006 007 public class Method extends ServiceBase { 008 009 private biz.hammurapi.jms.adapter.definition.Method definition; 010 011 private JmsService jmsService; 012 013 public Method(biz.hammurapi.jms.adapter.definition.Method definition) throws ConfigurationException { 014 this.definition = definition; 015 if (definition.getService()!=null) { 016 this.jmsService = new JmsService(definition.getService()); 017 } 018 } 019 020 private Class wrapperException; 021 022 protected void startInternal() throws ConfigurationException { 023 if (definition.getService()!=null) { 024 jmsService.start(); 025 } else if (definition.getServiceRef()!=null) { 026 jmsService = (JmsService) get(definition.getServiceRef()); 027 } else { 028 jmsService = ((ProxyService) getOwner()).getJmsService(); 029 } 030 031 if (jmsService==null) { 032 throw new ConfigurationException("Jms service for method "+definition.getName()+" cannot be located"); 033 } 034 035 if (!JmsAdapter.isBlank(definition.getWrapperException())) { 036 try { 037 wrapperException = Class.forName(definition.getWrapperException()); 038 } catch (ClassNotFoundException e) { 039 throw new ConfigurationException("Wrapper exception class not found: "+e, e); 040 } 041 042 if (!Exception.class.isAssignableFrom(wrapperException)) { 043 throw new ConfigurationException("Wrapper exception class is not a subclass of java.lang.Exception"); 044 } 045 } 046 047 } 048 049 public Class getWrapperException() { 050 return wrapperException; 051 } 052 053 protected void stopInternal() throws ConfigurationException { 054 if (definition.getService()!=null && jmsService!=null) { 055 jmsService.stop(); 056 } 057 } 058 059 public boolean isAsync() { 060 return definition.getAsynchronous(); 061 } 062 063 /** 064 * Invokes remote method 065 * @param state 066 * @param args 067 * @return 068 */ 069 public Object invoke(Object state, Object[] args, String declaringClass) throws Exception { 070 long start = System.currentTimeMillis(); 071 try { 072 Invocation invocation = new Invocation(state, declaringClass, definition.getName(), definition.getParameterArray(), args, null); 073 if (definition.getFireAndForget()) { 074 jmsService.send(invocation, definition.getAsynchronous()); 075 return null; 076 } else { 077 return jmsService.request(invocation); 078 } 079 } finally { 080 long now = System.currentTimeMillis(); 081 addMeasurement("request", now - start, now); 082 } 083 } 084 085 public JmsService getJmsService() { 086 return jmsService; 087 } 088 }