KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > bootstrap > DeploymentProperties


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.bootstrap;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28
29 /**
30  * Class that encapsulates Deployment Properties within the bootstrapper.
31  * This is the only class in all of Carbon that logs using System.out and uses
32  * a direct extension of RuntimeException for exception handling.
33  *
34  * Copyright 2002 Sapient
35  * @since carbon 1.0
36  * @author Douglas Voet, Jul 23, 2002
37  * @version $Revision: 1.11 $($Author: dvoet $ / $Date: 2003/05/05 21:21:11 $)
38  */

39 final class DeploymentProperties {
40     /** The handle to Apache-commons logger */
41     private Log log = LogFactory.getLog(this.getClass());
42
43     /**
44      * The default name of the carbon core deployement property file
45      */

46     private static final String JavaDoc DEPLOYMENT_CONFIG_FILE_NAME =
47         "CarbonDeploymentConfig.properties";
48
49     /** The internal store of properties */
50     private final Properties JavaDoc deploymentProperties = fetchDeploymentProperties();
51
52     /**
53      * This method provides access to properties that are specific to this
54      * deployment of Carbon.
55      * The system properties are searched for the requested property
56      * first. If it is not found, the value is returned from a properties
57      * file name by BootStrapper.DEPLOYMENT_CONFIG_FILE_NAME
58      * and located using the <code>ClassLoader.getResource</code>
59      * method. Deployment Properties
60      * should be properties that either change based on deployment or are
61      * required before the <code>ConfigurationService</code> is loaded. Cases
62      * of properties varying based on deployment should be minimal. In this
63      * case, the <code>DeploymentService</code> should be used.
64      * <p>
65      * This method has the same semantics as
66      * java.util.Properties.getProperty(String)
67      *
68      * @see java.lang.ClassLoader#getResource(String)
69      * @see java.util.Properties#getProperty(String)
70      * @see org.sape.carbon.services.deployment.DeploymentService
71      *
72      * @param key the name of the property
73      * @return String the value of the property or null if it does not exist
74      */

75     public String JavaDoc get(String JavaDoc key) {
76
77         String JavaDoc value = System.getProperty(key);
78
79         if (value == null || value.equals("")) {
80             synchronized (this) {
81                 value = this.deploymentProperties.getProperty(key);
82             }
83         }
84
85         return value;
86     }
87
88     /**
89      * This method sets Deployment properties. If the property exists within
90      * the System properties, it is replaced, otherwise, it is written to the
91      * deployment properties maintained by this class. It does not write the
92      * new value to the properties file. This method affects the smallest
93      * scope possible. It will not write to the system wide properties unless
94      * the property already exists because that could affect other applications
95      * running in the same JVM.
96      * <p>
97      * This method has the same semantics as
98      * java.util.Properties.setProperty(String, String)
99      *
100      * @see java.util.Properties#setProperty(String, String)
101      *
102      * @param key key to set a deployment property
103      * @param value value to set for the property
104      * @return Object the old value of the property
105      */

106     public Object JavaDoc set(String JavaDoc key, String JavaDoc value) {
107         if (System.getProperties().contains(key)) {
108             return System.setProperty(key, value);
109         } else {
110             synchronized (this) {
111                 if (value == null) {
112                     return this.deploymentProperties.remove(key);
113                 } else {
114                     return this.deploymentProperties.setProperty(key, value);
115                 }
116             }
117         }
118     }
119
120     /**
121      * Fetches the Properties object used for deploymentProperties
122      *
123      * @return Properties for deploymentProperties
124      */

125     private Properties JavaDoc fetchDeploymentProperties() {
126
127         log.trace("Attempting to load deployment properties");
128
129         // This will always load the file from the same Classpath, using the
130
// same ClassLoader as was used to load this class.
131
ClassLoader JavaDoc classLoader = BootStrapper.class.getClassLoader();
132         URL JavaDoc bootConfigURL =
133             classLoader.getResource(
134                 DeploymentProperties.DEPLOYMENT_CONFIG_FILE_NAME);
135
136         Properties JavaDoc bootProperties = new Properties JavaDoc();
137         if (bootConfigURL == null) {
138             log.info(
139                 "Deployment properties file ["
140                     + DeploymentProperties.DEPLOYMENT_CONFIG_FILE_NAME
141                     + "] not found by ClassLoader, "
142                     + "that's OK, none will be used");
143
144         } else {
145
146             InputStream JavaDoc bootConfigInputStream = null;
147             try {
148                 log.info(
149                     "Loading Deployment Properties resource from "
150                         + "ClassLoader: ["
151                         + bootConfigURL
152                         + "]");
153
154                 // Open the stream to the URL resource
155
bootConfigInputStream = bootConfigURL.openStream();
156                 bootProperties.load(bootConfigInputStream);
157
158             } catch (IOException JavaDoc ioe) {
159                 log.warn(
160                     "Deployment properties file could not be 'read'\n"
161                         + "Filename = ["
162                         + bootConfigURL.getPath()
163                         + "], none will be used", ioe);
164
165             } finally {
166                 // close configInputStream if it was openned
167
if (bootConfigInputStream != null) {
168                     try {
169                         bootConfigInputStream.close();
170                     } catch (IOException JavaDoc ioe) {
171                         // eat it, just wanted to make sure the stream is closed
172
}
173                 }
174             }
175         }
176         return bootProperties;
177     }
178 }
179
Popular Tags