Search in sources :

Example 1 with Subsystem

use of org.osgi.service.subsystem.Subsystem in project felix by apache.

the class WebConsolePlugin method asyncSubsystemOperation.

private void asyncSubsystemOperation(long id, final SubsystemOperation op) throws IOException {
    try {
        Collection<ServiceReference<Subsystem>> refs = bundleContext.getServiceReferences(Subsystem.class, "(" + SubsystemConstants.SUBSYSTEM_ID_PROPERTY + "=" + id + ")");
        if (refs.size() < 1)
            throw new IOException(UNABLE_TO_FIND_TARGET_SUBSYSTEM);
        final ServiceReference<Subsystem> ref = refs.iterator().next();
        new Thread(new Runnable() {

            @Override
            public void run() {
                Subsystem ss = bundleContext.getService(ref);
                try {
                    op.exec(ss);
                } finally {
                    bundleContext.ungetService(ref);
                }
            }
        }).start();
    } catch (InvalidSyntaxException e) {
        throw new IOException(e);
    }
}
Also used : Subsystem(org.osgi.service.subsystem.Subsystem) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with Subsystem

use of org.osgi.service.subsystem.Subsystem in project felix by apache.

the class WebConsolePlugin method installSubsystem.

private void installSubsystem(HttpServletRequest req) throws IOException {
    @SuppressWarnings("rawtypes") Map params = (Map) req.getAttribute(AbstractWebConsolePlugin.ATTR_FILEUPLOAD);
    final boolean start = getParameter(params, "subsystemstart") != null;
    FileItem[] subsystemItems = getFileItems(params, "subsystemfile");
    for (final FileItem subsystemItem : subsystemItems) {
        File tmpFile = null;
        try {
            // copy the data to a file for better processing
            tmpFile = File.createTempFile("installSubsystem", ".tmp");
            subsystemItem.write(tmpFile);
        } catch (Exception e) {
            log(LogService.LOG_ERROR, "Problem accessing uploaded subsystem file: " + subsystemItem.getName(), e);
            // remove the temporary file
            if (tmpFile != null) {
                tmpFile.delete();
                tmpFile = null;
            }
        }
        if (tmpFile != null) {
            final File file = tmpFile;
            // TODO support install in other subsystems than the root one
            // TODO currently this means that when installing more than one subsystem they
            // will be installed concurrently. Not sure if this is the best idea.
            // However the client UI does not support selecting more than one file, so
            // from a practical point of view this is currently not an issue.
            asyncSubsystemOperation(0, new SubsystemOperation() {

                @Override
                public void exec(Subsystem ss) {
                    try {
                        InputStream is = new FileInputStream(file);
                        try {
                            Subsystem nss = ss.install("inputstream:" + subsystemItem.getName(), is);
                            if (start)
                                nss.start();
                        } finally {
                            is.close();
                            file.delete();
                        }
                    } catch (IOException e) {
                        log(LogService.LOG_ERROR, "Problem installing subsystem", e);
                    }
                }
            });
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileItem(org.apache.commons.fileupload.FileItem) Subsystem(org.osgi.service.subsystem.Subsystem) Map(java.util.Map) File(java.io.File)

Example 3 with Subsystem

use of org.osgi.service.subsystem.Subsystem in project sling by apache.

the class UninstallSubsystemTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    final TaskResource tr = this.getResource();
    ctx.log("Uninstalling subsystem from {}", tr);
    Subsystem subsystem = null;
    try {
        subsystem = this.bundleContext.getService(this.subsystemReference);
        if (subsystem != null) {
            subsystem.uninstall();
            ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.UNINSTALLED));
            ctx.log("Uninstalled subsystem {}", subsystem);
        } else {
            ctx.log("Unable to uninstall subsystem {}.", tr);
            ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.IGNORED));
        }
    } finally {
        if (subsystem != null) {
            this.bundleContext.ungetService(this.subsystemReference);
        }
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Subsystem(org.osgi.service.subsystem.Subsystem) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask)

Example 4 with Subsystem

use of org.osgi.service.subsystem.Subsystem in project sling by apache.

the class Activator method start.

