KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > invokers > test > InvokerInterceptor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.invokers.test;
23
24 import java.rmi.RemoteException JavaDoc;
25 import javax.jms.QueueConnection JavaDoc;
26 import javax.jms.QueueSession JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28 import javax.jms.QueueRequestor JavaDoc;
29 import javax.jms.Queue JavaDoc;
30 import javax.jms.Message JavaDoc;
31 import javax.jms.QueueConnectionFactory JavaDoc;
32 import javax.jms.ObjectMessage JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35
36 import org.jboss.proxy.Interceptor;
37 import org.jboss.invocation.Invoker;
38 import org.jboss.invocation.Invocation;
39 import org.jboss.invocation.InvocationContext;
40 import org.jboss.logging.Logger;
41
42 /** An example client side InvokerInterceptor used to dynamically override
43  * the default InvokerInterceptor coming from the server to install one that
44  * routes invocations to either the server side transport layer, or jms
45  * depending on the invocation.
46  *
47  * @author Scott.Stark@jboss.org
48  * @version $Revision: 37406 $
49  */

50 public class InvokerInterceptor
51    extends Interceptor
52 {
53    private static Logger log = Logger.getLogger(InvokerInterceptor.class);
54    private transient QueueConnection JavaDoc queConn;
55    private transient QueueSession JavaDoc session;
56    private transient QueueRequestor JavaDoc requestor;
57
58    /**
59     */

60    public Object JavaDoc invoke(Invocation invocation)
61       throws Exception JavaDoc
62    {
63       Object JavaDoc returnValue = null;
64       InvocationContext ctx = invocation.getInvocationContext();
65       String JavaDoc methodName = invocation.getMethod().getName();
66       if( methodName.equals("doSomethingSlowly") )
67       {
68          returnValue = sendRecvJMS(invocation);
69       }
70       else
71       {
72          // Get the
73
Invoker invoker = ctx.getInvoker();
74          returnValue = invoker.invoke(invocation);
75          // If this is a remove close the jms connection
76
if( methodName.equals("remove") )
77          {
78             try
79             {
80                if( requestor != null )
81                   requestor.close();
82                if( session != null )
83                   session.close();
84                if( queConn != null )
85                   queConn.close();
86             }
87             catch(Exception JavaDoc e)
88             {
89                log.error("Failed to close jms", e);
90             }
91          }
92       }
93       return returnValue;
94    }
95
96    /**
97     * @param invocation
98     * @return The
99     */

100    private synchronized Object JavaDoc sendRecvJMS(Invocation invocation)
101       throws RemoteException JavaDoc
102    {
103       Object JavaDoc reply = null;
104       log.info("sendRecvJMS");
105       try
106       {
107          if( queConn == null )
108          {
109             InitialContext JavaDoc ctx = null;
110                ctx = new InitialContext JavaDoc();
111             QueueConnectionFactory JavaDoc qcf = (QueueConnectionFactory JavaDoc) ctx.lookup("ConnectionFactory");
112             queConn = qcf.createQueueConnection();
113             queConn.start();
114             session = queConn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
115             Queue JavaDoc queue = (Queue JavaDoc) ctx.lookup("queue/A");
116             requestor = new QueueRequestor JavaDoc(session, queue);
117          }
118          // Send the invocation via jms
119
Message JavaDoc msg = session.createObjectMessage(invocation.getArguments());
120          msg.setStringProperty("ejbName", "BusinessSession");
121          ObjectMessage JavaDoc replyMsg = (ObjectMessage JavaDoc) requestor.request(msg);
122          reply = replyMsg.getObject();
123       }
124       catch (NamingException JavaDoc e)
125       {
126          log.error("sendRecvJMS", e);
127          throw new RemoteException JavaDoc("sendRecvJMS", e);
128       }
129       catch (JMSException JavaDoc e)
130       {
131          log.error("sendRecvJMS", e);
132          throw new RemoteException JavaDoc("sendRecvJMS", e);
133       }
134       return reply;
135    }
136 }
137
Popular Tags