KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > ssl > UnifiedX509KeyManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.security.ssl;
24
25 import java.net.Socket JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.security.Principal JavaDoc;
30 import java.security.PrivateKey JavaDoc;
31 import java.security.cert.X509Certificate JavaDoc;
32 import javax.net.ssl.X509KeyManager;
33
34 /**
35  * This class combines an array of X509KeyManagers into one.
36  * @author Shing Wai Chan
37  **/

38 public class UnifiedX509KeyManager implements X509KeyManager {
39     private X509KeyManager[] mgrs = null;
40     private String JavaDoc[] tokenNames = null;
41
42     /**
43      * @param mgrs
44      * @param tokenNames Array of tokenNames with order corresponding to mgrs
45      */

46     public UnifiedX509KeyManager(X509KeyManager[] mgrs, String JavaDoc[] tokenNames) {
47         if (mgrs == null || tokenNames == null) {
48             throw new IllegalArgumentException JavaDoc("Null array of X509KeyManagers or tokenNames");
49         }
50         if (mgrs.length != tokenNames.length) {
51             throw new IllegalArgumentException JavaDoc("Size of X509KeyManagers array and tokenNames array do not match.");
52         }
53         this.mgrs = mgrs;
54         this.tokenNames = tokenNames;
55     }
56
57     // ---------- implements X509KeyManager ----------
58
public String JavaDoc chooseClientAlias(String JavaDoc[] keyType, Principal JavaDoc[] issuers,
59             Socket JavaDoc socket) {
60         String JavaDoc alias = null;
61         for (int i = 0; i < mgrs.length; i++) {
62             alias = mgrs[i].chooseClientAlias(keyType, issuers, socket);
63             if (alias != null) {
64                 break;
65             }
66         }
67         return alias;
68     }
69
70     public String JavaDoc chooseServerAlias(String JavaDoc keyType, Principal JavaDoc[] issuers,
71             Socket JavaDoc socket) {
72         String JavaDoc alias = null;
73         for (int i = 0; i < mgrs.length; i++) {
74             alias = mgrs[i].chooseServerAlias(keyType, issuers, socket);
75             if (alias != null) {
76                 break;
77             }
78         }
79         return alias;
80     }
81
82     public X509Certificate JavaDoc[] getCertificateChain(String JavaDoc alias) {
83         X509Certificate JavaDoc[] chain = null;
84         for (int i = 0; i < mgrs.length; i++) {
85             chain = mgrs[i].getCertificateChain(alias);
86             if (chain != null) {
87                 break;
88             }
89         }
90         return chain;
91     }
92
93     public String JavaDoc[] getClientAliases(String JavaDoc keyType, Principal JavaDoc[] issuers) {
94         ArrayList JavaDoc clientAliases = new ArrayList JavaDoc();
95         for (int i = 0; i < mgrs.length; i++) {
96             String JavaDoc[] clAliases = mgrs[i].getClientAliases(keyType, issuers);
97             if (clAliases != null && clAliases.length > 0) {
98                 for (int j = 0; j < clAliases.length; j++) {
99                     clientAliases.add(clAliases[j]);
100                 }
101             }
102         }
103
104         return (clientAliases.size() == 0) ? null :
105             (String JavaDoc[])clientAliases.toArray(new String JavaDoc[clientAliases.size()]);
106     }
107
108     public PrivateKey JavaDoc getPrivateKey(String JavaDoc alias) {
109         PrivateKey JavaDoc privKey = null;
110         for (int i = 0; i < mgrs.length; i++) {
111             privKey = mgrs[i].getPrivateKey(alias);
112             if (privKey != null) {
113                 break;
114             }
115         }
116         return privKey;
117     }
118
119     public String JavaDoc[] getServerAliases(String JavaDoc keyType, Principal JavaDoc[] issuers) {
120         ArrayList JavaDoc serverAliases = new ArrayList JavaDoc();
121         for (int i = 0; i < mgrs.length; i++) {
122             String JavaDoc[] serAliases = mgrs[i].getClientAliases(keyType, issuers);
123             if (serAliases != null && serAliases.length > 0) {
124                 for (int j = 0; j < serAliases.length; j++) {
125                     serverAliases.add(serAliases[j]);
126                 }
127             }
128         }
129
130         return (serverAliases.size() == 0) ? null :
131             (String JavaDoc[])serverAliases.toArray(new String JavaDoc[serverAliases.size()]);
132     }
133
134     // ---------- end of implements X509KeyManager ----------
135

136     X509KeyManager[] getX509KeyManagers() {
137         X509KeyManager[] kmgrs = new X509KeyManager[mgrs.length];
138         System.arraycopy(mgrs, 0, kmgrs, 0, mgrs.length);
139         return kmgrs;
140     }
141
142     String JavaDoc[] getTokenNames() {
143         String JavaDoc[] tokens = new String JavaDoc[tokenNames.length];
144         System.arraycopy(tokenNames, 0, tokens, 0, tokenNames.length);
145         return tokens;
146     }
147 }
148
Popular Tags