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. Additionally
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 <tt>ActiveMQInitialContextFactory</tt>.
035 */
036public class ActiveMQWASInitialContextFactory extends ActiveMQInitialContextFactory {
037
038    /**
039     * @see javax.naming.spi.InitialContextFactory#getInitialContext(java.util.Hashtable)
040     */
041    public Context getInitialContext(Hashtable environment) throws NamingException {
042        return super.getInitialContext(transformEnvironment(environment));
043    }
044
045    /**
046     * Performs following transformation of properties:
047     * <ul>
048     * <li>(java.naming.queue.xxx.yyy,value)=>(queue.xxx/yyy,value)
049     * <li>(java.naming.topic.xxx.yyy,value)=>(topic.xxx/yyy,value)
050     * <li>(java.naming.connectionFactoryNames,value)=>(connectionFactoryNames,value)
051     * <li>(java.naming.provider.url,url1;url2)=>java.naming.provider.url,url1,url1)
052     * <ul>
053     *
054     * @param environment properties for transformation
055     * @return environment after transformation
056     */
057    @SuppressWarnings("unchecked")
058    protected Hashtable transformEnvironment(Hashtable environment) {
059
060        Hashtable environment1 = new Hashtable();
061
062        Iterator it = environment.entrySet().iterator();
063
064        while (it.hasNext()) {
065            Map.Entry entry = (Map.Entry)it.next();
066            if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
067                String key = (String)entry.getKey();
068                String value = (String)entry.getValue();
069
070                if (key.startsWith("java.naming.queue.")) {
071                    String key1 = key.substring("java.naming.queue.".length());
072                    key1 = key1.replace('.', '/');
073                    environment1.put("queue." + key1, value);
074                } else if (key.startsWith("java.naming.topic.")) {
075                    String key1 = key.substring("java.naming.topic.".length());
076                    key1 = key1.replace('.', '/');
077                    environment1.put("topic." + key1, value);
078                } else if (key.startsWith("java.naming.connectionFactoryNames")) {
079                    String key1 = key.substring("java.naming.".length());
080                    environment1.put(key1, value);
081                } else if (key.startsWith("java.naming.connection")) {
082                    String key1 = key.substring("java.naming.".length());
083                    environment1.put(key1, value);
084                } else if (key.startsWith(Context.PROVIDER_URL)) {
085                    // Websphere administration console does not accept the , character
086                    // in provider url, so ; must be used all ; to ,
087                    value = value.replace(';', ',');
088                    environment1.put(Context.PROVIDER_URL, value);
089                } else {
090                    environment1.put(key, value);
091                }
092            }
093        }
094
095        return environment1;
096    }
097}