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.jndi;
018    
019    import java.io.Externalizable;
020    import java.io.IOException;
021    import java.io.ObjectInput;
022    import java.io.ObjectOutput;
023    import java.util.Properties;
024    
025    import javax.naming.NamingException;
026    import javax.naming.Reference;
027    
028    /**
029     * Facilitates objects to be stored in JNDI as properties
030     */
031    
032    public abstract class JNDIBaseStorable implements JNDIStorableInterface, Externalizable {
033    
034        private Properties properties;
035    
036        /**
037         * Set the properties that will represent the instance in JNDI
038         * 
039         * @param props
040         */
041        protected abstract void buildFromProperties(Properties props);
042    
043        /**
044         * Initialize the instance from properties stored in JNDI
045         * 
046         * @param props
047         */
048    
049        protected abstract void populateProperties(Properties props);
050    
051        /**
052         * set the properties for this instance as retrieved from JNDI
053         * 
054         * @param props
055         */
056    
057        public synchronized void setProperties(Properties props) {
058            this.properties = props;
059            buildFromProperties(props);
060        }
061    
062        /**
063         * Get the properties from this instance for storing in JNDI
064         * 
065         * @return the properties
066         */
067    
068        public synchronized Properties getProperties() {
069            if (this.properties == null) {
070                this.properties = new Properties();
071            }
072            populateProperties(this.properties);
073            return this.properties;
074        }
075    
076        /**
077         * Retrive a Reference for this instance to store in JNDI
078         * 
079         * @return the built Reference
080         * @throws NamingException if error on building Reference
081         */
082        public Reference getReference() throws NamingException {
083            return JNDIReferenceFactory.createReference(this.getClass().getName(), this);
084        }
085    
086        /**
087         * @param in
088         * @throws IOException
089         * @throws ClassNotFoundException
090         * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
091         */
092        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
093            Properties props = (Properties)in.readObject();
094            if (props != null) {
095                setProperties(props);
096            }
097    
098        }
099    
100        /**
101         * @param out
102         * @throws IOException
103         * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
104         */
105        public void writeExternal(ObjectOutput out) throws IOException {
106            out.writeObject(getProperties());
107    
108        }
109    
110    }