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