Project

General

Profile

1 4080 daigle
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements system utility methods
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
27
package edu.ucsb.nceas.metacat.util;
28
29 4481 daigle
import java.util.regex.Matcher;
30
import java.util.regex.Pattern;
31 4080 daigle
import javax.servlet.ServletContext;
32
import javax.servlet.http.HttpServletRequest;
33
34
import org.apache.log4j.Logger;
35
36
import edu.ucsb.nceas.metacat.MetaCatVersion;
37
import edu.ucsb.nceas.metacat.service.PropertyService;
38
import edu.ucsb.nceas.utilities.FileUtil;
39
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
40
41
public class SystemUtil {
42
43
	private static Logger logMetacat = Logger.getLogger(SystemUtil.class);
44
	private static String METACAT_SERVLET = "metacat";
45 4662 daigle
	private static int OS_CLASS = 0;
46
47
	// Class of OS.  If we need more granularity, we should create a version
48
	// list and access it separately.
49
	public static int WIN_OS = 1;
50
	public static int LINUX_OS = 2;
51
	public static int MAC_OS = 3;
52
	public static int OTHER_OS = 4;
53
54 4080 daigle
	/**
55
	 * private constructor - all methods are static so there is no no need to
56
	 * instantiate.
57
	 */
58
	private SystemUtil() {}
59
60
	/**
61 4662 daigle
	 * Get the OS for this system.
62
	 * @return an integer representing the class of OS.  Possibilities are:
63
	 *     WIN_OS = 1;
64
	 *     LINUX_OS = 2;
65
	 *     MAC_OS = 3;
66
	 *     OTHER_OS = 4;
67
	 */
68
	public static int getOsClass() {
69
		if (OS_CLASS > 0) {
70
			return OS_CLASS;
71
		}
72
73
		String osName = System.getProperty("os.name");
74
		if (osName.startsWith("Windows")) {
75
			OS_CLASS =  WIN_OS;
76
		} else if (osName.startsWith("Linux")) {
77
			OS_CLASS =  LINUX_OS;
78
		} else if (osName.startsWith("Mac")) {
79
			OS_CLASS =  MAC_OS;
80
		} else if (osName.startsWith("Mac")) {
81
			OS_CLASS =  OTHER_OS;
82
		}
83
84
		return OS_CLASS;
85
	}
86
87
	/**
88 4080 daigle
	 * Attempt to discover the server name. The name is retrieved from the http
89
	 * servlet request. This is used by configuration routines before the port
90
	 * has been populated in metacat.properties. it is possible the port that
91
	 * the user configures might be different than the name we get here. You
92
	 * should use getServerPort() instead of this method whenever possible.
93
	 *
94
	 * @param request
95
	 *            the http servlet request we will use to find the server name
96
	 *
97
	 * @return a string holding the server name
98
	 */
99
	public static String discoverServerName(HttpServletRequest request) {
100
		String serverName = request.getServerName();
101
102
		return serverName;
103
	}
104
105
	/**
106
	 * Attempt to discover the server port. The port is retrieved from the http
107
	 * servlet request. This is used by configuration routines before the port
108
	 * has been populated in metacat.properties. it is possible the port that
109
	 * the user configures might be different than the port we get here. You
110
	 * should use getServerPort() instead of this method whenever possible.
111
	 *
112
	 * @param request
113
	 *            the http servlet request we will use to find the server port
114
	 *
115
	 * @return a string holding the server port
116
	 */
117
	public static String discoverServerPort(HttpServletRequest request) {
118
		return Integer.toString(request.getServerPort());
119
	}
120
121
	/**
122
	 * Attempt to discover the server ssl port. The ssl port is assumed using
123
	 * the standard port. This is used by configuration routines before the port
124
	 * has been populated in metacat.properties. it is possible the prot that
125
	 * the user configures might be different than the port we get here. You
126
	 * should use getServerSSLPort() instead of this method whenever possible.
127
	 *
128
	 * @param request
129
	 *            the http servlet request we will use to find the server port
130
	 *
131
	 * @return a string holding the server ssl port
132
	 */
133
	public static String discoverServerSSLPort(HttpServletRequest request) {
134
		String serverPort = discoverServerPort(request);
135
136
		if (serverPort.length() == 4 && serverPort.charAt(0) == '8') {
137
			return "8443";
138
		}
139
140
		return "443";
141
	}
142
143
	/**
144
	 * Get the server URL which is made up of the server name + : + the http
145
	 * port number. Note that if the port is 80, it is left off.
146
	 *
147
	 * @return string holding the server URL
148
	 */
149
	public static String getServerURL() throws PropertyNotFoundException {
150
		String ServerURL = "http://" + PropertyService.getProperty("server.name");
151
		String httpPort = PropertyService.getProperty("server.httpPort");
152
		if (!httpPort.equals("80")) {
153
			ServerURL += ":" + httpPort;
154
		}
155
156
		return ServerURL;
157
	}
158
159
	/**
160
	 * Get the secure server URL which is made up of the server name + : + the
161
	 * https port number. Note that if the port is 443, it is left off.
162
	 *
163
	 * @return string holding the server URL
164
	 */
165
	public static String getSecureServerURL() throws PropertyNotFoundException {
166 4421 leinfelder
		String ServerURL = "https://" + getSecureServer();
167
		return ServerURL;
168
	}
169
170
	/**
171
	 * Get the secure server  which is made up of the server name + : + the
172
	 * https port number. Note that if the port is 443, it is left off.
173
	 * NOTE: does NOT include "https://"
174
	 * @see getSecureServerURL()
175
	 *
176
	 * @return string holding the secure server
177
	 */
178
	public static String getSecureServer() throws PropertyNotFoundException {
179
		String server = PropertyService.getProperty("server.name");
180 4080 daigle
		String httpPort = PropertyService.getProperty("server.httpSSLPort");
181
		if (!httpPort.equals("443")) {
182 4421 leinfelder
			server = server + ":" + httpPort;
183 4080 daigle
		}
184
185 4421 leinfelder
		return server;
186 4080 daigle
	}
187
188
	/**
189
	 * Get the CGI URL which is made up of the server URL + file separator + the
190
	 * CGI directory
191
	 *
192
	 * @return string holding the server URL
193
	 */
194
	public static String getCGI_URL() throws PropertyNotFoundException{
195 4189 daigle
		return getContextURL()
196 4080 daigle
				+ PropertyService.getProperty("application.cgiDir");
197
	}
198
199
	/**
200
	 * Get the server URL with the context. This is made up of the server URL +
201
	 * file separator + the context
202
	 *
203
	 * @return string holding the server URL with context
204
	 */
205
	public static String getContextURL() throws PropertyNotFoundException {
206 4662 daigle
		return getServerURL() + "/"
207 4080 daigle
				+ PropertyService.getProperty("application.context");
208
	}
209
210
	/**
211
	 * Get the servlet URL. This is made up of the server URL with context +
212
	 * file separator + the metacat servlet name
213
	 *
214
	 * @return string holding the servlet URL
215
	 */
216
	public static String getServletURL() throws PropertyNotFoundException {
217 4662 daigle
		return getContextURL() + "/" + METACAT_SERVLET;
218 4080 daigle
	}
219
220
	/**
221
	 * Get the style skins URL. This is made up of the server URL with context +
222
	 * file separator + "style" + file separator + "skins"
223
	 *
224
	 * @return string holding the style skins URL
225
	 */
226
	public static String getStyleSkinsURL() throws PropertyNotFoundException {
227 4662 daigle
		return getContextURL() + "/" + "style" + "/" + "skins";
228 4080 daigle
	}
229
230
	/**
231
	 * Get the style common URL. This is made up of the server URL with context +
232
	 * file separator + "style" + file separator + "common"
233
	 *
234
	 * @return string holding the style common URL
235
	 */
236
	public static String getStyleCommonURL() throws PropertyNotFoundException {
237 4662 daigle
		return getContextURL() + "/" + "style" + "/" + "common";
238 4080 daigle
	}
239
240
	/**
241
	 * Get the metacat version by getting the string representation from
242 4619 daigle
	 * metacat.properties and instantiating a MetaCatVersion object.
243
	 * The info is set in build.properties and then populated into metacat.properties
244
	 * at build time using Ant token replacement.
245 4080 daigle
	 *
246
	 * @return a MetaCatVersion object holding metacat version information
247
	 */
248
	public static MetaCatVersion getMetacatVersion() throws PropertyNotFoundException {
249
		String metacatVersionString =
250
			PropertyService.getProperty("application.metacatVersion");
251
		return new MetaCatVersion(metacatVersionString);
252
	}
253 4619 daigle
254
	/**
255
	 * Gets a string that holds some description about the release. Typically this is
256
	 * used during release candidates for display purposes and might hold something
257
	 * like "Release Candidate 1".  Normally it is empty for final production release.
258
	 * The info is set in build.properties and then populated into metacat.properties
259
	 * at build time using Ant token replacement.
260
	 *
261
	 * @return a MetaCatVersion object holding metacat version information
262
	 */
263
	public static String getMetacatReleaseInfo() throws PropertyNotFoundException {
264
		return PropertyService.getProperty("application.metacatReleaseInfo");
265
	}
266 4080 daigle
267
	/**
268 4141 daigle
	 * Get the context directory. This is made up of the deployment directory + file
269
	 * separator + context
270 4080 daigle
	 *
271
	 * @return string holding the context directory
272
	 */
273
	public static String getContextDir() throws PropertyNotFoundException {
274 4132 daigle
		return PropertyService.getProperty("application.deployDir") + FileUtil.getFS()
275 4080 daigle
				+ PropertyService.getProperty("application.context");
276
	}
277
278
	/**
279
	 * Attempt to discover the context for this application. This is a best
280
	 * guess scenario. It is used by configuration routines before the context
281
	 * has been populated in metacat.properties. You should always use
282
	 * getApplicationContext() instead of this method if possible.
283
	 *
284
	 * @param request
285
	 *            the http servlet request we will use to find the context
286
	 *
287
	 * @return a string holding the context
288
	 */
289
	public static String discoverApplicationContext(HttpServletRequest request) {
290
		String contextPath = request.getContextPath();
291
292
		if (contextPath.charAt(0) == '/') {
293
			contextPath = contextPath.substring(1);
294
		}
295
		logMetacat.debug("contextPath: " + contextPath);
296
297
		return contextPath;
298
	}
299 4662 daigle
300
	/**
301
	 * Attempt to discover the external (to the metacat installation)
302
	 * directory where metacat will hold backup files.   This functionality
303
	 * is used to populate the configuration utility initially.  The user
304
	 * can change the directory manually, so you can't rely on this method
305
	 * to give you the actual directory.
306
	 *
307
	 * @return a string holding the backup directory path
308
	 */
309
	public static String discoverExternalDir() {
310
		if (getOsClass() == WIN_OS) {
311
			return "C:\\Program Files\\metacat";
312
		}
313
314
		return "/var/metacat";
315
	}
316 4080 daigle
317
	/**
318
	 * Get the style skins directory. This is made up of the tomcat directory
319
	 * with context + file separator + "style" + file separator + "skins"
320
	 *
321
	 * @return string holding the style skins directory
322
	 */
323
	public static String getStyleSkinsDir() throws PropertyNotFoundException {
324
		return getContextDir() + FileUtil.getFS() + "style" + FileUtil.getFS()
325
				+ "skins";
326
	}
327
328
	/**
329
	 * Get the SQL directory. This is made up of the context directory + file
330
	 * separator + sql
331
	 *
332
	 * @return string holding the sql directory
333
	 */
334
	public static String getSQLDir() throws PropertyNotFoundException {
335 4110 daigle
		return getContextDir() + FileUtil.getFS() + "WEB-INF" + FileUtil.getFS() + "sql";
336 4080 daigle
	}
337
338
	/**
339
	 * Get the default style URL from metacat.properties.
340
	 *
341
	 * @return string holding the default style URL
342
	 */
343
	public static String getDefaultStyleURL() throws PropertyNotFoundException {
344 4662 daigle
		return getStyleCommonURL() + "/"
345 4080 daigle
				+ PropertyService.getProperty("application.default-style");
346
	}
347
348
	/**
349 4185 daigle
	 * Attempt to discover the deployment directory for this application. This is a
350 4080 daigle
	 * best guess scenario. It is used by configuration routines before the
351 4185 daigle
	 * deployment directory has been populated in metacat.properties.
352 4080 daigle
	 *
353
	 * @param request
354
	 *            the http servlet request we will use to find the tomcat directory
355
	 *
356 4481 daigle
	 * @return a string holding the web application directory
357 4080 daigle
	 */
358 4185 daigle
	public static String discoverDeployDir(HttpServletRequest request) {
359 4080 daigle
		ServletContext servletContext = request.getSession()
360
				.getServletContext();
361
		String realPath = servletContext.getRealPath(".");
362 4185 daigle
		String contextPath = request.getContextPath();
363
364
		logMetacat.debug("realPath: " + realPath);
365
		logMetacat.debug("contextPath: " + contextPath);
366 4481 daigle
367
		Pattern pattern = Pattern.compile(contextPath + "/\\.$");
368
		Matcher matcher = pattern.matcher(realPath);
369 4185 daigle
370 4481 daigle
		if (matcher.find()) {
371
			realPath = matcher.replaceFirst("");
372
		}
373
374 4185 daigle
		return realPath;
375 4080 daigle
	}
376
377
}