001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package javax.rmi.ssl;
020:
021: import java.io.IOException;
022: import java.net.ServerSocket;
023: import java.rmi.server.RMIServerSocketFactory;
024: import java.util.Arrays;
025: import javax.net.ssl.SSLServerSocket;
026: import javax.net.ssl.SSLServerSocketFactory;
027:
028: public class SslRMIServerSocketFactory implements
029: RMIServerSocketFactory {
030:
031: static SSLServerSocketFactory factory;
032:
033: private String[] enabledCipherSuites;
034:
035: private String[] enabledProtocols;
036:
037: private boolean needClientAuth;
038:
039: public SslRMIServerSocketFactory() {
040:
041: if (factory == null) {
042: factory = (SSLServerSocketFactory) SSLServerSocketFactory
043: .getDefault();
044: }
045: }
046:
047: public SslRMIServerSocketFactory(String[] enabledCipherSuites,
048: String[] enabledProtocols, boolean needClientAuth)
049: throws IllegalArgumentException {
050:
051: if (factory == null) {
052: factory = (SSLServerSocketFactory) SSLServerSocketFactory
053: .getDefault();
054: }
055: SSLServerSocket soc = null;
056: try {
057: soc = (SSLServerSocket) factory.createServerSocket();
058: if (enabledProtocols != null) {
059: soc.setEnabledProtocols(enabledProtocols);
060: }
061: if (enabledCipherSuites != null) {
062: soc.setEnabledCipherSuites(enabledCipherSuites);
063: }
064: this .enabledCipherSuites = enabledCipherSuites;
065: this .enabledProtocols = enabledProtocols;
066: this .needClientAuth = needClientAuth;
067: } catch (IOException e) {
068: throw new IllegalArgumentException(e);
069: } finally {
070: try {
071: soc.close();
072: } catch (IOException e) {
073: }
074: }
075:
076: }
077:
078: public final String[] getEnabledCipherSuites() {
079: return enabledCipherSuites;
080: }
081:
082: public final String[] getEnabledProtocols() {
083: return enabledProtocols;
084: }
085:
086: public final boolean getNeedClientAuth() {
087: return needClientAuth;
088: }
089:
090: public ServerSocket createServerSocket(int port) throws IOException {
091: SSLServerSocket soc;
092:
093: soc = (SSLServerSocket) factory.createServerSocket(port);
094: if (enabledProtocols != null) {
095: soc.setEnabledProtocols(enabledProtocols);
096: }
097: if (enabledCipherSuites != null) {
098: soc.setEnabledCipherSuites(enabledCipherSuites);
099: }
100: soc.setNeedClientAuth(needClientAuth);
101: return soc;
102: }
103:
104: public boolean equals(Object obj) {
105:
106: if (obj instanceof SslRMIServerSocketFactory
107: && Arrays.equals(enabledCipherSuites,
108: ((SslRMIServerSocketFactory) obj)
109: .getEnabledCipherSuites())
110: && Arrays.equals(enabledProtocols,
111: ((SslRMIServerSocketFactory) obj)
112: .getEnabledProtocols())
113: && (this .needClientAuth == ((SslRMIServerSocketFactory) obj)
114: .getNeedClientAuth())) {
115: return true;
116: }
117: return false;
118: }
119:
120: public int hashCode() {
121:
122: String hashSting = "javax.rmi.ssl.SslRMIServerSocketFactory"; //$NON-NLS-1$
123: if (enabledCipherSuites != null) {
124: for (int i = 0; i < enabledCipherSuites.length; i++) {
125: hashSting = hashSting + enabledCipherSuites[i];
126: }
127: }
128: if (enabledProtocols != null) {
129: for (int i = 0; i < enabledProtocols.length; i++) {
130: hashSting = hashSting + enabledProtocols[i];
131: }
132: }
133: return hashSting.hashCode();
134: }
135:
136: }
|