1 2 24 package com.sun.enterprise.connectors.inflow; 25 26 import com.sun.ejb.MessageBeanListener; 27 import com.sun.ejb.MessageBeanProtocolManager; 28 import com.sun.enterprise.J2EETransactionManager; 29 import com.sun.enterprise.Switch; 30 import com.sun.enterprise.connectors.ConnectorConstants; 31 import com.sun.logging.LogDomains; 32 import java.lang.reflect.InvocationHandler ; 33 import java.lang.reflect.Method ; 34 import java.lang.reflect.Proxy ; 35 import java.util.logging.Level ; 36 import java.util.logging.Logger ; 37 import com.sun.enterprise.Switch; 38 import com.sun.enterprise.admin.monitor.callflow.*; 39 40 48 49 public final class MessageEndpointInvocationHandler 50 implements InvocationHandler { 51 52 private MessageBeanListener listener_; 53 private boolean beforeDeliveryCalled = false; 54 55 private boolean throwTransactedExceptions_ = true; 56 private MessageBeanProtocolManager messageBeanPM_; 57 58 private static String MESSAGE_ENDPOINT 59 = "javax.resource.spi.endpoint.MessageEndpoint"; 60 61 private static String THROW_TRANSACTED_EXCEPTIONS_PROP 62 = "resourceadapter.throw.transacted.exceptions"; 63 64 private static Logger logger = 65 LogDomains.getLogger(LogDomains.RSR_LOGGER); 66 67 private Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent(); 68 69 75 public MessageEndpointInvocationHandler(MessageBeanListener listener, 76 MessageBeanProtocolManager pm) { 77 this.listener_ = listener; 78 this.messageBeanPM_ = pm; 79 80 throwTransactedExceptions_ = 81 ConnectorConstants.THROW_TRANSACTED_EXCEPTIONS; 82 83 if( throwTransactedExceptions_ != true ) { 84 logger.info(ConnectorConstants.THROW_TRANSACTED_EXCEPTIONS_PROP + 85 " set to false"); 86 } 87 88 } 89 90 98 public Object invoke(Object proxy, Method method, Object [] args) 99 throws Throwable { 100 101 104 String methodClassName = method.getDeclaringClass().getName(); 105 String methodName = method.getName(); 106 Object returnValue = null; 107 108 if (logger.isLoggable(Level.FINEST)) { 109 110 String msg = "Invoking method [" + methodName 111 + "] from class [" + methodClassName + "]"; 112 logger.log(Level.FINEST, msg); 113 } 114 115 if (MESSAGE_ENDPOINT.equals(methodClassName)) { 118 119 if ("beforeDelivery".equals(methodName)) { 120 Method onMessageMethod = (Method ) args[0]; 121 beforeDeliveryCalled = true; 122 listener_.beforeMessageDelivery(onMessageMethod, false); 123 124 } else if ("afterDelivery".equals(methodName)) { 125 beforeDeliveryCalled = false; listener_.afterMessageDelivery(); 127 128 } else if ("release".equals(methodName)) { 129 messageBeanPM_.destroyMessageBeanListener(listener_); 130 } else { 131 logger.log(Level.SEVERE, "endpointfactory.method_not_defined", 132 new Object [] {methodName, MESSAGE_ENDPOINT}); 133 throw new RuntimeException (methodName); 134 } 135 136 } else if ("java.lang.Object".equals(methodClassName)) { 137 returnValue = invokeJavaObjectMethod(this, method, args); 138 139 } else { 141 if (!beforeDeliveryCalled) { 143 J2EETransactionManager txManager = 144 Switch.getSwitch().getTransactionManager(); 145 boolean txImported = (txManager.getTransaction() != null); 146 listener_.beforeMessageDelivery(method, txImported); 147 } 148 149 try { 150 152 try{ 154 callFlowAgent.requestStart( 155 RequestType.REMOTE_ASYNC_MESSAGE); 156 callFlowAgent.startTime( 157 ContainerTypeOrApplicationType.EJB_CONTAINER); 158 } catch (Exception ex){ 163 logger.log(Level.WARNING, "Call Flow Agent threw exception" + ex); 164 } 165 166 returnValue = listener_.deliverMessage(args); 167 } catch (Throwable ex) { 168 if( messageBeanPM_.isDeliveryTransacted(method) ) { 169 if( throwTransactedExceptions_ ) { 170 throw ex; 171 } else { 172 logger.log(Level.INFO, "Resource adapter eating " + 173 " transacted exception", ex); 174 } 175 } else { 176 throw ex; 177 } 178 } finally { 179 180 try{ 182 callFlowAgent.endTime(); 183 callFlowAgent.requestEnd(); 184 } catch (Exception ex){ 185 logger.log(Level.WARNING, "Call Flow Agent threw exception" + ex); 186 } 187 188 if (!beforeDeliveryCalled) { 192 listener_.afterMessageDelivery(); 193 } 194 beforeDeliveryCalled = false; 195 } 196 } 197 return returnValue; 198 } 199 200 205 private Object invokeJavaObjectMethod( 206 InvocationHandler handler, Method method, Object [] args) 207 throws RuntimeException { 208 209 Object returnValue = null; 210 211 218 switch( method.getName().charAt(0) ) { 219 case 'e' : 220 Object other = Proxy.isProxyClass(args[0].getClass()) ? 221 Proxy.getInvocationHandler(args[0]) : args[0]; 222 returnValue = new Boolean (handler.equals(other)); 223 break; 224 case 'h' : 225 returnValue = new Integer (handler.hashCode()); 226 break; 227 case 't' : 228 returnValue = handler.toString(); 229 break; 230 default : 231 throw new RuntimeException (method.getName()); 232 } 233 234 return returnValue; 235 } 236 } 237 238 | Popular Tags |