Search in sources :

Example 1 with InstallableResource

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

the class LaunchpadConfigInstaller method checkPath.

/**
     * Check the path for installable artifacts.
     */
private boolean checkPath(final String rootPath, final String resourceType, Integer prio) {
    int count = 0;
    final Iterator<String> configPaths = resourceProvider.getChildren(rootPath);
    if (configPaths != null) {
        final int hintPos = rootPath.lastIndexOf('/');
        final String hint = rootPath.substring(hintPos + 1);
        while (configPaths.hasNext()) {
            String path = configPaths.next();
            if (path.endsWith("/")) {
                path = path.substring(0, path.length() - 1);
            }
            if (!checkPath(path, resourceType, prio)) {
                count++;
                final URL url = resourceProvider.getResource(path);
                if (url == null) {
                    logger.debug("Launchpad ignoring path '{}' due to null URL", path);
                    continue;
                }
                Dictionary<String, Object> dict = null;
                if (InstallableResource.TYPE_FILE.equals(resourceType)) {
                    dict = new Hashtable<String, Object>();
                    if (!hint.startsWith(INSTALL_NAME)) {
                        dict.put(InstallableResource.INSTALLATION_HINT, hint);
                    }
                    try {
                        dict.put(InstallableResource.RESOURCE_URI_HINT, url.toURI().toString());
                    } catch (final URISyntaxException e) {
                    // we just ignore this
                    }
                } else if (!hint.equals(CONFIG_NAME)) {
                    final int activeModes = isActive(hint);
                    if (activeModes == 0) {
                        logger.debug("Launchpad ignoring {} : {} due to unactivated run mode: {}", new Object[] { resourceType, path, hint });
                        continue;
                    }
                    prio = PRIORITY + PRIORITY_BOOST * activeModes;
                }
                long lastModified = -1;
                try {
                    lastModified = url.openConnection().getLastModified();
                } catch (final IOException e) {
                // we ignore this
                }
                logger.debug("Launchpad {} will be registered: {}", resourceType, path);
                final String digest = (lastModified > 0 ? String.valueOf(lastModified) : null);
                final InputStream stream = resourceProvider.getResourceAsStream(path);
                installables.add(new InstallableResource(path, stream, dict, digest, resourceType, prio));
            }
        }
    }
    return count > 0;
}
Also used : InputStream(java.io.InputStream) InstallableResource(org.apache.sling.installer.api.InstallableResource) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URL(java.net.URL)

Example 2 with InstallableResource

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

the class LaunchpadConfigInstallerTest method setup.

@Before
public void setup() {
    installer = Mockito.mock(OsgiInstaller.class);
    registered = new HashSet<String>();
    getChildrenReturnsNull = false;
    checkResourceTypes = false;
    final Answer<Void> rCollector = new Answer<Void>() {

        public Void answer(InvocationOnMock invocation) throws Throwable {
            assertEquals("launchpad", invocation.getArguments()[0]);
            final InstallableResource[] resources = (InstallableResource[]) invocation.getArguments()[1];
            for (InstallableResource r : resources) {
                String value = r.getId();
                final Object hint = r.getDictionary() == null ? null : r.getDictionary().get(InstallableResource.INSTALLATION_HINT);
                if (hint != null) {
                    value += "-H" + hint;
                }
                value += "-P" + r.getPriority();
                if (checkResourceTypes) {
                    value += "-T" + r.getType();
                }
                registered.add(value);
            }
            return null;
        }
    };
    Mockito.doAnswer(rCollector).when(installer).registerResources(Matchers.anyString(), Matchers.any(InstallableResource[].class));
}
Also used : Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) InstallableResource(org.apache.sling.installer.api.InstallableResource) OsgiInstaller(org.apache.sling.installer.api.OsgiInstaller) Before(org.junit.Before)

Example 3 with InstallableResource

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

the class UpdateHandlerTest method testSimpleUpdate.

