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
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    * 
    9    *       http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.webbeans.jms.util;
   18   
   19   import java.io.Serializable;
   20   
   21   import javassist.util.proxy.ProxyFactory;
   22   
   23   import javax.jms.Connection;
   24   import javax.jms.ConnectionFactory;
   25   import javax.jms.MessageConsumer;
   26   import javax.jms.MessageProducer;
   27   import javax.jms.Queue;
   28   import javax.jms.QueueConnection;
   29   import javax.jms.QueueConnectionFactory;
   30   import javax.jms.QueueReceiver;
   31   import javax.jms.QueueSender;
   32   import javax.jms.QueueSession;
   33   import javax.jms.Session;
   34   import javax.jms.Topic;
   35   import javax.jms.TopicConnection;
   36   import javax.jms.TopicConnectionFactory;
   37   import javax.jms.TopicPublisher;
   38   import javax.jms.TopicSession;
   39   import javax.jms.TopicSubscriber;
   40   
   41   import org.apache.webbeans.config.OpenWebBeansConfiguration;
   42   import org.apache.webbeans.exception.WebBeansCreationException;
   43   import org.apache.webbeans.exception.WebBeansException;
   44   import org.apache.webbeans.jms.JMSModel;
   45   import org.apache.webbeans.jms.component.JmsBean;
   46   import org.apache.webbeans.spi.JNDIService;
   47   import org.apache.webbeans.spi.ServiceLoader;
   48   import org.apache.webbeans.util.Asserts;
   49   
   50   public final class JmsUtil
   51   {
   52       private JmsUtil()
   53       {
   54           
   55       }
   56   
   57       public static boolean isJmsResourceClass(Class<?> clazz)
   58       {
   59           Asserts.assertNotNull(clazz,"clazz parameter can not be null");
   60           
   61           if(ConnectionFactory.class.isAssignableFrom(clazz) ||
   62                   Connection.class.isAssignableFrom(clazz) || 
   63                   Queue.class.isAssignableFrom(clazz) || 
   64                   Topic.class.isAssignableFrom(clazz) || 
   65                   MessageProducer.class.isAssignableFrom(clazz) ||
   66                   MessageConsumer.class.isAssignableFrom(clazz) ||
   67                   Session.class.isAssignableFrom(clazz))
   68           {
   69               return true;
   70           }
   71           
   72           return false;
   73           
   74       }
   75       
   76       public static boolean isJmsQueueTypeResource(Class<?> clazz)
   77       {
   78           if(QueueConnectionFactory.class.isAssignableFrom(clazz) ||
   79                   QueueConnection.class.isAssignableFrom(clazz) ||                 
   80                   QueueSender.class.isAssignableFrom(clazz) ||
   81                   QueueReceiver.class.isAssignableFrom(clazz) ||
   82                   QueueSession.class.isAssignableFrom(clazz))
   83           {
   84               return true;
   85           }
   86           
   87           return false;
   88       }
   89       
   90       public static boolean isJmsQueueResource(Class<?> clazz)
   91       {
   92           if(Queue.class.isAssignableFrom(clazz))
   93           {
   94               return true;
   95           }
   96           
   97           return false;
   98       }
   99       
  100       public static boolean isJmsTopicResource(Class<?> clazz)
  101       {
  102           if(Topic.class.isAssignableFrom(clazz))
  103           {
  104               return true;
  105           }
  106           
  107           return false;
  108       }
  109       
  110       
  111       public static boolean isJmsTopicTypeResource(Class<?> clazz)
  112       {
  113           if(TopicConnectionFactory.class.isAssignableFrom(clazz) ||
  114                   TopicConnection.class.isAssignableFrom(clazz) ||   
  115                   TopicSubscriber.class.isAssignableFrom(clazz) ||
  116                   TopicPublisher.class.isAssignableFrom(clazz) ||
  117                   TopicSession.class.isAssignableFrom(clazz))
  118           {
  119               return true;
  120           }
  121           
  122           return false;
  123       }
  124       
  125       private static JNDIService getJNDIService()
  126       {
  127          JNDIService jndiService = ServiceLoader.getService(JNDIService.class);
  128           
  129           if(jndiService == null)
  130           {
  131               throw new WebBeansCreationException("JNDI service is not available");            
  132           }
  133           
  134           return jndiService;
  135       }
  136       
  137       public static ConnectionFactory getConnectionFactory()
  138       {
  139           String connectionFactoryJndiName = OpenWebBeansConfiguration.getInstance().getProperty(JNDIService.JMS_CONNECTION_FACTORY_JNDI_KEY);
  140           
  141           if(connectionFactoryJndiName == null || connectionFactoryJndiName.equals(""))
  142           {
  143               connectionFactoryJndiName = JNDIService.JMS_CONNECTION_FACTORY_JNDI_DEFAULT_VALUE;
  144           }
  145           
  146           return getJNDIService().getObject(connectionFactoryJndiName, ConnectionFactory.class);
  147       }
  148       
  149       public static <T> T getInstanceFromJndi(JMSModel jmsModel, Class<T> jmsClass)
  150       {
  151           String jndiName = jmsModel.isJndiNameDefined() ? jmsModel.getJndiName() : jmsModel.getMappedName();
  152           
  153            
  154           T instance = getJNDIService().getObject(jndiName, jmsClass);
  155           
  156           return instance;
  157           
  158       }
  159       
  160       /**
  161        * Gets jms related object.
  162        * @param jmsComponent jms bean
  163        * @param intf injection point class
  164        * @return proxy object
  165        */
  166       public static Object createNewJmsProxy(JmsBean<?> jmsComponent, Class<?> intf)
  167       {
  168          Object result = null;
  169   
  170           try
  171           {
  172               ProxyFactory pf = new ProxyFactory();
  173               pf.setInterfaces(new Class<?>[] {
  174                       Closable.class,
  175                       Serializable.class,
  176                       intf});
  177               
  178               pf.setHandler(new JmsProxyHandler(jmsComponent,intf));
  179   
  180               result = pf.createClass().newInstance();
  181   
  182           }
  183           catch (Exception e)
  184           {
  185               throw new WebBeansException(e);
  186           }
  187   
  188           return result;
  189       }
  190       
  191   }

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