001    /**
002     *
003     * Copyright 2005 Pawel Tucholski
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * 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     *
017     **/
018    package org.activemq.jndi;
019    
020    import javax.naming.Context;
021    import javax.naming.NamingException;
022    import java.util.Hashtable;
023    import java.util.Iterator;
024    import java.util.Map;
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 <CODE>ActiveMQInitialContextFactory</CODE>.
035     */
036    public 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    
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.connectionFactoryNames,value)=>(connectionFactoryNames,value)
052         * <li>(java.naming.provider.url,url1;url2)=>java.naming.provider.url,url1,url1)
053         * <ul>
054         * 
055         * @param environment
056         *            properties for transformation
057         * @return environment after transformation
058         */
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                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                }
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                }
080                else if (key.startsWith("java.naming.connectionFactoryNames")) {
081                    String key1 = key.substring("java.naming.".length());
082                    environment1.put(key1, value);
083                }
084                else if (key.startsWith("java.naming.connection")) {
085                    String key1 = key.substring("java.naming.".length());
086                    environment1.put(key1, value);
087                }
088                else if (key.startsWith(Context.PROVIDER_URL)) {
089                    // websphere administration console does not exept , character
090                    // in provider url, so ; must be used
091                    // all ; to ,
092                    value = value.replace(';', ',');
093                    environment1.put(Context.PROVIDER_URL, value);
094                }
095                else {
096                    environment1.put(key, value);
097                }
098            }
099    
100            return environment1;
101        }
102    }