2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * 
19   * ========================================================================
20   */
21  package org.apache.cactus.spi.server;
22  
23  import javax.ejb.CreateException;
24  import javax.ejb.EJBException;
25  import javax.ejb.MessageDrivenBean;
26  import javax.ejb.MessageDrivenContext;
27  import javax.jms.JMSException;
28  import javax.jms.Message;
29  import javax.jms.MessageListener;
30  import javax.jms.QueueConnection;
31  import javax.jms.QueueConnectionFactory;
32  import javax.jms.QueueSession;
33  import javax.naming.InitialContext;
34  
35  import org.apache.cactus.internal.server.MessageDrivenBeanTestController;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /**
40   * Generic Message Driven Bean redirector that calls a test method on the
41   * server side.
42   *
43   * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
44   *
45   * @since 1.4
46   *
47   * @version $Id$
48   */
49  public class MessageDrivenBeanRedirector 
50      implements MessageDrivenBean, MessageListener
51  {
52      /**
53       * The Message context.
54       */
55      private MessageDrivenContext context;
56      QueueConnection connection; 
57      QueueSession session; 
58      
59      /**
60       * The logger.
61       */
62      private static final Log LOGGER = 
63          LogFactory.getLog(MessageDrivenBeanRedirector.class);
64  
65      /**
66       * Sets the Message context (automatically called by the container).
67       *
68       * @param theContext the Message context
69       */
70      public void setMessageDrivenContext(MessageDrivenContext theContext)
71      {
72          this.context = theContext;
73      }
74  
75      /**
76       * Called by the container to create an instance of this Message Driven
77       * bean.
78       *
79       * @exception CreateException see the EJB specification
80       */
81      public void ejbCreate() throws CreateException
82      {
83          LOGGER.debug("------------- MDB redirector service created");
84          
85          try 
86          {
87              InitialContext initContext = new InitialContext(); 
88              QueueConnectionFactory qcf = (QueueConnectionFactory) 
89              initContext.lookup("java:comp/env/jms/QCF1"); 
90              connection = qcf.createQueueConnection(); 
91              session = connection.createQueueSession(false, 
92                      QueueSession.AUTO_ACKNOWLEDGE); 
93              connection.start(); 
94          } 
95          catch(Exception e) 
96          {
97              throw new EJBException("Failed to initialize MyMDB", e); 
98          } 
99      }
100 
101     /**
102      * The container invokes this method when the instance is about to be
103      * discarded. This might happens if the container needs to reduce the size
104      * of the pool.
105      */
106     public void ejbRemove()
107     {
108         LOGGER.debug("------------- MDB redirector service removed");
109     }
110 
111     /**
112      * Receives a message from a JMS Queue and make it available to the
113      * test case.
114      *
115      * @param theMessage the JMS Message
116      */
117     public void onMessage(Message theMessage)
118     {
119         // Mark beginning of test on server side
120         LOGGER.debug("------------- Start MDB service");
121         
122         // Gather MDB implicit objects
123         MessageDrivenBeanImplicitObjects implicitObjects =
124             new MessageDrivenBeanImplicitObjects();
125         implicitObjects.setMessage(theMessage);
126         implicitObjects.setMessageDrivenBeanContext(this.context);
127 
128         // Call the controller to handle the message
129         MessageDrivenBeanTestController controller = new MessageDrivenBeanTestController();
130 
131         try 
132         {
133             controller.handleRequest(implicitObjects);
134         } 
135         catch (JMSException e) 
136         {
137             e.printStackTrace();
138         }
139         
140     }
141 
142 }