Search in sources :

Example 26 with TaskResource

use of org.apache.sling.installer.api.tasks.TaskResource in project sling by apache.

the class UpdateSubsystemTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    final TaskResource tr = this.getResource();
    ctx.log("Updating subsystem from {}", tr);
    Subsystem subsystem = null;
    try {
        subsystem = this.bundleContext.getService(this.subsystemReference);
        if (subsystem != null) {
            subsystem.stop();
            subsystem.uninstall();
            ctx.addTaskToCurrentCycle(new InstallSubsystemTask(this.getResourceGroup(), this.rootSubsystem));
        } else {
            ctx.log("Unable to update 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 27 with TaskResource

use of org.apache.sling.installer.api.tasks.TaskResource in project sling by apache.

the class UpdateHandlerTest method setUp.

@Before
public void setUp() {
    setupInstaller();
    serviceRegistrations.clear();
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(Constants.SERVICE_RANKING, 1000);
    serviceRegistrations.add(this.bundleContext.registerService(ResourceTransformer.class, new ResourceTransformer() {

        public TransformationResult[] transform(final RegisteredResource resource) {
            final int lastDot = resource.getURL().lastIndexOf('.');
            final int lastSlash = resource.getURL().lastIndexOf('/');
            if (resource.getURL().substring(lastDot + 1).equals(TYPE)) {
                final String id = resource.getURL().substring(lastSlash + 1, lastDot);
                final TransformationResult tr = new TransformationResult();
                tr.setId(id);
                tr.setResourceType(TYPE);
                return new TransformationResult[] { tr };
            }
            return null;
        }
    }, props));
    serviceRegistrations.add(this.bundleContext.registerService(InstallTaskFactory.class, new InstallTaskFactory() {

        public InstallTask createTask(final TaskResourceGroup toActivate) {
            final TaskResource tr = toActivate.getActiveResource();
            if (tr != null && tr.getEntityId().startsWith(TYPE)) {
                if (tr.getState() == ResourceState.INSTALL) {
                    installed.put(tr.getEntityId(), tr.getDictionary());
                    return new ChangeStateTask(toActivate, ResourceState.INSTALLED);
                } else {
                    installed.remove(tr.getEntityId());
                    return new ChangeStateTask(toActivate, ResourceState.UNINSTALLED);
                }
            }
            return null;
        }
    }, props));
}
Also used : InstallTaskFactory(org.apache.sling.installer.api.tasks.InstallTaskFactory) TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Hashtable(java.util.Hashtable) RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) TaskResourceGroup(org.apache.sling.installer.api.tasks.TaskResourceGroup) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask) ResourceTransformer(org.apache.sling.installer.api.tasks.ResourceTransformer) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) Before(org.junit.Before)

Example 28 with TaskResource

use of org.apache.sling.installer.api.tasks.TaskResource in project sling by apache.

the class InstallModelTask method execute.

@SuppressWarnings("deprecation")
@Override
public void execute(final InstallationContext ctx) {
    try {
        final TaskResource resource = this.getResource();
        ctx.log("Installing {}", resource.getEntityId());
        final String modelTxt = (String) resource.getAttribute(ModelTransformer.ATTR_MODEL);
        final Integer featureIndex = (Integer) resource.getAttribute(ModelTransformer.ATTR_FEATURE_INDEX);
        final String name = (String) resource.getAttribute(ModelTransformer.ATTR_FEATURE_NAME);
        if (modelTxt == null || featureIndex == null || name == null) {
            ctx.log("Unable to install model resource {} : no model found", resource);
            this.getResourceGroup().setFinishState(ResourceState.IGNORED);
        } else {
            final String path = (String) resource.getAttribute(ModelTransformer.ATTR_BASE_PATH);
            final File baseDir = (path == null ? null : new File(path));
            boolean success = false;
            try {
                final Result result = this.transform(name, modelTxt, featureIndex, resource, baseDir);
                if (result == null) {
                    ctx.log("Unable to install model resource {} : unable to create resources", resource);
                    this.getResourceGroup().setFinishState(ResourceState.IGNORED);
                } else {
                    // repo init first
                    if (result.repoinit != null) {
                        List<Operation> ops = null;
                        try (final Reader r = new StringReader(result.repoinit)) {
                            ops = this.repoInitParser.parse(r);
                        } catch (final IOException | RepoInitParsingException e) {
                            logger.error("Unable to parse repoinit block.", e);
                            ctx.log("Unable to install model resource {} : unable parse repoinit block.", resource);
                            this.getResourceGroup().setFinishState(ResourceState.IGNORED);
                            return;
                        }
                        // login admin is required for repo init
                        Session session = null;
                        try {
                            session = this.repository.loginAdministrative(null);
                            this.repoInitProcessor.apply(session, ops);
                            session.save();
                        } catch (final RepositoryException re) {
                            logger.error("Unable to process repoinit block.", re);
                            ctx.log("Unable to install model resource {} : unable to process repoinit block.", resource);
                            this.getResourceGroup().setFinishState(ResourceState.IGNORED);
                            return;
                        } finally {
                            if (session != null) {
                                session.logout();
                            }
                        }
                    }
                    if (!result.resources.isEmpty()) {
                        final OsgiInstaller installer = this.getService(OsgiInstaller.class);
                        if (installer != null) {
                            installer.registerResources("model-" + name, result.resources.toArray(new InstallableResource[result.resources.size()]));
                        } else {
                            ctx.log("Unable to install model resource {} : unable to get OSGi installer", resource);
                            this.getResourceGroup().setFinishState(ResourceState.IGNORED);
                            return;
                        }
                    }
                    this.getResourceGroup().setFinishState(ResourceState.INSTALLED);
                    success = true;
                }
            } finally {
                if (!success && baseDir != null) {
                    this.deleteDirectory(baseDir);
                }
            }
            if (success) {
                ctx.log("Installed {}", resource.getEntityId());
            }
        }
    } finally {
        this.cleanup();
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) InstallableResource(org.apache.sling.installer.api.InstallableResource) ModelArchiveReader(org.apache.sling.provisioning.model.io.ModelArchiveReader) Reader(java.io.Reader) StringReader(java.io.StringReader) ModelReader(org.apache.sling.provisioning.model.io.ModelReader) RepositoryException(javax.jcr.RepositoryException) Operation(org.apache.sling.repoinit.parser.operations.Operation) IOException(java.io.IOException) OsgiInstaller(org.apache.sling.installer.api.OsgiInstaller) StringReader(java.io.StringReader) RepoInitParsingException(org.apache.sling.repoinit.parser.RepoInitParsingException) File(java.io.File) Session(javax.jcr.Session)

Aggregations

TaskResource (org.apache.sling.installer.api.tasks.TaskResource)28 ChangeStateTask (org.apache.sling.installer.api.tasks.ChangeStateTask)9 InstallTask (org.apache.sling.installer.api.tasks.InstallTask)6 IOException (java.io.IOException)5 Version (org.osgi.framework.Version)5 InstallableResource (org.apache.sling.installer.api.InstallableResource)4 RegisteredResource (org.apache.sling.installer.api.tasks.RegisteredResource)4 Subsystem (org.osgi.service.subsystem.Subsystem)4 File (java.io.File)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 OsgiInstaller (org.apache.sling.installer.api.OsgiInstaller)2 UpdateHandler (org.apache.sling.installer.api.UpdateHandler)2 InstallTaskFactory (org.apache.sling.installer.api.tasks.InstallTaskFactory)2 TaskResourceGroup (org.apache.sling.installer.api.tasks.TaskResourceGroup)2 TransformationResult (org.apache.sling.installer.api.tasks.TransformationResult)2