1 |
4205
|
daigle
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that implements database configuration 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: daigle $'
|
9 |
|
|
* '$Date: 2008-07-24 13:47:03 -0700 (Thu, 24 Jul 2008) $'
|
10 |
|
|
* '$Revision: 4155 $'
|
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.admin;
|
28 |
|
|
|
29 |
|
|
import java.util.Vector;
|
30 |
|
|
|
31 |
|
|
import javax.servlet.http.HttpServletRequest;
|
32 |
|
|
import javax.servlet.http.HttpServletResponse;
|
33 |
|
|
|
34 |
|
|
import org.apache.log4j.Logger;
|
35 |
|
|
|
36 |
5030
|
daigle
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
37 |
5015
|
daigle
|
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
|
38 |
4205
|
daigle
|
import edu.ucsb.nceas.metacat.util.GeoserverUtil;
|
39 |
|
|
import edu.ucsb.nceas.metacat.util.RequestUtil;
|
40 |
5850
|
leinfelder
|
import edu.ucsb.nceas.metacat.util.SystemUtil;
|
41 |
|
|
import edu.ucsb.nceas.utilities.FileUtil;
|
42 |
4205
|
daigle
|
import edu.ucsb.nceas.utilities.GeneralPropertyException;
|
43 |
|
|
|
44 |
|
|
/**
|
45 |
|
|
* Control the display of the database configuration page and the processing
|
46 |
|
|
* of the configuration values.
|
47 |
|
|
*/
|
48 |
5027
|
daigle
|
public class GeoserverAdmin extends MetacatAdmin {
|
49 |
4205
|
daigle
|
|
50 |
|
|
private static GeoserverAdmin geoserverAdmin = null;
|
51 |
|
|
private Logger logMetacat = Logger.getLogger(GeoserverAdmin.class);
|
52 |
|
|
|
53 |
|
|
/**
|
54 |
|
|
* private constructor since this is a singleton
|
55 |
|
|
*/
|
56 |
|
|
private GeoserverAdmin() throws AdminException {
|
57 |
|
|
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* Get the single instance of GeoserverAdmin.
|
62 |
|
|
*
|
63 |
|
|
* @return the single instance of GeoserverAdmin
|
64 |
|
|
*/
|
65 |
|
|
public static GeoserverAdmin getInstance() throws AdminException {
|
66 |
|
|
if (geoserverAdmin == null) {
|
67 |
|
|
geoserverAdmin = new GeoserverAdmin();
|
68 |
|
|
}
|
69 |
|
|
return geoserverAdmin;
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
/**
|
73 |
|
|
* Handle configuration of the database the first time that Metacat starts
|
74 |
|
|
* or when it is explicitly called. Collect necessary update information
|
75 |
|
|
* from the administrator.
|
76 |
|
|
*
|
77 |
|
|
* @param request
|
78 |
|
|
* the http request information
|
79 |
|
|
* @param response
|
80 |
|
|
* the http response to be sent back to the client
|
81 |
|
|
*/
|
82 |
|
|
public void configureGeoserver(HttpServletRequest request,
|
83 |
|
|
HttpServletResponse response) throws AdminException {
|
84 |
|
|
|
85 |
|
|
String processForm = request.getParameter("processForm");
|
86 |
|
|
String bypass = request.getParameter("bypass");
|
87 |
|
|
String formErrors = (String) request.getAttribute("formErrors");
|
88 |
|
|
|
89 |
|
|
if (processForm == null || !processForm.equals("true") || formErrors != null) {
|
90 |
|
|
// The servlet configuration parameters have not been set, or there
|
91 |
|
|
// were form errors on the last attempt to configure, so redirect to
|
92 |
|
|
// the web form for configuring metacat
|
93 |
|
|
|
94 |
|
|
try {
|
95 |
5850
|
leinfelder
|
// get the current configuration values
|
96 |
4205
|
daigle
|
String username = PropertyService.getProperty("geoserver.username");
|
97 |
|
|
String password = PropertyService.getProperty("geoserver.password");
|
98 |
5847
|
leinfelder
|
String context = PropertyService.getProperty("geoserver.context");
|
99 |
|
|
String dataDir = PropertyService.getProperty("geoserver.GEOSERVER_DATA_DIR");
|
100 |
6126
|
leinfelder
|
String regenerateCache = PropertyService.getProperty("spatial.regenerateCacheOnRestart");
|
101 |
|
|
boolean regenerate = false;
|
102 |
|
|
if (regenerateCache != null) {
|
103 |
|
|
regenerate = Boolean.parseBoolean(regenerateCache);
|
104 |
|
|
}
|
105 |
|
|
|
106 |
5850
|
leinfelder
|
// provide a default based on the current installation
|
107 |
|
|
if (dataDir == null || dataDir.length() == 0) {
|
108 |
|
|
dataDir =
|
109 |
|
|
SystemUtil.getContextDir()
|
110 |
|
|
+ FileUtil.getFS()
|
111 |
|
|
+ "spatial"
|
112 |
|
|
+ FileUtil.getFS()
|
113 |
|
|
+ "geoserver"
|
114 |
|
|
+ FileUtil.getFS()
|
115 |
|
|
+ "data";
|
116 |
|
|
}
|
117 |
4205
|
daigle
|
|
118 |
|
|
request.setAttribute("geoserver.username", username);
|
119 |
|
|
request.setAttribute("geoserver.password", password);
|
120 |
5847
|
leinfelder
|
request.setAttribute("geoserver.context", context);
|
121 |
|
|
request.setAttribute("geoserver.GEOSERVER_DATA_DIR", dataDir);
|
122 |
6126
|
leinfelder
|
request.setAttribute("spatial.regenerateCacheOnRestart", regenerate);
|
123 |
4205
|
daigle
|
|
124 |
|
|
// Forward the request to the JSP page
|
125 |
|
|
RequestUtil.forwardRequest(request, response,
|
126 |
5027
|
daigle
|
"/admin/geoserver-configuration.jsp", null);
|
127 |
4205
|
daigle
|
} catch (GeneralPropertyException gpe) {
|
128 |
5030
|
daigle
|
throw new AdminException("GeoserverAdmin.configureGeoserver - Problem getting or " +
|
129 |
|
|
"setting property while initializing system properties page: " + gpe.getMessage());
|
130 |
5076
|
daigle
|
} catch (MetacatUtilException mue) {
|
131 |
|
|
throw new AdminException("GeoserverAdmin.configureGeoserver - utility problem while initializing "
|
132 |
|
|
+ "system properties page:" + mue.getMessage());
|
133 |
|
|
}
|
134 |
4205
|
daigle
|
} else if (bypass != null && bypass.equals("true")) {
|
135 |
|
|
Vector<String> processingErrors = new Vector<String>();
|
136 |
|
|
Vector<String> processingSuccess = new Vector<String>();
|
137 |
|
|
|
138 |
|
|
// Bypass the geoserver configuration. This will not keep
|
139 |
|
|
// Metacat from running.
|
140 |
|
|
try {
|
141 |
|
|
PropertyService.setProperty("configutil.geoserverConfigured",
|
142 |
|
|
PropertyService.BYPASSED);
|
143 |
|
|
|
144 |
|
|
} catch (GeneralPropertyException gpe) {
|
145 |
5030
|
daigle
|
String errorMessage = "GeoserverAdmin.configureGeoserver - Problem getting or setting property while "
|
146 |
4205
|
daigle
|
+ "processing system properties page: " + gpe.getMessage();
|
147 |
|
|
logMetacat.error(errorMessage);
|
148 |
|
|
processingErrors.add(errorMessage);
|
149 |
|
|
}
|
150 |
|
|
try {
|
151 |
|
|
if (processingErrors.size() > 0) {
|
152 |
|
|
RequestUtil.clearRequestMessages(request);
|
153 |
|
|
RequestUtil.setRequestErrors(request, processingErrors);
|
154 |
5027
|
daigle
|
RequestUtil.forwardRequest(request, response, "/admin", null);
|
155 |
4205
|
daigle
|
} else {
|
156 |
|
|
// Reload the main metacat configuration page
|
157 |
|
|
processingSuccess.add("Geoserver configuration successfully bypassed");
|
158 |
|
|
RequestUtil.clearRequestMessages(request);
|
159 |
|
|
RequestUtil.setRequestSuccess(request, processingSuccess);
|
160 |
|
|
RequestUtil.forwardRequest(request, response,
|
161 |
5027
|
daigle
|
"/admin?configureType=configure&processForm=false", null);
|
162 |
4205
|
daigle
|
}
|
163 |
5076
|
daigle
|
} catch (MetacatUtilException mue) {
|
164 |
|
|
throw new AdminException("GeoserverAdmin.configureGeoserver - utility problem while processing geoservices "
|
165 |
|
|
+ "geoservices page: " + mue.getMessage());
|
166 |
4205
|
daigle
|
}
|
167 |
|
|
|
168 |
|
|
} else {
|
169 |
|
|
// The configuration form is being submitted and needs to be
|
170 |
|
|
// processed, setting the properties in the configuration file
|
171 |
|
|
// then restart metacat
|
172 |
|
|
|
173 |
|
|
// The configuration form is being submitted and needs to be
|
174 |
|
|
// processed.
|
175 |
|
|
Vector<String> validationErrors = new Vector<String>();
|
176 |
|
|
Vector<String> processingErrors = new Vector<String>();
|
177 |
|
|
Vector<String> processingSuccess = new Vector<String>();
|
178 |
|
|
|
179 |
|
|
try {
|
180 |
|
|
// Validate that the options provided are legitimate. Note that
|
181 |
|
|
// we've allowed them to persist their entries. As of this point
|
182 |
|
|
// there is no other easy way to go back to the configure form
|
183 |
|
|
// and preserve their entries.
|
184 |
|
|
validationErrors.addAll(validateOptions(request));
|
185 |
|
|
|
186 |
|
|
String username = (String)request.getParameter("geoserver.username");
|
187 |
|
|
String password = (String)request.getParameter("geoserver.password");
|
188 |
5847
|
leinfelder
|
String context = (String)request.getParameter("geoserver.context");
|
189 |
|
|
String dataDir = (String)request.getParameter("geoserver.GEOSERVER_DATA_DIR");
|
190 |
6126
|
leinfelder
|
String regenerateCache = (String)request.getParameter("spatial.regenerateCacheOnRestart");
|
191 |
|
|
boolean regenerate = false;
|
192 |
|
|
if (regenerateCache != null) {
|
193 |
|
|
regenerate = Boolean.parseBoolean(regenerateCache);
|
194 |
|
|
}
|
195 |
|
|
|
196 |
4205
|
daigle
|
|
197 |
5847
|
leinfelder
|
// process the context/data dir
|
198 |
|
|
if (context != null && dataDir != null) {
|
199 |
|
|
PropertyService.setPropertyNoPersist("geoserver.context", context);
|
200 |
|
|
boolean reconfig = PropertyService.checkAndSetProperty(request, "geoserver.GEOSERVER_DATA_DIR");
|
201 |
|
|
PropertyService.persistProperties();
|
202 |
|
|
// put the web.xml in place
|
203 |
5867
|
leinfelder
|
reconfig = true; //force all the time in cases where geoserver has been redeployed
|
204 |
5847
|
leinfelder
|
if (reconfig) {
|
205 |
|
|
GeoserverUtil.writeConfig();
|
206 |
|
|
}
|
207 |
|
|
} else {
|
208 |
|
|
validationErrors.add("Context and Data Directory cannot be null");
|
209 |
|
|
}
|
210 |
|
|
|
211 |
4205
|
daigle
|
if (username == null || password == null) {
|
212 |
5847
|
leinfelder
|
validationErrors.add("User Name and Password cannot be null");
|
213 |
4205
|
daigle
|
} else {
|
214 |
5847
|
leinfelder
|
GeoserverUtil.changePassword(username, password);
|
215 |
4205
|
daigle
|
PropertyService.setPropertyNoPersist("geoserver.username", username);
|
216 |
|
|
PropertyService.setPropertyNoPersist("geoserver.password", password);
|
217 |
6126
|
leinfelder
|
PropertyService.setPropertyNoPersist("spatial.regenerateCacheOnRestart", Boolean.toString(regenerate));
|
218 |
4205
|
daigle
|
PropertyService.persistProperties();
|
219 |
|
|
|
220 |
|
|
// write the backup properties to a location outside the
|
221 |
|
|
// application directories so they will be available after
|
222 |
|
|
// the next upgrade
|
223 |
5028
|
daigle
|
PropertyService.persistMainBackupProperties();
|
224 |
4205
|
daigle
|
}
|
225 |
4854
|
daigle
|
} catch (MetacatUtilException ue) {
|
226 |
5030
|
daigle
|
String errorMessage = "GeoserverAdmin.configureGeoserver - Error updating geoserver password: " + ue.getMessage();
|
227 |
4205
|
daigle
|
logMetacat.error(errorMessage);
|
228 |
|
|
processingErrors.add(errorMessage);
|
229 |
|
|
} catch (GeneralPropertyException gpe) {
|
230 |
5030
|
daigle
|
String errorMessage = "GeoserverAdmin.configureGeoserver - Problem getting or setting property while "
|
231 |
4205
|
daigle
|
+ "processing system properties page: " + gpe.getMessage();
|
232 |
|
|
logMetacat.error(errorMessage);
|
233 |
|
|
processingErrors.add(errorMessage);
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
try {
|
237 |
|
|
if (validationErrors.size() > 0 || processingErrors.size() > 0) {
|
238 |
|
|
RequestUtil.clearRequestMessages(request);
|
239 |
|
|
RequestUtil.setRequestFormErrors(request, validationErrors);
|
240 |
|
|
RequestUtil.setRequestErrors(request, processingErrors);
|
241 |
5027
|
daigle
|
RequestUtil.forwardRequest(request, response, "/admin", null);
|
242 |
4205
|
daigle
|
} else {
|
243 |
|
|
// Now that the options have been set, change the
|
244 |
|
|
// 'propertiesConfigured' option to 'true'
|
245 |
|
|
PropertyService.setProperty("configutil.geoserverConfigured",
|
246 |
|
|
PropertyService.CONFIGURED);
|
247 |
|
|
|
248 |
|
|
// Reload the main metacat configuration page
|
249 |
|
|
processingSuccess.add("Geoserver successfully configured");
|
250 |
|
|
RequestUtil.clearRequestMessages(request);
|
251 |
|
|
RequestUtil.setRequestSuccess(request, processingSuccess);
|
252 |
|
|
RequestUtil.forwardRequest(request, response,
|
253 |
5027
|
daigle
|
"/admin?configureType=configure&processForm=false", null);
|
254 |
4205
|
daigle
|
}
|
255 |
5076
|
daigle
|
} catch (MetacatUtilException mue) {
|
256 |
|
|
throw new AdminException("GeoserverAdmin.configureGeoserver - utility problem while processing geoservices "
|
257 |
|
|
+ "geoservices page: " + mue.getMessage());
|
258 |
4205
|
daigle
|
} catch (GeneralPropertyException gpe) {
|
259 |
5030
|
daigle
|
throw new AdminException("GeoserverAdmin.configureGeoserver - problem with properties while "
|
260 |
4205
|
daigle
|
+ "processing geoservices configuration page: " + gpe.getMessage());
|
261 |
|
|
}
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
/**
|
266 |
|
|
* Validate the most important configuration options submitted by the user.
|
267 |
|
|
*
|
268 |
|
|
* @return a vector holding error message for any fields that fail
|
269 |
|
|
* validation.
|
270 |
|
|
*/
|
271 |
|
|
protected Vector<String> validateOptions(HttpServletRequest request) {
|
272 |
|
|
Vector<String> errorVector = new Vector<String>();
|
273 |
|
|
|
274 |
|
|
// TODO MCD validate options.
|
275 |
|
|
|
276 |
|
|
return errorVector;
|
277 |
|
|
}
|
278 |
|
|
}
|