View Javadoc

    1   /***
    2    * @(#)ServiceLocatorImpl.java
    3    *
    4    * JFoxSOAF, Service-Oriented Application Framework
    5    *
    6    * Copyright(c) JFoxSOAF Team
    7    *
    8    * Licensed under the GNU LGPL, Version 2.1 (the "License");
    9    * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   * http://www.gnu.org/copyleft/lesser.html
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   *
    20   * For more information, please visit:
    21   * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
    22   * http://www.huihoo.org/jfox/jfoxsoaf
    23   */
    24
    25  package org.huihoo.jfox.soaf.services.ejb;
    26
    27  import java.net.URL;
    28  import java.rmi.Remote;
    29  import java.util.Collections;
    30  import java.util.HashMap;
    31  import java.util.Map;
    32
    33  import javax.ejb.EJBHome;
    34  import javax.ejb.EJBLocalHome;
    35  import javax.jms.ConnectionFactory;
    36  import javax.jms.Destination;
    37  import javax.jms.Queue;
    38  import javax.jms.QueueConnectionFactory;
    39  import javax.jms.Topic;
    40  import javax.jms.TopicConnectionFactory;
    41  import javax.mail.Session;
    42  import javax.naming.InitialContext;
    43  import javax.naming.NamingException;
    44  import javax.rmi.PortableRemoteObject;
    45  import javax.sql.DataSource;
    46  import javax.transaction.UserTransaction;
    47  import javax.xml.rpc.Service;
    48
    49  import org.apache.commons.logging.Log;
    50  import org.apache.commons.logging.LogFactory;
    51  import org.huihoo.jfox.soaf.exception.ServiceLocatorException;
    52
    53  /***
    54   * <p>
    55   * ServiceLocator Implementation.
    56   * </p>
    57   *
    58   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    59   * @version $Revision: 1.14 $ $Date: 2005/05/22 06:49:31 $
    60   * @version Revision: 1.0
    61   */
    62
    63  public class ServiceLocatorImpl implements ServiceLocator {
    64
    65      private InitialContext ic = null;
    66
    67      private Map cache = null;
    68
    69      private final Log logger = LogFactory.getLog(getClass());
    70
    71      private DataSource dataSource = null;
    72
    73      private Session mailSession = null;
    74
    75      public ServiceLocatorImpl() throws ServiceLocatorException {
    76          try {
    77              ic = new InitialContext();
    78              cache = Collections.synchronizedMap(new HashMap());
    79          } catch (NamingException ne) {
    80              logger.error("ServiceService NamingException " + ne.getMessage());
    81              throw new ServiceLocatorException(ne);
    82          } catch (Exception e) {
    83              logger.error("ServiceLocator Exception " + e.getMessage());
    84              throw new ServiceLocatorException(e);
    85          }
    86      }
    87
    88      /***
    89       * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getBoolean(java.lang.String)
    90       */
    91      public boolean getBoolean(String envName) throws ServiceLocatorException {
    92          Boolean bool = null;
    93          try {
    94              bool = (Boolean) ic.lookup(envName);
    95          } catch (NamingException ne) {
    96              throw new ServiceLocatorException(ne);
    97          } catch (Exception e) {
    98              throw new ServiceLocatorException(e);
    99          }
    100         return bool.booleanValue();
    101     }
    102
    103     /***
    104      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getLocalHome(java.lang.String)
    105      */
    106     public EJBLocalHome getLocalHome(String jndiHomeName)
    107             throws ServiceLocatorException {
    108         EJBLocalHome home = null;
    109         try {
    110             if (cache.containsKey(jndiHomeName)) {
    111                 home = (EJBLocalHome) cache.get(jndiHomeName);
    112             } else {
    113                 home = (EJBLocalHome) ic.lookup(jndiHomeName);
    114                 cache.put(jndiHomeName, home);
    115             }
    116         } catch (NamingException ne) {
    117             throw new ServiceLocatorException(ne);
    118         } catch (Exception e) {
    119             throw new ServiceLocatorException(e);
    120         }
    121         return home;
    122     }
    123
    124     /***
    125      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getRemoteHome(java.lang.String,
    126      *      java.lang.ClassLoadProxy)
    127      */
    128     public EJBHome getRemoteHome(String jndiHomeName, Class className)
    129             throws ServiceLocatorException {
    130         EJBHome home = null;
    131         try {
    132             if (cache.containsKey(jndiHomeName)) {
    133                 home = (EJBHome) cache.get(jndiHomeName);
    134             } else {
    135                 Object objref = ic.lookup(jndiHomeName);
    136                 Object obj = PortableRemoteObject.narrow(objref, className);
    137                 home = (EJBHome) obj;
    138                 cache.put(jndiHomeName, home);
    139             }
    140         } catch (NamingException ne) {
    141             throw new ServiceLocatorException(ne);
    142         } catch (Exception e) {
    143             throw new ServiceLocatorException(e);
    144         }
    145         return home;
    146     }
    147
    148     /***
    149      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getQueue(java.lang.String)
    150      */
    151     public Queue getQueue(String queueName) throws ServiceLocatorException {
    152         Queue queue = null;
    153         try {
    154             if (cache.containsKey(queueName)) {
    155                 queue = (Queue) cache.get(queueName);
    156             } else {
    157                 queue = (Queue) ic.lookup(queueName);
    158                 cache.put(queueName, queue);
    159             }
    160         } catch (NamingException ne) {
    161             throw new ServiceLocatorException(ne);
    162         } catch (Exception e) {
    163             throw new ServiceLocatorException(e);
    164         }
    165         return queue;
    166     }
    167
    168     /***
    169      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getQueueConnectionFactory(java.lang.String)
    170      */
    171     public QueueConnectionFactory getQueueConnectionFactory(
    172             String qConnFactoryName) throws ServiceLocatorException {
    173         QueueConnectionFactory factory = null;
    174         try {
    175             if (cache.containsKey(qConnFactoryName)) {
    176                 factory = (QueueConnectionFactory) cache.get(qConnFactoryName);
    177             } else {
    178                 factory = (QueueConnectionFactory) ic.lookup(qConnFactoryName);
    179                 cache.put(qConnFactoryName, factory);
    180             }
    181         } catch (NamingException ne) {
    182             throw new ServiceLocatorException(ne);
    183         } catch (Exception e) {
    184             throw new ServiceLocatorException(e);
    185         }
    186         return factory;
    187     }
    188
    189     /***
    190      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getTopic(java.lang.String)
    191      */
    192     public Topic getTopic(String topicName) throws ServiceLocatorException {
    193         Topic topic = null;
    194         try {
    195             if (cache.containsKey(topicName)) {
    196                 topic = (Topic) cache.get(topicName);
    197             } else {
    198                 topic = (Topic) ic.lookup(topicName);
    199                 cache.put(topicName, topic);
    200             }
    201         } catch (NamingException ne) {
    202             throw new ServiceLocatorException(ne);
    203         } catch (Exception e) {
    204             throw new ServiceLocatorException(e);
    205         }
    206         return topic;
    207     }
    208
    209     /***
    210      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getTopicConnectionFactory(java.lang.String)
    211      */
    212     public TopicConnectionFactory getTopicConnectionFactory(
    213             String topicConnFactoryName) throws ServiceLocatorException {
    214         TopicConnectionFactory factory = null;
    215         try {
    216             if (cache.containsKey(topicConnFactoryName)) {
    217                 factory = (TopicConnectionFactory) cache
    218                         .get(topicConnFactoryName);
    219             } else {
    220                 factory = (TopicConnectionFactory) ic
    221                         .lookup(topicConnFactoryName);
    222                 cache.put(topicConnFactoryName, factory);
    223             }
    224         } catch (NamingException ne) {
    225             throw new ServiceLocatorException(ne);
    226         } catch (Exception e) {
    227             throw new ServiceLocatorException(e);
    228         }
    229         return factory;
    230     }
    231
    232     /***
    233      * @return the factory for the factory to get queue connections from
    234      */
    235     public ConnectionFactory getJMSConnectionFactory(String ConnFactoryName)
    236             throws ServiceLocatorException {
    237         ConnectionFactory factory = (ConnectionFactory) cache
    238                 .get(ConnFactoryName);
    239         if (factory == null) {
    240             try {
    241                 factory = (ConnectionFactory) ic.lookup(ConnFactoryName);
    242                 cache.put(ConnFactoryName, factory);
    243             } catch (Exception e) {
    244                 throw new ServiceLocatorException(e);
    245             }
    246         }
    247         return factory;
    248     }
    249
    250     /***
    251      * @return the Queue Destination to send messages to
    252      */
    253     public Destination getJMSDestination(String destName)
    254             throws ServiceLocatorException {
    255         Destination dest = (Destination) cache.get(destName);
    256         if (dest == null) {
    257             try {
    258                 dest = (Destination) ic.lookup(destName);
    259                 cache.put(destName, dest);
    260             } catch (Exception e) {
    261                 throw new ServiceLocatorException(e);
    262             }
    263         }
    264         return dest;
    265     }
    266
    267     /***
    268      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getDataSource(String)
    269      */
    270     public DataSource getDataSource(String dataSourceName)
    271             throws ServiceLocatorException {
    272
    273         try {
    274             if (cache.containsKey(dataSourceName)) {
    275                 dataSource = (DataSource) cache.get(dataSourceName);
    276             } else {
    277                 dataSource = (DataSource) ic.lookup(dataSourceName);
    278                 cache.put(dataSourceName, dataSource);
    279             }
    280         } catch (NamingException ne) {
    281             throw new ServiceLocatorException(ne);
    282         } catch (Exception e) {
    283             throw new ServiceLocatorException(e);
    284         }
    285         return dataSource;
    286     }
    287
    288     /***
    289      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getDataSource(String)
    290      */
    291     public Session getMailSession(String mailSessionName)
    292             throws ServiceLocatorException {
    293
    294         try {
    295             if (cache.containsKey(mailSessionName)) {
    296                 mailSession = (Session) cache.get(mailSessionName);
    297             } else {
    298                 mailSession = (Session) ic.lookup(mailSessionName);
    299                 cache.put(mailSessionName, mailSession);
    300             }
    301         } catch (NamingException ne) {
    302             throw new ServiceLocatorException(ne);
    303         } catch (Exception e) {
    304             throw new ServiceLocatorException(e);
    305         }
    306         return mailSession;
    307     }
    308
    309     /***
    310      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getString(String)
    311      */
    312     public String getString(String envName) throws ServiceLocatorException {
    313         String envEntry = null;
    314         try {
    315             envEntry = (String) ic.lookup(envName);
    316         } catch (NamingException ne) {
    317             throw new ServiceLocatorException(ne);
    318         } catch (Exception e) {
    319             throw new ServiceLocatorException(e);
    320         }
    321         return envEntry;
    322     }
    323
    324     /***
    325      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getUserTransaction(String)
    326      */
    327     public UserTransaction getUserTransaction(String utName)
    328             throws ServiceLocatorException {
    329         try {
    330             return (UserTransaction) ic.lookup(utName);
    331         } catch (Exception e) {
    332             throw new ServiceLocatorException(e);
    333         }
    334     }
    335
    336     /***
    337      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getUrl(String)
    338      */
    339     public URL getUrl(String envName) throws ServiceLocatorException {
    340         URL url = null;
    341         try {
    342             url = (URL) ic.lookup(envName);
    343         } catch (NamingException ne) {
    344             throw new ServiceLocatorException(ne);
    345         } catch (Exception e) {
    346             throw new ServiceLocatorException(e);
    347         }
    348         return url;
    349     }
    350
    351     /***
    352      * @see org.huihoo.jfox.soaf.services.ejb.ServiceLocator#getServicePort(String, Class)
    353      */
    354     public Remote getServicePort(String jndiHomeName, Class className)
    355             throws ServiceLocatorException {
    356         Remote servicePort = (Remote) cache.get(jndiHomeName);
    357         if (servicePort == null) {
    358             try {
    359                 Service service = (Service) ic.lookup(jndiHomeName);
    360                 servicePort = service.getPort(className);
    361                 cache.put(jndiHomeName, servicePort);
    362             } catch (Exception e) {
    363                 throw new ServiceLocatorException(e);
    364             }
    365         }
    366         return servicePort;
    367     }
    368
    369 }