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.global;
006:
007: import java.io.File;
008: import java.io.FileNotFoundException;
009: import java.util.logging.Logger;
010:
011: import javax.servlet.ServletContext;
012:
013: import org.geoserver.util.ReaderUtils;
014: import org.geotools.factory.Hints;
015: import org.springframework.beans.BeansException;
016: import org.springframework.beans.factory.BeanInitializationException;
017: import org.springframework.context.ApplicationContext;
018: import org.springframework.context.ApplicationContextAware;
019: import org.springframework.web.context.WebApplicationContext;
020: import org.vfny.geoserver.global.dto.DataDTO;
021: import org.vfny.geoserver.global.dto.GeoServerDTO;
022: import org.vfny.geoserver.global.dto.WCSDTO;
023: import org.vfny.geoserver.global.dto.WFSDTO;
024: import org.vfny.geoserver.global.dto.WMSDTO;
025: import org.vfny.geoserver.global.xml.XMLConfigReader;
026:
027: /**
028: * The application configuratoin facade.
029: *
030: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
031: *
032: */
033: public class Config implements ApplicationContextAware {
034: protected static final Logger LOGGER = org.geotools.util.logging.Logging
035: .getLogger("org.vfny.geoserver.global");
036: WebApplicationContext context;
037: XMLConfigReader reader;
038:
039: // public XMLConfigReader getXMLReader() throws ConfigurationException {
040: // return reader;
041: // }
042:
043: public File dataDirectory() {
044: return GeoserverDataDirectory.getGeoserverDataDirectory();
045: }
046:
047: public void setApplicationContext(ApplicationContext context)
048: throws BeansException {
049:
050: this .context = (WebApplicationContext) context;
051:
052: ServletContext sc = this .context.getServletContext();
053:
054: try {
055: GeoserverDataDirectory.init(this .context);
056: //before proceeding perform some configuration sanity checks
057: try {
058: doConfigSanityCheck();
059: } catch (ConfigurationException ce) {
060: LOGGER.severe(ce.getMessage());
061: throw new BeanInitializationException(ce.getMessage());
062: }
063:
064: reader = new XMLConfigReader(dataDirectory(), sc);
065: } catch (ConfigurationException e) {
066: String msg = "Error creating xml config reader";
067: throw new BeanInitializationException(msg, e);
068: }
069: }
070:
071: /**
072: * Performs a sanity check on the configuration files to allow for
073: * the early failure of the bean initialization and providing
074: * a meaningful failure message.
075: * <p>
076: * For the time being, it only ensures that the data dir exists.
077: * </p>
078: */
079: private void doConfigSanityCheck() throws ConfigurationException {
080: File dataDirectory = dataDirectory();
081: try {
082: ReaderUtils.checkFile(dataDirectory, true);
083: } catch (FileNotFoundException e) {
084: throw new ConfigurationException(
085: "Can't access the configuration directory. Reason: "
086: + e.getMessage());
087: }
088: }
089:
090: public WebApplicationContext getApplictionContext() {
091: return context;
092: }
093:
094: public DataDTO getData() {
095: return reader.getData();
096: }
097:
098: public GeoServerDTO getGeoServer() {
099: return reader.getGeoServer();
100: }
101:
102: public WMSDTO getWms() {
103: return reader.getWms();
104: }
105:
106: public WFSDTO getWfs() {
107: return reader.getWfs();
108: }
109:
110: public WCSDTO getWcs() {
111: return reader.getWcs();
112: }
113: }
|