View Javadoc

1   /*
2    * $Id: AbstractConfiguration.java,v 1.6 2005/06/01 19:55:51 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.core.libraries;
10  
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.io.OutputStream;
14  import java.util.Iterator;
15  import java.util.Map;
16  import java.util.Properties;
17  
18  /***
19   * Abstract implementation of IConfiguration.  Uses two 
20   * {@link Properties} objects and the {@link IConfiguration#USER_SUFFIX} and 
21   * {@link IConfiguration#SYSTEM_SUFFIX} values to implement the 
22   * functionality specified by IConfiguration.
23   * 
24   * @author aperezlo
25   */
26  public abstract class AbstractConfiguration implements IConfiguration
27  {
28     private static final int USER = 1;
29     private static final int SYSTEM = 2;
30     
31     private Properties _userProps = new Properties();
32     private Properties _systemProps = new Properties();
33     
34     private Properties getAProperties(int which)
35     {
36        Properties toReturn = new Properties();
37        Iterator i = null;
38        Map.Entry tempMapping = null;
39        String newKey = null;
40        String suffix = null;
41  
42        i = (which == SYSTEM) ? _systemProps.entrySet().iterator() 
43                            : _userProps.entrySet().iterator();
44  
45        while(i.hasNext())
46        {
47           tempMapping = (Map.Entry) i.next();
48           toReturn.setProperty((String) tempMapping.getKey(), (String) tempMapping.getValue());
49        }
50  
51        return toReturn;
52     }
53     private void setProperty(String key, String value, int which)
54     {
55        if(value != null)
56        {
57           if(which == SYSTEM)
58           {
59              _systemProps.setProperty(key, value);
60           }
61           else if(which == USER)
62           {
63              _userProps.setProperty(key, value);
64           }
65        }
66     }
67     private String getProperty(String key, int which)
68     {
69        String toReturn = null;
70        if(which == SYSTEM)
71        {
72           toReturn = (String) _systemProps.get(key);
73        }
74        else if(which == USER)
75        {
76           toReturn = (String) _userProps.get(key);
77        }
78        return toReturn;
79     }
80     
81     private void removeProperty(String key, int which)
82     {
83        if(which == SYSTEM)
84        {
85           _systemProps.remove(key);
86           
87        }
88        else if(which == USER)
89        {
90           _userProps.remove(key);
91        }
92     }
93     
94     /* (non-Javadoc)
95      * @see com.bbn.swede.core.libraries.IConfiguration#getSystemProperties()
96      */
97     public Properties getSystemProperties()
98     {
99        return getAProperties(SYSTEM);
100    }
101    
102    /* (non-Javadoc)
103     * @see com.bbn.swede.core.libraries.IConfiguration#getUserProperties()
104     */
105    public Properties getUserProperties()
106    {
107       return getAProperties(USER);
108    }
109 
110    /* (non-Javadoc)
111     * @see com.bbn.swede.core.libraries.IConfiguration#load(java.io.InputStream)
112     */
113    public void load(InputStream in) throws IOException
114    {
115       Properties tempProperties = new Properties();
116       tempProperties.load(in);
117       setProperties(tempProperties);
118    }
119 
120    /* (non-Javadoc)
121     * @see com.bbn.swede.core.libraries.IConfiguration#setProperties(java.util.Properties)
122     */
123    public void setProperties(Properties in)
124    {
125       Map.Entry tempMapping = null;
126       Iterator i = null;
127       
128       String key = null;
129       String value = null;
130       
131       _userProps = new Properties();
132       _systemProps = new Properties();
133       
134       i = in.entrySet().iterator();
135       while(i.hasNext())
136       {
137          tempMapping = (Map.Entry) i.next();
138          key = (String) tempMapping.getKey();
139          value = (String) tempMapping.getValue();
140          if(key.endsWith(SYSTEM_SUFFIX))
141          {
142             key = key.substring(0, key.lastIndexOf(SYSTEM_SUFFIX));
143             _systemProps.setProperty(key, value);
144          }
145          else if(key.endsWith(USER_SUFFIX))
146          {
147             key = key.substring(0, key.lastIndexOf(USER_SUFFIX));
148             _userProps.setProperty(key, value);
149          }
150       }
151    }
152    
153    /* (non-Javadoc)
154     * @see com.bbn.swede.core.libraries.IConfiguration#removeSystemProperty(java.lang.String)
155     */
156    public void removeSystemProperty(String key)
157    {
158       removeProperty(key, SYSTEM);
159    }
160    /* (non-Javadoc)
161     * @see com.bbn.swede.core.libraries.IConfiguration#removeUserProperty(java.lang.String)
162     */
163    public void removeUserProperty(String key)
164    {
165       removeProperty(key, USER);
166    }
167    
168    /* (non-Javadoc)
169     * @see com.bbn.swede.core.libraries.IConfiguration#setSystemProperties(java.util.Properties)
170     */
171    public void setSystemProperties(Properties p)
172    {
173       _systemProps = p;
174    }
175    /* (non-Javadoc)
176     * @see com.bbn.swede.core.libraries.IConfiguration#setUserProperties(java.util.Properties)
177     */
178    public void setUserProperties(Properties p)
179    {
180       _userProps = p;
181    }
182 
183    /* (non-Javadoc)
184     * @see com.bbn.swede.core.libraries.IConfiguration#setSystemProperty(java.lang.String, java.lang.String)
185     */
186    public void setSystemProperty(String key, String value)
187    {
188       setProperty(key, value, SYSTEM);
189    }
190    /* (non-Javadoc)
191     * @see com.bbn.swede.core.libraries.IConfiguration#setUserProperty(java.lang.String, java.lang.String)
192     */
193    public void setUserProperty(String key, String value)
194    {
195       setProperty(key, value, USER);
196    }
197    
198    /***
199     * This method returns a single {@link Properties} object with 
200     * all of the properties (both System and User).  Each user property 
201     * (i.e. those that have been added with {@link #setUserProperty}) will 
202     * appear in the returned object with the value of {@link IConfiguration#USER_SUFFIX} 
203     * appended, and System properties will similarly have {@link IConfiguration#SYSTEM_SUFFIX} 
204     * appended.
205     * <br />
206     * <br />
207     * 
208     * @see IConfiguration#getProperties
209     */
210    public Properties getProperties()
211    {
212       Properties tempProperties = new Properties();
213       Map.Entry tempMapping = null;
214       Iterator i = null;
215       String tempKey = null;
216       String tempValue = null;
217       
218       i = _systemProps.entrySet().iterator();
219       while(i.hasNext())
220       {
221          tempMapping = (Map.Entry) i.next();
222          tempKey = (String) tempMapping.getKey();
223          tempValue = (String) tempMapping.getValue();
224          
225          tempKey += SYSTEM_SUFFIX;
226          tempProperties.setProperty(tempKey, tempValue);
227       }
228       i = _userProps.entrySet().iterator();
229       while(i.hasNext())
230       {
231          tempMapping = (Map.Entry) i.next();
232          tempKey = (String) tempMapping.getKey();
233          tempValue = (String) tempMapping.getValue();
234          
235          tempKey += USER_SUFFIX;
236          tempProperties.setProperty(tempKey, tempValue);
237       }      
238 
239       return tempProperties;
240    }
241 
242    /* (non-Javadoc)
243     * @see com.bbn.swede.core.libraries.IConfiguration#store(java.io.OutputStream, java.lang.String)
244     */
245    public void store(OutputStream out, String header) throws IOException
246    {
247       Properties tempProperties = getProperties();
248       tempProperties.store(out, header);
249    }
250    
251    /*
252     *  (non-Javadoc)
253     * @see com.bbn.swede.core.libraries.IConfiguration#getPropertiesValidator()
254     */
255    public abstract IConfigurationValidator getPropertiesValidator();
256 
257    /*
258     *  (non-Javadoc)
259     * @see com.bbn.swede.core.libraries.IConfiguration#getSystemProperty(java.lang.String)
260     */
261    public String getSystemProperty(String key)
262    {
263       return getProperty(key, SYSTEM);
264    }
265    
266    /* (non-Javadoc)
267     * @see com.bbn.swede.core.libraries.IConfiguration#getUserProperty(java.lang.String)
268     */
269    public String getUserProperty(String key)
270    {
271       return getProperty(key, USER);
272    }
273 }