KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > Property


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.properties;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import com.sslexplorer.boot.AbstractPropertyKey;
26 import com.sslexplorer.boot.PropertyClass;
27 import com.sslexplorer.boot.PropertyClassManager;
28 import com.sslexplorer.boot.PropertyDefinition;
29 import com.sslexplorer.boot.PropertyList;
30 import com.sslexplorer.core.CoreEventConstants;
31 import com.sslexplorer.core.CoreServlet;
32 import com.sslexplorer.properties.impl.profile.ProfilePropertyKey;
33 import com.sslexplorer.security.SessionInfo;
34
35 /**
36  * Utility methods for dealing dealing with property values and definition. In
37  * general, all client code should access properties via these methods.
38  *
39  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
40  */

41 public class Property {
42
43     final static Log log = LogFactory.getLog(Property.class);
44
45     /*
46      * Prevent instantiation
47      */

48     private Property() {
49     }
50
51     /**
52      * Convenience method to search all property types for the specified
53      * definition. If no such definition can be found in any type
54      * <code>null</code> will be returned.
55      *
56      * @param key key
57      * @return definition or <code>null</codE> if no definition could be found
58      */

59     public static PropertyDefinition getDefinition(AbstractPropertyKey key) {
60         PropertyClassManager mgr = PropertyClassManager.getInstance();
61         PropertyClass propertyClass = mgr.getPropertyClass(key.getPropertyClassName());
62         if (propertyClass == null) {
63             throw new IllegalArgumentException JavaDoc("Invalid property class " + key.getPropertyClassName() + ".");
64         }
65         return propertyClass.getDefinition(key.getName());
66     }
67
68     /**
69      * Set the value for property and fire the appropriate events. An event will
70      * only be fired if the value of the property changes. <code>null</code>
71      * will be returned if the property cannot be set for some reason.
72      *
73      * @param key property key
74      * @param newValue new value
75      * @param sessionInfo session info
76      * @return old value
77      */

78     public static String JavaDoc setProperty(AbstractPropertyKey key, String JavaDoc newValue, SessionInfo sessionInfo) {
79
80         PropertyDefinition def = getDefinition(key);
81         PropertyProfile p = null;
82         try {
83             PropertyClass t = def.getPropertyClass();
84             String JavaDoc oldVal = t.storeProperty(key, newValue);
85             if ( ( oldVal == null && newValue != null ) || !oldVal.equals(newValue)) {
86                 if (key instanceof ProfilePropertyKey) {
87                     p = ProfilesFactory.getInstance().getPropertyProfile(((ProfilePropertyKey) key).getProfile());
88                 }
89                 CoreServlet.getServlet().fireCoreEvent(new PropertyChangeEvent(def,
90                                 CoreEventConstants.PROPERTY_CHANGED,
91                                 def,
92                                 sessionInfo,
93                                 p,
94                                 oldVal,
95                                 newValue,
96                                 PropertyChangeEvent.STATE_SUCCESSFUL));
97             }
98             return oldVal;
99         } catch (Exception JavaDoc e) {
100             log.error("Failed to set property.", e);
101             CoreServlet.getServlet().fireCoreEvent(new PropertyChangeEvent(def,
102                             CoreEventConstants.PROPERTY_CHANGED,
103                             def,
104                             sessionInfo,
105                             p,
106                             null,
107                             newValue,
108                             PropertyChangeEvent.STATE_UNSUCCESSFUL));
109         }
110         return null;
111     }
112
113     /**
114      * Set the value for property and fire the appropriate events. An event will
115      * only be fired if the value of the property changes. <code>null</code>
116      * will be returned if the property cannot be set for some reason.
117      *
118      * @param key property key
119      * @param newValue new value
120      * @param sessionInfo session info
121      * @return old value
122      */

123     public static boolean setProperty(AbstractPropertyKey key, boolean newValue, SessionInfo sessionInfo) {
124         String JavaDoc oldVal = setProperty(key, String.valueOf(newValue), sessionInfo);
125         assert oldVal != null; // booleans must have default
126
return Boolean.parseBoolean(oldVal);
127     }
128
129     /**
130      *
131      * Set the value for property and fire the appropriate events. An event will
132      * only be fired if the value of the property changes. <code>null</code>
133      * will be returned if the property cannot be set for some reason.
134      *
135      * @param key key
136      * @param newValue new value
137      * @param sessionInfo session info
138      * @return old value
139      */

140     public static PropertyList setProperty(AbstractPropertyKey key, PropertyList newValue, SessionInfo sessionInfo) {
141         String JavaDoc oldVal = setProperty(key, newValue.getAsPropertyText(), sessionInfo);
142         return oldVal == null ? null : new PropertyList(oldVal);
143
144     }
145
146     /**
147      *
148      * Set the value for property and fire the appropriate events. An event will
149      * only be fired if the value of the property changes. <code>-1</code>
150      * will be returned if the property cannot be set for some reason.
151      *
152      * @param key key
153      * @param newValue new value
154      * @param sessionInfo session info
155      * @return old value
156      */

157     public static int setProperty(AbstractPropertyKey key, int newValue, SessionInfo sessionInfo) {
158         String JavaDoc oldVal = setProperty(key, String.valueOf(newValue), sessionInfo);
159         return oldVal == null ? -1 : Integer.parseInt(oldVal);
160     }
161
162     /**
163      *
164      * Set the value for property and fire the appropriate events. An event will
165      * only be fired if the value of the property changes. <code>-1</code>
166      * will be returned if the property cannot be set for some reason.
167      *
168      * @param key key
169      * @param newValue new value
170      * @param sessionInfo session info
171      * @return old value
172      */

173     public static long setProperty(AbstractPropertyKey key, long newValue, SessionInfo sessionInfo) {
174         String JavaDoc oldVal = setProperty(key, String.valueOf(newValue), sessionInfo);
175         return oldVal == null ? -1 : Long.parseLong(oldVal);
176     }
177
178     /**
179      * Get a property as an string.
180      * <p>
181      * If the property has never been set then the default value provided in the
182      * {@link PropertyDefinition} will be returned.
183      *
184      * @param key property key
185      * @return value
186      * @throws IllegalArgumentException if property doesn't exist
187      */

188     public static String JavaDoc getProperty(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
189         PropertyDefinition def = getDefinition(key);
190         if (def == null) {
191             throw new IllegalArgumentException JavaDoc("Invalid key. " + key);
192         }
193         PropertyClass t = def.getPropertyClass();
194         return t.retrieveProperty(key);
195     }
196
197     /**
198      * Get a property as an integer. The value will be retrieved as a string
199      * then converted to a primitive int.
200      * <p>
201      * If the property has never been set then the default value provided in the
202      * {@link PropertyDefinition} will be returned.
203      *
204      * @param key property key
205      * @return value
206      * @throws IllegalArgumentException if property doesn't exist
207      */

208     public static int getPropertyInt(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
209         return Integer.parseInt(getProperty(key));
210     }
211
212     /**
213      * Get a property as a long. The value will be retrieved as a string then
214      * converted to a primitive long.
215      * <p>
216      * If the property has never been set then the default value provided in the
217      * {@link PropertyDefinition} will be returned.
218      *
219      * @param key property key
220      * @return value
221      * @throws IllegalArgumentException if property doesn't exist
222      */

223     public static long getPropertyLong(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
224         return Long.parseLong(getProperty(key));
225     }
226
227     /**
228      * Get a property as a boolen. The value will be retrieved as a string then
229      * converted to a primitive boolean, <code>true</code> if the string value
230      * is <i>true</i> otherwise <code>false</code>.
231      * <p>
232      * If the property has never been set then the default value provided in the
233      * {@link PropertyDefinition} will be returned.
234      * <p>
235      *
236      * @param key property key
237      * @return value
238      * @throws IllegalArgumentException if property doesn't exist
239      */

240     public static boolean getPropertyBoolean(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
241         return Boolean.parseBoolean(getProperty(key));
242     }
243
244     /**
245      * Get a property as a list. The value will be retrieved as a string then
246      * converted to a {@link PropertyList}.
247      * <p>
248      * If the property has never been set then the default value provided in the
249      * {@link PropertyDefinition} will be returned.
250      *
251      * @param key property key
252      * @return value
253      * @throws IllegalArgumentException if property doesn't exist
254      */

255     public static PropertyList getPropertyList(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
256         return new PropertyList(getProperty(key));
257     }
258
259 }
260
Popular Tags