Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.jms.util » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.jms.util;
   15   
   16   import java.lang.reflect.Method;
   17   import java.util.Map;
   18   import java.util.concurrent.ConcurrentHashMap;
   19   import java.util.concurrent.atomic.AtomicBoolean;
   20   
   21   import javax.jms.Connection;
   22   import javax.jms.ConnectionFactory;
   23   import javax.jms.Destination;
   24   import javax.jms.JMSException;
   25   import javax.jms.MessageConsumer;
   26   import javax.jms.MessageProducer;
   27   import javax.jms.QueueConnection;
   28   import javax.jms.QueueConnectionFactory;
   29   import javax.jms.Session;
   30   import javax.jms.TopicConnection;
   31   import javax.jms.TopicConnectionFactory;
   32   
   33   import org.apache.webbeans.exception.WebBeansException;
   34   import org.apache.webbeans.jms.JMSModel;
   35   import org.apache.webbeans.jms.JMSModel.JMSType;
   36   import org.apache.webbeans.jms.component.JmsBean;
   37   import org.apache.webbeans.util.ClassUtil;
   38   
   39   import javassist.util.proxy.MethodHandler;
   40   
   41   public class JmsProxyHandler implements MethodHandler
   42   {
   43       private JmsBean<?> jmsComponent = null;
   44   
   45       private static ConnectionFactory connectionFactory = null;
   46   
   47       private AtomicBoolean cfSet = new AtomicBoolean(false);
   48   
   49       private static Map<JMSType, Connection> connections = new ConcurrentHashMap<JMSType, Connection>();
   50   
   51       private static Map<String, Destination> dests = new ConcurrentHashMap<String, Destination>();
   52       
   53       private Object jmsObject = null;
   54       
   55       private Class<?> injectionClazz = null;
   56   
   57       public JmsProxyHandler(JmsBean<?> jmsComponent, Class<?> injectionClazz)
   58       {
   59           this.jmsComponent = jmsComponent;
   60           this.injectionClazz = injectionClazz;
   61       }
   62   
   63       public Object invoke(Object instance, Method method, Method proceed, Object[] arguments) throws Exception
   64       {
   65           if(method.getName().equals("closeJMSObject"))
   66           {
   67               close();
   68               
   69               return null;
   70           }
   71           
   72           if (!ClassUtil.isObjectMethod(method.getName()))
   73           {            
   74               Object cf = null;
   75   
   76               if (this.jmsObject == null)
   77               {
   78                   Class<?> jmsClazz = this.injectionClazz;
   79   
   80                   if (cf == null && Connection.class.isAssignableFrom(jmsClazz))
   81                   {
   82                       cf = createOrReturnQueueOrTopicConnection();
   83                   }
   84   
   85                   if (cf == null && Destination.class.isAssignableFrom(jmsClazz))
   86                   {
   87                       cf = createOrReturnQueueOrTopic();
   88   
   89                   }
   90   
   91                   if (cf == null && Session.class.isAssignableFrom(jmsClazz))
   92                   {
   93                       cf = createSession();
   94                   }
   95   
   96                   if (cf == null && MessageProducer.class.isAssignableFrom(jmsClazz))
   97                   {
   98                       cf = createMessageProducers();
   99                   }
  100   
  101                   if (cf == null && MessageConsumer.class.isAssignableFrom(jmsClazz))
  102                   {
  103                       cf = createMessageConsumers();
  104                   }
  105   
  106                   if (method.getName().equals("close"))
  107                   {
  108                       throw new UnsupportedOperationException("close method is not supported for JMS resources");
  109                   }
  110   
  111                   if (cf == null)
  112                   {
  113                       throw new WebBeansException("JMS Resource type is not correct!. Does not create JMS resource object to handle request");
  114                   }
  115   
  116                   this.jmsObject = cf;
  117               }
  118               else
  119               {
  120                   cf = this.jmsObject;
  121               }
  122   
  123               return method.invoke(cf, arguments);
  124           }
  125           else
  126           {
  127               return proceed.invoke(instance, arguments);
  128           }
  129       }
  130   
  131       private Object createOrReturnConnectionFactory()
  132       {
  133           if (connectionFactory != null)
  134           {
  135               return connectionFactory;
  136           }
  137           else
  138           {
  139               if (cfSet.compareAndSet(false, true))
  140               {
  141                   connectionFactory = JmsUtil.getConnectionFactory();
  142   
  143                   return connectionFactory;
  144               }
  145           }
  146   
  147           return null;
  148       }
  149   
  150       private Session createSession()
  151       {
  152           try
  153           {
  154   
  155               Connection connection = createOrReturnQueueOrTopicConnection();
  156   
  157               return connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  158   
  159           }
  160           catch (JMSException e)
  161           {
  162               throw new WebBeansException("Unable to create jms session", e);
  163           }
  164   
  165       }
  166   
  167       private MessageProducer createMessageProducers()
  168       {
  169           try
  170           {
  171               Connection connection = createOrReturnQueueOrTopicConnection();
  172   
  173               return connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createProducer(createOrReturnQueueOrTopic());
  174           }
  175           catch (JMSException e)
  176           {
  177               throw new WebBeansException("Unable to create jms message producer", e);
  178           }
  179   
  180       }
  181   
  182       private MessageConsumer createMessageConsumers()
  183       {
  184           try
  185           {
  186               Connection connection = createOrReturnQueueOrTopicConnection();
  187   
  188               return connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(createOrReturnQueueOrTopic());
  189           }
  190           catch (JMSException e)
  191           {
  192               throw new WebBeansException("Unable to create jms message producer", e);
  193           }
  194   
  195       }
  196   
  197       private Connection createOrReturnQueueOrTopicConnection()
  198       {
  199           JMSModel jmsModel = this.jmsComponent.getJmsModel();
  200   
  201           try
  202           {
  203               if (jmsModel.getJmsType().equals(JMSType.QUEUE))
  204               {
  205                   if (connections.containsKey(JMSType.QUEUE))
  206                   {
  207                       return connections.get(JMSType.QUEUE);
  208                   }
  209                   else
  210                   {
  211                       QueueConnectionFactory ccf = (QueueConnectionFactory) createOrReturnConnectionFactory();
  212                       QueueConnection qc = ccf.createQueueConnection();
  213                       connections.put(JMSType.QUEUE, qc);
  214   
  215                       return qc;
  216                   }
  217               }
  218               else if (jmsModel.getJmsType().equals(JMSType.TOPIC))
  219               {
  220                   if (connections.containsKey(JMSType.TOPIC))
  221                   {
  222                       return connections.get(JMSType.TOPIC);
  223                   }
  224                   else
  225                   {
  226                       TopicConnectionFactory ccf = (TopicConnectionFactory) createOrReturnConnectionFactory();
  227                       TopicConnection qc = ccf.createTopicConnection();
  228                       connections.put(JMSType.TOPIC, qc);
  229   
  230                       return qc;
  231                   }
  232   
  233               }
  234   
  235           }
  236           catch (JMSException e)
  237           {
  238               throw new WebBeansException("Unable to create jms connection", e);
  239           }
  240   
  241           return null;
  242       }
  243       
  244       private void close()
  245       {
  246           try
  247           {
  248               if(this.jmsObject != null)
  249               {
  250                   Method method = this.jmsObject.getClass().getMethod("close", new Class[]{});
  251                   
  252                   if(!method.isAccessible())
  253                   {
  254                       method.setAccessible(true);   
  255                   }
  256                   
  257                   method.invoke(this.jmsObject, new Object[]{});                
  258               }
  259               
  260           }
  261           catch (Exception e)
  262           {
  263               throw new WebBeansException("Unable to close JMS resources");
  264           }
  265           
  266       }
  267   
  268       private Destination createOrReturnQueueOrTopic()
  269       {
  270           JMSModel jmsModel = this.jmsComponent.getJmsModel();
  271           String jndiName = jmsModel.isJndiNameDefined() ? jmsModel.getJndiName() : jmsModel.getMappedName();
  272   
  273           if (dests.get(jndiName) != null)
  274           {
  275               return dests.get(jndiName);
  276           }
  277   
  278           Destination res = (Destination) JmsUtil.getInstanceFromJndi(this.jmsComponent.getJmsModel(), this.injectionClazz);
  279   
  280           dests.put(jndiName, res);
  281   
  282           return res;
  283   
  284       }
  285   
  286       public static void clearConnections()
  287       {
  288           try
  289           {
  290               connectionFactory = null;
  291   
  292               for (Connection connection : connections.values())
  293               {
  294                   connection.close();
  295               }
  296   
  297               connections = null;
  298   
  299               dests.clear();
  300               dests = null;
  301   
  302           }
  303           catch (Exception e)
  304           {
  305               throw new WebBeansException(e);
  306           }
  307       }
  308   
  309   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.jms.util » [javadoc | source]