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            if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
070                String key = (String)entry.getKey();
071                String value = (String)entry.getValue();
072
073                if (key.startsWith("java.naming.queue")) {
074                    String key1 = key.substring("java.naming.queue.".length());
075                    key1 = key1.replace('.', '/');
076                    environment1.put("queue." + key1, value);
077                } else if (key.startsWith("java.naming.topic")) {
078                    String key1 = key.substring("java.naming.topic.".length());
079                    key1 = key1.replace('.', '/');
080                    environment1.put("topic." + key1, value);
081                } else if (key.startsWith("java.naming.connectionFactoryNames")) {
082                    String key1 = key.substring("java.naming.".length());
083                    environment1.put(key1, value);
084                } else if (key.startsWith("java.naming.connection")) {
085                    String key1 = key.substring("java.naming.".length());
086                    environment1.put(key1, value);
087                } else if (key.startsWith(Context.PROVIDER_URL)) {
088                    // websphere administration console does not accept the , character
089                    // in provider url, so ; must be used
090                    // all ; to ,
091                    value = value.replace(';', ',');
092                    environment1.put(Context.PROVIDER_URL, value);
093                } else {
094                    environment1.put(key, value);
095                }
096            }
097        }
098
099        return environment1;
100    }
101}