Search in sources :

Example 16 with TransformationResult

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

the class SubsystemBaseTransformerTest method testOtherRunMode.

@Test
public void testOtherRunMode() throws Exception {
    SlingSettingsService slingSettings = Mockito.mock(SlingSettingsService.class);
    Mockito.when(slingSettings.getRunModes()).thenReturn(new HashSet<String>(Arrays.asList("bar", "tar")));
    SubsystemBaseTransformer sbt = new SubsystemBaseTransformer(slingSettings);
    URL testArchive = getClass().getResource("/test1.subsystem-base");
    RegisteredResource resource = new TestRegisteredResource(testArchive);
    TransformationResult[] tra = sbt.transform(resource);
    assertEquals(1, tra.length);
    TransformationResult tr = tra[0];
    assertEquals("esa", tr.getResourceType());
    assertEquals("test1", tr.getId());
    DeleteOnCloseFileInputStream dcis = (DeleteOnCloseFileInputStream) tr.getInputStream();
    assertTrue("The file backing the stream should exist", dcis.file.exists());
    Set<String> foundBundles = new HashSet<>();
    try (ZipInputStream zis = new ZipInputStream(dcis);
        JarFile jf = new JarFile(testArchive.getFile())) {
        ZipEntry ze = null;
        while ((ze = zis.getNextEntry()) != null) {
            foundBundles.add(ze.getName());
            switch(ze.getName()) {
                case "OSGI-INF/SUBSYSTEM.MF":
                    Manifest mf = new Manifest(zis);
                    Attributes attrs = mf.getMainAttributes();
                    assertEquals("test1", attrs.getValue("Subsystem-SymbolicName"));
                    assertEquals("osgi.subsystem.composite", attrs.getValue("Subsystem-Type"));
                    assertEquals("(c) 2015 yeah!", attrs.getValue("Subsystem-Copyright"));
                    assertEquals("Extra subsystem headers can go here including very long ones " + "that would span multiple lines in a manifest", attrs.getValue("Subsystem-Description"));
                    assertEquals("org.apache.sling.commons.osgi;version=2.3.0;type=osgi.bundle;start-order:=0," + "org.apache.sling.commons.json;version=2.0.12;type=osgi.bundle;start-order:=10," + "org.apache.sling.commons.mime;version=2.1.8;type=osgi.bundle;start-order:=10," + "org.apache.sling.commons.threads;version=3.2.0;type=osgi.bundle;start-order:=20," + "org.apache.sling.commons.contentdetection;version=1.0.2;type=osgi.bundle;start-order:=100", attrs.getValue("Subsystem-Content"));
                    break;
                case "org.apache.sling.commons.osgi-2.3.0.jar":
                    ZipEntry oze = jf.getEntry("Potential_Bundles/0/org.apache.sling.commons.osgi-2.3.0.jar");
                    assertArtifactsEqual(oze.getName(), jf.getInputStream(oze), zis);
                    break;
                case "org.apache.sling.commons.json-2.0.12.jar":
                    ZipEntry jze = jf.getEntry("Potential_Bundles/10/org.apache.sling.commons.json-2.0.12.jar");
                    assertArtifactsEqual(jze.getName(), jf.getInputStream(jze), zis);
                    break;
                case "org.apache.sling.commons.mime-2.1.8.jar":
                    ZipEntry mze = jf.getEntry("Potential_Bundles/10/org.apache.sling.commons.mime-2.1.8.jar");
                    assertArtifactsEqual(mze.getName(), jf.getInputStream(mze), zis);
                    break;
                case "org.apache.sling.commons.threads-3.2.0.jar":
                    ZipEntry tze = jf.getEntry("Potential_Bundles/20/org.apache.sling.commons.threads-3.2.0.jar");
                    assertArtifactsEqual(tze.getName(), jf.getInputStream(tze), zis);
                    break;
                case "org.apache.sling.commons.contentdetection-1.0.2.jar":
                    ZipEntry cze = jf.getEntry("Potential_Bundles/100/org.apache.sling.commons.contentdetection-1.0.2.jar");
                    assertArtifactsEqual(cze.getName(), jf.getInputStream(cze), zis);
                    break;
            }
        }
    }
    assertEquals(new HashSet<>(Arrays.asList("OSGI-INF/SUBSYSTEM.MF", "org.apache.sling.commons.osgi-2.3.0.jar", "org.apache.sling.commons.json-2.0.12.jar", "org.apache.sling.commons.mime-2.1.8.jar", "org.apache.sling.commons.threads-3.2.0.jar", "org.apache.sling.commons.contentdetection-1.0.2.jar")), foundBundles);
    assertFalse("After closing the stream the temp file should have been deleted.", dcis.file.exists());
}
Also used : RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) ZipEntry(java.util.zip.ZipEntry) Attributes(java.util.jar.Attributes) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) SlingSettingsService(org.apache.sling.settings.SlingSettingsService) URL(java.net.URL) ZipInputStream(java.util.zip.ZipInputStream) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) DeleteOnCloseFileInputStream(org.apache.sling.installer.factories.subsystems.base.impl.SubsystemBaseTransformer.DeleteOnCloseFileInputStream) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with TransformationResult

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

