001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.servlet;
028:
029: import javax.servlet.Servlet;
030:
031: import org.cougaar.core.component.BindingSite;
032: import org.cougaar.core.component.Component;
033: import org.cougaar.core.component.ServiceBroker;
034: import org.cougaar.core.service.ServletService;
035: import org.cougaar.util.GenericStateModelAdapter;
036:
037: /**
038: * Abstract base-class for a Component that obtains the ServletService
039: * and registers a Servlet.
040: */
041: public abstract class BaseServletComponent extends
042: GenericStateModelAdapter implements Component {
043: // subclasses are free to use both of these:
044: protected BindingSite bindingSite;
045: protected ServiceBroker serviceBroker;
046:
047: // this class handles the "servletService" details:
048: protected ServletService servletService;
049:
050: public BaseServletComponent() {
051: super ();
052: }
053:
054: public void setBindingSite(BindingSite bindingSite) {
055: this .bindingSite = bindingSite;
056: }
057:
058: public void setServiceBroker(ServiceBroker sb) {
059: this .serviceBroker = sb;
060: }
061:
062: /**
063: * Capture the (optional) load-time parameters.
064: * <p>
065: * This is typically a List of Strings.
066: */
067: public void setParameter(Object o) {
068: }
069:
070: public void load() {
071: super .load();
072:
073: // get the servlet service
074: servletService = (ServletService) serviceBroker.getService(
075: this , ServletService.class, null);
076: if (servletService == null) {
077: throw new RuntimeException(
078: "Unable to obtain servlet service");
079: }
080:
081: // get the path for the servlet
082: String path = getPath();
083:
084: // load the servlet instance
085: Servlet servlet = createServlet();
086:
087: // register the servlet
088: if (servlet != null) {
089: try {
090: servletService.register(path, servlet);
091: } catch (Exception e) {
092: throw new RuntimeException(
093: "Unable to register servlet \""
094: + servlet.getClass().getName()
095: + "\" with path \"" + path + "\"", e);
096: }
097: }
098: }
099:
100: public void unload() {
101: super .unload();
102: // release the servlet service, which will automatically
103: // unregister the servlet
104: if (servletService != null) {
105: serviceBroker.releaseService(this , ServletService.class,
106: servletService);
107: }
108: // your subclass should also release its services here!
109: }
110:
111: /**
112: * Get the path for the Servlet's registration.
113: */
114: protected abstract String getPath();
115:
116: /**
117: * Create the Servlet instance.
118: * <p>
119: * This is done within "load()", and is also a good time to
120: * aquire additional services from the "serviceBroker"
121: * (for example, BlackboardService).
122: */
123: protected abstract Servlet createServlet();
124:
125: }
|