View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2toolkit library.
13   *
14   *  The Initial Owner of the Original Code is
15   *  Paul Monday
16   *
17   *  Portions created by Power Of Two S.R.L. are
18   *  Copyright (C) Power Of Two S.R.L.
19   *  All Rights Reserved.
20   *
21   * Contributor(s):
22   */
23  
24  package com.pow2.resources;
25  
26  import java.io.Serializable;
27  import java.util.Properties;
28  import java.util.Enumeration;
29  
30  import org.apache.log4j.Category;
31  
32  
33  /***
34   *  Property container class.
35   *
36   * @author     Paul Monday
37   * @created    23 aprile 2002
38   */
39  public abstract class PropertyContainerImpl
40       implements PropertyContainer, Serializable
41  {
42    /*** Log4J category. */
43    protected Category cat = Category.getInstance(this.getClass());
44  
45    /***  properties table */
46    protected Properties properties = new Properties();
47  
48  
49    /***  Constructor for the PropertyContainerImpl object */
50    public PropertyContainerImpl()
51    {
52    }
53  
54  
55    /***
56     * Add a property associated with a token name.
57     * <br>
58     * If the token already exists, the value will be replaced.
59     * If the token does not exist, it will be added with the value.
60     *
61     * @param  value  is an object that cannot be null
62     * @param  token  is a key that can be used to retrieve the value
63     */
64    public void addProperty(Object value, String token)
65    {
66      if (value == null || token == null)
67        return;
68  
69      if (properties.containsKey(token))
70        properties.remove(token);
71  
72      properties.put(token, value);
73    }
74  
75  
76    /***
77     *  Retrieve a value by a particular token.
78     *
79     * @param  token  is a key that can be used to retrieve the value
80     * @return        String is the value associated with the token.  It
81     *                will not be null
82     */
83    public String getProperty(String token)
84    {
85      return (token != null) ? properties.getProperty(token) : null;
86    }
87  
88  
89    /***
90     *  Retrieve a value by a particular token.
91     *
92     * @param  token  is a key that can be used to retrieve the value
93     * @return        Object is the value associated with the token.  It
94     *                will not be null
95     */
96    public Object getObject(String token)
97    {
98      return (token != null) ? properties.get(token) : null;
99    }
100 
101 
102   /***
103    * Retrieve all property keys currently in use.
104    *
105    * @return    String[] is an array of all valid token names
106    */
107   public String[] getPropertyKeys()
108   {
109     String keys[] = null;
110 
111     synchronized (properties)
112     {
113       int           s = properties.size();
114       keys          = new String[s];
115       Enumeration e = properties.keys();
116       int           i = 0;
117 
118       while (e.hasMoreElements())
119       {
120         keys[i] = (String) e.nextElement();
121         i++;
122       }
123     }
124 
125     return keys;
126   }
127 
128 
129   /***
130    *  Remove a value associated with a particular token.
131    *
132    * @param  token  is a key associated with a value that was added
133    */
134   public void removeProperty(String token)
135   {
136     if (token == null)
137       return;
138 
139     properties.remove(token);
140   }
141 }