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 |
4812
|
daigle
|
import java.util.Vector;
|
30 |
4481
|
daigle
|
import java.util.regex.Matcher;
|
31 |
|
|
import java.util.regex.Pattern;
|
32 |
4080
|
daigle
|
import javax.servlet.ServletContext;
|
33 |
|
|
import javax.servlet.http.HttpServletRequest;
|
34 |
|
|
|
35 |
|
|
import org.apache.log4j.Logger;
|
36 |
|
|
|
37 |
5027
|
daigle
|
import edu.ucsb.nceas.metacat.MetacatVersion;
|
38 |
5030
|
daigle
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
39 |
4795
|
daigle
|
import edu.ucsb.nceas.metacat.service.ServiceService;
|
40 |
5015
|
daigle
|
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
|
41 |
|
|
import edu.ucsb.nceas.metacat.shared.ServiceException;
|
42 |
4080
|
daigle
|
import edu.ucsb.nceas.utilities.FileUtil;
|
43 |
|
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
44 |
4812
|
daigle
|
import edu.ucsb.nceas.utilities.StringUtil;
|
45 |
4854
|
daigle
|
import edu.ucsb.nceas.utilities.UtilException;
|
46 |
4080
|
daigle
|
|
47 |
|
|
public class SystemUtil {
|
48 |
|
|
|
49 |
|
|
private static Logger logMetacat = Logger.getLogger(SystemUtil.class);
|
50 |
|
|
private static String METACAT_SERVLET = "metacat";
|
51 |
5030
|
daigle
|
// private static String METACAT_WEB_SERVLET = "metacatweb";
|
52 |
4662
|
daigle
|
private static int OS_CLASS = 0;
|
53 |
|
|
|
54 |
|
|
// Class of OS. If we need more granularity, we should create a version
|
55 |
|
|
// list and access it separately.
|
56 |
|
|
public static int WIN_OS = 1;
|
57 |
|
|
public static int LINUX_OS = 2;
|
58 |
|
|
public static int MAC_OS = 3;
|
59 |
|
|
public static int OTHER_OS = 4;
|
60 |
|
|
|
61 |
4080
|
daigle
|
/**
|
62 |
|
|
* private constructor - all methods are static so there is no no need to
|
63 |
|
|
* instantiate.
|
64 |
|
|
*/
|
65 |
|
|
private SystemUtil() {}
|
66 |
|
|
|
67 |
|
|
/**
|
68 |
4662
|
daigle
|
* Get the OS for this system.
|
69 |
|
|
* @return an integer representing the class of OS. Possibilities are:
|
70 |
|
|
* WIN_OS = 1;
|
71 |
|
|
* LINUX_OS = 2;
|
72 |
|
|
* MAC_OS = 3;
|
73 |
|
|
* OTHER_OS = 4;
|
74 |
|
|
*/
|
75 |
|
|
public static int getOsClass() {
|
76 |
|
|
if (OS_CLASS > 0) {
|
77 |
|
|
return OS_CLASS;
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
String osName = System.getProperty("os.name");
|
81 |
|
|
if (osName.startsWith("Windows")) {
|
82 |
|
|
OS_CLASS = WIN_OS;
|
83 |
|
|
} else if (osName.startsWith("Linux")) {
|
84 |
|
|
OS_CLASS = LINUX_OS;
|
85 |
|
|
} else if (osName.startsWith("Mac")) {
|
86 |
|
|
OS_CLASS = MAC_OS;
|
87 |
|
|
} else if (osName.startsWith("Mac")) {
|
88 |
|
|
OS_CLASS = OTHER_OS;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
return OS_CLASS;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
/**
|
95 |
4080
|
daigle
|
* Attempt to discover the server name. The name is retrieved from the http
|
96 |
|
|
* servlet request. This is used by configuration routines before the port
|
97 |
|
|
* has been populated in metacat.properties. it is possible the port that
|
98 |
|
|
* the user configures might be different than the name we get here. You
|
99 |
|
|
* should use getServerPort() instead of this method whenever possible.
|
100 |
|
|
*
|
101 |
|
|
* @param request
|
102 |
|
|
* the http servlet request we will use to find the server name
|
103 |
|
|
*
|
104 |
|
|
* @return a string holding the server name
|
105 |
|
|
*/
|
106 |
|
|
public static String discoverServerName(HttpServletRequest request) {
|
107 |
|
|
String serverName = request.getServerName();
|
108 |
|
|
|
109 |
|
|
return serverName;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
/**
|
113 |
|
|
* Attempt to discover the server port. The port is retrieved from the http
|
114 |
|
|
* servlet request. This is used by configuration routines before the port
|
115 |
|
|
* has been populated in metacat.properties. it is possible the port that
|
116 |
|
|
* the user configures might be different than the port we get here. You
|
117 |
|
|
* should use getServerPort() instead of this method whenever possible.
|
118 |
|
|
*
|
119 |
|
|
* @param request
|
120 |
|
|
* the http servlet request we will use to find the server port
|
121 |
|
|
*
|
122 |
|
|
* @return a string holding the server port
|
123 |
|
|
*/
|
124 |
|
|
public static String discoverServerPort(HttpServletRequest request) {
|
125 |
|
|
return Integer.toString(request.getServerPort());
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
/**
|
129 |
|
|
* Attempt to discover the server ssl port. The ssl port is assumed using
|
130 |
|
|
* the standard port. This is used by configuration routines before the port
|
131 |
|
|
* has been populated in metacat.properties. it is possible the prot that
|
132 |
|
|
* the user configures might be different than the port we get here. You
|
133 |
|
|
* should use getServerSSLPort() instead of this method whenever possible.
|
134 |
|
|
*
|
135 |
|
|
* @param request
|
136 |
|
|
* the http servlet request we will use to find the server port
|
137 |
|
|
*
|
138 |
|
|
* @return a string holding the server ssl port
|
139 |
|
|
*/
|
140 |
|
|
public static String discoverServerSSLPort(HttpServletRequest request) {
|
141 |
|
|
String serverPort = discoverServerPort(request);
|
142 |
|
|
|
143 |
|
|
if (serverPort.length() == 4 && serverPort.charAt(0) == '8') {
|
144 |
|
|
return "8443";
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
return "443";
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
/**
|
151 |
|
|
* Get the server URL which is made up of the server name + : + the http
|
152 |
|
|
* port number. Note that if the port is 80, it is left off.
|
153 |
|
|
*
|
154 |
|
|
* @return string holding the server URL
|
155 |
|
|
*/
|
156 |
|
|
public static String getServerURL() throws PropertyNotFoundException {
|
157 |
5791
|
berkley
|
String httpPort = PropertyService.getProperty("server.httpPort");
|
158 |
|
|
|
159 |
|
|
String serverURL = "http://";
|
160 |
|
|
if(httpPort.equals("443") || httpPort.equals("8443"))
|
161 |
|
|
{
|
162 |
|
|
serverURL = "https://";
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
serverURL += PropertyService.getProperty("server.name");
|
166 |
|
|
|
167 |
4080
|
daigle
|
if (!httpPort.equals("80")) {
|
168 |
5791
|
berkley
|
serverURL += ":" + httpPort;
|
169 |
4080
|
daigle
|
}
|
170 |
|
|
|
171 |
5791
|
berkley
|
return serverURL;
|
172 |
4080
|
daigle
|
}
|
173 |
|
|
|
174 |
|
|
/**
|
175 |
|
|
* Get the secure server URL which is made up of the server name + : + the
|
176 |
|
|
* https port number. Note that if the port is 443, it is left off.
|
177 |
|
|
*
|
178 |
|
|
* @return string holding the server URL
|
179 |
|
|
*/
|
180 |
|
|
public static String getSecureServerURL() throws PropertyNotFoundException {
|
181 |
4421
|
leinfelder
|
String ServerURL = "https://" + getSecureServer();
|
182 |
|
|
return ServerURL;
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
/**
|
186 |
|
|
* Get the secure server which is made up of the server name + : + the
|
187 |
|
|
* https port number. Note that if the port is 443, it is left off.
|
188 |
|
|
* NOTE: does NOT include "https://"
|
189 |
|
|
* @see getSecureServerURL()
|
190 |
|
|
*
|
191 |
|
|
* @return string holding the secure server
|
192 |
|
|
*/
|
193 |
|
|
public static String getSecureServer() throws PropertyNotFoundException {
|
194 |
|
|
String server = PropertyService.getProperty("server.name");
|
195 |
4080
|
daigle
|
String httpPort = PropertyService.getProperty("server.httpSSLPort");
|
196 |
|
|
if (!httpPort.equals("443")) {
|
197 |
4421
|
leinfelder
|
server = server + ":" + httpPort;
|
198 |
4080
|
daigle
|
}
|
199 |
|
|
|
200 |
4421
|
leinfelder
|
return server;
|
201 |
4080
|
daigle
|
}
|
202 |
|
|
|
203 |
|
|
/**
|
204 |
|
|
* Get the CGI URL which is made up of the server URL + file separator + the
|
205 |
|
|
* CGI directory
|
206 |
|
|
*
|
207 |
|
|
* @return string holding the server URL
|
208 |
|
|
*/
|
209 |
|
|
public static String getCGI_URL() throws PropertyNotFoundException{
|
210 |
4189
|
daigle
|
return getContextURL()
|
211 |
4080
|
daigle
|
+ PropertyService.getProperty("application.cgiDir");
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
/**
|
215 |
|
|
* Get the server URL with the context. This is made up of the server URL +
|
216 |
|
|
* file separator + the context
|
217 |
|
|
*
|
218 |
|
|
* @return string holding the server URL with context
|
219 |
|
|
*/
|
220 |
|
|
public static String getContextURL() throws PropertyNotFoundException {
|
221 |
4662
|
daigle
|
return getServerURL() + "/"
|
222 |
4080
|
daigle
|
+ PropertyService.getProperty("application.context");
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
/**
|
226 |
|
|
* Get the servlet URL. This is made up of the server URL with context +
|
227 |
|
|
* file separator + the metacat servlet name
|
228 |
|
|
*
|
229 |
|
|
* @return string holding the servlet URL
|
230 |
|
|
*/
|
231 |
|
|
public static String getServletURL() throws PropertyNotFoundException {
|
232 |
4662
|
daigle
|
return getContextURL() + "/" + METACAT_SERVLET;
|
233 |
4080
|
daigle
|
}
|
234 |
5027
|
daigle
|
|
235 |
5030
|
daigle
|
// /**
|
236 |
|
|
// * Get the web front end servlet URL. This is made up of the server URL with
|
237 |
|
|
// * context + file separator + the metacat web servlet name
|
238 |
|
|
// *
|
239 |
|
|
// * @return string holding the web servlet URL
|
240 |
|
|
// */
|
241 |
|
|
// public static String getWebServletURL() throws PropertyNotFoundException {
|
242 |
|
|
// return getContextURL() + "/" + METACAT_WEB_SERVLET;
|
243 |
|
|
// }
|
244 |
4080
|
daigle
|
|
245 |
|
|
/**
|
246 |
|
|
* Get the style skins URL. This is made up of the server URL with context +
|
247 |
|
|
* file separator + "style" + file separator + "skins"
|
248 |
|
|
*
|
249 |
|
|
* @return string holding the style skins URL
|
250 |
|
|
*/
|
251 |
|
|
public static String getStyleSkinsURL() throws PropertyNotFoundException {
|
252 |
4662
|
daigle
|
return getContextURL() + "/" + "style" + "/" + "skins";
|
253 |
4080
|
daigle
|
}
|
254 |
|
|
|
255 |
|
|
/**
|
256 |
|
|
* Get the style common URL. This is made up of the server URL with context +
|
257 |
|
|
* file separator + "style" + file separator + "common"
|
258 |
|
|
*
|
259 |
|
|
* @return string holding the style common URL
|
260 |
|
|
*/
|
261 |
|
|
public static String getStyleCommonURL() throws PropertyNotFoundException {
|
262 |
4662
|
daigle
|
return getContextURL() + "/" + "style" + "/" + "common";
|
263 |
4080
|
daigle
|
}
|
264 |
|
|
|
265 |
|
|
/**
|
266 |
|
|
* Get the metacat version by getting the string representation from
|
267 |
5030
|
daigle
|
* metacat.properties and instantiating a MetacatVersion object.
|
268 |
4619
|
daigle
|
* The info is set in build.properties and then populated into metacat.properties
|
269 |
|
|
* at build time using Ant token replacement.
|
270 |
4080
|
daigle
|
*
|
271 |
5030
|
daigle
|
* @return a MetacatVersion object holding metacat version information
|
272 |
4080
|
daigle
|
*/
|
273 |
5027
|
daigle
|
public static MetacatVersion getMetacatVersion() throws PropertyNotFoundException {
|
274 |
4080
|
daigle
|
String metacatVersionString =
|
275 |
|
|
PropertyService.getProperty("application.metacatVersion");
|
276 |
5027
|
daigle
|
return new MetacatVersion(metacatVersionString);
|
277 |
4080
|
daigle
|
}
|
278 |
4619
|
daigle
|
|
279 |
|
|
/**
|
280 |
|
|
* Gets a string that holds some description about the release. Typically this is
|
281 |
|
|
* used during release candidates for display purposes and might hold something
|
282 |
|
|
* like "Release Candidate 1". Normally it is empty for final production release.
|
283 |
|
|
* The info is set in build.properties and then populated into metacat.properties
|
284 |
|
|
* at build time using Ant token replacement.
|
285 |
|
|
*
|
286 |
5030
|
daigle
|
* @return a MetacatVersion object holding metacat version information
|
287 |
4619
|
daigle
|
*/
|
288 |
|
|
public static String getMetacatReleaseInfo() throws PropertyNotFoundException {
|
289 |
|
|
return PropertyService.getProperty("application.metacatReleaseInfo");
|
290 |
|
|
}
|
291 |
4080
|
daigle
|
|
292 |
|
|
/**
|
293 |
4141
|
daigle
|
* Get the context directory. This is made up of the deployment directory + file
|
294 |
|
|
* separator + context
|
295 |
4080
|
daigle
|
*
|
296 |
|
|
* @return string holding the context directory
|
297 |
|
|
*/
|
298 |
|
|
public static String getContextDir() throws PropertyNotFoundException {
|
299 |
4132
|
daigle
|
return PropertyService.getProperty("application.deployDir") + FileUtil.getFS()
|
300 |
4080
|
daigle
|
+ PropertyService.getProperty("application.context");
|
301 |
|
|
}
|
302 |
|
|
|
303 |
|
|
/**
|
304 |
|
|
* Attempt to discover the context for this application. This is a best
|
305 |
|
|
* guess scenario. It is used by configuration routines before the context
|
306 |
|
|
* has been populated in metacat.properties. You should always use
|
307 |
|
|
* getApplicationContext() instead of this method if possible.
|
308 |
|
|
*
|
309 |
4795
|
daigle
|
* @param servletContext
|
310 |
|
|
* the servlet context we will use to find the application context
|
311 |
4080
|
daigle
|
*
|
312 |
|
|
* @return a string holding the context
|
313 |
|
|
*/
|
314 |
4795
|
daigle
|
public static String discoverApplicationContext(ServletContext servletContext) {
|
315 |
|
|
String applicationContext = "";
|
316 |
|
|
String realPath = servletContext.getRealPath("/");
|
317 |
4080
|
daigle
|
|
318 |
4795
|
daigle
|
if (realPath.charAt(realPath.length() - 1) == '/') {
|
319 |
|
|
realPath = realPath.substring(0, realPath.length() - 1);
|
320 |
4080
|
daigle
|
}
|
321 |
4795
|
daigle
|
|
322 |
|
|
int lastSlashIndex = realPath.lastIndexOf('/');
|
323 |
|
|
if (lastSlashIndex != -1) {
|
324 |
|
|
applicationContext = realPath.substring(lastSlashIndex + 1);
|
325 |
|
|
}
|
326 |
|
|
|
327 |
|
|
logMetacat.debug("application context: " + applicationContext);
|
328 |
4080
|
daigle
|
|
329 |
4795
|
daigle
|
return applicationContext;
|
330 |
4080
|
daigle
|
}
|
331 |
4662
|
daigle
|
|
332 |
|
|
/**
|
333 |
4805
|
daigle
|
* Gets the stored backup location. This location is held in a file at
|
334 |
|
|
* <user_home>/.<application_context>/backup-location
|
335 |
|
|
*
|
336 |
|
|
* @return a string holding the backup location. Null if none could be found.
|
337 |
|
|
*/
|
338 |
4951
|
daigle
|
public static String getStoredBackupDir() throws MetacatUtilException {
|
339 |
4805
|
daigle
|
String applicationContext = null;
|
340 |
|
|
try {
|
341 |
|
|
applicationContext = ServiceService.getRealApplicationContext();
|
342 |
|
|
// Check if there is a file at
|
343 |
|
|
// <user_home>/<application_context>/backup-location. If so, it
|
344 |
|
|
// should contain one line that is a file that points to a writable
|
345 |
|
|
// directory. If that is true, use that value as the backup dir.
|
346 |
|
|
String storedBackupFileLoc = getUserHomeDir() + FileUtil.getFS() + "."
|
347 |
|
|
+ applicationContext + FileUtil.getFS() + "backup-location";
|
348 |
|
|
if (FileUtil.getFileStatus(storedBackupFileLoc) >= FileUtil.EXISTS_READABLE) {
|
349 |
|
|
String storedBackupDirLoc = FileUtil
|
350 |
|
|
.readFileToString(storedBackupFileLoc);
|
351 |
|
|
if (FileUtil.isDirectory(storedBackupDirLoc)
|
352 |
|
|
&& FileUtil.getFileStatus(storedBackupDirLoc) > FileUtil.EXISTS_READABLE) {
|
353 |
|
|
return storedBackupDirLoc;
|
354 |
|
|
}
|
355 |
|
|
}
|
356 |
4854
|
daigle
|
} catch (UtilException ue) {
|
357 |
|
|
logMetacat.warn("Utility problem finding backup location: " + ue.getMessage());
|
358 |
4805
|
daigle
|
} catch (ServiceException se) {
|
359 |
|
|
logMetacat.warn("Could not get real application context: " + se.getMessage());
|
360 |
|
|
}
|
361 |
|
|
return null;
|
362 |
|
|
}
|
363 |
|
|
|
364 |
4951
|
daigle
|
public static void writeStoredBackupFile(String backupPath) throws MetacatUtilException {
|
365 |
4805
|
daigle
|
String applicationContext = null;
|
366 |
|
|
try {
|
367 |
|
|
applicationContext = ServiceService.getRealApplicationContext();
|
368 |
|
|
// Write the backup path to
|
369 |
|
|
// <user_home>/.<application_context>/backup-location.
|
370 |
|
|
String storedBackupFileDir = getUserHomeDir() + FileUtil.getFS() + "." + applicationContext;
|
371 |
|
|
String storedBackupFileLoc = storedBackupFileDir + FileUtil.getFS() + "backup-location";
|
372 |
|
|
if (!FileUtil.isDirectory(storedBackupFileDir)) {
|
373 |
|
|
FileUtil.createDirectory(storedBackupFileDir);
|
374 |
|
|
}
|
375 |
|
|
if (FileUtil.getFileStatus(storedBackupFileLoc) == FileUtil.DOES_NOT_EXIST) {
|
376 |
|
|
FileUtil.createFile(storedBackupFileLoc);
|
377 |
|
|
}
|
378 |
|
|
if (FileUtil.getFileStatus(storedBackupFileLoc) < FileUtil.EXISTS_READ_WRITABLE) {
|
379 |
|
|
throw new UtilException("Stored backup location file is not writable: " + storedBackupFileLoc);
|
380 |
|
|
}
|
381 |
|
|
|
382 |
|
|
FileUtil.writeFile(storedBackupFileLoc, backupPath);
|
383 |
|
|
|
384 |
4854
|
daigle
|
} catch (UtilException ue) {
|
385 |
|
|
logMetacat.warn("Utility error writing backup file: " + ue.getMessage());
|
386 |
4805
|
daigle
|
} catch (ServiceException se) {
|
387 |
4854
|
daigle
|
logMetacat.warn("Service error getting real application context: " + se.getMessage());
|
388 |
|
|
}
|
389 |
4805
|
daigle
|
}
|
390 |
|
|
|
391 |
|
|
/**
|
392 |
4662
|
daigle
|
* Attempt to discover the external (to the metacat installation)
|
393 |
|
|
* directory where metacat will hold backup files. This functionality
|
394 |
|
|
* is used to populate the configuration utility initially. The user
|
395 |
|
|
* can change the directory manually, so you can't rely on this method
|
396 |
4712
|
daigle
|
* to give you the actual directory. Here are the steps taken to discover
|
397 |
|
|
* the directory:
|
398 |
4662
|
daigle
|
*
|
399 |
4805
|
daigle
|
* -- 1) Look for an existing hidden (.<application_context>) directory in a default system directory. Get
|
400 |
4712
|
daigle
|
* the default base directory for the OS. (See application.windowsBackupBaseDir and
|
401 |
|
|
* application.linuxBackupBaseDir in metacat.properties.) If a directory called
|
402 |
4795
|
daigle
|
* <base_dir>/metacat/.<application_context> exists, return <base_dir>/metacat.
|
403 |
4805
|
daigle
|
* -- 2) Otherwise, look for an existing hidden (.metacat) directory in the user directory. If a directory
|
404 |
4795
|
daigle
|
* called <user_dir>/metacat/.<application_context> exists for the user that started tomcat,
|
405 |
|
|
* return <user_dir>/metacat.
|
406 |
4805
|
daigle
|
* -- 3) Otherwise, look for an existing metacat directory in a default system directory. Get
|
407 |
4795
|
daigle
|
* the default base directory for the OS. (See application.windowsBackupBaseDir and
|
408 |
|
|
* application.linuxBackupBaseDir in metacat.properties.) If a directory called
|
409 |
|
|
* <base_dir>/metacat exists, return <base_dir>/metacat.
|
410 |
4805
|
daigle
|
* -- 4) Otherwise, look for an existing metacat directory in the user directory. If a directory
|
411 |
4795
|
daigle
|
* called <user_dir>/metacat/ exists for the user that started tomcat, return <user_dir>/metacat.
|
412 |
4805
|
daigle
|
* -- 5) Otherwise, is the <base_dir> writable by the user that started tomcat? If so, return
|
413 |
4795
|
daigle
|
* <base_dir>/metacat
|
414 |
4805
|
daigle
|
* -- 6) Does the <user_home> exist? If so, return <user_home>/metacat
|
415 |
|
|
* -- 7) Otherwise, return null
|
416 |
4712
|
daigle
|
*
|
417 |
4662
|
daigle
|
* @return a string holding the backup directory path
|
418 |
|
|
*/
|
419 |
4951
|
daigle
|
public static String discoverExternalDir() throws MetacatUtilException {
|
420 |
4795
|
daigle
|
String applicationContext = null;
|
421 |
4712
|
daigle
|
|
422 |
|
|
try {
|
423 |
4795
|
daigle
|
applicationContext = ServiceService.getRealApplicationContext();
|
424 |
|
|
|
425 |
|
|
// Set the default location using the os
|
426 |
|
|
String systemDir = "";
|
427 |
|
|
if (getOsClass() == WIN_OS) {
|
428 |
5030
|
daigle
|
systemDir = "C:\\Program Files";
|
429 |
4795
|
daigle
|
} else {
|
430 |
5030
|
daigle
|
systemDir = "/var";
|
431 |
4795
|
daigle
|
}
|
432 |
|
|
String systemMetacatDir = systemDir + FileUtil.getFS() + "metacat";
|
433 |
|
|
String systemBackupDir = systemMetacatDir + FileUtil.getFS() + "."
|
434 |
|
|
+ applicationContext;
|
435 |
|
|
|
436 |
|
|
String userHomeDir = getUserHomeDir();
|
437 |
|
|
String userHomeMetacatDir = userHomeDir + FileUtil.getFS() + "metacat";
|
438 |
|
|
String userHomeBackupDir = userHomeMetacatDir + FileUtil.getFS() + "." + applicationContext;
|
439 |
|
|
|
440 |
|
|
// If <system_dir>/metacat/.<application_context> exists writable,
|
441 |
|
|
// return <system_dir>/metacat
|
442 |
|
|
if ((FileUtil.getFileStatus(systemBackupDir) >= FileUtil.EXISTS_READ_WRITABLE)) {
|
443 |
|
|
return systemMetacatDir;
|
444 |
|
|
}
|
445 |
|
|
|
446 |
|
|
// Otherwise if <user_dir>/metacat/.<application_context> exists writable, return
|
447 |
|
|
// <user_dir>/metacat
|
448 |
|
|
if ((FileUtil.getFileStatus(userHomeBackupDir) >= FileUtil.EXISTS_READ_WRITABLE)) {
|
449 |
|
|
return userHomeMetacatDir;
|
450 |
|
|
}
|
451 |
|
|
|
452 |
|
|
// Otherwise if <system_dir>/metacat exists writable, create
|
453 |
|
|
// <system_dir>/metacat/.<application_context> and return <system_dir>/metacat
|
454 |
|
|
if ((FileUtil.getFileStatus(systemMetacatDir) >= FileUtil.EXISTS_READ_WRITABLE)) {
|
455 |
|
|
// go ahead and create the backup hidden dir
|
456 |
|
|
FileUtil.createDirectory(systemBackupDir);
|
457 |
|
|
return systemMetacatDir;
|
458 |
|
|
}
|
459 |
|
|
|
460 |
|
|
// Otherwise if <user_dir>/metacat exists writable, create
|
461 |
|
|
// <user_dir>/metacat/.<application_context> and return <user_dir>/metacat
|
462 |
|
|
if ((FileUtil.getFileStatus(userHomeMetacatDir) >= FileUtil.EXISTS_READ_WRITABLE)) {
|
463 |
|
|
// go ahead and create the backup hidden dir
|
464 |
|
|
FileUtil.createDirectory(userHomeBackupDir);
|
465 |
|
|
return userHomeMetacatDir;
|
466 |
|
|
}
|
467 |
|
|
|
468 |
|
|
// Otherwise if <system_dir> exists, create
|
469 |
|
|
// <system_dir>/metacat/.<application_context> and return <system_dir>/metacat
|
470 |
|
|
if ((FileUtil.getFileStatus(systemDir) >= FileUtil.EXISTS_READ_WRITABLE)) {
|
471 |
|
|
// go ahead and create the backup hidden dir
|
472 |
|
|
FileUtil.createDirectory(systemBackupDir);
|
473 |
|
|
return systemMetacatDir;
|
474 |
|
|
}
|
475 |
|
|
|
476 |
|
|
// Otherwise if <user_dir> exists, return <user_dir> create
|
477 |
|
|
// <user_dir>/metacat/.<application_context> and return <user_dir>/metacat
|
478 |
|
|
if ((FileUtil.getFileStatus(userHomeDir) >= FileUtil.EXISTS_READ_WRITABLE)) {
|
479 |
|
|
// go ahead and create the backup hidden dir
|
480 |
|
|
FileUtil.createDirectory(userHomeBackupDir);
|
481 |
|
|
return userHomeMetacatDir;
|
482 |
|
|
}
|
483 |
|
|
|
484 |
|
|
} catch (ServiceException se) {
|
485 |
|
|
logMetacat.warn("Could not get real application context: " + se.getMessage());
|
486 |
4951
|
daigle
|
} catch (UtilException ue) {
|
487 |
|
|
logMetacat.warn("Could not create directory: " + ue.getMessage());
|
488 |
5030
|
daigle
|
}
|
489 |
|
|
|
490 |
4712
|
daigle
|
// Otherwise, return userHomeDir
|
491 |
|
|
return null;
|
492 |
4662
|
daigle
|
}
|
493 |
4712
|
daigle
|
|
494 |
|
|
/**
|
495 |
|
|
* Store the location of the backup file location into a file at
|
496 |
4795
|
daigle
|
* <user_home>/<application_dir>/backup-location
|
497 |
4712
|
daigle
|
*
|
498 |
|
|
* @param externalDir the backup file location.
|
499 |
|
|
*/
|
500 |
|
|
public static void storeExternalDirLocation(String externalDir) {
|
501 |
|
|
if (getUserHomeDir() != null) {
|
502 |
4795
|
daigle
|
String applicationContext = null;
|
503 |
|
|
String storedBackupLocDir = null;
|
504 |
|
|
String storedBackupLocFile = null;
|
505 |
4712
|
daigle
|
try {
|
506 |
4795
|
daigle
|
applicationContext = ServiceService.getRealApplicationContext();
|
507 |
|
|
storedBackupLocDir = getUserHomeDir() + FileUtil.getFS() + "."
|
508 |
|
|
+ applicationContext;
|
509 |
|
|
storedBackupLocFile = storedBackupLocDir + FileUtil.getFS()
|
510 |
|
|
+ "backup-location";
|
511 |
|
|
|
512 |
4725
|
daigle
|
FileUtil.createDirectory(storedBackupLocDir);
|
513 |
|
|
FileUtil.writeFile(storedBackupLocFile, externalDir);
|
514 |
4795
|
daigle
|
} catch (ServiceException se) {
|
515 |
|
|
logMetacat.error("Could not get real application directory while trying to write "
|
516 |
|
|
+ "stored backup directory: "+ storedBackupLocFile + " : " + se.getMessage());
|
517 |
4854
|
daigle
|
} catch (UtilException ue) {
|
518 |
|
|
logMetacat.error("Could not write backup location file into "
|
519 |
|
|
+ "stored backup directory: "+ storedBackupLocFile + " : " + ue.getMessage());
|
520 |
4712
|
daigle
|
}
|
521 |
|
|
} else {
|
522 |
|
|
logMetacat.warn("Could not write out stored backup directory."
|
523 |
|
|
+ " User directory does not exist");
|
524 |
|
|
}
|
525 |
|
|
}
|
526 |
4080
|
daigle
|
|
527 |
|
|
/**
|
528 |
|
|
* Get the style skins directory. This is made up of the tomcat directory
|
529 |
|
|
* with context + file separator + "style" + file separator + "skins"
|
530 |
|
|
*
|
531 |
|
|
* @return string holding the style skins directory
|
532 |
|
|
*/
|
533 |
|
|
public static String getStyleSkinsDir() throws PropertyNotFoundException {
|
534 |
|
|
return getContextDir() + FileUtil.getFS() + "style" + FileUtil.getFS()
|
535 |
|
|
+ "skins";
|
536 |
|
|
}
|
537 |
|
|
|
538 |
|
|
/**
|
539 |
|
|
* Get the SQL directory. This is made up of the context directory + file
|
540 |
|
|
* separator + sql
|
541 |
|
|
*
|
542 |
|
|
* @return string holding the sql directory
|
543 |
|
|
*/
|
544 |
|
|
public static String getSQLDir() throws PropertyNotFoundException {
|
545 |
4110
|
daigle
|
return getContextDir() + FileUtil.getFS() + "WEB-INF" + FileUtil.getFS() + "sql";
|
546 |
4080
|
daigle
|
}
|
547 |
|
|
|
548 |
|
|
/**
|
549 |
|
|
* Get the default style URL from metacat.properties.
|
550 |
|
|
*
|
551 |
|
|
* @return string holding the default style URL
|
552 |
|
|
*/
|
553 |
|
|
public static String getDefaultStyleURL() throws PropertyNotFoundException {
|
554 |
4662
|
daigle
|
return getStyleCommonURL() + "/"
|
555 |
4080
|
daigle
|
+ PropertyService.getProperty("application.default-style");
|
556 |
|
|
}
|
557 |
|
|
|
558 |
|
|
/**
|
559 |
4185
|
daigle
|
* Attempt to discover the deployment directory for this application. This is a
|
560 |
4080
|
daigle
|
* best guess scenario. It is used by configuration routines before the
|
561 |
4185
|
daigle
|
* deployment directory has been populated in metacat.properties.
|
562 |
4080
|
daigle
|
*
|
563 |
|
|
* @param request
|
564 |
|
|
* the http servlet request we will use to find the tomcat directory
|
565 |
|
|
*
|
566 |
4481
|
daigle
|
* @return a string holding the web application directory
|
567 |
4080
|
daigle
|
*/
|
568 |
4185
|
daigle
|
public static String discoverDeployDir(HttpServletRequest request) {
|
569 |
4080
|
daigle
|
ServletContext servletContext = request.getSession()
|
570 |
|
|
.getServletContext();
|
571 |
|
|
String realPath = servletContext.getRealPath(".");
|
572 |
4185
|
daigle
|
String contextPath = request.getContextPath();
|
573 |
|
|
|
574 |
|
|
logMetacat.debug("realPath: " + realPath);
|
575 |
|
|
logMetacat.debug("contextPath: " + contextPath);
|
576 |
4481
|
daigle
|
|
577 |
|
|
Pattern pattern = Pattern.compile(contextPath + "/\\.$");
|
578 |
|
|
Matcher matcher = pattern.matcher(realPath);
|
579 |
4185
|
daigle
|
|
580 |
4481
|
daigle
|
if (matcher.find()) {
|
581 |
|
|
realPath = matcher.replaceFirst("");
|
582 |
|
|
}
|
583 |
|
|
|
584 |
4185
|
daigle
|
return realPath;
|
585 |
4080
|
daigle
|
}
|
586 |
4712
|
daigle
|
|
587 |
|
|
/**
|
588 |
|
|
* Get the current user's home directory
|
589 |
|
|
*
|
590 |
|
|
* @return a string holding the home directory
|
591 |
|
|
*/
|
592 |
|
|
public static String getUserHomeDir() {
|
593 |
|
|
return System.getProperty("user.home");
|
594 |
|
|
}
|
595 |
4812
|
daigle
|
|
596 |
|
|
/**
|
597 |
|
|
* Get a list of xml paths that need to be indexed
|
598 |
|
|
*/
|
599 |
4854
|
daigle
|
public static Vector<String> getPathsForIndexing() throws MetacatUtilException {
|
600 |
4812
|
daigle
|
Vector <String> indexPaths = null;
|
601 |
|
|
try {
|
602 |
|
|
indexPaths =
|
603 |
|
|
StringUtil.toVector(PropertyService.getProperty("xml.indexPaths"), ',');
|
604 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
605 |
4854
|
daigle
|
throw new MetacatUtilException("could not get index paths: " + pnfe.getMessage());
|
606 |
4812
|
daigle
|
}
|
607 |
|
|
|
608 |
|
|
return indexPaths;
|
609 |
|
|
}
|
610 |
|
|
}
|