001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.jndi;
018
019import java.util.Hashtable;
020import java.util.Iterator;
021import java.util.Map;
022
023import javax.naming.Context;
024import javax.naming.NamingException;
025
026/**
027 * This implementation of <CODE>InitialContextFactory</CODE> should be used
028 * when ActiveMQ is used as WebSphere Generic JMS Provider. It is proved that it
029 * works on WebSphere 5.1. The reason for using this class is that custom
030 * property defined for Generic JMS Provider are passed to InitialContextFactory
031 * only if it begins with java.naming or javax.naming prefix. Additionaly
032 * provider url for the JMS provider can not contain ',' character that is
033 * necessary when the list of nodes is provided. So the role of this class is to
034 * transform properties before passing it to
035 * <CODE>ActiveMQInitialContextFactory</CODE>.
036 * 
037 * @author Pawel Tucholski
038 */
039public class ActiveMQWASInitialContextFactory extends ActiveMQInitialContextFactory {
040
041    /**
042     * @see javax.naming.spi.InitialContextFactory#getInitialContext(java.util.Hashtable)
043     */
044    public Context getInitialContext(Hashtable environment) throws NamingException {
045
046        return super.getInitialContext(transformEnvironment(environment));
047    }
048
049    /**
050     * Performs following transformation of properties:
051     * <ul>
052     * <li>(java.naming.queue.xxx.yyy,value)=>(queue.xxx/yyy,value)
053     * <li>(java.naming.topic.xxx.yyy,value)=>(topic.xxx/yyy,value)
054     * <li>(java.naming.connectionFactoryNames,value)=>(connectionFactoryNames,value)
055     * <li>(java.naming.provider.url,url1;url2)=>java.naming.provider.url,url1,url1)
056     * <ul>
057     * 
058     * @param environment properties for transformation
059     * @return environment after transformation
060     */
061    protected Hashtable transformEnvironment(Hashtable environment) {
062
063        Hashtable environment1 = new Hashtable();
064
065        Iterator it = environment.entrySet().iterator();
066
067        while (it.hasNext()) {
068            Map.Entry entry = (Map.Entry)it.next();
069            String key = (String)entry.getKey();
070            String value = (String)entry.getValue();
071
072            if (key.startsWith("java.naming.queue")) {
073                String key1 = key.substring("java.naming.queue.".length());
074                key1 = key1.replace('.', '/');
075                environment1.put("queue." + key1, value);
076            } else if (key.startsWith("java.naming.topic")) {
077                String key1 = key.substring("java.naming.topic.".length());
078                key1 = key1.replace('.', '/');
079                environment1.put("topic." + key1, value);
080            } else if (key.startsWith("java.naming.connectionFactoryNames")) {
081                String key1 = key.substring("java.naming.".length());
082                environment1.put(key1, value);
083            } else if (key.startsWith("java.naming.connection")) {
084                String key1 = key.substring("java.naming.".length());
085                environment1.put(key1, value);
086            } else if (key.startsWith(Context.PROVIDER_URL)) {
087                // websphere administration console does not exept , character
088                // in provider url, so ; must be used
089                // all ; to ,
090                value = value.replace(';', ',');
091                environment1.put(Context.PROVIDER_URL, value);
092            } else {
093                environment1.put(key, value);
094            }
095        }
096
097        return environment1;
098    }
099}