the class SubsystemInstaller method transform.

/**
     * @see org.apache.sling.installer.api.tasks.ResourceTransformer#transform(org.apache.sling.installer.api.tasks.RegisteredResource)
     */
public TransformationResult[] transform(final RegisteredResource resource) {
    if (resource.getType().equals(InstallableResource.TYPE_FILE)) {
        if (resource.getURL().endsWith("." + TYPE_SUBSYSTEM)) {
            logger.info("Found potential subsystem resource {}", resource);
            final SubsystemInfo headers = readSubsystemHeaders(resource);
            if (headers != null) {
                // check the version for validity
                boolean validVersion = true;
                try {
                    new Version(headers.version);
                } catch (final IllegalArgumentException iae) {
                    logger.info("Rejecting subsystem {} from {} due to invalid version information: {}.", new Object[] { headers.symbolicName, resource, headers.version });
                    validVersion = false;
                }
                if (validVersion) {
                    final Map<String, Object> attr = new HashMap<String, Object>();
                    attr.put(SubsystemConstants.SUBSYSTEM_SYMBOLICNAME, headers.symbolicName);
                    attr.put(SubsystemConstants.SUBSYSTEM_VERSION, headers.version);
                    final TransformationResult tr = new TransformationResult();
                    tr.setId(headers.symbolicName);
                    tr.setResourceType(TYPE_SUBSYSTEM);
                    tr.setAttributes(attr);
                    tr.setVersion(new Version(headers.version));
                    return new TransformationResult[] { tr };
                }
            } else {
                logger.info("Subsystem resource does not have required headers.");
            }
        }
    }
    return null;
}
Also used : Version(org.osgi.framework.Version) HashMap(java.util.HashMap) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult)

Example 18 with TransformationResult

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

Aggregations

TransformationResult (org.apache.sling.installer.api.tasks.TransformationResult)18 HashMap (java.util.HashMap)9 IOException (java.io.IOException)6 Version (org.osgi.framework.Version)6 RegisteredResource (org.apache.sling.installer.api.tasks.RegisteredResource)5 HashSet (java.util.HashSet)3 Attributes (java.util.jar.Attributes)3 Manifest (java.util.jar.Manifest)3 ZipInputStream (java.util.zip.ZipInputStream)3 InstallableResource (org.apache.sling.installer.api.InstallableResource)3 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Hashtable (java.util.Hashtable)2 JarFile (java.util.jar.JarFile)2 ZipEntry (java.util.zip.ZipEntry)2 ResourceTransformer (org.apache.sling.installer.api.tasks.ResourceTransformer)2 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)2 DeleteOnCloseFileInputStream (org.apache.sling.installer.factories.subsystems.base.impl.SubsystemBaseTransformer.DeleteOnCloseFileInputStream)2 SlingSettingsService (org.apache.sling.settings.SlingSettingsService)2 Test (org.junit.Test)2