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());
}
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;
}
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));
}
Aggregations