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