Home » geronimo-2.2-source-release » org.apache.geronimo.console.jmsmanager.helper » [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   
   18   package org.apache.geronimo.console.jmsmanager.helper;
   19   
   20   import java.util.Enumeration;
   21   import java.util.LinkedList;
   22   import java.util.List;
   23   
   24   import javax.jms.Connection;
   25   import javax.jms.ConnectionFactory;
   26   import javax.jms.DeliveryMode;
   27   import javax.jms.Destination;
   28   import javax.jms.JMSException;
   29   import javax.jms.Message;
   30   import javax.jms.MessageProducer;
   31   import javax.jms.Queue;
   32   import javax.jms.QueueBrowser;
   33   import javax.jms.QueueSession;
   34   import javax.jms.Session;
   35   import javax.jms.TextMessage;
   36   import javax.management.ObjectName;
   37   import javax.portlet.PortletRequest;
   38   
   39   import org.apache.geronimo.console.jmsmanager.DestinationStatistics;
   40   import org.apache.geronimo.console.jmsmanager.DestinationType;
   41   import org.apache.geronimo.console.jmsmanager.JMSDestinationInfo;
   42   import org.apache.geronimo.console.jmsmanager.JMSMessageInfo;
   43   import org.apache.geronimo.console.util.PortletManager;
   44   import org.apache.geronimo.gbean.AbstractName;
   45   import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
   46   import org.apache.geronimo.kernel.Kernel;
   47   import org.apache.geronimo.kernel.KernelRegistry;
   48   import org.apache.geronimo.kernel.proxy.GeronimoManagedBean;
   49   import org.apache.geronimo.management.geronimo.JCAAdminObject;
   50   import org.apache.geronimo.management.geronimo.JCAManagedConnectionFactory;
   51   import org.apache.geronimo.management.geronimo.ResourceAdapterModule;
   52   import org.slf4j.Logger;
   53   import org.slf4j.LoggerFactory;
   54   
   55   /**
   56    * @version $Rev: 813742 $ $Date: 2009-09-11 02:38:26 -0700 (Fri, 11 Sep 2009) $
   57    */
   58   public abstract class JMSMessageHelper {
   59   
   60       protected static final Kernel kernel = KernelRegistry.getSingleKernel();
   61   
   62       private static final Logger logger = LoggerFactory.getLogger(JMSMessageHelper.class);
   63   
   64       public void sendMessage(PortletRequest portletRequest, JMSDestinationInfo destinationInfo, JMSMessageInfo messageInfo) throws JMSException {
   65           Destination destination = getDestination(portletRequest, destinationInfo);
   66           if (destination == null) {
   67               throw new JMSException("Unable to find the destination....Not sending message");
   68           }
   69           ConnectionFactory connectionFactory = getConnectionFactory(portletRequest, destinationInfo);
   70           if (connectionFactory == null) {
   71               throw new JMSException("Unable to find the Connection Factory....Not sending message");
   72           }
   73           Connection connection = null;
   74           Session session = null;
   75           try {
   76               connection = connectionFactory.createConnection();
   77               connection.start();
   78               session = connection.createSession(true, Session.DUPS_OK_ACKNOWLEDGE);
   79               MessageProducer producer = session.createProducer(destination);
   80               int deliveryMode = 0;
   81               if (messageInfo.isPersistent()) {
   82                   deliveryMode = DeliveryMode.PERSISTENT;
   83               } else {
   84                   deliveryMode = DeliveryMode.NON_PERSISTENT;
   85               }
   86               producer.setDeliveryMode(deliveryMode);
   87               TextMessage msg = session.createTextMessage();
   88               msg.setText(messageInfo.getMessage());
   89               msg.setJMSCorrelationID(messageInfo.getCorrelationId());
   90               msg.setJMSPriority(messageInfo.getPriority());
   91               msg.setJMSType(messageInfo.getJmsType());
   92               producer.send(msg, deliveryMode, messageInfo.getPriority(), TextMessage.DEFAULT_TIME_TO_LIVE);
   93               session.commit();
   94           } finally {
   95               if (connection != null)
   96                   try {
   97                       connection.close();
   98                   } catch (Exception e) {
   99                   }
  100           }
  101       }
  102   
  103       public JMSMessageInfo[] getMessagesList(PortletRequest portletRequest, JMSDestinationInfo jmsDestinationInfo, String selector) throws JMSException {
  104           Destination destination = getDestination(portletRequest, jmsDestinationInfo);
  105           if (destination == null) {
  106               throw new JMSException("Fail to find the destination " + jmsDestinationInfo.getPhysicalName());
  107           }
  108           if (jmsDestinationInfo.getType().equals(DestinationType.Queue)) {
  109               return getMessageFromQueue(portletRequest, jmsDestinationInfo, selector);
  110           } else {
  111               return getMessagesFromTopic(portletRequest, jmsDestinationInfo, selector);
  112           }
  113       }
  114   
  115       protected ConnectionFactory getConnectionFactory(PortletRequest portletRequest, JMSDestinationInfo destinationInfo) throws JMSException {
  116           JCAManagedConnectionFactory[] jcaManagedConnectionFactories = PortletManager.getOutboundFactoriesForRA(portletRequest, destinationInfo.getResourceAdapterModuleAbName(), destinationInfo
  117                   .getType().getConnectionFactoryInterfaces());
  118           if (jcaManagedConnectionFactories != null && jcaManagedConnectionFactories.length > 0) {
  119               try {
  120                   return (ConnectionFactory) (jcaManagedConnectionFactories[0].getConnectionFactory());
  121               } catch (Exception e) {
  122               }
  123           }
  124           return null;
  125       }
  126   
  127       protected Destination getDestination(PortletRequest portletRequest, JMSDestinationInfo destinationInfo) {
  128           Destination dest = null;
  129           try {
  130               ResourceAdapterModule resourceAdapterModule = (ResourceAdapterModule) PortletManager.getManagementHelper(portletRequest).getObject(destinationInfo.getResourceAdapterModuleAbName());
  131               JCAAdminObject[] jcaAdminObjects = PortletManager.getAdminObjectsForRA(portletRequest, resourceAdapterModule, new String[] { destinationInfo.getType().getDestinationInterface() });
  132               String targetPhysicalName = destinationInfo.getPhysicalName() == null ? "" : destinationInfo.getPhysicalName();
  133               for (JCAAdminObject jcaAdminObject : jcaAdminObjects) {
  134                   GeronimoManagedBean bean = (GeronimoManagedBean) jcaAdminObject;
  135                   ObjectName name = ObjectName.getInstance(bean.getObjectName());
  136                   String queueName = name.getKeyProperty(NameFactory.J2EE_NAME);
  137                   String currentPhysicalName = null;
  138                   try {
  139                       currentPhysicalName = (String) jcaAdminObject.getConfigProperty("PhysicalName");
  140                   } catch (Exception e) {
  141                       logger.warn("PhysicalName undefined, using queueName as PhysicalName");
  142                       currentPhysicalName = queueName;
  143                   }
  144                   if (targetPhysicalName.equals(currentPhysicalName)) {
  145                       AbstractName absName = kernel.getAbstractNameFor(bean);
  146                       dest = (Destination) kernel.invoke(absName, "$getResource");
  147                       return dest;
  148                   }
  149               }
  150           } catch (Exception ex) {
  151               logger.error("Failed to get destination", ex);
  152           }
  153           return dest;
  154       }
  155   
  156       protected abstract JMSMessageInfo[] getMessagesFromTopic(PortletRequest portletRequest, JMSDestinationInfo destinationInfo, String selector) throws JMSException;
  157   
  158       @SuppressWarnings("unchecked")
  159       protected JMSMessageInfo[] getMessageFromQueue(PortletRequest portletRequest, JMSDestinationInfo jmsDestinationInfo, String selector) throws JMSException {
  160           Connection connection = null;
  161           try {
  162               ConnectionFactory connectionFactory = getConnectionFactory(portletRequest, jmsDestinationInfo);            
  163               Queue queue = (Queue) getDestination(portletRequest, jmsDestinationInfo);
  164               List<JMSMessageInfo> messages = new LinkedList<JMSMessageInfo>();
  165               connection = connectionFactory.createConnection();
  166               connection.start();
  167               Session session = connection.createSession(false, QueueSession.AUTO_ACKNOWLEDGE);
  168               QueueBrowser qBrowser = session.createBrowser(queue);
  169               for (Enumeration e = qBrowser.getEnumeration(); e.hasMoreElements();) {
  170                   Message message = (Message) e.nextElement();
  171                   JMSMessageInfo messageInfo = new JMSMessageInfo();
  172                   messageInfo.setPriority(message.getJMSPriority());
  173                   messageInfo.setMessageId(message.getJMSMessageID());
  174                   messageInfo.setDestination(message.getJMSDestination().toString());
  175                   messageInfo.setTimeStamp(message.getJMSTimestamp());
  176                   messageInfo.setExpiration(message.getJMSExpiration());
  177                   messageInfo.setJmsType(message.getJMSType());
  178                   messageInfo.setReplyTo(message.getJMSReplyTo() == null ? "" : message.getJMSReplyTo().toString());
  179                   messageInfo.setCorrelationId(message.getJMSCorrelationID());
  180                   if (message instanceof TextMessage) {
  181                       messageInfo.setMessage(((TextMessage) message).getText());
  182                   } else {
  183                       messageInfo.setMessage("Only Text Messages will be displayed..");
  184                   }
  185                   messages.add(messageInfo);
  186               }
  187               return messages.toArray(new JMSMessageInfo[0]);
  188           } catch (JMSException e) {
  189               logger.error(e.getMessage(), e);
  190               throw e;
  191           } finally {
  192               if (connection != null) {
  193                   try {
  194                       connection.close();
  195                   } catch (Exception e) {
  196                   }
  197               }
  198           }
  199       }
  200   
  201       public abstract void purge(PortletRequest portletRequest, JMSDestinationInfo destinationInfo) throws JMSException;
  202   
  203       public abstract DestinationStatistics getDestinationStatistics(PortletRequest portletRequest, JMSDestinationInfo destinationInfo) throws JMSException;
  204   }

Home » geronimo-2.2-source-release » org.apache.geronimo.console.jmsmanager.helper » [javadoc | source]