Search in sources :

Example 41 with LogService

use of org.osgi.service.log.LogService in project felix by apache.

the class DropResourceCommand method doExecute.

protected void doExecute(DeploymentSessionImpl session) throws Exception {
    // Allow proper rollback in case the drop fails...
    addRollback(new RollbackCommitAction(session));
    AbstractDeploymentPackage target = session.getTargetAbstractDeploymentPackage();
    AbstractDeploymentPackage source = session.getSourceAbstractDeploymentPackage();
    BundleContext context = session.getBundleContext();
    LogService log = session.getLog();
    ResourceInfoImpl[] orderedTargetResources = target.getOrderedResourceInfos();
    for (int i = orderedTargetResources.length - 1; i >= 0; i--) {
        ResourceInfoImpl resourceInfo = orderedTargetResources[i];
        // FELIX-4491: only resources that need to be processed should be handled...
        if (!resourceInfo.isProcessedResource()) {
            session.getLog().log(LogService.LOG_INFO, "Ignoring non-processed resource: " + resourceInfo.getPath());
            continue;
        }
        String path = resourceInfo.getPath();
        if (source.getResourceInfoByPath(path) == null) {
            ServiceReference ref = target.getResourceProcessor(path);
            if (ref != null) {
                ResourceProcessor resourceProcessor = (ResourceProcessor) context.getService(ref);
                if (resourceProcessor != null) {
                    try {
                        if (m_commitCommand.addResourceProcessor(resourceProcessor)) {
                            resourceProcessor.begin(session);
                        }
                        resourceProcessor.dropped(path);
                    } catch (Exception e) {
                        log.log(LogService.LOG_WARNING, "Not allowed to drop resource '" + path + "'", e);
                    }
                }
            }
        }
    }
}
Also used : ResourceInfoImpl(org.apache.felix.deploymentadmin.ResourceInfoImpl) AbstractDeploymentPackage(org.apache.felix.deploymentadmin.AbstractDeploymentPackage) LogService(org.osgi.service.log.LogService) ResourceProcessor(org.osgi.service.deploymentadmin.spi.ResourceProcessor) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 42 with LogService

use of org.osgi.service.log.LogService in project felix by apache.

the class StartBundleCommand method doExecute.

protected void doExecute(DeploymentSessionImpl session) throws Exception {
    AbstractDeploymentPackage source = session.getSourceAbstractDeploymentPackage();
    PackageAdmin packageAdmin = session.getPackageAdmin();
    RefreshPackagesListener listener = new RefreshPackagesListener();
    LogService log = session.getLog();
    session.getBundleContext().addFrameworkListener(listener);
    packageAdmin.refreshPackages(null);
    m_refreshMonitor.waitForRefresh();
    session.getBundleContext().removeFrameworkListener(listener);
    // start source bundles
    BundleInfoImpl[] bundleInfos = source.getOrderedBundleInfos();
    for (int i = 0; i < bundleInfos.length; i++) {
        BundleInfoImpl bundleInfoImpl = bundleInfos[i];
        if (!bundleInfoImpl.isCustomizer()) {
            String symbolicName = bundleInfoImpl.getSymbolicName();
            Bundle bundle = source.getBundle(symbolicName);
            if (bundle != null) {
                if (isFragmentBundle(bundle)) {
                    log.log(LogService.LOG_INFO, "Skipping fragment bundle '" + symbolicName + "'");
                } else {
                    try {
                        bundle.start();
                    } catch (Exception be) {
                        log.log(LogService.LOG_WARNING, "Could not start bundle '" + symbolicName + "'", be);
                    }
                }
            } else {
                log.log(LogService.LOG_WARNING, "Could not start bundle '" + symbolicName + "' because it is not present in the framework");
            }
        }
    }
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) BundleInfoImpl(org.apache.felix.deploymentadmin.BundleInfoImpl) Bundle(org.osgi.framework.Bundle) AbstractDeploymentPackage(org.apache.felix.deploymentadmin.AbstractDeploymentPackage) LogService(org.osgi.service.log.LogService)

Example 43 with LogService

use of org.osgi.service.log.LogService in project felix by apache.

the class StopBundleCommand method doExecute.

