001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.action;
006:
007: import org.apache.struts.action.ActionForm;
008: import org.apache.struts.action.ActionForward;
009: import org.apache.struts.action.ActionMapping;
010: import org.geoserver.wfs.WFS;
011: import org.geotools.validation.xml.XMLReader;
012: import org.vfny.geoserver.config.validation.ValidationConfig;
013: import org.vfny.geoserver.global.ConfigurationException;
014: import org.vfny.geoserver.global.GeoserverDataDirectory;
015: import org.vfny.geoserver.global.UserContainer;
016: import org.vfny.geoserver.global.dto.DataDTO;
017: import org.vfny.geoserver.global.dto.GeoServerDTO;
018: import org.vfny.geoserver.global.dto.WCSDTO;
019: import org.vfny.geoserver.global.dto.WFSDTO;
020: import org.vfny.geoserver.global.dto.WMSDTO;
021: import org.vfny.geoserver.global.xml.XMLConfigReader;
022: import java.io.File;
023: import java.io.IOException;
024: import java.util.Map;
025: import java.util.logging.Level;
026: import javax.servlet.ServletContext;
027: import javax.servlet.ServletException;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: /**
032: * Load GeoServer configuration.
033: *
034: * <p>
035: * The existing getServer instances is updated with a call to load(..) based on
036: * the existing XML configuration files.
037: * </p>
038: *
039: * <p>
040: * It seems this class also creates the GeoServer instance in a lazy fashion!
041: * That would mean that if this class cannot load, the application cannot
042: * load? This could not possibly be the case, because the load action should
043: * only appear when logged in.
044: * </p>
045: *
046: * <p>
047: * Load need to remain on the current page, right now it takes us on a wild
048: * ride back to the welcome screen.
049: * </p>
050: *
051: * <p>
052: * Q: Does this need to load the Validation Processor as well?
053: * </p>
054: * @REVISIT: There seems to be quite a bit of code duplication in this class
055: * with GeoServerPlugIn, loading things, especially with the
056: * validation stuff. Anyway we could cut that down? Have one call
057: * the other? Too close to release to do sucha refactoring but
058: * in 1.4 we should. -CH
059: */
060: public class LoadXMLAction extends ConfigAction {
061: public ActionForward execute(ActionMapping mapping,
062: ActionForm form, UserContainer user,
063: HttpServletRequest request, HttpServletResponse response)
064: throws IOException, ServletException {
065: ActionForward r1 = loadValidation(mapping, form, request,
066: response);
067: ActionForward r2 = loadGeoserver(mapping, form, request,
068: response);
069:
070: getApplicationState().fireChange();
071: return mapping.findForward("config");
072: }
073:
074: private ActionForward loadGeoserver(ActionMapping mapping,
075: ActionForm form,
076: //UserContainer user,
077: HttpServletRequest request, HttpServletResponse response)
078: throws IOException, ServletException {
079: ServletContext sc = request.getSession().getServletContext();
080:
081: WMSDTO wmsDTO = null;
082: WFSDTO wfsDTO = null;
083: WCSDTO wcsDTO = null;
084: GeoServerDTO geoserverDTO = null;
085: DataDTO dataDTO = null;
086:
087: //DJB: changed for geoserver_data_dir
088: // File rootDir = new File(sc.getRealPath("/"));
089: File rootDir = GeoserverDataDirectory
090: .getGeoserverDataDirectory();
091:
092: XMLConfigReader configReader;
093:
094: try {
095: configReader = new XMLConfigReader(rootDir, sc);
096: } catch (ConfigurationException configException) {
097: configException.printStackTrace();
098:
099: return mapping.findForward("welcome");
100:
101: //throw new ServletException( configException );
102: }
103:
104: if (configReader.isInitialized()) {
105: // These are on separate lines so we can tell with the
106: // stack trace/debugger where things go wrong
107: wmsDTO = configReader.getWms();
108: wfsDTO = configReader.getWfs();
109: wcsDTO = configReader.getWcs();
110: geoserverDTO = configReader.getGeoServer();
111: dataDTO = configReader.getData();
112: } else {
113: System.err
114: .println("Config Reader not initialized for LoadXMLAction.execute().");
115:
116: return mapping.findForward("welcome");
117:
118: // throw new ServletException( new ConfigurationException( "An error occured loading the initial configuration" ));
119: }
120:
121: // Update GeoServer
122: try {
123: getWCS(request).load(wcsDTO);
124: getWFS(request).load(wfsDTO);
125: getWMS(request).load(wmsDTO);
126: getWCS(request).getGeoServer().load(geoserverDTO);
127: getWCS(request).getData().load(dataDTO);
128: getWFS(request).getGeoServer().load(geoserverDTO);
129: getWFS(request).getData().load(dataDTO);
130: } catch (ConfigurationException configException) {
131: configException.printStackTrace();
132:
133: return mapping.findForward("welcome");
134:
135: // throw new ServletException( configException );
136: }
137:
138: // Update Config
139: getGlobalConfig().update(geoserverDTO);
140: getDataConfig().update(dataDTO);
141: getWCSConfig().update(wcsDTO);
142: getWFSConfig().update(wfsDTO);
143: getWMSConfig().update(wmsDTO);
144:
145: getApplicationState(request).notifyLoadXML();
146:
147: // We need to stash the current page?
148: // or can we use null or something?
149: //
150: if (LOGGER.isLoggable(Level.FINER)) {
151: LOGGER.finer(new StringBuffer("request: ").append(
152: request.getServletPath()).toString());
153: LOGGER.finer(new StringBuffer("forward: ").append(
154: mapping.getForward()).toString());
155: }
156:
157: return mapping.findForward("config");
158: }
159:
160: private ActionForward loadValidation(ActionMapping mapping,
161: ActionForm form,
162: //UserContainer user,
163: HttpServletRequest request, HttpServletResponse response)
164: throws IOException, ServletException {
165: ServletContext sc = request.getSession().getServletContext();
166:
167: WFS wfs = getWFS(request);
168:
169: if (wfs == null) {
170: // lazy creation on load?
171: loadGeoserver(mapping, form, request, response);
172: }
173:
174: //CH- fixed for data dir, looks like this got missed first time around.
175: File rootDir = GeoserverDataDirectory
176: .getGeoserverDataDirectory();
177:
178: try {
179: File plugInDir = findConfigDir(rootDir, "plugIns");
180: File validationDir = findConfigDir(rootDir, "validation");
181: Map plugIns = XMLReader.loadPlugIns(plugInDir);
182: Map testSuites = XMLReader.loadValidations(validationDir,
183: plugIns);
184: ValidationConfig vc = new ValidationConfig(plugIns,
185: testSuites);
186: sc.setAttribute(ValidationConfig.CONFIG_KEY, vc);
187: } catch (Exception e) {
188: // LOG error
189: e.printStackTrace();
190:
191: return mapping.findForward("config.validation");
192: }
193:
194: return mapping.findForward("config.validation");
195: }
196:
197: private File findConfigDir(File rootDir, String name)
198: throws Exception {
199: return GeoserverDataDirectory.findConfigDir(rootDir, name);
200: }
201: }
|