KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > ejb > cmp3 > base > PropertiesHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.ejb.cmp3.base;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import java.util.logging.Level JavaDoc;
29 import java.security.AccessController JavaDoc;
30 import java.security.PrivilegedAction JavaDoc;
31 import oracle.toplink.essentials.config.*;
32 import oracle.toplink.essentials.internal.localization.ExceptionLocalization;
33 import oracle.toplink.essentials.internal.sessions.AbstractSession;
34 import oracle.toplink.essentials.logging.SessionLog;
35
36 /**
37  *
38  * The class processes some of TopLink properties.
39  * The class may be used for any properties, but it only makes sense
40  * to use it in the following two cases:
41  * 1. There is a set of legal values defined
42  * either requiring translation (like CacheTypeProp);
43  * or not (like LoggingLevelProp).
44  * 2. Property is really a prefix from which the property obtained by
45  * appending entity or class name (like DescriptorCustomizerProp -
46  * it corresponds to "toplink.descriptor.customizer." prefix that allows to
47  * define propereties like "toplink.descriptor.customizer.myPackage.MyClass").
48  *
49  * TopLink properties and their values defined in oracle.toplink.essentials.config package.
50  *
51  * To add a new property:
52  * Define a new property in TopLinkProperties;
53  * Add a class containing property's values if required to config package (like CacheType);
54  * Alternatively values may be already defined elsewhere (like in LoggingLevelProp).
55  * Add an inner class to this class extending Prop corresponding to the new property (like CacheTypeProp);
56  * The first constructor parameter is property name; the second is default value;
57  * In constructor
58  * provide 2-dimensional value array in case the values should be translated (like CacheTypeProp);
59  * in case translation is not required provide a single-dimension array (like LoggingLevelProp).
60  * In inner class Prop static initializer addProp an instance of the new prop class (like addProp(new CacheTypeProp())).
61  *
62  * @see TopLinkProperties
63  * @see CacheType
64  * @see TargetDatabase
65  * @see TargetServer
66  *
67  */