/**
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
public void start(final BundleContext context) throws Exception {
    this.rootSubsystemTracker = new ServiceTracker<Subsystem, Subsystem>(context, context.createFilter("(&(" + Constants.OBJECTCLASS + "=" + Subsystem.class.getName() + ")" + "(" + SubsystemConstants.SUBSYSTEM_ID_PROPERTY + "=0))"), new ServiceTrackerCustomizer<Subsystem, Subsystem>() {

        public Subsystem addingService(final ServiceReference<Subsystem> reference) {
            final Subsystem service = context.getService(reference);
            if (service != null) {
                registerInstaller(context, service);
            }
            return service;
        }

        public void modifiedService(final ServiceReference<Subsystem> reference, final Subsystem service) {
        // nothing to do
        }

        public void removedService(final ServiceReference<Subsystem> reference, final Subsystem service) {
            unregisterInstaller();
        }
    });
    this.rootSubsystemTracker.open();
}
Also used : ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) Subsystem(org.osgi.service.subsystem.Subsystem) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with Subsystem

use of org.osgi.service.subsystem.Subsystem in project sling by apache.

the class SubsystemInstaller method createTask.

/**
     * @see org.apache.sling.installer.api.tasks.InstallTaskFactory#createTask(org.apache.sling.installer.api.tasks.TaskResourceGroup)
     */
public InstallTask createTask(final TaskResourceGroup toActivate) {
    final InstallTask result;
    final TaskResource rsrc = toActivate.getActiveResource();
    if (rsrc.getType().equals(TYPE_SUBSYSTEM)) {
        // check if the required info is available
        final SubsystemInfo info = checkResource(toActivate);
        if (info == null) {
            // ignore as info is missing
            result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
        } else {
            // search a subsystem with the symbolic name
            final ServiceReference<Subsystem> ref = this.getSubsystemReference(info.symbolicName);
            final Subsystem currentSubsystem = (ref != null ? this.bundleContext.getService(ref) : null);
            try {
                final Version newVersion = new Version(info.version);
                final Version oldVersion = (ref == null ? null : (Version) ref.getProperty("subsystem.version"));
                // Install
                if (rsrc.getState() == ResourceState.INSTALL) {
                    if (oldVersion != null) {
                        final int compare = oldVersion.compareTo(newVersion);
                        if (compare < 0) {
                            // installed version is lower -> update
                            result = new UpdateSubsystemTask(toActivate, this.bundleContext, ref, this.rootSubsystem);
                        } else if (compare == 0 && isSnapshot(newVersion)) {
                            // same version but snapshot -> update
                            result = new UpdateSubsystemTask(toActivate, this.bundleContext, ref, this.rootSubsystem);
                        } else if (compare == 0 && currentSubsystem != null && currentSubsystem.getState() != State.ACTIVE) {
                            // try to start the version
                            result = new StartSubsystemTask(toActivate, currentSubsystem);
                        } else {
                            logger.info("{} is not installed, subsystem with same or higher version is already installed: {}", info, newVersion);
                            result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
                        }
                    } else {
                        result = new InstallSubsystemTask(toActivate, this.rootSubsystem);
                    }
                // Uninstall
                } else if (rsrc.getState() == ResourceState.UNINSTALL) {
                    if (oldVersion == null) {
                        logger.error("Nothing to uninstall. {} is currently not installed.", info);
                        result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
                    } else {
                        final int compare = oldVersion.compareTo(newVersion);
                        if (compare == 0) {
                            result = new UninstallSubsystemTask(toActivate, this.bundleContext, ref);
                        } else {
                            logger.error("Nothing to uninstall. {} is currently not installed, different version is installed {}", info, oldVersion);
                            result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
                        }
                    }
                } else {
                    result = null;
                }
            } finally {
                if (currentSubsystem != null) {
                    this.bundleContext.ungetService(ref);
                }
            }
        }
    } else {
        result = null;
    }
    return result;
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Version(org.osgi.framework.Version) Subsystem(org.osgi.service.subsystem.Subsystem) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask) InstallTask(org.apache.sling.installer.api.tasks.InstallTask)

Aggregations

Subsystem (org.osgi.service.subsystem.Subsystem)202 Test (org.junit.Test)151 SubsystemTest (org.apache.aries.subsystem.itests.SubsystemTest)78 AriesSubsystem (org.apache.aries.subsystem.AriesSubsystem)52 SubsystemException (org.osgi.service.subsystem.SubsystemException)50 Bundle (org.osgi.framework.Bundle)48 SubsystemArchiveBuilder (org.apache.aries.subsystem.itests.util.SubsystemArchiveBuilder)31 BundleArchiveBuilder (org.apache.aries.subsystem.itests.util.BundleArchiveBuilder)22 BasicSubsystem (org.apache.aries.subsystem.core.internal.BasicSubsystem)19 IOException (java.io.IOException)13 Hashtable (java.util.Hashtable)9 ServiceReference (org.osgi.framework.ServiceReference)8 File (java.io.File)7 BundleContext (org.osgi.framework.BundleContext)7 ArrayList (java.util.ArrayList)6 TestCapability (org.apache.aries.subsystem.itests.util.TestCapability)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 FileInputStream (java.io.FileInputStream)5 Callable (java.util.concurrent.Callable)5 BundleException (org.osgi.framework.BundleException)5