protected void doExecute(DeploymentSessionImpl session) throws Exception {
    LogService log = session.getLog();
    AbstractDeploymentPackage target = session.getTargetAbstractDeploymentPackage();
    BundleInfo[] bundleInfos = target.getOrderedBundleInfos();
    for (int i = 0; i < bundleInfos.length; i++) {
        if (isCancelled()) {
            throw new DeploymentException(CODE_CANCELLED);
        }
        String symbolicName = bundleInfos[i].getSymbolicName();
        Bundle bundle = target.getBundle(symbolicName);
        if (bundle != null) {
            if (omitBundleStop(session, symbolicName)) {
                continue;
            }
            if (isFragmentBundle(bundle)) {
                log.log(LogService.LOG_INFO, "Skipping fragment bundle '" + symbolicName + "'");
            } else {
                addRollback(new StartBundleRunnable(session, bundle));
                try {
                    bundle.stop();
                } catch (Exception e) {
                    log.log(LogService.LOG_WARNING, "Could not stop bundle '" + symbolicName + "'", e);
                }
            }
        } else {
            log.log(LogService.LOG_WARNING, "Could not stop bundle '" + symbolicName + "' because it was not present in the framework");
        }
    }
}
Also used : BundleInfo(org.osgi.service.deploymentadmin.BundleInfo) Bundle(org.osgi.framework.Bundle) DeploymentException(org.osgi.service.deploymentadmin.DeploymentException) AbstractDeploymentPackage(org.apache.felix.deploymentadmin.AbstractDeploymentPackage) LogService(org.osgi.service.log.LogService) DeploymentException(org.osgi.service.deploymentadmin.DeploymentException)

Example 44 with LogService

use of org.osgi.service.log.LogService in project felix by apache.

the class UpdateCommand method doExecute.

protected void doExecute(DeploymentSessionImpl session) throws Exception {
    AbstractDeploymentPackage source = session.getSourceAbstractDeploymentPackage();
    AbstractDeploymentPackage targetPackage = session.getTargetAbstractDeploymentPackage();
    BundleContext context = session.getBundleContext();
    LogService log = session.getLog();
    Map expectedBundles = new HashMap();
    AbstractInfo[] bundleInfos = (AbstractInfo[]) source.getBundleInfos();
    for (int i = 0; i < bundleInfos.length; i++) {
        AbstractInfo bundleInfo = bundleInfos[i];
        if (!bundleInfo.isMissing()) {
            expectedBundles.put(bundleInfo.getPath(), bundleInfo);
        }
    }
    try {
        while (!expectedBundles.isEmpty()) {
            AbstractInfo entry = source.getNextEntry();
            if (entry == null) {
                throw new DeploymentException(CODE_OTHER_ERROR, "Expected more bundles in the stream: " + expectedBundles.keySet());
            }
            String name = entry.getPath();
            BundleInfoImpl bundleInfo = (BundleInfoImpl) expectedBundles.remove(name);
            if (bundleInfo == null) {
                if (isLocalizationFile(name)) {
                    // FELIX-518: do not try to process signature or localization files...
                    continue;
                }
                throw new DeploymentException(CODE_OTHER_ERROR, "Resource '" + name + "' is not described in the manifest.");
            }
            String bsn = bundleInfo.getSymbolicName();
            Version sourceVersion = bundleInfo.getVersion();
            Bundle bundle = targetPackage.getBundle(bsn);
            try {
                if (bundle == null) {
                    // new bundle, install it
                    bundle = context.installBundle(BUNDLE_LOCATION_PREFIX + bsn, new BundleInputStream(source.getCurrentEntryStream()));
                    addRollback(new UninstallBundleRunnable(bundle, log));
                } else {
                    // existing bundle, update it
                    Version currentVersion = getVersion(bundle);
                    if (!sourceVersion.equals(currentVersion)) {
                        bundle.update(new BundleInputStream(source.getCurrentEntryStream()));
                        addRollback(new UpdateBundleRunnable(bundle, targetPackage, log));
                    }
                }
            } catch (Exception be) {
                if (isCancelled()) {
                    return;
                }
                throw new DeploymentException(CODE_OTHER_ERROR, "Could not install new bundle '" + name + "' (" + bsn + ")", be);
            }
            if (!bundle.getSymbolicName().equals(bsn)) {
                throw new DeploymentException(CODE_BUNDLE_NAME_ERROR, "Installed/updated bundle symbolicname (" + bundle.getSymbolicName() + ") do not match what was installed/updated: " + bsn);
            }
            Version targetVersion = getVersion(bundle);
            if (!sourceVersion.equals(targetVersion)) {
                throw new DeploymentException(CODE_OTHER_ERROR, "Installed/updated bundle version (" + targetVersion + ") do not match what was installed/updated: " + sourceVersion + ", offending bundle = " + bsn);
            }
        }
    } catch (IOException e) {
        throw new DeploymentException(CODE_OTHER_ERROR, "Problem while reading stream", e);
    }
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) AbstractDeploymentPackage(org.apache.felix.deploymentadmin.AbstractDeploymentPackage) IOException(java.io.IOException) DeploymentException(org.osgi.service.deploymentadmin.DeploymentException) IOException(java.io.IOException) AbstractInfo(org.apache.felix.deploymentadmin.AbstractInfo) Version(org.osgi.framework.Version) BundleInfoImpl(org.apache.felix.deploymentadmin.BundleInfoImpl) DeploymentException(org.osgi.service.deploymentadmin.DeploymentException) Map(java.util.Map) HashMap(java.util.HashMap) LogService(org.osgi.service.log.LogService) BundleContext(org.osgi.framework.BundleContext)

