001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.invokers.test;
023:
024: import java.rmi.RemoteException;
025: import javax.jms.QueueConnection;
026: import javax.jms.QueueSession;
027: import javax.jms.JMSException;
028: import javax.jms.QueueRequestor;
029: import javax.jms.Queue;
030: import javax.jms.Message;
031: import javax.jms.QueueConnectionFactory;
032: import javax.jms.ObjectMessage;
033: import javax.naming.InitialContext;
034: import javax.naming.NamingException;
035:
036: import org.jboss.proxy.Interceptor;
037: import org.jboss.invocation.Invoker;
038: import org.jboss.invocation.Invocation;
039: import org.jboss.invocation.InvocationContext;
040: import org.jboss.logging.Logger;
041:
042: /** An example client side InvokerInterceptor used to dynamically override
043: * the default InvokerInterceptor coming from the server to install one that
044: * routes invocations to either the server side transport layer, or jms
045: * depending on the invocation.
046: *
047: * @author Scott.Stark@jboss.org
048: * @version $Revision: 57211 $
049: */
050: public class InvokerInterceptor extends Interceptor {
051: private static Logger log = Logger
052: .getLogger(InvokerInterceptor.class);
053: private transient QueueConnection queConn;
054: private transient QueueSession session;
055: private transient QueueRequestor requestor;
056:
057: /**
058: */
059: public Object invoke(Invocation invocation) throws Exception {
060: Object returnValue = null;
061: InvocationContext ctx = invocation.getInvocationContext();
062: String methodName = invocation.getMethod().getName();
063: if (methodName.equals("doSomethingSlowly")) {
064: returnValue = sendRecvJMS(invocation);
065: } else {
066: // Get the
067: Invoker invoker = ctx.getInvoker();
068: returnValue = invoker.invoke(invocation);
069: // If this is a remove close the jms connection
070: if (methodName.equals("remove")) {
071: try {
072: if (requestor != null)
073: requestor.close();
074: if (session != null)
075: session.close();
076: if (queConn != null)
077: queConn.close();
078: } catch (Exception e) {
079: log.error("Failed to close jms", e);
080: }
081: }
082: }
083: return returnValue;
084: }
085:
086: /**
087: * @param invocation
088: * @return The
089: */
090: private synchronized Object sendRecvJMS(Invocation invocation)
091: throws RemoteException {
092: Object reply = null;
093: log.info("sendRecvJMS");
094: try {
095: if (queConn == null) {
096: InitialContext ctx = null;
097: ctx = new InitialContext();
098: QueueConnectionFactory qcf = (QueueConnectionFactory) ctx
099: .lookup("ConnectionFactory");
100: queConn = qcf.createQueueConnection();
101: queConn.start();
102: session = queConn.createQueueSession(false,
103: QueueSession.AUTO_ACKNOWLEDGE);
104: Queue queue = (Queue) ctx.lookup("queue/A");
105: requestor = new QueueRequestor(session, queue);
106: }
107: // Send the invocation via jms
108: Message msg = session.createObjectMessage(invocation
109: .getArguments());
110: msg.setStringProperty("ejbName", "BusinessSession");
111: ObjectMessage replyMsg = (ObjectMessage) requestor
112: .request(msg);
113: reply = replyMsg.getObject();
114: } catch (NamingException e) {
115: log.error("sendRecvJMS", e);
116: throw new RemoteException("sendRecvJMS", e);
117: } catch (JMSException e) {
118: log.error("sendRecvJMS", e);
119: throw new RemoteException("sendRecvJMS", e);
120: }
121: return reply;
122: }
123: }
|