Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » resource » activemq » [javadoc | source]
    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   package org.apache.openejb.resource.activemq;
   19   
   20   import org.apache.activemq.broker.BrokerFactory;
   21   import org.apache.activemq.broker.BrokerService;
   22   import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter;
   23   import org.apache.activemq.store.memory.MemoryPersistenceAdapter;
   24   import org.apache.openejb.loader.SystemInstance;
   25   import org.apache.openejb.spi.ContainerSystem;
   26   
   27   import javax.naming.Context;
   28   import javax.naming.NamingException;
   29   import javax.sql.DataSource;
   30   import java.net.URI;
   31   import java.util.Map;
   32   import java.util.Properties;
   33   
   34   public class OpenEjbBrokerFactory implements BrokerFactory.BrokerFactoryHandler {
   35       private static final ThreadLocal<Properties> threadProperties = new ThreadLocal<Properties>();
   36   
   37       public static void setThreadProperties(Properties value) {
   38           threadProperties.set(value);
   39       }
   40   
   41       public BrokerService createBroker(URI brokerURI) throws Exception {
   42           URI uri = new URI(brokerURI.getRawSchemeSpecificPart());
   43           BrokerService broker = BrokerFactory.createBroker(uri);
   44   
   45           Properties properties = getLowerCaseProperties();
   46   
   47           Object value = properties.get("datasource");
   48           if (value instanceof String && value.toString().length() == 0) {
   49               value = null;
   50           }
   51   
   52           if (value != null) {
   53               DataSource dataSource;
   54               if (value instanceof DataSource) {
   55                   dataSource = (DataSource) value;
   56               } else {
   57                   String resouceId = (String) value;
   58   
   59                   try {
   60                       ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
   61                       Context context = containerSystem.getJNDIContext();
   62                       Object obj = context.lookup("openejb/Resource/" + resouceId);
   63                       if (!(obj instanceof DataSource)) {
   64                           throw new IllegalArgumentException("Resource with id " + resouceId +
   65                                   " is not a DataSource, but is " + obj.getClass().getName());
   66                       }
   67                       dataSource = (DataSource) obj;
   68                   } catch (NamingException e) {
   69                       throw new IllegalArgumentException("Unknown datasource " + resouceId);
   70                   }
   71               }
   72   
   73               JDBCPersistenceAdapter persistenceAdapter = new JDBCPersistenceAdapter();
   74               persistenceAdapter.setDataSource(dataSource);
   75               broker.setPersistenceAdapter(persistenceAdapter);
   76           } else {
   77               MemoryPersistenceAdapter persistenceAdapter = new MemoryPersistenceAdapter();
   78               broker.setPersistenceAdapter(persistenceAdapter);
   79           }
   80   
   81           return broker;
   82       }
   83   
   84   
   85       private Properties getLowerCaseProperties() {
   86           Properties properties = threadProperties.get();
   87           Properties newProperties = new Properties();
   88           if (properties != null) {
   89               for (Map.Entry<Object, Object> entry : properties.entrySet()) {
   90                   Object key = entry.getKey();
   91                   if (key instanceof String) {
   92                       key = ((String) key).toLowerCase();
   93                   }
   94                   newProperties.put(key, entry.getValue());
   95               }
   96           }
   97           return newProperties;
   98       }
   99   }

Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » resource » activemq » [javadoc | source]