Project

General

Profile

1
/**
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: leinfelder $'
9
 *     '$Date: 2008-10-08 11:29:12 -0700 (Wed, 08 Oct 2008) $'
10
 * '$Revision: 4421 $'
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
import javax.servlet.ServletContext;
30
import javax.servlet.http.HttpServletRequest;
31

    
32
import org.apache.log4j.Logger;
33

    
34
import edu.ucsb.nceas.metacat.MetaCatVersion;
35
import edu.ucsb.nceas.metacat.service.PropertyService;
36
import edu.ucsb.nceas.utilities.FileUtil;
37
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
38

    
39
public class SystemUtil {
40

    
41
	private static Logger logMetacat = Logger.getLogger(SystemUtil.class);
42
	private static String METACAT_SERVLET = "metacat";
43

    
44
	/**
45
	 * private constructor - all methods are static so there is no no need to
46
	 * instantiate.
47
	 */
48
	private SystemUtil() {}
49

    
50
	/**
51
	 * Attempt to discover the server name. The name is retrieved from the http
52
	 * servlet request. This is used by configuration routines before the port
53
	 * has been populated in metacat.properties. it is possible the port that
54
	 * the user configures might be different than the name we get here. You
55
	 * should use getServerPort() instead of this method whenever possible.
56
	 * 
57
	 * @param request
58
	 *            the http servlet request we will use to find the server name
59
	 * 
60
	 * @return a string holding the server name
61
	 */
62
	public static String discoverServerName(HttpServletRequest request) {
63
		String serverName = request.getServerName();
64

    
65
		return serverName;
66
	}
67

    
68
	/**
69
	 * Attempt to discover the server port. The port is retrieved from the http
70
	 * servlet request. This is used by configuration routines before the port
71
	 * has been populated in metacat.properties. it is possible the port that
72
	 * the user configures might be different than the port we get here. You
73
	 * should use getServerPort() instead of this method whenever possible.
74
	 * 
75
	 * @param request
76
	 *            the http servlet request we will use to find the server port
77
	 * 
78
	 * @return a string holding the server port
79
	 */
80
	public static String discoverServerPort(HttpServletRequest request) {
81
		return Integer.toString(request.getServerPort());
82
	}
83
	
84
	/**
85
	 * Attempt to discover the server ssl port. The ssl port is assumed using
86
	 * the standard port. This is used by configuration routines before the port
87
	 * has been populated in metacat.properties. it is possible the prot that
88
	 * the user configures might be different than the port we get here. You
89
	 * should use getServerSSLPort() instead of this method whenever possible.
90
	 * 
91
	 * @param request
92
	 *            the http servlet request we will use to find the server port
93
	 * 
94
	 * @return a string holding the server ssl port
95
	 */
96
	public static String discoverServerSSLPort(HttpServletRequest request) {
97
		String serverPort = discoverServerPort(request);
98

    
99
		if (serverPort.length() == 4 && serverPort.charAt(0) == '8') {
100
			return "8443";
101
		}
102

    
103
		return "443";
104
	}
105

    
106
	/**
107
	 * Get the server URL which is made up of the server name + : + the http
108
	 * port number. Note that if the port is 80, it is left off.
109
	 * 
110
	 * @return string holding the server URL
111
	 */
112
	public static String getServerURL() throws PropertyNotFoundException {
113
		String ServerURL = "http://" + PropertyService.getProperty("server.name");
114
		String httpPort = PropertyService.getProperty("server.httpPort");
115
		if (!httpPort.equals("80")) {
116
			ServerURL += ":" + httpPort;
117
		}
118

    
119
		return ServerURL;
120
	}
121

    
122
	/**
123
	 * Get the secure server URL which is made up of the server name + : + the
124
	 * https port number. Note that if the port is 443, it is left off.
125
	 * 
126
	 * @return string holding the server URL
127
	 */
128
	public static String getSecureServerURL() throws PropertyNotFoundException {
129
		String ServerURL = "https://" + getSecureServer();
130
		return ServerURL;
131
	}
132
	
133
	/**
134
	 * Get the secure server  which is made up of the server name + : + the
135
	 * https port number. Note that if the port is 443, it is left off.
136
	 * NOTE: does NOT include "https://"
137
	 * @see getSecureServerURL()
138
	 * 
139
	 * @return string holding the secure server
140
	 */
141
	public static String getSecureServer() throws PropertyNotFoundException {
142
		String server = PropertyService.getProperty("server.name");
143
		String httpPort = PropertyService.getProperty("server.httpSSLPort");
144
		if (!httpPort.equals("443")) {
145
			server = server + ":" + httpPort;
146
		}
147

    
148
		return server;
149
	}
150

    
151
	/**
152
	 * Get the CGI URL which is made up of the server URL + file separator + the
153
	 * CGI directory
154
	 * 
155
	 * @return string holding the server URL
156
	 */
157
	public static String getCGI_URL() throws PropertyNotFoundException{
158
		return getContextURL() 
159
				+ PropertyService.getProperty("application.cgiDir");
160
	}
161

    
162
	/**
163
	 * Get the server URL with the context. This is made up of the server URL +
164
	 * file separator + the context
165
	 * 
166
	 * @return string holding the server URL with context
167
	 */
168
	public static String getContextURL() throws PropertyNotFoundException {
169
		return getServerURL() + FileUtil.getFS()
170
				+ PropertyService.getProperty("application.context");
171
	}
