KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > jndi > JNDIBaseStorable


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.jndi;
19
20 import javax.naming.NamingException JavaDoc;
21 import javax.naming.Reference JavaDoc;
22 import java.io.Externalizable JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.ObjectInput JavaDoc;
25 import java.io.ObjectOutput JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 /**
29  * Faciliates objects to be stored in JNDI as properties
30  */

31
32 public abstract class JNDIBaseStorable implements JNDIStorableInterface, Externalizable JavaDoc{
33     
34     private Properties JavaDoc properties = null;
35
36
37     /**
38      * Set the properties that will represent the instance in JNDI
39      *
40      * @param props
41      */

42     protected abstract void buildFromProperties(Properties JavaDoc props);
43
44     /**
45      * Initialize the instance from properties stored in JNDI
46      *
47      * @param props
48      */

49
50     protected abstract void populateProperties(Properties JavaDoc props);
51
52     /**
53      * set the properties for this instance as retrieved from JNDI
54      *
55      * @param props
56      */

57
58     public synchronized void setProperties(Properties JavaDoc props) {
59         this.properties = props;
60         buildFromProperties(props);
61     }
62
63     /**
64      * Get the properties from this instance for storing in JNDI
65      *
66      * @return the properties
67      */

68
69     public synchronized Properties JavaDoc getProperties() {
70         if (this.properties == null) {
71             this.properties = new Properties JavaDoc();
72         }
73         populateProperties(this.properties);
74         return this.properties;
75     }
76
77
78     /**
79      * Retrive a Reference for this instance to store in JNDI
80      *
81      * @return the built Reference
82      * @throws NamingException if error on building Reference
83      */

84     public Reference JavaDoc getReference() throws NamingException JavaDoc {
85         return JNDIReferenceFactory.createReference(this.getClass().getName(), this);
86     }
87
88     /**
89      * @param in
90      * @throws IOException
91      * @throws ClassNotFoundException
92      * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
93      */

94     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc,ClassNotFoundException JavaDoc{
95         Properties JavaDoc props = (Properties JavaDoc)in.readObject();
96         if (props != null) {
97             setProperties(props);
98         }
99         
100     }
101
102     /**
103      * @param out
104      * @throws IOException
105      * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
106      */

107     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc{
108         out.writeObject(getProperties());
109         
110     }
111
112 }
113
114
Popular Tags