View Javadoc

1   /*
2     The TauP Toolkit: Flexible Seismic Travel-Time and Raypath Utilities.
3     Copyright (C) 1998-2000 University of South Carolina
4   
5     This program is free software; you can redistribute it and/or
6     modify it under the terms of the GNU General Public License
7     as published by the Free Software Foundation; either version 2
8     of the License, or (at your option) any later version.
9   
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14  
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  
19    The current version can be found at 
20    <A HREF="www.seis.sc.edu">http://www.seis.sc.edu</A>
21  
22    Bug reports and comments should be directed to 
23    H. Philip Crotwell, crotwell@seis.sc.edu or
24    Tom Owens, owens@seis.sc.edu
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      protectedong> 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  	// load default properties
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  		// This should not happen.
92  	    }
93  	    InputStream in = c.getResourceAsStream(packageName+"/"+
94  						  defaultPropFileName);
95  	    if (in != null) {
96  		defaultProps.load(in);
97  	    } else {
98  		// didn't find as a resource so
99  		// loop over each entry in the CLASSPATH, looking for taup.jar
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 				//we've found the path to the jar, so exit the while
120 				offset = classPath.length()+1;
121 			    }
122 			}
123 		    }
124 		}
125 	    }
126 	} catch (FileNotFoundException e) {
127 	    // can't find defaults, so we'll just have to use an empty
128 	    // properties object
129 	}
130 			
131 
132 	// create program properties with default
133 	Properties applicationProps = new Properties(defaultProps);
134 
135 	// append/overwrite with user's directory .taup
136 	try {
137 	    applicationProps.load( new FileInputStream(
138 						       System.getProperty("user.home")+
139 						       System.getProperty("file.separator")+".taup"));
140 	} catch (FileNotFoundException ee) {
141 	    // file doesn't exist, so go on
142 	}
143 
144 	// append/overwrite with current directory .taup
145 	try {
146 	    /* Check for .taup in the current directory. */
147 	    applicationProps.load( new FileInputStream(
148 						       System.getProperty("user.dir")+
149 						       System.getProperty("file.separator")+".taup"));
150 	} catch (FileNotFoundException e) {
151 	    // file doesn't exist, so go on
152 	}
153 
154 	// check for taup.model.path in system properties
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 }