1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package edu.sc.seis.TauP;
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.util.Properties;
38 import java.util.zip.ZipEntry;
39 import java.util.zip.ZipFile;
40
41 /*** convenience class for loading properties.
42 *
43 * @version 1.1.3 Wed Jul 18 15:00:35 GMT 2001
44
45
46
47 * @author H. Philip Crotwell
48 *
49 */
50 public class PropertyLoader {
51
52 protected static String jarFileName = "taup.jar";
53
54 protected static String defaultPropFileName = "defaultProps";
55
56 protected static String packageName =/package-summary.html">ong> static String packageName = "/edu/sc/seis/TauP";
57
58 protected static String userPropFileName = ".taup";
59
60 /*** loads the properties from a file. First the default properties
61 * are loaded from the distribution jar file, then the users
62 * properties are loaded, overwriting the default values.
63 * This uses ".taup" in the users home directory, followed by
64 * ".taup" in the current directory if it can be found.
65 * If neither can be found then the default Properties object
66 * is returned unmodified.
67 *
68 * A special case is made for the taup.model.path property. If it
69 * is defined in the system properties, then the system version
70 * is prepended to the users version. This allows for setting
71 * system wide search paths on UNIX via an environment variable,
72 * which is transformed into a property by the sh scripts, while
73 * still allowing individual users as well as non-UNIX systems
74 * to customize the search path. */
75 public static Properties load() throws IOException {
76
77 Properties defaultProps = new Properties();
78
79
80 try {
81 String classPath = System.getProperty("java.class.path");
82 String pathEntry = "";
83 int offset = 0;
84 int pathSepIndex;
85 File jarFile;
86
87 Class c=null;
88 try {
89 c = Class.forName("edu.sc.seis.TauP.PropertyLoader");
90 } catch (Exception ex) {
91
92 }
93 InputStream in = c.getResourceAsStream(packageName+"/"+
94 defaultPropFileName);
95 if (in != null) {
96 defaultProps.load(in);
97 } else {
98
99
100 while (offset < classPath.length()) {
101 pathSepIndex = classPath.indexOf(File.pathSeparatorChar, offset);
102 if (pathSepIndex != -1) {
103 pathEntry = classPath.substring(offset,pathSepIndex);
104 offset = pathSepIndex+1;
105 } else {
106 pathEntry = classPath.substring(offset);
107 offset = classPath.length();
108 }
109 if (pathEntry.endsWith(jarFileName)) {
110 jarFile = new File(pathEntry);
111 if (jarFile.exists() && jarFile.isFile() &&
112 jarFile.getName().equals(jarFileName) && jarFile.canRead()) {
113 ZipFile zippy = new ZipFile(jarFile);
114 ZipEntry zipEntry = zippy.getEntry(defaultPropFileName);
115 if (zipEntry != null) {
116 defaultProps.load( zippy.getInputStream( zipEntry));
117 zippy.close();
118
119
120 offset = classPath.length()+1;
121 }
122 }
123 }
124 }
125 }
126 } catch (FileNotFoundException e) {
127
128
129 }
130
131
132
133 Properties applicationProps = new Properties(defaultProps);
134
135
136 try {
137 applicationProps.load( new FileInputStream(
138 System.getProperty("user.home")+
139 System.getProperty("file.separator")+".taup"));
140 } catch (FileNotFoundException ee) {
141
142 }
143
144
145 try {
146
147 applicationProps.load( new FileInputStream(
148 System.getProperty("user.dir")+
149 System.getProperty("file.separator")+".taup"));
150 } catch (FileNotFoundException e) {
151
152 }
153
154
155 String taupPath = "taup.model.path";
156 Properties sysProps = System.getProperties();
157 if (sysProps.containsKey(taupPath)) {
158 if (applicationProps.containsKey(taupPath)) {
159 applicationProps.put(taupPath, sysProps.getProperty(taupPath) +
160 sysProps.getProperty("path.separator") +
161 applicationProps.getProperty(taupPath));
162 } else {
163 applicationProps.put(taupPath, sysProps.getProperty(taupPath));
164 }
165 }
166
167 return applicationProps;
168 }
169
170 /*** writes the current system properties out to the file given. */
171 public static void save(Properties props) throws IOException {
172 save(props, ".taup");
173 }
174
175 /*** writes the current system properties out to the file given. */
176 public static void save(Properties props, String filename)
177 throws IOException {
178 FileOutputStream propFile = new FileOutputStream(filename);
179 props.save(propFile, "---Properties for the TauP toolkit---");
180 propFile.close();
181 }
182
183 public static void main(String[] args) {
184 try {
185 Properties props = PropertyLoader.load();
186 props.put("Key", "Value and another value");
187 save(props, "testProperties");
188 } catch (IOException e) {
189 System.out.println("Caught IOException: "+e.getMessage());
190 }
191 }
192 }