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.servlets;
006:
007: import org.geoserver.ows.OutputStrategyFactory;
008: import org.geoserver.ows.ServiceStrategy;
009: import org.springframework.beans.BeansException;
010: import org.springframework.context.ApplicationContext;
011: import org.springframework.context.ApplicationContextAware;
012: import org.springframework.web.context.WebApplicationContext;
013: import org.vfny.geoserver.global.GeoServer;
014: import org.vfny.geoserver.util.PartialBufferedOutputStream2;
015: import java.util.Iterator;
016: import java.util.Map;
017: import java.util.logging.Level;
018: import java.util.logging.Logger;
019: import javax.servlet.ServletContext;
020: import javax.servlet.http.HttpServletResponse;
021:
022: public class ServiceStrategyFactory implements OutputStrategyFactory,
023: ApplicationContextAware {
024: /** Class logger */
025: static Logger LOGGER = org.geotools.util.logging.Logging
026: .getLogger("org.vfny.geoserver.servlets");
027:
028: /**
029: * GeoServer configuratoin
030: */
031: GeoServer geoServer;
032:
033: /**
034: * The application context
035: */
036: ApplicationContext context;
037:
038: /**
039: * The default service strategy
040: */
041: String serviceStrategy;
042:
043: /**
044: * The default buffer size when the partial buffer strategy is used
045: */
046: int partialBufferSize = PartialBufferedOutputStream2.DEFAULT_BUFFER_SIZE;
047:
048: public ServiceStrategyFactory(GeoServer geoServer) {
049: this .geoServer = geoServer;
050: }
051:
052: public void setApplicationContext(ApplicationContext context)
053: throws BeansException {
054: this .context = context;
055: }
056:
057: public void setServiceStrategy(String serviceStrategy) {
058: this .serviceStrategy = serviceStrategy;
059: }
060:
061: public void setPartialBufferSize(int partialBufferSize) {
062: this .partialBufferSize = partialBufferSize;
063: }
064:
065: public ServletContext getServletContext() {
066: return ((WebApplicationContext) context).getServletContext();
067: }
068:
069: public ServiceStrategy createOutputStrategy(
070: HttpServletResponse response) {
071: //If verbose exceptions is on then lets make sure they actually get the
072: // exception by using the file strategy.
073: ServiceStrategy theStrategy = null;
074:
075: if (geoServer.isVerboseExceptions()) {
076: theStrategy = (ServiceStrategy) context
077: .getBean("fileServiceStrategy");
078: } else {
079: if (serviceStrategy == null) {
080: // none set, look up in web applicatino context
081: serviceStrategy = getServletContext().getInitParameter(
082: "serviceStrategy");
083: }
084:
085: // do a lookup
086: if (serviceStrategy != null) {
087: theStrategy = (ServiceStrategy) context
088: .getBean(serviceStrategy);
089: }
090: }
091:
092: if (theStrategy == null) {
093: // default to buffer
094: theStrategy = (ServiceStrategy) context
095: .getBean("bufferServiceStrategy");
096: }
097:
098: // clone the strategy since at the moment the strategies are marked as singletons
099: // in the web.xml file.
100: try {
101: theStrategy = (ServiceStrategy) theStrategy.clone();
102: } catch (CloneNotSupportedException e) {
103: LOGGER.log(Level.SEVERE,
104: "Programming error found, service strategies should be cloneable, "
105: + e, e);
106: throw new RuntimeException(
107: "Found a strategy that does not support cloning...",
108: e);
109: }
110:
111: // TODO: this hack should be removed once modules have their own config
112: if (theStrategy instanceof PartialBufferStrategy2) {
113: if (partialBufferSize == 0) {
114: String size = getServletContext().getInitParameter(
115: "PARTIAL_BUFFER_STRATEGY_SIZE");
116:
117: if (size != null) {
118: try {
119: partialBufferSize = Integer.valueOf(size)
120: .intValue();
121:
122: if (partialBufferSize <= 0) {
123: LOGGER
124: .warning("Invalid partial buffer size, defaulting to "
125: + PartialBufferedOutputStream2.DEFAULT_BUFFER_SIZE
126: + " (was "
127: + partialBufferSize + ")");
128: partialBufferSize = 0;
129: }
130: } catch (NumberFormatException nfe) {
131: LOGGER
132: .warning("Invalid partial buffer size, defaulting to "
133: + PartialBufferedOutputStream2.DEFAULT_BUFFER_SIZE
134: + " (was "
135: + partialBufferSize
136: + ")");
137: partialBufferSize = 0;
138: }
139: }
140: }
141:
142: ((PartialBufferStrategy2) theStrategy)
143: .setBufferSize(partialBufferSize);
144: }
145:
146: return theStrategy;
147: }
148: }
|