|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2003, 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.equinox.internal.security.ui; |
| 12 |
|
| 13 |
import java.io.PrintWriter; |
| 14 |
import java.security.Provider; |
| 15 |
import java.security.Security; |
| 16 |
import java.util.*; |
| 17 |
import org.eclipse.ui.about.ISystemSummarySection; |
| 18 |
|
| 19 |
public class SecuritySummary implements ISystemSummarySection { |
| 20 |
|
| 21 |
private static final String ALG_ALIAS = "Alg.Alias."; //$NON-NLS-1$ |
| 22 |
private static final String PROVIDER = "Provider."; //$NON-NLS-1$ |
| 23 |
|
| 24 |
public void write(PrintWriter writer) { |
| 25 |
|
| 26 |
Provider[] providers = Security.getProviders(); |
| 27 |
writer.println("Installed providers (" + providers.length + "): "); |
| 28 |
writer.println(); |
| 29 |
|
| 30 |
for (int i = 0; i < providers.length; i++) { |
| 31 |
appendProvider(writer, providers[i], i); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
private void appendProvider(PrintWriter writer, Provider provider, int index) { |
| 36 |
writer.println(" Provider: " + provider.getName() + ", Version: " + provider.getVersion() + ", Class: " + provider.getClass().getName()); |
| 37 |
writer.println(" Description: " + provider.getInfo()); |
| 38 |
ProviderService[] services = getServices(provider); |
| 39 |
writer.println(" Available services (" + services.length + "):"); |
| 40 |
for (int i = 0; i < services.length; i++) { |
| 41 |
appendService(writer, services[i], i); |
| 42 |
} |
| 43 |
writer.println(); |
| 44 |
} |
| 45 |
|
| 46 |
private void appendService(PrintWriter writer, ProviderService service, int index) { |
| 47 |
writer.println(" Service: " + service.getType() + ", Algorithm: " + service.getAlgorithm() + ", Class: " + service.getClassName()); |
| 48 |
List aliases = service.getAliases(); |
| 49 |
if (null != aliases && (0 < aliases.size())) { |
| 50 |
writer.print(" Aliases: "); |
| 51 |
for (Iterator it = aliases.iterator(); it.hasNext();) { |
| 52 |
writer.print((String) it.next()); |
| 53 |
if (it.hasNext()) { |
| 54 |
writer.print(", "); |
| 55 |
} |
| 56 |
} |
| 57 |
writer.println(); |
| 58 |
} |
| 59 |
Map attributes = service.getAttributes(); |
| 60 |
if ((null != attributes) && (0 < attributes.size())) { |
| 61 |
writer.println(" Attributes:"); |
| 62 |
Set keys = attributes.keySet(); |
| 63 |
for (Iterator it = keys.iterator(); it.hasNext();) { |
| 64 |
String key = (String) it.next(); |
| 65 |
writer.print(" " + key + ": "); |
| 66 |
writer.println((String) attributes.get(key)); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
private static ProviderService[] getServices(Provider provider) { |
| 72 |
|
| 73 |
Set providerKeys = provider.keySet(); |
| 74 |
Hashtable serviceList = new Hashtable(); |
| 75 |
Hashtable attributeMap = new Hashtable(); // "type" => "Hashtable of (attribute,value) pairs" |
| 76 |
Hashtable aliasMap = new Hashtable(); // "type" => "Arraylist of aliases" |
| 77 |
for (Iterator it = providerKeys.iterator(); it.hasNext();) { |
| 78 |
String key = (String) it.next(); |
| 79 |
|
| 80 |
// this is provider info, available off the Provider API |
| 81 |
if (key.startsWith(PROVIDER)) { |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
// this is an alias |
| 86 |
if (key.startsWith(ALG_ALIAS)) { |
| 87 |
String value = key.substring(key.indexOf(ALG_ALIAS) + ALG_ALIAS.length(), key.length()); |
| 88 |
String type = (String) provider.get(key); |
| 89 |
String algo = value.substring(0, value.indexOf('.')); |
| 90 |
String alias = value.substring(value.indexOf('.') + 1, value.length()); |
| 91 |
ArrayList aliasList = (ArrayList) aliasMap.get(type + '.' + algo); |
| 92 |
if (aliasList == null) { |
| 93 |
aliasList = new ArrayList(); |
| 94 |
aliasList.add(alias); |
| 95 |
aliasMap.put(type, aliasList); |
| 96 |
} else { |
| 97 |
aliasList.add(alias); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// this is an attribute |
| 102 |
else if (key.indexOf(' ') > -1) { |
| 103 |
String type = key.substring(0, key.indexOf('.')); |
| 104 |
String algorithm = key.substring(key.indexOf('.') + 1, key.indexOf(' ')); |
| 105 |
String attribute = key.substring(key.indexOf(' ') + 1, key.length()); |
| 106 |
String value = (String) provider.get(key); |
| 107 |
Hashtable attributeTable = (Hashtable) attributeMap.get(type + '.' + algorithm); |
| 108 |
if (attributeTable == null) { |
| 109 |
attributeTable = new Hashtable(); |
| 110 |
attributeTable.put(attribute, value); |
| 111 |
attributeMap.put(type + '.' + algorithm, attributeTable); |
| 112 |
} else { |
| 113 |
attributeTable.put(attribute, value); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// else this is a service |
| 118 |
else { |
| 119 |
serviceList.put(key, provider.get(key)); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
ProviderService[] serviceArray = new ProviderService[serviceList.size()]; |
| 124 |
Set serviceKeys = serviceList.keySet(); |
| 125 |
int serviceCount = 0; |
| 126 |
for (Iterator it = serviceKeys.iterator(); it.hasNext();) { |
| 127 |
String key = (String) it.next(); |
| 128 |
String type = key.substring(0, key.indexOf('.')); |
| 129 |
String algo = key.substring(key.indexOf('.') + 1, key.length()); |
| 130 |
String className = (String) serviceList.get(key); |
| 131 |
List aliases = (List) aliasMap.get(algo); |
| 132 |
Map attributes = (Map) attributeMap.get(key); |
| 133 |
|
| 134 |
serviceArray[serviceCount] = new ProviderService(type, algo, className, aliases, attributes); |
| 135 |
serviceCount++; |
| 136 |
} |
| 137 |
return serviceArray; |
| 138 |
} |
| 139 |
|
| 140 |
private static class ProviderService { |
| 141 |
|
| 142 |
private final String type; |
| 143 |
private final String algorithm; |
| 144 |
private final String className; |
| 145 |
private final List aliases; |
| 146 |
private final Map attributes; |
| 147 |
|
| 148 |
public ProviderService(String type, String algorithm, String className, List aliases, Map attributes) { |
| 149 |
this.type = type; |
| 150 |
this.algorithm = algorithm; |
| 151 |
this.className = className; |
| 152 |
this.aliases = aliases; |
| 153 |
this.attributes = attributes; |
| 154 |
} |
| 155 |
|
| 156 |
public String getType() { |
| 157 |
return type; |
| 158 |
} |
| 159 |
|
| 160 |
public String getAlgorithm() { |
| 161 |
return algorithm; |
| 162 |
} |
| 163 |
|
| 164 |
public String getClassName() { |
| 165 |
return className; |
| 166 |
} |
| 167 |
|
| 168 |
public List getAliases() { |
| 169 |
return aliases; |
| 170 |
} |
| 171 |
|
| 172 |
public Map getAttributes() { |
| 173 |
return attributes; |
| 174 |
} |
| 175 |
} |
| 176 |
} |