Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2005, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.osgi.framework.internal;
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
A plugin that installs/starts bundles on framework startup.

Author(s):
thomas.diesler@jboss.com
Since:
18-Aug-2009
 
 
     // Provide logging
     static final Logger log = Logger.getLogger(DefaultAutoInstallProvider.class);
 
 
     static void addService(ServiceTarget serviceTarget) {
         DefaultAutoInstallProvider service = new DefaultAutoInstallProvider();
         ServiceBuilder<AutoInstallProviderbuilder = serviceTarget.addService(.service);
         builder.addDependency(.BundleManager.classservice.injectedBundleManager);
         builder.addDependency(.);
         builder.setInitialMode(.);
         builder.install();
     }
 
     private DefaultAutoInstallProvider() {
     }
 
     @Override
     public void start(StartContext contextthrows StartException {
         super.start(context);
         try {
             List<URLautoInstall = new ArrayList<URL>();
             List<URLautoStart = new ArrayList<URL>();
 
             BundleManager bundleManager = .getValue();
             String propValue = (StringbundleManager.getProperty(.);
             if (propValue != null) {
                 for (String path : propValue.split(",")) {
                     URL url = toURL(bundleManagerpath.trim());
                     if (url != null) {
                         autoInstall.add(url);
                     }
                 }
             }
             propValue = (StringbundleManager.getProperty(.);
             if (propValue != null) {
                 for (String path : propValue.split(",")) {
                     URL url = toURL(bundleManagerpath.trim());
                     if (url != null) {
                         autoStart.add(url);
                    }
                }
            }
            ServiceTarget serviceTarget = context.getChildTarget();
            installBundles(serviceTargetautoInstallautoStart);
        } catch (BundleException ex) {
            throw new IllegalStateException("Cannot start auto install bundles"ex);
        }
    }
    @Override
        return this;
    }
    private void installBundles(ServiceTarget serviceTargetfinal List<URLautoInstallfinal List<URLautoStartthrows BundleException {
        // Add the autoStart bundles to autoInstall
        autoInstall.addAll(autoStart);
        final BundleManager bundleManager = .getValue();
        final Map<ServiceNameDeploymentpendingServices = new HashMap<ServiceNameDeployment>();
        // Install autoInstall bundles
        for (URL url : autoInstall) {
            BundleInfo info = BundleInfo.createBundleInfo(url);
            Deployment dep = DeploymentFactory.createDeployment(info);
            dep.setAutoStart(autoStart.contains(url));
            ServiceName serviceName = bundleManager.installBundle(serviceTargetdep);
            pendingServices.put(serviceNamedep);
        }
        // Install a service that has a dependency on all pending bundle INSTALLED services
        ServiceName servicesInstalled = ..append("INSTALLED");
        ServiceBuilder<Voidbuilder = serviceTarget.addService(servicesInstallednew AbstractService<Void>() {
            public void start(StartContext contextthrows StartException {
                .debugf("Auto bundles installed");
            }
        });
        builder.addDependencies(pendingServices.keySet());
        builder.install();
        // Install a service that starts the bundles
        builder = serviceTarget.addService(.new AbstractService<Void>() {
            public void start(StartContext contextthrows StartException {
                for (Deployment dep : pendingServices.values()) {
                    if (dep.isAutoStart()) {
                        Bundle bundle = dep.getAttachment(Bundle.class);
                        try {
                            bundle.start();
                        } catch (BundleException ex) {
                            .errorf(ex"Cannot start persistent bundle: %s"bundle);
                        }
                    }
                }
                .debugf("Auto bundles started");
            }
        });
        builder.addDependencies(servicesInstalled);
        builder.install();
    }
    private URL toURL(final BundleManager bundleManagerfinal String path) {
        URL pathURL = null;
        PropertyProvider provider = new PropertyProvider() {
            @Override
            public String getProperty(String key) {
                return (StringbundleManager.getProperty(key);
            }
        };
        String realPath = StringPropertyReplacer.replaceProperties(pathprovider);
        try {
            pathURL = new URL(realPath);
        } catch (MalformedURLException ex) {
            // ignore
        }
        if (pathURL == null) {
            try {
                File file = new File(realPath);
                if (file.exists())
                    pathURL = file.toURI().toURL();
            } catch (MalformedURLException ex) {
                throw new IllegalArgumentException("Invalid path: " + realPathex);
            }
        }
        if (pathURL == null)
            throw new IllegalArgumentException("Invalid path: " + realPath);
        return pathURL;
    }
New to GrepCode? Check out our FAQ X