Search in sources :

Example 21 with ServiceConfigurationError

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();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) ServiceConfigurationError(java.util.ServiceConfigurationError) JConsolePlugin(com.sun.tools.jconsole.JConsolePlugin)

Example 22 with ServiceConfigurationError

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);
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) ImageInputStream(javax.imageio.stream.ImageInputStream) Rectangle(java.awt.Rectangle) ExtractorException(org.georchestra.extractorapp.ws.ExtractorException) AffineTransform(java.awt.geom.AffineTransform) XAffineTransform(org.geotools.referencing.operation.matrix.XAffineTransform) ServiceConfigurationError(java.util.ServiceConfigurationError) ImageReader(javax.imageio.ImageReader) FactoryException(org.opengis.referencing.FactoryException) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) ExtractorException(org.georchestra.extractorapp.ws.ExtractorException) TransformException(org.opengis.referencing.operation.TransformException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 23 with ServiceConfigurationError

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) {
    }
}
Also used : IClearable(com.helger.commons.state.IClearable) ServiceConfigurationError(java.util.ServiceConfigurationError) Test(org.junit.Test)

Example 24 with ServiceConfigurationError

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();
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ServiceConfigurationError(java.util.ServiceConfigurationError) URLConnection(java.net.URLConnection) ImmutableSet(com.google.common.collect.ImmutableSet) BufferedReader(java.io.BufferedReader)

Example 25 with ServiceConfigurationError

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();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ServiceConfigurationError(java.util.ServiceConfigurationError) IOException(java.io.IOException) URL(java.net.URL) ImmutableSet(com.google.common.collect.ImmutableSet)

Aggregations

ServiceConfigurationError (java.util.ServiceConfigurationError)95 IOException (java.io.IOException)22 ArrayList (java.util.ArrayList)16 Iterator (java.util.Iterator)15 File (java.io.File)10 URL (java.net.URL)10 URLClassLoader (java.net.URLClassLoader)7 ServiceLoader (java.util.ServiceLoader)7 Test (org.junit.Test)7 BufferedReader (java.io.BufferedReader)6 InputStream (java.io.InputStream)6 InputStreamReader (java.io.InputStreamReader)6 NoSuchElementException (java.util.NoSuchElementException)6 CharsetProvider (java.nio.charset.spi.CharsetProvider)5 HashMap (java.util.HashMap)5 FileInputStream (java.io.FileInputStream)4 Path (java.nio.file.Path)4 SQLException (java.sql.SQLException)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4