@Test
public void testSimpleUpdate() throws Exception {
    final UpdateHandlerImpl up = new UpdateHandlerImpl();
    final Dictionary<String, Object> dict = new Hashtable<String, Object>();
    dict.put(UpdateHandler.PROPERTY_SCHEMES, TYPE);
    this.serviceRegistrations.add(this.bundleContext.registerService(UpdateHandler.class, up, dict));
    final Dictionary<String, Object> data = new Hashtable<String, Object>();
    data.put("foo", "bar");
    final InstallableResource ir = new InstallableResource("/resource/a." + TYPE, null, data, null, InstallableResource.TYPE_PROPERTIES, null);
    final Barrier b = this.getInstallerListenerBarrier();
    this.installer.registerResources(TYPE, new InstallableResource[] { ir });
    b.block();
    b.reg.unregister();
    assertNotNull("Resource should be installed: " + installed, installed.get(TYPE) + ":a");
    final Dictionary<String, Object> newData = new Hashtable<String, Object>();
    data.put("bar", "foo");
    this.resourceChangeListener.resourceAddedOrUpdated(TYPE, "a", null, newData, null);
    final UpdateResult ur = up.waitForUpdate();
    assertNotNull(ur);
    assertEquals(TYPE + ":/resource/b/a." + TYPE, ur.getURL());
    this.resourceChangeListener.resourceRemoved(TYPE, "a");
    final UpdateResult r2 = up.waitForUpdate();
    assertNotNull(r2);
    assertEquals(TYPE + ":/resource/b/a." + TYPE, r2.getURL());
}
Also used : UpdateHandler(org.apache.sling.installer.api.UpdateHandler) Hashtable(java.util.Hashtable) InstallableResource(org.apache.sling.installer.api.InstallableResource) CyclicBarrier(java.util.concurrent.CyclicBarrier) UpdateResult(org.apache.sling.installer.api.UpdateResult) Test(org.junit.Test)

Example 4 with InstallableResource

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

the class WatchedFolder method scanNode.

private void scanNode(final Node folder, final ScanResult result, final Set<String> resourcesSeen) throws RepositoryException {
    final NodeIterator it = folder.getNodes();
    while (it.hasNext()) {
        final Node n = it.nextNode();
        boolean processed = false;
        for (JcrInstaller.NodeConverter nc : converters) {
            final InstallableResource r = nc.convertNode(n, priority);
            if (r != null) {
                processed = true;
                resourcesSeen.add(r.getId());
                final String oldDigest = digests.get(r.getId());
                if (r.getDigest().equals(oldDigest)) {
                    logger.debug("Digest didn't change, ignoring " + r);
                } else {
                    result.toAdd.add(r);
                }
                break;
            }
        }
        if (!processed) {
            this.scanNode(n, result, resourcesSeen);
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) InstallableResource(org.apache.sling.installer.api.InstallableResource) Node(javax.jcr.Node)

Example 5 with InstallableResource

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

the class WatchedFolder method scan.

/**
     * Scan the contents of our folder and return the corresponding
     * <code>ScanResult</code> containing the <code>InstallableResource</code>s.
     */
public ScanResult scan() throws RepositoryException {
    logger.debug("Scanning {}", path);
    Node folder = null;
    if (session.itemExists(path)) {
        final Item i = session.getItem(path);
        if (i.isNode()) {
            folder = (Node) i;
        }
    }
    // Return an InstallableResource for all child nodes for which we have a NodeConverter
    final ScanResult result = new ScanResult();
    final Set<String> resourcesSeen = new HashSet<String>();
    if (folder != null) {
        scanNode(folder, result, resourcesSeen);
    }
    // unregistered from OsgiInstaller
    for (final String url : existingResourceUrls) {
        if (!resourcesSeen.contains(url)) {
            result.toRemove.add(url);
        }
    }
    for (final String u : result.toRemove) {
        existingResourceUrls.remove(u);
        digests.remove(u);
    }
    // Update saved digests of the resources that we're returning
    for (final InstallableResource r : result.toAdd) {
        existingResourceUrls.add(r.getId());
        digests.put(r.getId(), r.getDigest());
    }
    needsScan = false;
    return result;
}
Also used : Item(javax.jcr.Item) InstallableResource(org.apache.sling.installer.api.InstallableResource) Node(javax.jcr.Node) HashSet(java.util.HashSet)

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