KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > RegistryProperties


1 /*******************************************************************************
2  * Copyright (c) 2006 Eclipse Foundation 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.core.internal.registry;
10
11 import java.util.Properties JavaDoc;
12 import org.eclipse.core.internal.runtime.RuntimeLog;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15
16 /**
17  * Simple Property mechanism to chain property lookup from local registry properties,
18  * to BundleContext properties (if available) or System properties otherwise.
19  */

20 public class RegistryProperties {
21     
22     public static final String JavaDoc empty = ""; //$NON-NLS-1$
23

24     private static Properties JavaDoc registryProperties = new Properties JavaDoc();
25     private static Object JavaDoc context = null; // BundleContext, but specified as Object to avoid class loading
26

27     public static void setContext(Object JavaDoc object) {
28         context = object;
29     }
30
31     public static String JavaDoc getProperty(String JavaDoc propertyName) {
32         String JavaDoc propertyValue = registryProperties.getProperty(propertyName);
33         if (propertyValue != null)
34             return propertyValue;
35
36         return getContextProperty(propertyName);
37     }
38
39     public static String JavaDoc getProperty(String JavaDoc property, String JavaDoc defaultValue) {
40         String JavaDoc result = RegistryProperties.getProperty(property);
41         return result == null ? defaultValue : result;
42     }
43
44     public static void setProperty(String JavaDoc propertyName, String JavaDoc propertyValue) {
45         registryProperties.setProperty(propertyName, propertyValue);
46     }
47
48     // The registry could be used as a stand-alone utility without OSGi.
49
// Try to obtain the property from the OSGi context, but only use bundleContext if
50
// it was already set by Activator indicating that OSGi layer is present.
51
private static String JavaDoc getContextProperty(final String JavaDoc propertyName) {
52         if (context == null)
53             return System.getProperty(propertyName);
54
55         final String JavaDoc[] result = new String JavaDoc[1];
56         try {
57             // Wrap BundleContext into an inner class to make sure it will only get loaded
58
// if OSGi layer is present.
59
Runnable JavaDoc innerClass = new Runnable JavaDoc() {
60                 public void run() {
61                     org.osgi.framework.BundleContext bundleContext = (org.osgi.framework.BundleContext) context;
62                     result[0] = bundleContext.getProperty(propertyName);
63                 }
64             };
65             innerClass.run();
66         } catch (Exception JavaDoc e) {
67             // If we are here, it is likely means that context was set, but OSGi layer
68
// is not present or non-standard. This should not happen, but let's give
69
// the program a chance to continue - properties should have reasonable
70
// default values.
71
IStatus status = new Status(Status.ERROR, IRegistryConstants.RUNTIME_NAME, 0, e.getMessage(), e);
72             RuntimeLog.log(status);
73             return null;
74         }
75         return result[0];
76     }
77 }
78
Popular Tags