68 public class PropertiesHandler {
69     
70     /**
71      * INTERNAL:
72      * Gets property value from the map, if none found looks in System properties.
73      * Use this to get a value for a non-prefixed property.
74      * Could be used on prefixes (like "oracle.toplink.cache-type.") too,
75      * but will always return null
76      * Throws IllegalArgumentException in case the property value is illegal.
77      */

78     public static String JavaDoc getPropertyValue(String JavaDoc name, Map JavaDoc m) {
79         return Prop.getPropertyValueToApply(name, m, null);
80     }
81     
82     public static String JavaDoc getPropertyValueLogDebug(String JavaDoc name, Map JavaDoc m, AbstractSession session) {
83         return Prop.getPropertyValueToApply(name, m, session);
84     }
85     
86     /**
87      * INTERNAL:
88      * Gets property value from the map, if none found looks in System properties.
89      * Use this to get a value for a prefixed property:
90      * for "oracle.toplink.cache-type.Employee"
91      * pass "oracle.toplink.cache-type.", "Employee".
92      * Throws IllegalArgumentException in case the property value is illegal.
93      */

94     public static String JavaDoc getPrefixedPropertyValue(String JavaDoc prefix, String JavaDoc suffix, Map JavaDoc m) {
95         return (String JavaDoc)getPrefixValues(prefix, m).get(suffix);
96     }
97
98     /**
99      * INTERNAL:
100      * Gets properties' values from the map, if none found looks in System properties.
101      * In the returned map values keyed by suffixes.
102      * Use it with prefixes (like "oracle.toplink.cache-type.").
103      * Could be used on simple properties (not prefixes, too),
104      * but will always return either an empty map or a map containing a single
105      * value keyed by an empty String.
106      * Throws IllegalArgumentException in case the property value is illegal.
107      */

108     public static Map JavaDoc getPrefixValues(String JavaDoc prefix, Map JavaDoc m) {
109         return Prop.getPrefixValuesToApply(prefix, m, null);
110     }
111     
112     public static Map JavaDoc getPrefixValuesLogDebug(String JavaDoc prefix, Map JavaDoc m, AbstractSession session) {
113         return Prop.getPrefixValuesToApply(prefix, m, session);
114     }
115     
116     /**
117      * INTERNAL:
118      * Returns the default property value that should be applied.
119      * Throws IllegalArgumentException in case the name doesn't correspond
120      * to any property.
121      */

122     public static String JavaDoc getDefaultPropertyValue(String JavaDoc name) {
123         return Prop.getDefaultPropertyValueToApply(name, null);
124     }
125     
126     public static String JavaDoc getDefaultPropertyValueLogDebug(String JavaDoc name, AbstractSession session) {
127         return Prop.getDefaultPropertyValueToApply(name, session);
128     }
129     
130     /**
131      * INTERNAL:
132      * Empty String value indicates that the default property value
133      * should be used.
134      */

135     protected static boolean shouldUseDefault(String JavaDoc value) {
136         return value != null && value.length() == 0;
137     }
138     
139     protected static abstract class Prop {
140         static HashMap JavaDoc mainMap = new HashMap JavaDoc();
141         Object JavaDoc[] valueArray;
142         HashMap JavaDoc valueMap;
143         String JavaDoc name;
144         String JavaDoc defaultValue;
145         String JavaDoc defaultValueToApply;
146         boolean valueToApplyMayBeNull;
147         boolean shouldReturnOriginalValueIfValueToApplyNotFound;
148         
149         static {
150             addProp(new LoggingLevelProp());
151             addProp(new TargetDatabaseProp());
152             addProp(new TargetServerProp());
153             addProp(new CacheSizeProp());
154             addProp(new CacheTypeProp());
155             addProp(new CacheSharedProp());
156             addProp(new DescriptorCustomizerProp());
157         }
158         
159         Prop(String JavaDoc name) {
160             this.name = name;
161         }
162         
163         Prop(String JavaDoc name, String JavaDoc defaultValue) {
164             this(name);
165             this.defaultValue = defaultValue;
166         }
167
168         static String JavaDoc getPropertyValueFromMap(String JavaDoc name, Map JavaDoc m) {
169             String JavaDoc value = (String JavaDoc)m.get(name);
170             return value == null ? System.getProperty(name) : value;
171         }
172     
173         // Collect all entries corresponding to the prefix name.
174
// Note that entries from Map m override those from System properties.
175
static Map JavaDoc getPrefixValuesFromMap(String JavaDoc name, Map JavaDoc m) {
176             Map JavaDoc mapOut = new HashMap JavaDoc();
177             
178             Iterator JavaDoc it = (Iterator JavaDoc)AccessController.doPrivileged(
179                 new PrivilegedAction JavaDoc() {
180                     public Object JavaDoc run() {
181                         return System.getProperties().entrySet().iterator();
182                     }
183                 }
184             );
185
186             while(it.hasNext()) {
187                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
188                 String JavaDoc str = (String JavaDoc)entry.getKey();
189                 if(str.startsWith(name)) {
190                     String JavaDoc entityName = str.substring(name.length(), str.length());
191                     mapOut.put(entityName, entry.getValue());
192                 }
193             }
194             
195             it = m.entrySet().iterator();
196             while(it.hasNext()) {
197                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
198                 String JavaDoc str = (String JavaDoc)entry.getKey();
199                 if(str.startsWith(name)) {
200                     String JavaDoc entityName = str.substring(name.length(), str.length());
201                     mapOut.put(entityName, entry.getValue());
202                 }
203             }
204             
205             return mapOut;
206         }
207     
208         static String JavaDoc getPropertyValue(String JavaDoc name, boolean shouldUseDefault, Map JavaDoc m, AbstractSession session) {
209             Prop prop = (Prop)mainMap.get(name);
210             if(prop == null) {
211                 // it's not our property
212
return null;
213             }
214             String JavaDoc value = (String JavaDoc)getPropertyValueFromMap(name, m);
215             if(value == null) {
216                 return null;
217             }
218             return prop.getValueToApply(value, shouldUseDefault, session);
219         }
220                 
221         static String JavaDoc getPropertyValueToApply(String JavaDoc name, Map JavaDoc m, AbstractSession session) {
222             Prop prop = (Prop)mainMap.get(name);
223             if(prop == null) {
224                 return null;
225             }
226             String JavaDoc value = (String JavaDoc)getPropertyValueFromMap(name, m);
227             if(value == null) {
228                 return null;
229             }
230             return prop.getValueToApply(value, shouldUseDefault(value), session);
231         }
232                 
233         static Map JavaDoc getPrefixValuesToApply(String JavaDoc prefix, Map JavaDoc m, AbstractSession session) {
234             Prop prop = (Prop)mainMap.get(prefix);
235             if(prop == null) {
236                 // prefix doesn't correspond to a Prop object - it's not our property.
237
return new HashMap JavaDoc(0);
238             }
239             
240             // mapps suffixes to property values
241
Map JavaDoc mapIn = (Map JavaDoc)getPrefixValuesFromMap(prefix, m);
242             if(mapIn.isEmpty()) {
243                 return mapIn;
244             }
245             
246             HashMap JavaDoc mapOut = new HashMap JavaDoc(mapIn.size());
247             Iterator JavaDoc it = mapIn.entrySet().iterator();
248             while(it.hasNext()) {
249                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
250                 String JavaDoc suffix = (String JavaDoc)entry.getKey();
251                 String JavaDoc value = (String JavaDoc)entry.getValue();
252                 mapOut.put(suffix, prop.getValueToApply(value, shouldUseDefault(value), suffix, session));
253             }
254             // mapps suffixes to valuesToApply
255
return mapOut;
256         }
257         
258         static String JavaDoc getDefaultPropertyValueToApply(String JavaDoc name, AbstractSession session) {
259             Prop prop = (Prop)mainMap.get(name);
260             if(prop == null) {
261                 throw new IllegalArgumentException JavaDoc(ExceptionLocalization.buildMessage("ejb30-default-for-unknown-property", new Object JavaDoc[]{name}));
262             }
263             prop.logDefault(session);
264             return prop.defaultValueToApply;
265         }
266                 
267         String JavaDoc getValueToApply(String JavaDoc value, boolean shouldUseDefault, AbstractSession session) {
268             return getValueToApply(value, shouldUseDefault, null, session);
269         }
270         
271         // suffix is used only for properties-prefixes (like CacheType)
272
String JavaDoc getValueToApply(String JavaDoc value, boolean shouldUseDefault, String JavaDoc suffix, AbstractSession session) {
273             if(shouldUseDefault) {
274                 logDefault(session, suffix);
275                 return defaultValueToApply;
276             }
277             String JavaDoc valueToApply = value;
278             if(valueMap != null) {
279                 String JavaDoc key = getUpperCaseString(value);
280                 valueToApply = (String JavaDoc)valueMap.get(key);
281                 if(valueToApply == null) {
282                     boolean notFound = true;
283                     if(valueToApplyMayBeNull) {
284                         notFound = !valueMap.containsKey(key);
285                     }
286                     if(notFound) {
287                         if(shouldReturnOriginalValueIfValueToApplyNotFound) {
288                             valueToApply = value;
289                         } else {
290                             String JavaDoc propertyName = name;
291                             if(suffix != null) {
292                                 propertyName = propertyName + suffix;
293                             }
294                             throw new IllegalArgumentException JavaDoc(ExceptionLocalization.buildMessage("ejb30-illegal-property-value", new Object JavaDoc[]{propertyName, getPrintValue(value)}));
295                         }
296                     }
297                 }
298             }
299             log(session, value, valueToApply, suffix);
300             return valueToApply;
301         }
302
303         static String JavaDoc getUpperCaseString(String JavaDoc value) {
304             if(value != null) {
305                 return value.toUpperCase();
306             } else {
307                 return null;
308             }
309         }
310         static String JavaDoc getPrintValue(String JavaDoc value) {
311             if(value != null) {
312                 return value;
313             } else {
314                 return "null";
315             }
316         }
317         void initialize() {
318             if(valueArray != null) {
319                 valueMap = new HashMap JavaDoc(valueArray.length);
320                 if(valueArray instanceof Object JavaDoc[][]) {
321                     Object JavaDoc[][] valueArray2 = (Object JavaDoc[][])valueArray;
322                     for(int i=0; i<valueArray2.length; i++) {
323                         valueMap.put(getUpperCaseString((String JavaDoc)valueArray2[i][0]), valueArray2[i][1]);
324                         if(valueArray2[i][1] == null) {
325                             valueToApplyMayBeNull = true;
326                         }
327                     }
328                 } else {
329                     for(int i=0; i<valueArray.length; i++) {
330                         valueMap.put(getUpperCaseString((String JavaDoc)valueArray[i]), valueArray[i]);
331                         if(valueArray[i] == null) {
332                             valueToApplyMayBeNull = true;
333                         }
334                     }
335                 }
336                 defaultValueToApply = (String JavaDoc)valueMap.get(getUpperCaseString(defaultValue));
337             } else {
338                 defaultValueToApply = defaultValue;
339             }
340         }
341
342         void logDefault(AbstractSession session) {
343             logDefault(session, null);
344         }
345         
346         void logDefault(AbstractSession session, String JavaDoc suffix) {
347             if(session != null) {
348                 String JavaDoc propertyName = name;
349                 if(suffix != null) {
350                     propertyName = propertyName + suffix;
351                 }
352                 if(defaultValue != defaultValueToApply) {
353                     session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "handler_property_value_default", new Object JavaDoc[]{propertyName, defaultValue, defaultValueToApply});
354                 } else {
355                     session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "property_value_default", new Object JavaDoc[]{propertyName, defaultValue});
356                 }
357             }
358         }
359         
360         void log(AbstractSession session, String JavaDoc value, String JavaDoc valueToApply, String JavaDoc suffix) {
361             if(session != null) {
362                 String JavaDoc propertyName = name;
363                 if(suffix != null) {
364                     propertyName = propertyName + suffix;
365                 }
366                 if(value != valueToApply) {
367                     session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "handler_property_value_specified", new Object JavaDoc[]{propertyName, value, valueToApply});
368                 } else {
369                     session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "property_value_specified", new Object JavaDoc[]{propertyName, value});
370                 }
371             }
372         }
373         
374         static void addProp(Prop prop) {
375             prop.initialize();
376             mainMap.put(prop.name, prop);
377         }
378     }
379
380     protected static class LoggingLevelProp extends Prop {
381         LoggingLevelProp() {
382             super(TopLinkProperties.LOGGING_LEVEL, Level.CONFIG.getName());
383             valueArray = new Object JavaDoc[] {
384                 Level.OFF.getName(),
385                 Level.SEVERE.getName(),
386                 Level.OFF.getName(),
387                 Level.WARNING.getName(),
388                 Level.INFO.getName(),
389                 Level.CONFIG.getName(),
390                 Level.FINE.getName(),
391                 Level.FINER.getName(),
392                 Level.FINEST.getName(),
393                 Level.ALL.getName()
394             };
395         }
396     }
397
398     protected static class TargetDatabaseProp extends Prop {
399         TargetDatabaseProp() {
400             super(TopLinkProperties.TARGET_DATABASE, TargetDatabase.DEFAULT);
401             this.shouldReturnOriginalValueIfValueToApplyNotFound = true;
402             String JavaDoc pcg = "oracle.toplink.essentials.platform.database.";
403             valueArray = new Object JavaDoc[][] {
404                 {TargetDatabase.Auto, pcg + "DatabasePlatform"},
405                 {TargetDatabase.Oracle, pcg + "oracle.OraclePlatform"},
406                 {TargetDatabase.Attunity, pcg + "AttunityPlatform"},
407                 {TargetDatabase.Cloudscape, pcg + "CloudscapePlatform"},
408                 {TargetDatabase.Database, pcg + "DatabasePlatform"},
409                 {TargetDatabase.DB2Mainframe, pcg + "DB2MainframePlatform"},
410                 {TargetDatabase.DB2, pcg + "DB2Platform"},
411                 {TargetDatabase.DBase, pcg + "DBasePlatform"},
412                 {TargetDatabase.Derby, pcg + "DerbyPlatform"},
413                 {TargetDatabase.HSQL, pcg + "HSQLPlatform"},
414                 {TargetDatabase.Informix, pcg + "InformixPlatform"},
415                 {TargetDatabase.JavaDB, pcg + "JavaDBPlatform"},
416                 {TargetDatabase.MySQL4, pcg + "MySQL4Platform"},
417                 {TargetDatabase.PointBase, pcg + "PointBasePlatform"},
418                 {TargetDatabase.PostgreSQL, pcg + "PostgreSQLPlatform"},
419                 {TargetDatabase.SQLAnyWhere, pcg + "SQLAnyWherePlatform"},
420                 {TargetDatabase.SQLServer, pcg + "SQLServerPlatform"},
421                 {TargetDatabase.Sybase, pcg + "SybasePlatform"},
422                 {TargetDatabase.TimesTen, pcg + "TimesTenPlatform"}
423             };
424         }
425     }
426
427     protected static class TargetServerProp extends Prop {
428         TargetServerProp() {
429             super(TopLinkProperties.TARGET_SERVER, TargetServer.DEFAULT);
430             this.shouldReturnOriginalValueIfValueToApplyNotFound = true;
431             String JavaDoc pcg = "oracle.toplink.essentials.platform.server.";
432             valueArray = new Object JavaDoc[][] {
433                 {TargetServer.None, pcg + "NoServerPlatform"},
434                 {TargetServer.OC4J_10_1_3, pcg + "oc4j.Oc4jPlatform"},
435                 {TargetServer.SunAS9, pcg + "sunas.SunAS9ServerPlatform"}
436             };
437         }
438     }
439
440     protected static class CacheSizeProp extends Prop {
441         CacheSizeProp() {
442             super(TopLinkProperties.CACHE_SIZE_, Integer.toString(1000));
443         }
444     }
445
446     protected static class CacheTypeProp extends Prop {
447         CacheTypeProp() {
448             super(TopLinkProperties.CACHE_TYPE_, CacheType.DEFAULT);
449             String JavaDoc pcg = "oracle.toplink.essentials.internal.identitymaps.";
450             valueArray = new Object JavaDoc[][] {
451                 {CacheType.Weak, pcg + "WeakIdentityMap"},
452                 {CacheType.SoftWeak, pcg + "SoftCacheWeakIdentityMap"},
453                 {CacheType.HardWeak, pcg + "HardCacheWeakIdentityMap"},
454                 {CacheType.Full, pcg + "FullIdentityMap"},
455                 {CacheType.NONE, pcg + "NoIdentityMap"}
456             };
457         }
458     }
459
460     protected static class CacheSharedProp extends Prop {
461         CacheSharedProp() {
462             super(TopLinkProperties.CACHE_SHARED_, "false");
463             valueArray = new Object JavaDoc[] {
464                 "true",
465                 "false"
466             };
467         }
468     }
469
470     protected static class DescriptorCustomizerProp extends Prop {
471         DescriptorCustomizerProp() {
472             super(TopLinkProperties.DESCRIPTOR_CUSTOMIZER_);
473         }
474     }
475
476 }
477
Popular Tags