Search in sources :

Example 11 with ServiceConfigurationError

use of java.util.ServiceConfigurationError in project aries by apache.

the class AriesRepositoryGenerator method startFramework.

/**
   * Start OSGi framework and install the necessary bundles
   * @return
   * @throws BundleException
   */
public BundleContext startFramework() throws BundleException {
    ServiceLoader<FrameworkFactory> factoryLoader = ServiceLoader.load(FrameworkFactory.class, getClass().getClassLoader());
    Iterator<FrameworkFactory> factoryIterator = factoryLoader.iterator();
    //Map<String, String> osgiPropertyMap = new HashMap<String, String>();
    if (!!!factoryIterator.hasNext()) {
        System.out.println("Unable to locate the osgi jar");
    }
    try {
        FrameworkFactory frameworkFactory = factoryIterator.next();
        framework = frameworkFactory.newFramework(Collections.EMPTY_MAP);
    } catch (ServiceConfigurationError sce) {
        sce.printStackTrace();
    }
    framework.init();
    framework.start();
    // install the bundles in the current directory
    Collection<Bundle> installedBundles = new ArrayList<Bundle>();
    File bundleDir = new File(".");
    File[] jars = bundleDir.listFiles();
    try {
        for (File jar : jars) {
            if (jar.isFile() && (jar.getName().endsWith(".jar"))) {
                String location = URLDecoder.decode(jar.toURI().toURL().toExternalForm(), "UTF-8");
                if (shouldInstall(location, getIgnoreList())) {
                    installedBundles.add(framework.getBundleContext().installBundle("reference:" + location));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    ServiceReference paRef = framework.getBundleContext().getServiceReference(PackageAdmin.class.getCanonicalName());
    if (paRef != null) {
        try {
            PackageAdmin admin = (PackageAdmin) framework.getBundleContext().getService(paRef);
            admin.resolveBundles(installedBundles.toArray(new Bundle[installedBundles.size()]));
        } finally {
            framework.getBundleContext().ungetService(paRef);
        }
    } else {
        System.out.println("Unable to find the service reference for package admin");
    }
    //loop through the list of installed bundles to start them
    for (Bundle bundle : installedBundles) {
        try {
            if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) == null) {
                //start the bundle using its activiation policy
                bundle.start(Bundle.START_ACTIVATION_POLICY);
            } else {
            //nothing to start, we have got a fragment
            }
        } catch (BundleException e) {
            e.printStackTrace();
            continue;
        }
    }
    return framework.getBundleContext();
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ServiceConfigurationError(java.util.ServiceConfigurationError) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) ServiceReference(org.osgi.framework.ServiceReference) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) BundleException(org.osgi.framework.BundleException) File(java.io.File)

Example 12 with ServiceConfigurationError

use of java.util.ServiceConfigurationError in project lucene-solr by apache.

the class SPIClassIterator method loadNextProfile.

private boolean loadNextProfile() {
    ArrayList<String> lines = null;
    while (profilesEnum.hasMoreElements()) {
        if (lines != null) {
            lines.clear();
        } else {
            lines = new ArrayList<>();
        }
        final URL url = profilesEnum.nextElement();
        try {
            final InputStream in = url.openStream();
            boolean success = false;
            try {
                final BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
                String line;
                while ((line = reader.readLine()) != null) {
                    final int pos = line.indexOf('#');
                    if (pos >= 0) {
                        line = line.substring(0, pos);
                    }
                    line = line.trim();
                    if (line.length() > 0) {
                        lines.add(line);
                    }
                }
                success = true;
            } finally {
                if (success) {
                    IOUtils.close(in);
                } else {
                    IOUtils.closeWhileHandlingException(in);
                }
            }
        } catch (IOException ioe) {
            throw new ServiceConfigurationError("Error loading SPI class list from URL: " + url, ioe);
        }
        if (!lines.isEmpty()) {
            this.linesIterator = lines.iterator();
            return true;
        }
    }
    return false;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ServiceConfigurationError(java.util.ServiceConfigurationError) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

ServiceConfigurationError (java.util.ServiceConfigurationError)12 IOException (java.io.IOException)3 Iterator (java.util.Iterator)3 ArrayList (java.util.ArrayList)2 Configuration (org.apache.hadoop.conf.Configuration)2 Test (org.junit.Test)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 CharsetProvider (java.nio.charset.spi.CharsetProvider)1 SQLException (java.sql.SQLException)1 LinkedHashMap (java.util.LinkedHashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 ServiceLoader (java.util.ServiceLoader)1 ClientProtocolProvider (org.apache.hadoop.mapreduce.protocol.ClientProtocolProvider)1 Bundle (org.osgi.framework.Bundle)1 BundleException (org.osgi.framework.BundleException)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1