View Javadoc

1   /*
2    * $Id: Config.java,v 1.2 2011/01/02 15:04:11 oboehm Exp $
3    *
4    * Copyright (c) 2008 by Oliver Boehm
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   * (c)reated 22.10.2008 by oliver (ob@oasd.de)
19   */
20  package patterntesting.sample;
21  
22  import java.io.*;
23  import java.util.Properties;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.impl.LogFactoryImpl;
27  
28  import patterntesting.annotation.check.runtime.*;
29  import patterntesting.runtime.NullConstants;
30  
31  /**
32   * This class is an example how NullPointerExceptions may happen.
33   *
34   * @author <a href="boehm@javatux.de">oliver</a>
35   * @since 22.10.2008
36   * @version $Revision: 1.2 $
37   */
38  public class Config {
39  
40      private static final Log log = LogFactoryImpl.getLog(Config.class);
41      private static String propertyResource = "default.properties";
42      private static Properties properties = null;
43      
44      /** Utility class - no need to instantiate it. */
45      private Config() {}
46  
47      /**
48       * Sets the resource.
49       *
50       * @param name the new resource
51       */
52      public static void setResource(final String name) {
53          propertyResource = name;
54          properties = null;
55      }
56  
57      /**
58       * Gets the resource.
59       *
60       * @return the resource
61       */
62      public static String getResource() {
63          return propertyResource;
64      }
65  
66      /**
67       * Gets the version.
68       *
69       * @return the version
70       */
71      public static String getVersion() {
72          return getProperty("patterntesting.sample.version");
73      }
74  
75      /**
76       * This is an example how to use NULL_OBJECT as argument. If you don't want to
77       * set a prefix or suffix use NullConstants.NULL_OBJECT as argument. <br/>
78       * BTW: The code would also work if you would use NullConstants.NULL_STRING as
79       * arguemnt (because it is an empty string).
80       *
81       * @param prefix e.g. "v" or NullConstants.NULL_OBJECT if you don't want to set a
82       * prefix
83       * @param version e.g. "0.9.7"
84       * @param suffix e.g. "-SNAPSHOT" or NullConstants.NULL_OBJECT if you don't want to set
85       * a suffix
86       *
87       * @since 13-Jun-2009
88       * @author oliver
89       */
90      public static void setVersion(final Object prefix, String version,
91              final Object suffix) {
92          if (prefix != NullConstants.NULL_OBJECT) {
93              version = prefix.toString() + version;
94          }
95          if (suffix != NullConstants.NULL_OBJECT) {
96              version = version + suffix.toString();
97          }
98          setProperty("patterntesting.sample.version", version);
99      }
100 
101     /**
102      * This is an example for a method which may return null.
103      *
104      * @param name of the key
105      *
106      * @return value of the property
107      */
108     @MayReturnNull
109     public static String getProperty(final String name) {
110         Properties props = getProperties();
111         return props.getProperty(name);
112     }
113 
114     /**
115      * This is a (bad) example how you can use @NullArgsAllowed and combine it
116      * with @NotNull.
117      * <br/>
118      * Don't use such kind of code in your project. A better approach would be
119      * to divide this methods into:
120      * <ul>
121      * <li>setProperty(String, String)</li>
122      * <li>resetProperty(String)
123      * </ul>
124      *
125      * @param key the key
126      * @param value the value
127      */
128     @NullArgsAllowed
129     public static void setProperty(@NotNull final String key, final String value) {
130         Properties properties = getProperties();
131         if (value == null) {
132             properties.remove(key);
133         } else {
134             properties.setProperty(key, value);
135         }
136     }
137 
138     /**
139      * Gets the properties.
140      *
141      * @return the properties
142      */
143     public static synchronized Properties getProperties() {
144         if (properties != null) {
145             return properties;
146         }
147         properties = new Properties();
148         try {
149             properties.load(getPropertyStream());
150             log.info(propertyResource + " loaded");
151             return properties;
152         } catch (IOException ioe) {
153             log.warn("can't load properties", ioe);
154             return null;
155         }
156     }
157 
158     /**
159      * Gets the property stream.
160      *
161      * @return the property stream
162      */
163     public static InputStream getPropertyStream() {
164         return Config.class.getResourceAsStream(propertyResource);
165     }
166 
167     /**
168      * Gets the OS.
169      *
170      * @return the OS
171      */
172     public static String getOS() {
173         return System.getProperty("os.name");
174     }
175 
176     /**
177      * Gets the JDK home.
178      *
179      * @return the JDK home
180      */
181     public static String getJdkHome() {
182         return System.getenv("JDK_HOME");
183     }
184 
185     /**
186      * Gets the JDK home dir.
187      *
188      * @return the JDK home dir
189      */
190     public static File getJdkHomeDir() {
191         String jdkHome = getJdkHome();
192         assert jdkHome != null;
193         return new File(jdkHome);
194     }
195 
196 }