18 package com.mysql.clusterj;
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.List;
85 @SuppressWarnings(
"unchecked")
89 List<T> result =
new ArrayList<T>();
90 String factoryName = cls.getName();
91 String serviceName =
"META-INF/services/" + factoryName;
92 Enumeration<URL> urls = null;
94 urls = loader.getResources(serviceName);
95 }
catch (IOException ex) {
98 while (urls.hasMoreElements()) {
99 InputStream inputStream = null;
100 InputStreamReader inputStreamReader = null;
101 BufferedReader bufferedReader = null;
103 URL url = urls.nextElement();
104 inputStream = url.openStream();
105 inputStreamReader =
new InputStreamReader(inputStream);
106 bufferedReader =
new BufferedReader(inputStreamReader);
107 factoryName = bufferedReader.readLine();
108 Class<T> serviceClass = (Class<T>)Class.forName(factoryName,
true, loader);
109 T service = serviceClass.newInstance();
110 if (service != null) {
113 }
catch (IOException ex) {
114 errorMessages.append(ex.toString());
115 }
catch (ClassNotFoundException ex) {
116 errorMessages.append(ex.toString());
117 }
catch (InstantiationException ex) {
118 errorMessages.append(ex.toString());
119 }
catch (IllegalAccessException ex) {
120 errorMessages.append(ex.toString());
123 if (inputStream != null) {
126 }
catch (IOException ioex) {
144 if (services.size() != 0) {
145 return services.get(0);
147 String factoryName = cls.getName();
148 String serviceName =
"META-INF/services/" + factoryName;
150 "No instance for service " + factoryName +
151 " could be found. " +
152 "Make sure that there is a file " + serviceName +
153 " in your class path naming the factory class." +
166 @SuppressWarnings(
"unchecked")
168 if (implementationClassName == null) {
172 ClassLoader loader = Thread.currentThread().getContextClassLoader();
173 Class<?> clazz = Class.forName(implementationClassName,
true, loader);
174 Class<T> serviceClass = null;
175 if (!(cls.isAssignableFrom(clazz))) {
176 throw new ClassCastException(cls.getName() +
" " + implementationClassName);
178 serviceClass = (Class<T>)clazz;
179 T service = serviceClass.newInstance();
181 }
catch (ClassNotFoundException e) {
183 }
catch (ClassCastException e) {
185 }
catch (InstantiationException e) {
187 }
catch (IllegalAccessException e) {