Example 45 with LogService

use of org.osgi.service.log.LogService in project felix by apache.

the class Activator method init.

/**
 * Initialize our DependencyManager Runtime service.
 *
 * We depend on the OSGi LogService, and we track all started bundles which do have a
 * "DependencyManager-Component" Manifest header.
 * If the "dm.runtime.log=true" or "dm.runtime.packageAdmin=true" parameter is configured in the Felix config.properties
 * then we'll use a required/temporal service dependency over the respective service.
 * These temporal dependencies avoid us to be restarted if the respective service is temporarily
 * unavailable (that is: when the service is updating).
 * if the "dm.runtime.log" or "dm.runtime.packageAdmin" is not configured or it it is set to false, then we'll use
 * an optional dependency over the respective service, in order to use a NullObject in case
 * the service is not available.
 */
@Override
public void init(BundleContext context, DependencyManager dm) throws Exception {
    boolean logEnabled = "true".equalsIgnoreCase(context.getProperty(CONF_LOG));
    Log.instance().enableLogs(logEnabled);
    Component component = createComponent().setImplementation(DependencyManagerRuntime.class).setComposition("getComposition").add(createBundleDependency().setRequired(false).setStateMask(Bundle.ACTIVE).setFilter("(DependencyManager-Component=*)").setCallbacks("bundleStarted", "bundleStopped")).add(createServiceDependency().setRequired(true).setService(PackageAdmin.class)).add(createServiceDependency().setRequired(logEnabled).setService(LogService.class));
    dm.add(component);
}
Also used : Component(org.apache.felix.dm.Component) LogService(org.osgi.service.log.LogService)

Aggregations

LogService (org.osgi.service.log.LogService)57 ServiceTracker (org.osgi.util.tracker.ServiceTracker)15 File (java.io.File)10 ServiceReference (org.osgi.framework.ServiceReference)10 AbstractDeploymentPackage (org.apache.felix.deploymentadmin.AbstractDeploymentPackage)7 Bundle (org.osgi.framework.Bundle)7 HashMap (java.util.HashMap)6 PrintStream (java.io.PrintStream)5 StringWriter (java.io.StringWriter)5 Builder (aQute.bnd.osgi.Builder)4 Jar (aQute.bnd.osgi.Jar)4 Resource (aQute.bnd.osgi.Resource)4 XmlTester (aQute.bnd.test.XmlTester)4 Attributes (java.util.jar.Attributes)4 BundleInfoImpl (org.apache.felix.deploymentadmin.BundleInfoImpl)4 Before (org.junit.Before)4 BundleContext (org.osgi.framework.BundleContext)4 IOException (java.io.IOException)3 Date (java.util.Date)3 Matchers.anyString (org.mockito.Matchers.anyString)3