Search in sources :

Example 26 with InstallableResource

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

the class ConfigInstallTest method testInstallAndRemoveConfig.

@Test
public void testInstallAndRemoveConfig() 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 config
    final InstallableResource[] rsrc = getInstallableResource(cfgPid, cfgData);
    installer.updateResources(URL_SCHEME, rsrc, null);
    Configuration cfg = waitForConfiguration("After installing", cfgPid, true);
    assertEquals("Config value must match", "bar", cfg.getProperties().get("foo"));
    // remove resource
    installer.updateResources(URL_SCHEME, null, new String[] { rsrc[0].getId() });
    waitForConfiguration("After removing", cfgPid, false);
    // Reinstalling with same digest must work
    installer.updateResources(URL_SCHEME, rsrc, null);
    cfg = waitForConfiguration("After reinstalling", 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 27 with InstallableResource

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

the class BundleInstaller method storeConfig.

/**
     * Used to stores/overrides the ids of installed resource for a bundle.
     * @param bundle
     * @param resources
     * @throws IOException
     */
private void storeConfig(Bundle bundle, Collection<InstallableResource> resources) throws IOException {
    synchronized (configDir) {
        File config = new File(configDir, bundle.getBundleId() + ".resources");
        if (config.exists()) {
            config.delete();
        }
        FileOutputStream out = new FileOutputStream(config);
        List<String> ids = new ArrayList<String>(resources.size());
        for (InstallableResource resource : resources) {
            ids.add(resource.getId());
        }
        try {
            IOUtils.writeLines(ids, null, out, "UTF-8");
        } finally {
            IOUtils.closeQuietly(out);
        }
    }
}
Also used : InstallableResource(org.apache.sling.installer.api.InstallableResource) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) File(java.io.File)

Example 28 with InstallableResource

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

the class BundleInstaller method createInstallableResource.

/**
     * Creates an {@link InstallableResource} for {@link URL}s of files within
     * the parsed bundle.
     *
     * @param bundle the bundle containing the parsed resource
     * @param bundleResource a resource within the bundle that need to be installed
     *
     * @return the installable resource or <code>null</code> in case of an error
     */
private InstallableResource createInstallableResource(Bundle bundle, String path, URL bundleResource) {
    //define the id
    String relPath = getInstallableResourceId(path, bundleResource);
    String name = FilenameUtils.getName(relPath);
    if (name == null || name.isEmpty()) {
        //ignore directories!
        return null;
    }
    InstallableResource resource;
    try {
        /*
             * Notes:
             *  - use <relativepath> as id
             *  - parse null as type to enable autodetection for configs as
             *    implemented by InternalReseouce.create(..)
             *  - we use the symbolic name and the modification date of the bundle as digest
             *  - the Dictionary will be ignored if an input stream is present
             *    so it is best to parse null
             *  - No idea how the priority is used by the Sling Installer. For
             *    now parse null than the default priority is used.
             */
        resource = new InstallableResource(relPath, bundleResource.openStream(), null, String.valueOf(bundle.getSymbolicName() + bundle.getLastModified()), null, null);
        log.info(" ... found installable resource " + bundleResource);
    } catch (IOException e) {
        log.error(String.format("Unable to process configuration File %s from Bundle %s", bundleResource, bundle.getSymbolicName()), e);
        return null;
    }
    return resource;
}
Also used : InstallableResource(org.apache.sling.installer.api.InstallableResource) IOException(java.io.IOException)

Example 29 with InstallableResource

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

the class DictionaryDigestTest method testDictionaryOrderDoesNotMatter.

@org.junit.Test
public void testDictionaryOrderDoesNotMatter() throws Exception {
    final Dictionary<String, Object> a = new Hashtable<String, Object>();
    a.put("one", "A");
    a.put("two", "B");
    a.put("three", "C");
    final Dictionary<String, Object> b = new Hashtable<String, Object>();
    b.put("two", "B");
    b.put("one", "A");
    b.put("three", "C");
    assertEquals("Same data in different order must have same digest", create(new InstallableResource("a", null, a, null, null, null)).getDigest(), create(new InstallableResource("a", null, b, null, null, null)).getDigest());
}
Also used : Hashtable(java.util.Hashtable) InstallableResource(org.apache.sling.installer.api.InstallableResource)

Example 30 with InstallableResource

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

the class InternalResourceTest method testSimpleProps.

@Test
public void testSimpleProps() throws IOException {
    final String[] types = new String[] { InstallableResource.TYPE_CONFIG, InstallableResource.TYPE_PROPERTIES, null, "zip" };
    for (int i = 0; i < types.length; i++) {
        final InstallableResource instRes = new InstallableResource("1", null, getSimpleDict(), null, types[i], null);
        final InternalResource ir = InternalResource.create(SCHEME, instRes);
        assertEquals(SCHEME + ":1", ir.getURL());
        assertEquals("1", ir.getId());
        assertNotNull(ir.getDictionary());
        assertIsSimpleDict(ir.getDictionary());
        assertNotNull(ir.getPrivateCopyOfDictionary());
        assertIsSimpleDict(ir.getPrivateCopyOfDictionary());
        assertNull(ir.getInputStream());
        assertNull(ir.getPrivateCopyOfFile());
        if ("zip".equals(types[i])) {
            assertEquals("zip", ir.getType());
        } else {
            assertEquals(InstallableResource.TYPE_PROPERTIES, ir.getType());
        }
        assertNotNull(ir.getDigest());
        assertEquals(InstallableResource.DEFAULT_PRIORITY, ir.getPriority());
    }
}
Also used : InstallableResource(org.apache.sling.installer.api.InstallableResource) Test(org.junit.Test)

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