KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > FrameworkProperties


1 /*******************************************************************************
2  * Copyright (c) 2006 Cognos Incorporated, IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  *******************************************************************************/

9 package org.eclipse.osgi.framework.internal.core;
10
11 import java.util.Properties JavaDoc;
12 import java.util.PropertyPermission JavaDoc;
13
14 /*
15  * This class should be used in ALL places in the framework implementation to get "system" properties.
16  * The static methods on this class should be used instead of the System#getProperty, System#setProperty etc methods.
17  */

18 public class FrameworkProperties {
19     
20     private static Properties JavaDoc properties;
21
22     // A flag of some sort will have to be supported.
23
// Many existing plugins get framework propeties directly from System instead of BundleContext.
24
// Note that the OSGi TCK is one example where this property MUST be set to false because many TCK bundles set and read system properties.
25
private static final String JavaDoc USING_SYSTEM_PROPERTIES_KEY = "osgi.framework.useSystemProperties"; //$NON-NLS-1$
26

27     static {
28         Properties JavaDoc systemProperties = System.getProperties();
29         String JavaDoc usingSystemProperties = systemProperties.getProperty(USING_SYSTEM_PROPERTIES_KEY);
30         if (usingSystemProperties == null || usingSystemProperties.equalsIgnoreCase(Boolean.TRUE.toString())) {
31             properties = systemProperties;
32         } else {
33             // use systemProperties for a snapshot
34
// also see requirements in Bundlecontext.getProperty(...))
35
properties = new Properties JavaDoc();
36             // snapshot of System properties for uses of getProperties who expect to see framework properties set as System properties
37
// we need to do this for all system properties because the properties object is used to back
38
// BundleContext#getProperty method which expects all system properties to be available
39
properties.putAll(systemProperties);
40         }
41     }
42         
43     public static Properties JavaDoc getProperties() {
44         SecurityManager JavaDoc sm = System.getSecurityManager();
45         if (sm != null)
46             sm.checkPropertiesAccess();
47         return properties;
48     }
49     
50     public static String JavaDoc getProperty(String JavaDoc key) {
51         return getProperty(key, null);
52     }
53
54     public static String JavaDoc getProperty(String JavaDoc key, String JavaDoc defaultValue) {
55         SecurityManager JavaDoc sm = System.getSecurityManager();
56         if (sm != null)
57             sm.checkPropertyAccess(key);
58         return properties.getProperty(key, defaultValue);
59     }
60
61     public static String JavaDoc setProperty(String JavaDoc key, String JavaDoc value) {
62         SecurityManager JavaDoc sm = System.getSecurityManager();
63         if (sm != null)
64             sm.checkPermission(new PropertyPermission JavaDoc(key, "write")); //$NON-NLS-1$
65
return (String JavaDoc) properties.put(key, value);
66     }
67
68     public static String JavaDoc clearProperty(String JavaDoc key) {
69         SecurityManager JavaDoc sm = System.getSecurityManager();
70         if (sm != null)
71             sm.checkPermission(new PropertyPermission JavaDoc(key, "write")); //$NON-NLS-1$
72
return (String JavaDoc) properties.remove(key);
73     }
74 }
75
Popular Tags