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.utilities.GeneralPropertyException;
41

    
42
/**
43
 * Control the display of the database configuration page and the processing
44
 * of the configuration values.
45
 */
46
public class GeoserverAdmin extends MetacatAdmin {
47

    
48
	private static GeoserverAdmin geoserverAdmin = null;
49
	private Logger logMetacat = Logger.getLogger(GeoserverAdmin.class);
50

    
51
	/**
52
	 * private constructor since this is a singleton
53
	 */
54
	private GeoserverAdmin() throws AdminException {
55

    
56
	}
57

    
58
	/**
59
	 * Get the single instance of GeoserverAdmin.
60
	 * 
61
	 * @return the single instance of GeoserverAdmin
62
	 */
63
	public static GeoserverAdmin getInstance() throws AdminException {
64
		if (geoserverAdmin == null) {
65
			geoserverAdmin = new GeoserverAdmin();
66
		}
67
		return geoserverAdmin;
68
	}
69

    
70
	/**
71
	 * Handle configuration of the database the first time that Metacat starts
72
	 * or when it is explicitly called. Collect necessary update information
73
	 * from the administrator.
74
	 * 
75
	 * @param request
76
	 *            the http request information
77
	 * @param response
78
	 *            the http response to be sent back to the client
79
	 */
80
	public void configureGeoserver(HttpServletRequest request,
81
			HttpServletResponse response) throws AdminException {
82

    
83
		String processForm = request.getParameter("processForm");
84
		String bypass = request.getParameter("bypass");
85
		String formErrors = (String) request.getAttribute("formErrors");
86

    
87
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
88
			// The servlet configuration parameters have not been set, or there
89
			// were form errors on the last attempt to configure, so redirect to
90
			// the web form for configuring metacat
91

    
92
			try {
93
				// get the current metacat version and the database version. If
94
				// the database version is older that the metacat version, run
95
				// the appropriate scripts to get them synchronized.
96

    
97
				String username = PropertyService.getProperty("geoserver.username");
98
				String password = PropertyService.getProperty("geoserver.password");
99
				String context = PropertyService.getProperty("geoserver.context");
100
				String dataDir = PropertyService.getProperty("geoserver.GEOSERVER_DATA_DIR");
101
				
102
				request.setAttribute("geoserver.username", username);
103
				request.setAttribute("geoserver.password", password);
104
				request.setAttribute("geoserver.context", context);
105
				request.setAttribute("geoserver.GEOSERVER_DATA_DIR", dataDir);
106

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

    
156
			// The configuration form is being submitted and needs to be
157
			// processed.
158
			Vector<String> validationErrors = new Vector<String>();
159
			Vector<String> processingErrors = new Vector<String>();
160
			Vector<String> processingSuccess = new Vector<String>();
161

    
162
			try {
163
				// Validate that the options provided are legitimate. Note that
164
				// we've allowed them to persist their entries. As of this point
165
				// there is no other easy way to go back to the configure form
166
				// and preserve their entries.
167
				validationErrors.addAll(validateOptions(request));
168
				
169
				String username = (String)request.getParameter("geoserver.username");
170
				String password = (String)request.getParameter("geoserver.password");
171
				String context = (String)request.getParameter("geoserver.context");
172
				String dataDir = (String)request.getParameter("geoserver.GEOSERVER_DATA_DIR");
173
				
174
				// process the context/data dir
175
				if (context != null && dataDir != null) {
176
					PropertyService.setPropertyNoPersist("geoserver.context", context);
177
					boolean reconfig = PropertyService.checkAndSetProperty(request, "geoserver.GEOSERVER_DATA_DIR");
178
					PropertyService.persistProperties();
179
					// put the web.xml in place
180
					if (reconfig) {
181
						GeoserverUtil.writeConfig();
182
					}
183
				} else {
184
					validationErrors.add("Context and Data Directory cannot be null");
185
				}
186
				
187
				if (username == null || password == null) {
188
					validationErrors.add("User Name and Password cannot be null");
189
				} else {
190
					GeoserverUtil.changePassword(username, password);
191
					PropertyService.setPropertyNoPersist("geoserver.username", username);
192
					PropertyService.setPropertyNoPersist("geoserver.password", password);
193
					PropertyService.persistProperties();
194

    
195
					// write the backup properties to a location outside the
196
					// application directories so they will be available after
197
					// the next upgrade
198
					PropertyService.persistMainBackupProperties();
199
				}
200
			} catch (MetacatUtilException ue) {
201
				String errorMessage = "GeoserverAdmin.configureGeoserver - Error updating geoserver password: " + ue.getMessage();
202
				logMetacat.error(errorMessage);
203
				processingErrors.add(errorMessage);
204
			} catch (GeneralPropertyException gpe) {
205
				String errorMessage = "GeoserverAdmin.configureGeoserver - Problem getting or setting property while "
206
						+ "processing system properties page: " + gpe.getMessage();
207
				logMetacat.error(errorMessage);
208
				processingErrors.add(errorMessage);
209
			}
210

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

    
240
	/**
241
	 * Validate the most important configuration options submitted by the user.
242
	 * 
243
	 * @return a vector holding error message for any fields that fail
244
	 *         validation.
245
	 */
246
	protected Vector<String> validateOptions(HttpServletRequest request) {
247
		Vector<String> errorVector = new Vector<String>();
248

    
249
		// TODO MCD validate options.
250

    
251
		return errorVector;
252
	}
253
}
(5-5/10)