001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 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.lib.web.engine;
028:
029: import java.io.IOException;
030: import java.net.BindException;
031: import java.util.ArrayList;
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Map;
037: import javax.servlet.Servlet;
038: import javax.servlet.ServletException;
039: import org.cougaar.core.component.Component;
040: import org.cougaar.core.component.ServiceBroker;
041: import org.cougaar.core.component.ServiceProvider;
042: import org.cougaar.util.GenericStateModelAdapter;
043:
044: /**
045: * This component advertises the {@link ServletEngineRegistryService}.
046: */
047: public class ServletEngineRegistry extends GenericStateModelAdapter
048: implements Component {
049:
050: private ServiceBroker sb;
051:
052: private ServiceProvider ses_sp;
053: private ServiceProvider sers_sp;
054:
055: private final Object lock = new Object();
056: private List engines = Collections.EMPTY_LIST;
057:
058: private boolean configured;
059: private int httpPort;
060: private int httpsPort;
061: private Map options;
062: private boolean running;
063: private Servlet gateway;
064:
065: public void setServiceBroker(ServiceBroker sb) {
066: this .sb = sb;
067: }
068:
069: public void load() {
070: super .load();
071:
072: // advertise registry
073: final ServletEngineRegistry thiz = this ;
074: final ServletEngineRegistryService sers = new ServletEngineRegistryService() {
075: public void add(ServletEngine engine) {
076: thiz.add(engine);
077: }
078:
079: public void remove(ServletEngine engine) {
080: thiz.remove(engine);
081: }
082: };
083: final Class sers_cl = ServletEngineRegistryService.class;
084: sers_sp = new ServiceProvider() {
085: public Object getService(ServiceBroker sb, Object req,
086: Class scl) {
087: return (sers_cl.isAssignableFrom(scl) ? sers : null);
088: }
089:
090: public void releaseService(ServiceBroker sb, Object req,
091: Class scl, Object svc) {
092: }
093: };
094: sb.addService(sers_cl, sers_sp);
095:
096: // advertise engine
097: final ServletEngineService ses = new ServletEngineService() {
098: public void setGateway(Servlet s) throws ServletException {
099: thiz.setGateway(s);
100: }
101:
102: public Map getNamingEntries() {
103: return thiz.getNamingEntries();
104: }
105: };
106: final Class ses_cl = ServletEngineService.class;
107: ses_sp = new ServiceProvider() {
108: public Object getService(ServiceBroker sb, Object req,
109: Class scl) {
110: return (ses_cl.isAssignableFrom(scl) ? ses : null);
111: }
112:
113: public void releaseService(ServiceBroker sb, Object req,
114: Class scl, Object svc) {
115: }
116: };
117: sb.addService(ses_cl, ses_sp);
118: }
119:
120: public void unload() {
121: // revoke engine
122: if (ses_sp != null) {
123: Class ses_cl = ServletEngineService.class;
124: sb.revokeService(ses_cl, ses_sp);
125: ses_sp = null;
126: }
127:
128: // revoke registry
129: if (sers_sp != null) {
130: Class sers_cl = ServletEngineRegistryService.class;
131: sb.revokeService(sers_cl, sers_sp);
132: sers_sp = null;
133: }
134:
135: super .unload();
136: }
137:
138: //
139: // registry is copy-on-write
140: //
141:
142: private void add(ServletEngine engine) {
143: synchronized (lock) {
144: if (engines.contains(engine)) {
145: return;
146: }
147: List l = new ArrayList(engines);
148: l.add(engine);
149: engines = Collections.unmodifiableList(l);
150: }
151: if (gateway != null) {
152: try {
153: engine.setGateway(gateway);
154: } catch (Exception e) {
155: throw new RuntimeException("SetGateway failed", e);
156: }
157: }
158: }
159:
160: private void remove(ServletEngine engine) {
161: synchronized (lock) {
162: if (!engines.contains(engine)) {
163: return;
164: }
165: List l = new ArrayList(engines);
166: l.remove(engine);
167: engines = Collections.unmodifiableList(l);
168: }
169: }
170:
171: private List getEngines() {
172: synchronized (lock) {
173: return engines;
174: }
175: }
176:
177: //
178: // engine is backed by registry
179: //
180:
181: private void setGateway(Servlet s) throws ServletException {
182: this .gateway = s;
183:
184: // set the gateway in all engines
185: List l = getEngines();
186: for (int i = 0; i < l.size(); i++) {
187: ServletEngine ei = (ServletEngine) l.get(i);
188: ei.setGateway(s);
189: }
190: }
191:
192: private Map getNamingEntries() {
193: // ask all engines for their entries, prefer entries from first-loaded
194: // engines (e.g. if engine A and B both want to advertise "http", we'll
195: // only keep A's entry).
196: Map ret = new HashMap();
197: List l = getEngines();
198: for (int i = 0; i < l.size(); i++) {
199: ServletEngine ei = (ServletEngine) l.get(i);
200: Map m = ei.getNamingEntries();
201: if (m == null || m.isEmpty())
202: continue;
203: for (Iterator iter = m.entrySet().iterator(); iter
204: .hasNext();) {
205: Map.Entry me = (Map.Entry) iter.next();
206: Object key = me.getKey();
207: if (ret.containsKey(key))
208: continue;
209: ret.put(key, me.getValue());
210: }
211: }
212: return Collections.unmodifiableMap(ret);
213: }
214: }
|