KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > remoting > connector > ConnectorFactoryUtil


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9 package org.jboss.mx.remoting.connector;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17 import javax.management.remote.JMXProviderException JavaDoc;
18
19 /**
20  * This is a help class for the JMXConnectorFactory and JMXConnectorServerFactory. They both have the same requirements
21  * in regards to processing JMXServiceURL, so just putting the code in one place instead of the same code in both of the
22  * classes.
23  *
24  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
25  */

26 public class ConnectorFactoryUtil
27 {
28    private ConnectorFactoryUtil()
29    {
30       // need to only have public static methods
31
}
32
33    public static String JavaDoc normalizeProtocol(String JavaDoc protocol)
34    {
35       // convert specical characters per spec.
36
protocol = protocol.replace('+', '.');
37       protocol = protocol.replace('-', '_');
38       return protocol;
39    }
40
41    public static void validateEnvironmentMap(Map JavaDoc environment)
42    {
43       // Per spec (javadoc), all the keys in the environment map must be Strings.
44
// In order to be totally compliant, will check for this, but really just slows things down.
45
Set JavaDoc keys = environment.keySet();
46       Iterator JavaDoc itr = keys.iterator();
47       while(itr.hasNext())
48       {
49          Object JavaDoc keyValue = null;
50          try
51          {
52             keyValue = itr.next();
53             String JavaDoc keyAsString = (String JavaDoc) keyValue;
54          }
55          catch(ClassCastException JavaDoc e)
56          {
57             throw new IllegalArgumentException JavaDoc("All keys within environment map must be of type java.lang.String. " +
58                                                keyValue + " is of type " + keyValue.getClass().getName());
59          }
60       }
61    }
62
63    public static ClassLoader JavaDoc locateClassLoader(Map JavaDoc environment, String JavaDoc classLoaderKey)
64    {
65       ClassLoader JavaDoc classloader = null;
66
67       Object JavaDoc val = environment.get(classLoaderKey);
68       if(val != null)
69       {
70          if(val instanceof ClassLoader JavaDoc)
71          {
72             classloader = (ClassLoader JavaDoc) val;
73          }
74          else
75          {
76             throw new IllegalArgumentException JavaDoc("Error using value specified for " + classLoaderKey +
77                                                " because is not an instance of " + ClassLoader JavaDoc.class.getName());
78          }
79       }
80       else
81       {
82          // just use current caller's thread's classloader
83
classloader = Thread.currentThread().getContextClassLoader();
84       }
85
86       environment.put(classLoaderKey, classloader);
87
88
89       return classloader;
90    }
91
92    public static List JavaDoc locateProviderPackage(Map JavaDoc environment, String JavaDoc providerPackageKey)
93          throws JMXProviderException JavaDoc
94    {
95
96       String JavaDoc providerPackage = null;
97
98       // First look for the provider package within the environment map
99
Object JavaDoc val = environment.get(providerPackageKey);
100
101       // once again, to be spec compliant ("If the provider package list is not a String,
102
// or if it contains an element that is an empty string, a JMXProviderException is thrown.")
103
if(providerPackage != null)
104       {
105          if(val instanceof String JavaDoc)
106          {
107             providerPackage = ((String JavaDoc) val).trim();
108             // now have to make sure not an empty entry
109
if(providerPackage.startsWith("|") || providerPackage.endsWith("|") ||
110                providerPackage.indexOf("||") != -1)
111             {
112
113                throw new JMXProviderException JavaDoc("Error processing " + providerPackageKey + " from the " +
114                                               "environment map. An empty provider package exists.");
115             }
116          }
117          else
118          {
119             throw new JMXProviderException JavaDoc("Error processing " + providerPackageKey + " from the " +
120                                            "environment map. Is is not of type String.");
121          }
122       }
123       else
124       {
125          // Now try system property
126
providerPackage = System.getProperty(providerPackageKey);
127       }
128
129       List JavaDoc providerPackages = new ArrayList JavaDoc();
130       if(providerPackage != null && providerPackage.length() > 0)
131       {
132          StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(providerPackage, "|");
133          while(tokenizer.hasMoreElements())
134          {
135             providerPackages.add(tokenizer.nextElement());
136          }
137       }
138
139       return providerPackages;
140    }
141
142 }
Popular Tags