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
				// provide a default based on the current installation
101
				if (dataDir == null || dataDir.length() == 0) {
102
					dataDir = 
103
						SystemUtil.getContextDir()
104
						+ FileUtil.getFS()
105
						+ "spatial"
106
						+ FileUtil.getFS()
107
						+ "geoserver"
108
						+ FileUtil.getFS()
109
						+ "data";
110
				}
111
				
112
				request.setAttribute("geoserver.username", username);
113
				request.setAttribute("geoserver.password", password);
114
				request.setAttribute("geoserver.context", context);
115
				request.setAttribute("geoserver.GEOSERVER_DATA_DIR", dataDir);
116

    
117
				// Forward the request to the JSP page
118
				RequestUtil.forwardRequest(request, response,
119
						"/admin/geoserver-configuration.jsp", null);
120
			} catch (GeneralPropertyException gpe) {
121
				throw new AdminException("GeoserverAdmin.configureGeoserver - Problem getting or " + 
122
						"setting property while initializing system properties page: " + gpe.getMessage());
123
			} catch (MetacatUtilException mue) {
124
				throw new AdminException("GeoserverAdmin.configureGeoserver - utility problem while initializing "
125
						+ "system properties page:" + mue.getMessage());
126
			} 
127
		} else if (bypass != null && bypass.equals("true")) {
128
			Vector<String> processingErrors = new Vector<String>();
129
			Vector<String> processingSuccess = new Vector<String>();
130
			
131
			// Bypass the geoserver configuration. This will not keep
132
			// Metacat from running.
133
			try {
134
				PropertyService.setProperty("configutil.geoserverConfigured",
135
						PropertyService.BYPASSED);
136
				
137
			} catch (GeneralPropertyException gpe) {
138
				String errorMessage = "GeoserverAdmin.configureGeoserver - Problem getting or setting property while "
139
					+ "processing system properties page: " + gpe.getMessage();
140
				logMetacat.error(errorMessage);
141
				processingErrors.add(errorMessage);
142
			}
143
			try {
144
				if (processingErrors.size() > 0) {
145
					RequestUtil.clearRequestMessages(request);
146
					RequestUtil.setRequestErrors(request, processingErrors);
147
					RequestUtil.forwardRequest(request, response, "/admin", null);
148
				} else {			
149
					// Reload the main metacat configuration page
150
					processingSuccess.add("Geoserver configuration successfully bypassed");
151
					RequestUtil.clearRequestMessages(request);
152
					RequestUtil.setRequestSuccess(request, processingSuccess);
153
					RequestUtil.forwardRequest(request, response, 
154
							"/admin?configureType=configure&processForm=false", null);
155
				}
156
			} catch (MetacatUtilException mue) {
157
				throw new AdminException("GeoserverAdmin.configureGeoserver - utility problem while processing geoservices "
158
						+ "geoservices page: " + mue.getMessage());
159
			} 
160
		
161
		} else {
162
			// The configuration form is being submitted and needs to be
163
			// processed, setting the properties in the configuration file
164
			// then restart metacat
165

    
166
			// The configuration form is being submitted and needs to be
167
			// processed.
168
			Vector<String> validationErrors = new Vector<String>();
169
			Vector<String> processingErrors = new Vector<String>();
170
			Vector<String> processingSuccess = new Vector<String>();
171

    
172
			try {
173
				// Validate that the options provided are legitimate. Note that
174
				// we've allowed them to persist their entries. As of this point
175
				// there is no other easy way to go back to the configure form
176
				// and preserve their entries.
177
				validationErrors.addAll(validateOptions(request));
178
				
179
				String username = (String)request.getParameter("geoserver.username");
180
				String password = (String)request.getParameter("geoserver.password");
181
				String context = (String)request.getParameter("geoserver.context");
182
				String dataDir = (String)request.getParameter("geoserver.GEOSERVER_DATA_DIR");
183
				
184
				// process the context/data dir
185
				if (context != null && dataDir != null) {
186
					PropertyService.setPropertyNoPersist("geoserver.context", context);
187
					boolean reconfig = PropertyService.checkAndSetProperty(request, "geoserver.GEOSERVER_DATA_DIR");
188
					PropertyService.persistProperties();
189
					// put the web.xml in place
190
					reconfig = true; //force all the time in cases where geoserver has been redeployed
191
					if (reconfig) {
192
						GeoserverUtil.writeConfig();
193
					}
194
				} else {
195
					validationErrors.add("Context and Data Directory cannot be null");
196
				}
197
				
198
				if (username == null || password == null) {
199
					validationErrors.add("User Name and Password cannot be null");
200
				} else {
201
					GeoserverUtil.changePassword(username, password);
202
					PropertyService.setPropertyNoPersist("geoserver.username", username);
203
					PropertyService.setPropertyNoPersist("geoserver.password", password);
204
					PropertyService.persistProperties();
205

    
206
					// write the backup properties to a location outside the
207
					// application directories so they will be available after
208
					// the next upgrade
209
					PropertyService.persistMainBackupProperties();
210
				}
211
			} catch (MetacatUtilException ue) {
212
				String errorMessage = "GeoserverAdmin.configureGeoserver - Error updating geoserver password: " + ue.getMessage();
213
				logMetacat.error(errorMessage);
214
				processingErrors.add(errorMessage);
215
			} catch (GeneralPropertyException gpe) {
216
				String errorMessage = "GeoserverAdmin.configureGeoserver - Problem getting or setting property while "
217
						+ "processing system properties page: " + gpe.getMessage();
218
				logMetacat.error(errorMessage);
219
				processingErrors.add(errorMessage);
220
			}
221

    
222
			try {
223
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
224
					RequestUtil.clearRequestMessages(request);
225
					RequestUtil.setRequestFormErrors(request, validationErrors);
226
					RequestUtil.setRequestErrors(request, processingErrors);
227
					RequestUtil.forwardRequest(request, response, "/admin", null);
228
				} else {
229
					// Now that the options have been set, change the
230
					// 'propertiesConfigured' option to 'true'
231
					PropertyService.setProperty("configutil.geoserverConfigured",
232
							PropertyService.CONFIGURED);
233
					
234
					// Reload the main metacat configuration page
235
					processingSuccess.add("Geoserver successfully configured");
236
					RequestUtil.clearRequestMessages(request);
237
					RequestUtil.setRequestSuccess(request, processingSuccess);
238
					RequestUtil.forwardRequest(request, response, 
239
							"/admin?configureType=configure&processForm=false", null);
240
				}
241
			} catch (MetacatUtilException mue) {
242
				throw new AdminException("GeoserverAdmin.configureGeoserver - utility problem while processing geoservices "
243
						+ "geoservices page: " + mue.getMessage());
244
			} catch (GeneralPropertyException gpe) {
245
				throw new AdminException("GeoserverAdmin.configureGeoserver - problem with properties while "
246
						+ "processing geoservices configuration page: " + gpe.getMessage());
247
			}
248
		}
249
	}
250

    
251
	/**
252
	 * Validate the most important configuration options submitted by the user.
253
	 * 
254
	 * @return a vector holding error message for any fields that fail
255
	 *         validation.
256
	 */
257
	protected Vector<String> validateOptions(HttpServletRequest request) {
258
		Vector<String> errorVector = new Vector<String>();
259

    
260
		// TODO MCD validate options.
261

    
262
		return errorVector;
263
	}
264
}
(5-5/10)