View Javadoc

1   /*
2    * $Id: DebugUtils.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util;
12  
13  import java.security.Provider;
14  import java.security.Security;
15  import java.util.HashSet;
16  import java.util.Iterator;
17  import java.util.Set;
18  
19  /**
20   * Useful for enumerating debug information about the current Java environment
21   */
22  // @ThreadSafe
23  public final class DebugUtils
24  {
25  
26      /** Do not instanciate. */
27      private DebugUtils ()
28      {
29          // no-op
30      }
31  
32      /**
33       * @return all available services types
34       */
35      public static String[] listSecurityServiceTypes()
36      {
37          Set result = new HashSet();
38  
39          // All all providers
40          Provider[] providers = Security.getProviders();
41          for (int i = 0; i < providers.length; i++)
42          {
43              // Get services provided by each provider
44              Set keys = providers[i].keySet();
45              for (Iterator it = keys.iterator(); it.hasNext();)
46              {
47                  String key = (String) it.next();
48                  key = key.split(" ")[0];
49  
50                  if (key.startsWith("Alg.Alias."))
51                  {
52                      // Strip the alias
53                      key = key.substring(10);
54                  }
55                  int ix = key.indexOf('.');
56                  result.add(key.substring(0, ix));
57              }
58          }
59          return (String[]) result.toArray(new String[result.size()]);
60      }
61  
62      /**
63       * @return the available implementations for a service type
64       */
65      public static String[] listCryptoImplementations(String serviceType)
66      {
67          Set result = new HashSet();
68  
69          // All all providers
70          Provider[] providers = Security.getProviders();
71          for (int i = 0; i < providers.length; i++)
72          {
73              // Get services provided by each provider
74              Set keys = providers[i].keySet();
75              for (Iterator it = keys.iterator(); it.hasNext();)
76              {
77                  String key = (String) it.next();
78                  key = key.split(" ")[0];
79  
80                  if (key.startsWith(serviceType + "."))
81                  {
82                      result.add(key.substring(serviceType.length() + 1));
83                  }
84                  else if (key.startsWith("Alg.Alias." + serviceType + "."))
85                  {
86                      // This is an alias
87                      result.add(key.substring(serviceType.length() + 11));
88                  }
89              }
90          }
91          return (String[]) result.toArray(new String[result.size()]);
92      }
93  }