001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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.mts.base;
028:
029: import java.rmi.server.RMISocketFactory;
030: import java.util.Map;
031:
032: import javax.net.ssl.SSLServerSocketFactory;
033: import javax.net.ssl.SSLSocketFactory;
034:
035: import org.cougaar.core.component.ComponentSupport;
036: import org.cougaar.core.component.ServiceBroker;
037: import org.cougaar.core.component.ServiceProvider;
038: import org.cougaar.core.node.NodeControlService;
039: import org.cougaar.core.service.SocketFactoryService;
040:
041: /**
042: * Provide SocketFactoryService via the std (aspected) MTS for the rest of the world.
043: */
044:
045: public final class SocketFactorySPC extends ComponentSupport {
046: private SFSP _sfsp;
047: private SFS _sfs;
048: private ServiceBroker rootsb;
049:
050: public void load() {
051: super .load();
052: // get root service broker
053: ServiceBroker sb = getServiceBroker();
054: NodeControlService ncs = (NodeControlService) sb.getService(
055: this , NodeControlService.class, null);
056: if (ncs == null) {
057: throw new RuntimeException(
058: "Unable to obtain NodeControlService");
059: }
060: rootsb = ncs.getRootServiceBroker();
061: sb.releaseService(this , NodeControlService.class, ncs);
062: // advertise service
063: _sfsp = new SFSP();
064: _sfs = new SFS();
065: rootsb.addService(SocketFactoryService.class, _sfsp);
066: }
067:
068: public void unload() {
069: if (rootsb != null) {
070: rootsb.revokeService(SocketFactoryService.class, _sfsp);
071: }
072: _sfsp = null;
073: _sfs = null;
074: super .unload();
075: }
076:
077: private class SFSP implements ServiceProvider {
078: public Object getService(ServiceBroker sb, Object requestor,
079: Class serviceClass) {
080: if (serviceClass == SocketFactoryService.class) {
081: return _sfs;
082: }
083: return null;
084: }
085:
086: public void releaseService(ServiceBroker sb, Object requestor,
087: Class serviceClass, Object service) {
088: // no need to do anything
089: }
090: }
091:
092: private class SFS implements SocketFactoryService {
093: /** Get an appropriate SocketFactory instance.
094: * the return value is typed Object because RMISocketFactory and SSLSocketFactory
095: * do not otherwise share a superclass.
096: * Implementations will generally support SSLSocketFactory, SSLServerSocketFactory, and RMISocketFactory.
097: * RMISocketFactory may be parameterized (via the second argument) with "ssl"=Boolean (default FALSE) and
098: * "aspects"=Boolean (default FALSE).
099: * <p>
100: * Example:<br>
101: * <code>
102: * Map params = new HashMap(); params.put("ssl", Boolean.TRUE);<br>
103: * RMISocketFactory rsf = (RMISocketFactory) socketFactoryService.getSocketFactory(RMISocketFactory.class, params);<br>
104: * </code>
105: * @param clazz Specifies the class required. If the class cannot be supported by
106: * the service, it will return null.
107: * @param m Allows arbitrary preferences and parameters to be specified.
108: * @return an object which is instanceof the requested class or null.
109: **/
110: public Object getSocketFactory(Class clazz, Map m) {
111: if (clazz == SSLSocketFactory.class) {
112: return SocketFactory.getSSLSocketFactory();
113: } else if (clazz == SSLServerSocketFactory.class) {
114: return SocketFactory.getSSLServerSocketFactory();
115: } else if (clazz == RMISocketFactory.class) {
116: boolean sslp = (m.get("ssl") == Boolean.TRUE);
117: boolean aspects = (m.get("aspects") == Boolean.TRUE);
118: return new SocketFactory(sslp, aspects);
119: } else {
120: return null;
121: }
122: }
123: }
124: }
|