Search in sources :

Example 1 with OsgiInstaller

use of org.apache.sling.installer.api.OsgiInstaller in project sling by apache.

the class ServicesListener method notifyChange.

/**
     * Notify of service changes from the listeners.
     * If all services are available, register listener and pass resources
     * to the OSGi installer.
     */
public synchronized void notifyChange() {
    // check if all services are available
    final OsgiInstaller installer = (OsgiInstaller) this.installerListener.getService();
    final LaunchpadContentProvider lcp = (LaunchpadContentProvider) this.providerListener.getService();
    final StartupHandler handler = (StartupHandler) this.startupListener.getService();
    final SlingSettingsService settings = (SlingSettingsService) this.settingsListener.getService();
    if (installer != null && lcp != null && handler != null && settings != null) {
        if (!this.installed) {
            this.installed = true;
            this.launchpadListener = new LaunchpadListener(handler);
            final Dictionary<String, Object> props = new Hashtable<String, Object>();
            props.put(Constants.SERVICE_DESCRIPTION, "Apache Sling Launchpad Startup Listener");
            props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
            this.launchpadListenerReg = this.bundleContext.registerService(InstallationListener.class.getName(), launchpadListener, props);
            LaunchpadConfigInstaller.install(installer, lcp, settings.getRunModes());
        }
    }
}
Also used : LaunchpadContentProvider(org.apache.sling.launchpad.api.LaunchpadContentProvider) Hashtable(java.util.Hashtable) StartupHandler(org.apache.sling.launchpad.api.StartupHandler) OsgiInstaller(org.apache.sling.installer.api.OsgiInstaller) SlingSettingsService(org.apache.sling.settings.SlingSettingsService)

Example 2 with OsgiInstaller

use of org.apache.sling.installer.api.OsgiInstaller in project sling by apache.

the class ServicesListener method notifyChange.

public synchronized void notifyChange() {
    final boolean shouldRun = this.installer.hasConfigurations();
    if ((shouldRun && !running) || (!shouldRun && running)) {
        final OsgiInstaller installer = (OsgiInstaller) this.installerListener.getService();
        final SlingSettingsService settings = (SlingSettingsService) this.settingsListener.getService();
        if (installer != null && settings != null && !running) {
            logger.debug("Starting scanner");
            this.startScanner(installer, settings);
        } else if (running && (installer == null || settings == null)) {
            logger.debug("Stopping scanner");
            this.stopScanner();
        }
    }
}
Also used : OsgiInstaller(org.apache.sling.installer.api.OsgiInstaller) SlingSettingsService(org.apache.sling.settings.SlingSettingsService)

Example 3 with OsgiInstaller

use of org.apache.sling.installer.api.OsgiInstaller in project sling by apache.

the class UninstallModelTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    try {
        final OsgiInstaller installer = this.getService(OsgiInstaller.class);
        if (installer == null) {
            ctx.log("Unable to get OSGi Installer service!");
        } else {
            final TaskResource resource = this.getResource();
            ctx.log("Uninstalling {}", resource.getEntityId());
            installer.registerResources("model-" + resource.getAttribute(ModelTransformer.ATTR_FEATURE_NAME), null);
            final String path = (String) resource.getAttribute(ModelTransformer.ATTR_BASE_PATH);
            if (path != null) {
                final File dir = new File(path);
                deleteDirectory(dir);
            }
            this.getResourceGroup().setFinishState(ResourceState.UNINSTALLED);
            ctx.log("Uninstalled {}", resource.getEntityId());
        }
    } finally {
        this.cleanup();
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) File(java.io.File) OsgiInstaller(org.apache.sling.installer.api.OsgiInstaller)

Example 4 with OsgiInstaller

use of org.apache.sling.installer.api.OsgiInstaller 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

OsgiInstaller (org.apache.sling.installer.api.OsgiInstaller)4 File (java.io.File)2 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)2 SlingSettingsService (org.apache.sling.settings.SlingSettingsService)2 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Hashtable (java.util.Hashtable)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 InstallableResource (org.apache.sling.installer.api.InstallableResource)1 LaunchpadContentProvider (org.apache.sling.launchpad.api.LaunchpadContentProvider)1 StartupHandler (org.apache.sling.launchpad.api.StartupHandler)1 ModelArchiveReader (org.apache.sling.provisioning.model.io.ModelArchiveReader)1 ModelReader (org.apache.sling.provisioning.model.io.ModelReader)1 RepoInitParsingException (org.apache.sling.repoinit.parser.RepoInitParsingException)1 Operation (org.apache.sling.repoinit.parser.operations.Operation)1