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: package org.cougaar.lib.web.arch.root;
027:
028: import java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.List;
031: import java.util.Map;
032:
033: import javax.servlet.Servlet;
034: import javax.servlet.ServletConfig;
035: import javax.servlet.ServletException;
036: import javax.servlet.SingleThreadModel;
037:
038: import org.cougaar.lib.web.arch.ServletRegistry;
039: import org.cougaar.lib.web.arch.util.SynchronizedServlet;
040:
041: /**
042: * The {@link ServletRegistry} implementation for the {@link
043: * RootServlet}, which contains the local names and binds/unbinds
044: * these names in the {@link GlobalRegistry}.
045: */
046: public class RootServletRegistry implements ServletRegistry {
047:
048: // Map<String, Servlet>
049: private final Map localServlets;
050:
051: /**
052: * Global registry for all (name, "scheme://host:port") pairs.
053: */
054: private final GlobalRegistry globReg;
055:
056: private ServletConfig config;
057:
058: /**
059: */
060: public RootServletRegistry(GlobalRegistry globReg) {
061: this .globReg = globReg;
062:
063: if (globReg == null) {
064: throw new NullPointerException();
065: }
066:
067: localServlets = new HashMap(13);
068: }
069:
070: /**
071: */
072: public Object get(String name) {
073: synchronized (localServlets) {
074: return localServlets.get(name);
075: }
076: }
077:
078: public List listNames() {
079: synchronized (localServlets) {
080: return new ArrayList(localServlets.keySet());
081: }
082: }
083:
084: public void init(ServletConfig config) {
085: this .config = config;
086: }
087:
088: public ServletConfig getServletConfig() {
089: return config;
090: }
091:
092: /**
093: */
094: public void register(String name, Servlet servlet) {
095: if ((name == null) || (servlet == null)) {
096: throw new NullPointerException();
097: }
098:
099: // wrap if SingleThreadModel
100: if (servlet instanceof SingleThreadModel) {
101: servlet = new SynchronizedServlet(servlet);
102: }
103:
104: // init with config
105: try {
106: servlet.init(config);
107: } catch (ServletException se) {
108: throw new RuntimeException("Unable to initialize servlet: "
109: + se.getMessage());
110: }
111:
112: // register locally
113: synchronized (localServlets) {
114: Object o = localServlets.put(name, servlet);
115: if (o != null) {
116: // un-put! somewhat wasteful
117: localServlets.put(name, o);
118: throw new IllegalArgumentException("Name \"" + name
119: + "\" already in local use");
120: }
121: }
122:
123: // register globally
124: try {
125: globReg.rebind(name);
126: } catch (Exception e) {
127: // release locally
128: synchronized (localServlets) {
129: localServlets.remove(name);
130: }
131: // un-init
132: try {
133: servlet.destroy();
134: } catch (RuntimeException re) {
135: // ignore
136: }
137: throw new RuntimeException("Unable to bind \"" + name
138: + "\" in the global registry", e);
139: }
140: }
141:
142: /**
143: */
144: public boolean unregister(String name) {
145: boolean ret = true;
146:
147: // quick-check locally
148: if (get(name) == null) {
149: return false;
150: }
151:
152: // unregister globally
153: try {
154: globReg.unbind(name);
155: } catch (Exception e) {
156: // ignore
157: ret = false;
158: }
159:
160: // unregister locally
161: Servlet s;
162: synchronized (localServlets) {
163: s = (Servlet) localServlets.remove(name);
164: }
165:
166: // un-init
167: if (s != null) {
168: try {
169: s.destroy();
170: } catch (RuntimeException re) {
171: // ignore
172: }
173: }
174:
175: return ret;
176: }
177:
178: public void unregisterAll() {
179: List l = listNames();
180: for (int i = 0; i < l.size(); i++) {
181: String name = (String) l.get(i);
182: unregister(name);
183: }
184: }
185:
186: }
|