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