001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.resource.activemq;
018:
019: import org.apache.activemq.broker.BrokerFactory;
020: import org.apache.activemq.broker.BrokerService;
021: import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter;
022: import org.apache.activemq.store.memory.MemoryPersistenceAdapter;
023: import org.apache.openejb.loader.SystemInstance;
024: import org.apache.openejb.spi.ContainerSystem;
025:
026: import javax.naming.Context;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029: import javax.sql.DataSource;
030: import java.net.URI;
031: import java.util.Map;
032: import java.util.Properties;
033:
034: public class OpenEjbBrokerFactory implements
035: BrokerFactory.BrokerFactoryHandler {
036: private static final ThreadLocal<Properties> threadProperties = new ThreadLocal<Properties>();
037:
038: public static void setThreadProperties(Properties value) {
039: threadProperties.set(value);
040: }
041:
042: public BrokerService createBroker(URI brokerURI) throws Exception {
043: URI uri = new URI(brokerURI.getRawSchemeSpecificPart());
044: BrokerService broker = BrokerFactory.createBroker(uri);
045:
046: Properties properties = getLowerCaseProperties();
047:
048: Object value = properties.get("datasource");
049: if (value instanceof String && value.toString().length() == 0) {
050: value = null;
051: }
052:
053: if (value != null) {
054: DataSource dataSource;
055: if (value instanceof DataSource) {
056: dataSource = (DataSource) value;
057: } else {
058: String resouceId = (String) value;
059:
060: try {
061: ContainerSystem containerSystem = SystemInstance
062: .get().getComponent(ContainerSystem.class);
063: Context context = containerSystem.getJNDIContext();
064: Object obj = context
065: .lookup("java:openejb/Resource/"
066: + resouceId);
067: if (!(obj instanceof DataSource)) {
068: throw new IllegalArgumentException(
069: "Resource with id "
070: + resouceId
071: + " is not a DataSource, but is "
072: + obj.getClass().getName());
073: }
074: dataSource = (DataSource) obj;
075: } catch (NamingException e) {
076: throw new IllegalArgumentException(
077: "Unknown datasource " + resouceId);
078: }
079: }
080:
081: JDBCPersistenceAdapter persistenceAdapter = new JDBCPersistenceAdapter();
082: persistenceAdapter.setDataSource(dataSource);
083: broker.setPersistenceAdapter(persistenceAdapter);
084: } else {
085: MemoryPersistenceAdapter persistenceAdapter = new MemoryPersistenceAdapter();
086: broker.setPersistenceAdapter(persistenceAdapter);
087: }
088:
089: return broker;
090: }
091:
092: private Properties getLowerCaseProperties() {
093: Properties properties = threadProperties.get();
094: Properties newProperties = new Properties();
095: if (properties != null) {
096: for (Map.Entry<Object, Object> entry : properties
097: .entrySet()) {
098: Object key = entry.getKey();
099: if (key instanceof String) {
100: key = ((String) key).toLowerCase();
101: }
102: newProperties.put(key, entry.getValue());
103: }
104: }
105: return newProperties;
106: }
107: }
|