Project

General

Profile

1
/**
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
import edu.ucsb.nceas.metacat.properties.PropertyService;
37
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
38
import edu.ucsb.nceas.metacat.util.GeoserverUtil;
39
import edu.ucsb.nceas.metacat.util.RequestUtil;
40
import edu.ucsb.nceas.metacat.util.SystemUtil;
41
import edu.ucsb.nceas.utilities.FileUtil;
42
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
public class GeoserverAdmin extends MetacatAdmin {
49

    
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
				// get the current configuration values
96
				String username = PropertyService.getProperty("geoserver.username");
97
				String password = PropertyService.getProperty("geoserver.password");
98
				String context = PropertyService.getProperty("geoserver.context");
99
				String dataDir = PropertyService.getProperty("geoserver.GEOSERVER_DATA_DIR");
100
				String regenerateCache = PropertyService.getProperty("spatial.regenerateCacheOnRestart");
101
				boolean regenerate = false;
102
				if (regenerateCache != null) {
103
					regenerate = Boolean.parseBoolean(regenerateCache);
104
				}
105
				
106
				// 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
				
118
				request.setAttribute("geoserver.username", username);
119
				request.setAttribute("geoserver.password", password);
120
				request.setAttribute("geoserver.context", context);
121
				request.setAttribute("geoserver.GEOSERVER_DATA_DIR", dataDir);
122
				request.setAttribute("spatial.regenerateCacheOnRestart", regenerate);
123

    
124
				// Forward the request to the JSP page
125
				RequestUtil.forwardRequest(request, response,
126
						"/admin/geoserver-configuration.jsp", null);
127
			} catch (GeneralPropertyException gpe) {
128
				throw new AdminException("GeoserverAdmin.configureGeoserver - Problem getting or " + 
129
						"setting property while initializing system properties page: " + gpe.getMessage());
130
			} catch (MetacatUtilException mue) {
131
				throw new AdminException("GeoserverAdmin.configureGeoserver - utility problem while initializing "
132
						+ "system properties page:" + mue.getMessage());
133
			} 
134
		} 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
				String errorMessage = "GeoserverAdmin.configureGeoserver - Problem getting or setting property while "
146
					+ "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
					RequestUtil.forwardRequest(request, response, "/admin", null);
155
				} 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
							"/admin?configureType=configure&processForm=false", null);
162
				}
163
			} catch (MetacatUtilException mue) {
164
				throw new AdminException("GeoserverAdmin.configureGeoserver - utility problem while processing geoservices "
165
						+ "geoservices page: " + mue.getMessage());
166
			} 
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
				String context = (String)request.getParameter("geoserver.context");
189
				String dataDir = (String)request.getParameter("geoserver.GEOSERVER_DATA_DIR");
190
				String regenerateCache = (String)request.getParameter("spatial.regenerateCacheOnRestart");
191
				boolean regenerate = false;
192
				if (regenerateCache != null) {
193
					regenerate = Boolean.parseBoolean(regenerateCache);
194
				}
195

    
196
				
197
				// process the context/data dir
198
				if (context == null || dataDir == null) {
199
					validationErrors.add("Context and Data Directory cannot be null");
200
				} else {
201
					PropertyService.setPropertyNoPersist("geoserver.context", context);
202
					boolean reconfig = PropertyService.checkAndSetProperty(request, "geoserver.GEOSERVER_DATA_DIR");
203
					PropertyService.persistProperties();
204
					// put the web.xml in place
205
					reconfig = true; //force all the time in cases where geoserver has been redeployed
206
					if (reconfig) {
207
						GeoserverUtil.writeConfig();
208
					}
209
				}
210
				
211
				if (username == null || password == null) {
212
					validationErrors.add("User Name and Password cannot be null");
213
				} else {
214
					GeoserverUtil.changePassword(username, password);
215
					PropertyService.setPropertyNoPersist("geoserver.username", username);
216
					PropertyService.setPropertyNoPersist("geoserver.password", password);
217
					PropertyService.setPropertyNoPersist("spatial.regenerateCacheOnRestart", Boolean.toString(regenerate));
218
					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
					PropertyService.persistMainBackupProperties();
224
				}
225
			} catch (MetacatUtilException ue) {
226
				String errorMessage = "GeoserverAdmin.configureGeoserver - Error updating geoserver password: " + ue.getMessage();
227
				logMetacat.error(errorMessage);
228
				processingErrors.add(errorMessage);
229
			} catch (GeneralPropertyException gpe) {
230
				String errorMessage = "GeoserverAdmin.configureGeoserver - Problem getting or setting property while "
231
						+ "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
					RequestUtil.forwardRequest(request, response, "/admin", null);
242
				} 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
							"/admin?configureType=configure&processForm=false", null);
254
				}
255
			} catch (MetacatUtilException mue) {
256
				throw new AdminException("GeoserverAdmin.configureGeoserver - utility problem while processing geoservices "
257
						+ "geoservices page: " + mue.getMessage());
258
			} catch (GeneralPropertyException gpe) {
259
				throw new AdminException("GeoserverAdmin.configureGeoserver - problem with properties while "
260
						+ "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
}
(5-5/10)