use of java.util.ServiceConfigurationError in project visualvm by oracle.
the class JConsolePluginWrapper method initPluginService.
private void initPluginService(String pluginPath) {
if (pluginPath.length() > 0) {
try {
ClassLoader pluginCL = new URLClassLoader(pathToURLs(pluginPath), JConsolePluginWrapper.class.getClassLoader());
ServiceLoader<JConsolePlugin> plugins = ServiceLoader.load(JConsolePlugin.class, pluginCL);
// Validate all plugins
for (JConsolePlugin p : plugins) {
// NOI18N
LOGGER.finer("JConsole plugin " + p.getClass().getName() + " loaded.");
}
pluginService = plugins;
} catch (ServiceConfigurationError e) {
// Error occurs during initialization of plugin
// NOI18N
LOGGER.warning("Fail to load JConsole plugin: " + e.getMessage());
// NOI18N
LOGGER.throwing(JConsolePluginWrapper.class.getName(), "initPluginService", e);
} catch (MalformedURLException e) {
// NOI18N
LOGGER.warning("Invalid JConsole plugin path: " + e.getMessage());
// NOI18N
LOGGER.throwing(JConsolePluginWrapper.class.getName(), "initPluginService", e);
}
}
if (pluginService == null) {
initEmptyPlugin();
}
}
use of java.util.ServiceConfigurationError in project georchestra by georchestra.
the class WcsCoverageReader method writeWorldImageExt.
/**
* Write world image extensions : TFW, PRJ
*
* @param request
* @param file
* @param in
* @throws IOException
*/
private void writeWorldImageExt(WcsReaderRequest request, File file) throws IOException {
String baseFilePath = file.getPath().substring(0, file.getPath().lastIndexOf('.'));
// Compute image size and image transformed BBOX
ReferencedEnvelope transformedBBox;
AffineTransform transform;
int width = -1;
int height = -1;
String ext = FileUtils.extension(file);
try {
final Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(ext.substring(1));
while (readers.hasNext() && width < 0 && height < 0) {
ImageInputStream stream = null;
try {
ImageReader reader = readers.next();
stream = ImageIO.createImageInputStream(file.getAbsoluteFile());
reader.setInput(stream, true, false);
width = reader.getWidth(0);
height = reader.getHeight(0);
break;
} catch (Exception e) {
width = -1;
height = -1;
// try next reader;
} finally {
if (stream != null) {
stream.close();
}
}
}
transformedBBox = request.requestBbox.transform(request.responseCRS, true, 10);
if (width < 0) {
width = (int) Math.round(transformedBBox.getWidth() / request.crsResolution());
}
if (height < 0) {
height = (int) Math.round(transformedBBox.getHeight() / request.crsResolution());
}
Rectangle imageSize = new Rectangle(width, height);
transform = RendererUtilities.worldToScreenTransform(transformedBBox, imageSize);
transform.invert();
} catch (Exception e) {
throw new ExtractorException(e);
} catch (ServiceConfigurationError e) {
throw new RuntimeException(e.getMessage());
}
// Generate TFW PRJ files
createWorldFile(transform, ext, baseFilePath);
createPrjFile(request.responseCRS, baseFilePath);
}
use of java.util.ServiceConfigurationError in project ph-commons by phax.
the class ServiceLoaderFuncTest method testLoadValid.
@Test
public void testLoadValid() {
// Empty service file present
IClearable aNext;
final ServiceLoader<IClearable> aSL = ServiceLoader.load(IClearable.class);
final Iterator<IClearable> it = aSL.iterator();
assertNotNull(it);
assertTrue(it.hasNext());
try {
// first item is the invalid one (does not implement IClearable)
aNext = it.next();
fail();
} catch (final ServiceConfigurationError ex) {
}
assertTrue(it.hasNext());
// now get the valid one
aNext = it.next();
assertNotNull(aNext);
assertTrue(aNext instanceof MockSPIClearableValid);
assertEquals(0, ((MockSPIClearableValid) aNext).getCallCount());
assertFalse(it.hasNext());
try {
it.remove();
fail();
} catch (final UnsupportedOperationException ex) {
}
}
use of java.util.ServiceConfigurationError in project auto by google.
the class SimpleServiceLoader method providerClassesFromUrl.
private static <T> ImmutableSet<Class<? extends T>> providerClassesFromUrl(URL resourceUrl, Class<? extends T> service, ClassLoader loader, Optional<Pattern> allowedMissingClasses) throws IOException {
ImmutableSet.Builder<Class<? extends T>> providerClasses = ImmutableSet.builder();
URLConnection urlConnection = resourceUrl.openConnection();
urlConnection.setUseCaches(false);
List<String> lines;
try (InputStream in = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, UTF_8))) {
lines = reader.lines().collect(toList());
}
List<String> classNames = lines.stream().map(SimpleServiceLoader::parseClassName).flatMap(Streams::stream).collect(toList());
for (String className : classNames) {
Class<?> c;
try {
c = Class.forName(className, false, loader);
} catch (ClassNotFoundException e) {
if (allowedMissingClasses.isPresent() && allowedMissingClasses.get().matcher(className).matches()) {
continue;
}
throw new ServiceConfigurationError("Could not load " + className, e);
}
if (!service.isAssignableFrom(c)) {
throw new ServiceConfigurationError("Class " + className + " is not assignable to " + service.getName());
}
providerClasses.add(c.asSubclass(service));
}
return providerClasses.build();
}
use of java.util.ServiceConfigurationError in project auto by google.
the class SimpleServiceLoader method load.
public static <T> ImmutableList<T> load(Class<? extends T> service, ClassLoader loader, Optional<Pattern> allowedMissingClasses) {
String resourceName = "META-INF/services/" + service.getName();
List<URL> resourceUrls;
try {
resourceUrls = Collections.list(loader.getResources(resourceName));
} catch (IOException e) {
throw new ServiceConfigurationError("Could not look up " + resourceName, e);
}
ImmutableSet.Builder<Class<? extends T>> providerClasses = ImmutableSet.builder();
for (URL resourceUrl : resourceUrls) {
try {
providerClasses.addAll(providerClassesFromUrl(resourceUrl, service, loader, allowedMissingClasses));
} catch (IOException e) {
throw new ServiceConfigurationError("Could not read " + resourceUrl, e);
}
}
ImmutableList.Builder<T> providers = ImmutableList.builder();
for (Class<? extends T> providerClass : providerClasses.build()) {
try {
T provider = providerClass.getConstructor().newInstance();
providers.add(provider);
} catch (ReflectiveOperationException e) {
throw new ServiceConfigurationError("Could not construct " + providerClass.getName(), e);
}
}
return providers.build();
}
Aggregations