172

    
173
	/**
174
	 * Get the servlet URL. This is made up of the server URL with context +
175
	 * file separator + the metacat servlet name
176
	 * 
177
	 * @return string holding the servlet URL
178
	 */
179
	public static String getServletURL() throws PropertyNotFoundException {
180
		return getContextURL() + FileUtil.getFS() + METACAT_SERVLET;
181
	}
182

    
183
	/**
184
	 * Get the style skins URL. This is made up of the server URL with context +
185
	 * file separator + "style" + file separator + "skins"
186
	 * 
187
	 * @return string holding the style skins URL
188
	 */
189
	public static String getStyleSkinsURL() throws PropertyNotFoundException {
190
		return getContextURL() + FileUtil.getFS() + "style" + FileUtil.getFS()
191
				+ "skins";
192
	}
193

    
194
	/**
195
	 * Get the style common URL. This is made up of the server URL with context +
196
	 * file separator + "style" + file separator + "common"
197
	 * 
198
	 * @return string holding the style common URL
199
	 */
200
	public static String getStyleCommonURL() throws PropertyNotFoundException {
201
		return getContextURL() + FileUtil.getFS() + "style" + FileUtil.getFS()
202
				+ "common";
203
	}
204
	
205
	/**
206
	 * Get the metacat version by getting the string representation from
207
	 * metacat.properties and instantiating a MetaCatVersion object
208
	 * 
209
	 * @return a MetaCatVersion object holding metacat version information
210
	 */
211
	public static MetaCatVersion getMetacatVersion() throws PropertyNotFoundException {
212
		String metacatVersionString = 
213
			PropertyService.getProperty("application.metacatVersion");
214
		return new MetaCatVersion(metacatVersionString);
215
	}
216

    
217
	/**
218
	 * Get the context directory. This is made up of the deployment directory + file
219
	 * separator + context
220
	 * 
221
	 * @return string holding the context directory
222
	 */
223
	public static String getContextDir() throws PropertyNotFoundException {
224
		return PropertyService.getProperty("application.deployDir") + FileUtil.getFS()
225
				+ PropertyService.getProperty("application.context");
226
	}
227

    
228
	/**
229
	 * Attempt to discover the context for this application. This is a best
230
	 * guess scenario. It is used by configuration routines before the context
231
	 * has been populated in metacat.properties. You should always use
232
	 * getApplicationContext() instead of this method if possible.
233
	 * 
234
	 * @param request
235
	 *            the http servlet request we will use to find the context
236
	 * 
237
	 * @return a string holding the context
238
	 */
239
	public static String discoverApplicationContext(HttpServletRequest request) {
240
		String contextPath = request.getContextPath();
241

    
242
		if (contextPath.charAt(0) == '/') {
243
			contextPath = contextPath.substring(1);
244
		}
245
		logMetacat.debug("contextPath: " + contextPath);
246

    
247
		return contextPath;
248
	}
249

    
250
	/**
251
	 * Get the style skins directory. This is made up of the tomcat directory
252
	 * with context + file separator + "style" + file separator + "skins"
253
	 * 
254
	 * @return string holding the style skins directory
255
	 */
256
	public static String getStyleSkinsDir() throws PropertyNotFoundException {
257
		return getContextDir() + FileUtil.getFS() + "style" + FileUtil.getFS()
258
				+ "skins";
259
	}
260

    
261
	/**
262
	 * Get the SQL directory. This is made up of the context directory + file
263
	 * separator + sql
264
	 * 
265
	 * @return string holding the sql directory
266
	 */
267
	public static String getSQLDir() throws PropertyNotFoundException {
268
		return getContextDir() + FileUtil.getFS() + "WEB-INF" + FileUtil.getFS() + "sql";
269
	}
270

    
271
	/**
272
	 * Get the default style URL from metacat.properties.
273
	 * 
274
	 * @return string holding the default style URL
275
	 */
276
	public static String getDefaultStyleURL() throws PropertyNotFoundException {
277
		return getStyleCommonURL() + FileUtil.getFS()
278
				+ PropertyService.getProperty("application.default-style");
279
	}
280

    
281
	/**
282
	 * Attempt to discover the deployment directory for this application. This is a
283
	 * best guess scenario. It is used by configuration routines before the
284
	 * deployment directory has been populated in metacat.properties. 
285
	 * 
286
	 * @param request
287
	 *            the http servlet request we will use to find the tomcat directory
288
	 * 
289
	 * @return a string holding the tomcat directory
290
	 */
291
	public static String discoverDeployDir(HttpServletRequest request) {
292
		ServletContext servletContext = request.getSession()
293
				.getServletContext();
294
		String realPath = servletContext.getRealPath(".");
295
		String contextName = servletContext.getServletContextName();
296
		String serverInfo = servletContext.getServerInfo();
297
		String contextPath = request.getContextPath();
298
		String servletPath = request.getServletPath();
299
		
300
		logMetacat.debug("realPath: " + realPath);
301
		logMetacat.debug("contextName: " + contextName);
302
		logMetacat.debug("serverInfo: " + serverInfo);
303
		logMetacat.debug("contextPath: " + contextPath);
304
		logMetacat.debug("servletPath: " + servletPath);
305
		
306

    
307
		return realPath;
308
	}
309

    
310
}
(10-10/11)