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     */
017    package org.apache.activemq.pool;
018    
019    import java.util.Properties;
020    import javax.naming.NamingException;
021    import javax.naming.Reference;
022    import org.apache.activemq.ActiveMQConnectionFactory;
023    import org.apache.activemq.jndi.JNDIReferenceFactory;
024    import org.apache.activemq.jndi.JNDIStorableInterface;
025    import org.apache.activemq.pool.PooledConnectionFactory;
026    
027    /**
028    * AmqJNDIPooledConnectionFactory.java
029    * Created by linus on 2008-03-07.
030    */
031    public class AmqJNDIPooledConnectionFactory extends PooledConnectionFactory
032            implements JNDIStorableInterface {
033        private Properties properties;
034    
035        public AmqJNDIPooledConnectionFactory() {
036            super();
037        }
038    
039        /**
040         * set the properties for this instance as retrieved from JNDI
041         * 
042         * @param props
043         */
044        public synchronized void setProperties(Properties props) {
045            this.properties = props;
046            buildFromProperties(props);
047        }
048    
049        /**
050         * Get the properties from this instance for storing in JNDI
051         * 
052         * @return the properties
053         */
054        public synchronized Properties getProperties() {
055            if (this.properties == null) {
056                this.properties = new Properties();
057            }
058            populateProperties(this.properties);
059            return this.properties;
060        }
061    
062        /**
063         * Retrive a Reference for this instance to store in JNDI
064         * 
065         * @return the built Reference
066         * @throws NamingException
067         *             if error on building Reference
068         */
069        public Reference getReference() throws NamingException {
070            return JNDIReferenceFactory.createReference(this.getClass().getName(),
071                    this);
072        }
073    
074        public void buildFromProperties(Properties properties) {
075            if (properties == null) {
076                properties = new Properties();
077            }
078            ((ActiveMQConnectionFactory) getConnectionFactory())
079                    .buildFromProperties(properties);
080            String temp = properties.getProperty("maximumActive");
081            if (temp != null && temp.length() > 0) {
082                setMaximumActive(Integer.parseInt(temp));
083            }
084            temp = properties.getProperty("maxConnections");
085            if (temp != null && temp.length() > 0) {
086                setMaxConnections(Integer.parseInt(temp));
087            }
088        }
089    
090        public void populateProperties(Properties props) {
091            ((ActiveMQConnectionFactory) getConnectionFactory())
092                    .populateProperties(props);
093            props
094                    .setProperty("maximumActive", Integer
095                            .toString(getMaximumActive()));
096            props.setProperty("maxConnections", Integer
097                    .toString(getMaxConnections()));
098        }
099    }