Search in sources :

Example 41 with InstallableResource

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

the class ConfigInstallTest method testInstallUpdateRemoveConfigResource.

@Test
public void testInstallUpdateRemoveConfigResource() throws Exception {
    final Dictionary<String, Object> cfgData = new Hashtable<String, Object>();
    cfgData.put("foo", "bar");
    final String cfgPid = getClass().getSimpleName() + "." + uniqueID();
    // install config
    final InstallableResource rsrc = new InstallableResource("/configA/" + cfgPid, null, cfgData, null, InstallableResource.TYPE_PROPERTIES, 10);
    installer.updateResources(URL_SCHEME, new InstallableResource[] { rsrc }, null);
    // get config
    waitForConfigValue("After installing", cfgPid, "foo", "bar");
    // create second configuration
    final Dictionary<String, Object> secondData = new Hashtable<String, Object>();
    secondData.put("foo", "bla");
    final InstallableResource rsrc2 = new InstallableResource("/configB/" + cfgPid, null, secondData, null, InstallableResource.TYPE_PROPERTIES, 20);
    installer.updateResources(URL_SCHEME, new InstallableResource[] { rsrc2 }, null);
    // get updated config
    waitForConfigValue("After updating", cfgPid, "foo", "bla");
    // remove config
    installer.updateResources(URL_SCHEME, null, new String[] { "/configB/" + cfgPid });
    waitForConfigValue("After deleting", cfgPid, "foo", "bar");
}
Also used : Hashtable(java.util.Hashtable) InstallableResource(org.apache.sling.installer.api.InstallableResource) Test(org.junit.Test)

Example 42 with InstallableResource

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

the class ConfigInstallTest method testInstallUpdateRemoveConfig.

@Test
public void testInstallUpdateRemoveConfig() throws Exception {
    final Dictionary<String, Object> cfgData = new Hashtable<String, Object>();
    cfgData.put("foo", "bar");
    final String cfgPid = getClass().getSimpleName() + "." + uniqueID();
    // install config
    final InstallableResource rsrc = new InstallableResource("/configA/" + cfgPid, null, cfgData, null, InstallableResource.TYPE_PROPERTIES, 10);
    installer.updateResources(URL_SCHEME, new InstallableResource[] { rsrc }, null);
    // get config
    final Configuration cfg = waitForConfigValue("After installing", cfgPid, "foo", "bar");
    // update configuration
    final Dictionary<String, Object> secondData = new Hashtable<String, Object>();
    secondData.put("foo", "bla");
    cfg.update(secondData);
    waitForResource(URL_SCHEME + ":/configA/" + cfgPid, ResourceState.IGNORED);
    // get updated config
    final Configuration secondCfg = waitForConfigValue("After updating", cfgPid, "foo", "bla");
    // remove config
    secondCfg.delete();
    waitForResource(URL_SCHEME + ":/configA/" + cfgPid, ResourceState.INSTALLED);
    waitForConfigValue("After deleting", cfgPid, "foo", "bar");
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) InstallableResource(org.apache.sling.installer.api.InstallableResource) Test(org.junit.Test)

Example 43 with InstallableResource

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

the class ConfigInstallTest method testInstallConfigWindowsPath.

@Test
public void testInstallConfigWindowsPath() throws Exception {
    final Dictionary<String, Object> cfgData = new Hashtable<String, Object>();
    cfgData.put("foo", "bar");
    final String cfgPid = getClass().getSimpleName() + "." + uniqueID();
    assertNull("Config " + cfgPid + " must not be found before test", findConfiguration(cfgPid));
    // install configs
    //Following patterns work fine. Problem occurs one with windows seprator
    // - "c:/foo/bar/"
    // - "/foo/bar/"
    final String uri = "c:\\foo\bar\\";
    final InstallableResource result = new MockInstallableResource(uri + cfgPid, copy(cfgData), null, null, 100);
    final InstallableResource[] rsrc = new InstallableResource[] { result };
    installer.updateResources(URL_SCHEME, rsrc, null);
    Configuration cfg = waitForConfiguration("After installing", cfgPid, true);
    assertEquals("Config value must match", "bar", cfg.getProperties().get("foo"));
    // remove again
    installer.updateResources(URL_SCHEME, null, new String[] { rsrc[0].getId() });
    waitForConfiguration("After removing for the second time", cfgPid, false);
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) InstallableResource(org.apache.sling.installer.api.InstallableResource) Test(org.junit.Test)

Example 44 with InstallableResource

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

the class Installer method createResource.

private InstallableResource createResource(final File file) {
    try {
        // check for run modes
        final String name = file.getAbsolutePath().substring(this.prefix.length()).replace(File.separatorChar, '/');
        boolean isActive = true;
        Integer prio = null;
        final int pos = name.indexOf('/');
        if (pos != -1 && name.startsWith("install.")) {
            final String runModes = name.substring(8, pos);
            final int activeModes = this.isActive(runModes);
            if (activeModes > 0) {
                prio = InstallableResource.DEFAULT_PRIORITY + activeModes;
            } else {
                isActive = false;
            }
        }
        if (isActive) {
            final InputStream is = new FileInputStream(file);
            final String digest = String.valueOf(file.lastModified());
            // if this is a bundle check for start level directory!
            final Dictionary<String, Object> dict = new Hashtable<String, Object>();
            if (file.getName().endsWith(".jar") || file.getName().endsWith(".war")) {
                final String parentName = file.getParentFile().getName();
                try {
                    final int startLevel = Integer.valueOf(parentName);
                    if (startLevel > 0) {
                        dict.put(InstallableResource.BUNDLE_START_LEVEL, startLevel);
                    }
                } catch (NumberFormatException nfe) {
                // ignore this
                }
            }
            dict.put(InstallableResource.RESOURCE_URI_HINT, file.toURI().toString());
            return new InstallableResource(file.getAbsolutePath(), is, dict, digest, null, prio);
        } else {
            logger.info("Ignoring inactive resource at {}", file);
        }
    } catch (IOException io) {
        logger.error("Unable to read file " + file, io);
    }
    return null;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Hashtable(java.util.Hashtable) InstallableResource(org.apache.sling.installer.api.InstallableResource) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 45 with InstallableResource

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

InstallableResource (org.apache.sling.installer.api.InstallableResource)47 Hashtable (java.util.Hashtable)23 Test (org.junit.Test)21 File (java.io.File)8 IOException (java.io.IOException)8 InputStream (java.io.InputStream)7 Configuration (org.osgi.service.cm.Configuration)6 FileInputStream (java.io.FileInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)3 TransformationResult (org.apache.sling.installer.api.tasks.TransformationResult)3 FileOutputStream (java.io.FileOutputStream)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 URL (java.net.URL)2 LinkedList (java.util.LinkedList)2 Node (javax.jcr.Node)2 OsgiInstaller (org.apache.sling.installer.api.OsgiInstaller)2 ModelArchiveReader (org.apache.sling.provisioning.model.io.ModelArchiveReader)2