Package: ConfigBase

ConfigBase

nameinstructionbranchcomplexitylinemethod
ConfigBase()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getProperty(String, String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
init(String, Class)
M: 130 C: 20
13%
M: 12 C: 2
14%
M: 7 C: 1
13%
M: 26 C: 6
19%
M: 0 C: 1
100%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (c) 2009 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ is free software: you can redistribute it and/or modify
10: *        it under the terms of the GNU General Public License as published by
11: *        the Free Software Foundation, either version 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ is distributed in the hope that it will be useful,
15: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
16: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: package org.overture.util;
25:
26: import java.io.IOException;
27: import java.io.InputStream;
28: import java.lang.reflect.Field;
29: import java.util.Properties;
30:
31: public class ConfigBase
32: {
33:         private static Properties props = new Properties();
34:
35:         public static void init(String resource, Class<?> target) throws Exception
36:         {
37:                 InputStream fis = null;
38:                 String propertyFile = resource;
39:
40:                 try
41:                 {
42:                         try
43:                         {
44:                                 fis = ConfigBase.class.getResourceAsStream("/" + resource);
45:
46:•                                if (fis == null)
47:                                 {
48:                                         // properties file is not on the classpath
49:                                         return;
50:                                 }
51:
52:                                 props.load(fis);
53:                         } catch (Exception ex)
54:                         {
55:                                 throw new Exception(propertyFile + ": " + ex.getMessage());
56:                         }
57:
58:                         String name = "?";
59:                         String value = "?";
60:
61:                         try
62:                         {
63:•                                for (Field f : target.getFields())
64:                                 {
65:                                         name = f.getName();
66:                                         Class<?> type = f.getType();
67:                                         value = props.getProperty(name.replace('_', '.'));
68:
69:•                                        if (value != null)
70:                                         {
71:•                                                if (type == Integer.TYPE)
72:                                                 {
73:                                                         f.setInt(target, Integer.parseInt(value));
74:•                                                } else if (type == Boolean.TYPE)
75:                                                 {
76:                                                         f.setBoolean(target, Boolean.parseBoolean(value));
77:•                                                } else if (type == String.class)
78:                                                 {
79:                                                         f.set(target, value);
80:                                                 } else
81:                                                 {
82:                                                         throw new Exception("Cannot process " + name
83:                                                                         + ", Java type " + type + " unsupported");
84:                                                 }
85:                                         }
86:                                 }
87:                         } catch (Exception ex)
88:                         {
89:                                 throw new Exception(propertyFile + ": (" + name + " = " + value
90:                                                 + ") " + ex.getMessage());
91:                         }
92:                 } finally
93:                 {
94:•                        if (fis != null)
95:                         {
96:                                 try
97:                                 {
98:                                         fis.close();
99:                                 } catch (IOException e)
100:                                 {
101:                                         // so?
102:                                 }
103:                         }
104:                 }
105:         }
106:
107:         public static String getProperty(String key, String def)
108:         {
109:                 return props.getProperty(key, def);
110:         }
111: }