use of org.apache.sling.provisioning.model.Model in project sling by apache.
the class PreparePackageMojoTest method testSubsystemBaseGeneration.
@Test
public void testSubsystemBaseGeneration() throws Exception {
// Provide the system with some artifacts that are known to be in the local .m2 repo
// These are explicitly included in the test section of the pom.xml
PreparePackageMojo ppm = getMojoUnderTest("org.apache.sling/org.apache.sling.commons.classloader/1.3.2", "org.apache.sling/org.apache.sling.commons.classloader/1.3.2/app", "org.apache.sling/org.apache.sling.commons.contentdetection/1.0.2", "org.apache.sling/org.apache.sling.commons.johnzon/1.0.0", "org.apache.sling/org.apache.sling.commons.mime/2.1.8", "org.apache.sling/org.apache.sling.commons.osgi/2.3.0", "org.apache.sling/org.apache.sling.commons.threads/3.2.0");
try {
// The launchpad feature is a prerequisite for the model
String modelTxt = "[feature name=:launchpad]\n" + "[artifacts]\n" + " org.apache.sling/org.apache.sling.commons.classloader/1.3.2\n" + "" + "[feature name=test1 type=osgi.subsystem.composite]\n" + "" + "[:subsystem-manifest startLevel=123]\n" + " Subsystem-Description: Extra subsystem headers can go here including very long ones that would span multiple lines in a manifest\n" + " Subsystem-Copyright: (c) 2015 yeah!\n" + "" + "[artifacts]\n" + " org.apache.sling/org.apache.sling.commons.osgi/2.3.0\n" + "" + "[artifacts startLevel=10]\n" + " org.apache.sling/org.apache.sling.commons.johnzon/1.0.0\n" + " org.apache.sling/org.apache.sling.commons.mime/2.1.8\n" + "" + "[artifacts startLevel=20 runModes=foo,bar,:blah]\n" + " org.apache.sling/org.apache.sling.commons.threads/3.2.0\n" + "" + "[artifacts startLevel=100 runModes=bar]\n" + " org.apache.sling/org.apache.sling.commons.contentdetection/1.0.2\n";
Model model = ModelReader.read(new StringReader(modelTxt), null);
ppm.execute(model);
File generatedFile = new File(ppm.getTmpDir() + "/test1.subsystem-base");
try (JarFile jf = new JarFile(generatedFile)) {
// Test META-INF/MANIFEST.MF
Manifest mf = jf.getManifest();
Attributes attrs = mf.getMainAttributes();
String expected = "Potential_Bundles/0/org.apache.sling.commons.osgi-2.3.0.jar|" + "Potential_Bundles/10/org.apache.sling.commons.johnzon-1.0.0.jar|" + "Potential_Bundles/10/org.apache.sling.commons.mime-2.1.8.jar";
assertEquals(expected, attrs.getValue("_all_"));
assertEquals("Potential_Bundles/20/org.apache.sling.commons.threads-3.2.0.jar", attrs.getValue("foo"));
assertEquals("Potential_Bundles/20/org.apache.sling.commons.threads-3.2.0.jar|" + "Potential_Bundles/100/org.apache.sling.commons.contentdetection-1.0.2.jar", attrs.getValue("bar"));
// Test SUBSYSTEM-MANIFEST-BASE.MF
ZipEntry smbZE = jf.getEntry("SUBSYSTEM-MANIFEST-BASE.MF");
try (InputStream smbIS = jf.getInputStream(smbZE)) {
Manifest smbMF = new Manifest(smbIS);
Attributes smbAttrs = smbMF.getMainAttributes();
assertEquals("test1", smbAttrs.getValue("Subsystem-SymbolicName"));
assertEquals("osgi.subsystem.composite", smbAttrs.getValue("Subsystem-Type"));
assertEquals("(c) 2015 yeah!", smbAttrs.getValue("Subsystem-Copyright"));
assertEquals("Extra subsystem headers can go here including very long ones " + "that would span multiple lines in a manifest", smbAttrs.getValue("Subsystem-Description"));
}
// Test embedded bundles
File mrr = getMavenRepoRoot();
File soj = getMavenArtifactFile(mrr, "org.apache.sling", "org.apache.sling.commons.osgi", "2.3.0");
ZipEntry sojZE = jf.getEntry("Potential_Bundles/0/org.apache.sling.commons.osgi-2.3.0.jar");
try (InputStream is = jf.getInputStream(sojZE)) {
assertArtifactsEqual(soj, is);
}
File sjj = getMavenArtifactFile(mrr, "org.apache.sling", "org.apache.sling.commons.johnzon", "1.0.0");
ZipEntry sjZE = jf.getEntry("Potential_Bundles/10/org.apache.sling.commons.johnzon-1.0.0.jar");
try (InputStream is = jf.getInputStream(sjZE)) {
assertArtifactsEqual(sjj, is);
}
File smj = getMavenArtifactFile(mrr, "org.apache.sling", "org.apache.sling.commons.mime", "2.1.8");
ZipEntry smjZE = jf.getEntry("Potential_Bundles/10/org.apache.sling.commons.mime-2.1.8.jar");
try (InputStream is = jf.getInputStream(smjZE)) {
assertArtifactsEqual(smj, is);
}
File stj = getMavenArtifactFile(mrr, "org.apache.sling", "org.apache.sling.commons.threads", "3.2.0");
ZipEntry stjZE = jf.getEntry("Potential_Bundles/20/org.apache.sling.commons.threads-3.2.0.jar");
try (InputStream is = jf.getInputStream(stjZE)) {
assertArtifactsEqual(stj, is);
}
File ctj = getMavenArtifactFile(mrr, "org.apache.sling", "org.apache.sling.commons.contentdetection", "1.0.2");
ZipEntry ctjZE = jf.getEntry("Potential_Bundles/100/org.apache.sling.commons.contentdetection-1.0.2.jar");
try (InputStream is = jf.getInputStream(ctjZE)) {
assertArtifactsEqual(ctj, is);
}
}
} finally {
FileUtils.deleteDirectory(new File(ppm.project.getBuild().getDirectory()));
}
}
use of org.apache.sling.provisioning.model.Model in project sling by apache.
the class LaunchpadComparer method run.
public void run() throws Exception {
System.out.format("Computing differences between Launchpad versions %s and %s...%n", firstVersion, secondVersion);
// 1. download artifacts
AetherSetup aether = new AetherSetup();
File fromFile = aether.download(Artifacts.launchpadCoordinates(firstVersion));
File toFile = aether.download(Artifacts.launchpadCoordinates(secondVersion));
// 2. parse artifact definitions
Model model;
try (BufferedReader reader = Files.newBufferedReader(toFile.toPath())) {
model = ModelUtility.getEffectiveModel(ModelReader.read(reader, null));
}
Map<ArtifactKey, Artifact> to = model.getFeatures().stream().flatMap(f -> f.getRunModes().stream()).flatMap(r -> r.getArtifactGroups().stream()).flatMap(g -> StreamSupport.stream(g.spliterator(), false)).collect(Collectors.toMap(a -> new ArtifactKey(a), Function.identity()));
BundleList readBundleList = BundleListUtils.readBundleList(fromFile);
Map<ArtifactKey, Artifact> from = readBundleList.getStartLevels().stream().flatMap(sl -> sl.getBundles().stream()).collect(Collectors.toMap(b -> new ArtifactKey(b), LaunchpadComparer::newArtifact));
// 3. generate added / removed / changed
Set<Artifact> removed = Sets.difference(from.keySet(), to.keySet()).stream().map(k -> from.get(k)).collect(Collectors.toSet());
Set<Artifact> added = Sets.difference(to.keySet(), from.keySet()).stream().map(k -> to.get(k)).collect(Collectors.toSet());
Map<ArtifactKey, VersionChange> changed = to.values().stream().filter(k -> !added.contains(k) && !removed.contains(k)).map(k -> new ArtifactKey(k)).filter(k -> !Objects.equals(to.get(k).getVersion(), from.get(k).getVersion())).collect(Collectors.toMap(Function.identity(), k -> new VersionChange(from.get(k).getVersion(), to.get(k).getVersion())));
// 4. output changes
System.out.println("Added ");
added.stream().sorted().forEach(LaunchpadComparer::outputFormatted);
System.out.println("Removed ");
removed.stream().sorted().forEach(LaunchpadComparer::outputFormatted);
System.out.println("Changed");
changed.entrySet().stream().sorted((a, b) -> a.getKey().compareTo(b.getKey())).forEach(LaunchpadComparer::outputFormatted);
}
use of org.apache.sling.provisioning.model.Model in project sling by apache.
the class PreparePackageMojoTest method testBSNRenaming.
@Test
public void testBSNRenaming() throws Exception {
// Provide the system with some artifacts that are known to be in the local .m2 repo
// These are explicitly included in the test section of the pom.xml
PreparePackageMojo ppm = getMojoUnderTest("org.apache.sling/org.apache.sling.commons.classloader/1.3.2", "org.apache.sling/org.apache.sling.commons.classloader/1.3.2/app", "org.apache.sling/org.apache.sling.commons.johnzon/1.0.0");
try {
String modelTxt = "[feature name=:launchpad]\n" + "[artifacts]\n" + " org.apache.sling/org.apache.sling.commons.classloader/1.3.2\n" + "" + "[feature name=rename_test]\n" + " org.apache.sling/org.apache.sling.commons.johnzon/1.0.0 [bundle:rename-bsn=r-foo.bar.renamed.sling.commons.johnzon]\n";
Model model = ModelReader.read(new StringReader(modelTxt), null);
ppm.execute(model);
File orgJar = getMavenArtifactFile(getMavenRepoRoot(), "org.apache.sling", "org.apache.sling.commons.johnzon", "1.0.0");
File generatedJar = new File(ppm.getTmpDir() + "/r-foo.bar.renamed.sling.commons.johnzon-1.0.0.jar");
compareJarContents(orgJar, generatedJar);
try (JarFile jfOrg = new JarFile(orgJar);
JarFile jfNew = new JarFile(generatedJar)) {
Manifest mfOrg = jfOrg.getManifest();
Manifest mfNew = jfNew.getManifest();
Attributes orgAttrs = mfOrg.getMainAttributes();
Attributes newAttrs = mfNew.getMainAttributes();
for (Object key : orgAttrs.keySet()) {
String orgVal = orgAttrs.getValue(key.toString());
String newVal = newAttrs.getValue(key.toString());
if ("Bundle-SymbolicName".equals(key.toString())) {
assertEquals("Should have recorded the original Bundle-SymbolicName", orgVal, newAttrs.getValue("X-Original-Bundle-SymbolicName"));
assertEquals("r-foo.bar.renamed.sling.commons.johnzon", newVal);
} else {
assertEquals("Different keys: " + key, orgVal, newVal);
}
}
}
} finally {
FileUtils.deleteDirectory(new File(ppm.project.getBuild().getDirectory()));
}
}
use of org.apache.sling.provisioning.model.Model in project sling by apache.
the class IOTest method testMultilineConfiguration.
@Test
public void testMultilineConfiguration() throws Exception {
final Model m = ModelUtility.getEffectiveModel(U.readCompleteTestModel(new String[] { "configadmin.txt" }));
final List<Configuration> configs = new ArrayList<Configuration>();
for (final Configuration c : m.getFeature("configadmin").getRunMode().getConfigurations()) {
configs.add(c);
}
assertEquals(5, configs.size());
final Configuration cfgA = configs.get(0);
assertEquals("org.apache.test.A", cfgA.getPid());
assertNull(cfgA.getFactoryPid());
assertEquals(1, cfgA.getProperties().size());
assertEquals("A", cfgA.getProperties().get("name"));
final Configuration cfgB = configs.get(1);
assertEquals("org.apache.test.B", cfgB.getPid());
assertNull(cfgB.getFactoryPid());
assertEquals(2, cfgB.getProperties().size());
assertEquals("B", cfgB.getProperties().get("name"));
assertArrayEquals(new String[] { "one", "two", "three" }, (String[]) cfgB.getProperties().get("array"));
final Configuration cfgC = configs.get(2);
assertEquals("org.apache.test.C", cfgC.getPid());
assertNull(cfgC.getFactoryPid());
assertEquals(2, cfgC.getProperties().size());
assertEquals("C", cfgC.getProperties().get("name"));
assertArrayEquals(new Integer[] { 1, 2, 3 }, (Integer[]) cfgC.getProperties().get("array"));
final Configuration cfgD = configs.get(3);
assertEquals("org.apache.test.D", cfgD.getPid());
assertEquals("Here is\na multiline\nstring", cfgD.getProperties().get("textA"));
assertEquals("Another one\nusing\nescaped newlines", cfgD.getProperties().get("textB"));
final Configuration cfgE = configs.get(4);
assertEquals("org.apache.test.E", cfgE.getPid());
assertNull(cfgE.getFactoryPid());
assertEquals(4, cfgE.getProperties().size());
// TODO values will need to change once SLING-5914 is fixed
assertEquals(6.0995758E-316, cfgE.getProperties().get("doubleValue"));
assertEquals(6.461264E-31f, cfgE.getProperties().get("floatValue"));
assertArrayEquals(new Double[] { 1.598088874E-315d, 2.09215452E-315d }, (Double[]) cfgE.getProperties().get("doubles"));
assertArrayEquals(new Float[] { 3.7971794E-20f, 1.4675382E-16f }, (Float[]) cfgE.getProperties().get("floats"));
}
use of org.apache.sling.provisioning.model.Model in project sling by apache.
the class FrameworkSetup method call.
public Object call() throws Exception {
final Model model = require(Launcher.MODEL_KEY, Model.class);
final LauncherListener listener = (LauncherListener) get(Launcher.LISTENER_KEY);
log.info("Setting OSGi framework properties");
final Map<String, String> fprops = new FrameworkProperties(model).getProperties(null);
log.info("Starting the OSGi framework");
final FrameworkFactory factory = (FrameworkFactory) getClass().getClassLoader().loadClass("org.apache.felix.framework.FrameworkFactory").newInstance();
final Framework framework = factory.newFramework(fprops);
framework.start();
final RunModeFilter rmFilter = new RunModeFilter();
final Configurations cfg = new Configurations(framework.getBundleContext(), model, rmFilter);
setShutdownHook(framework, new Closeable[] { cfg });
log.info("OSGi framework started");
log.info("Installing bundles from provisioning model");
final BundlesInstaller bi = new BundlesInstaller(model, rmFilter);
final BundleContext bc = framework.getBundleContext();
bi.installBundles(bc, Launcher.NOT_CRANKSTART_FILTER);
cfg.maybeConfigure();
// TODO shall we gradually increase start levels like the launchpad does?? Reuse that DefaultStartupHandler code?
final Bundle[] bundles = bc.getBundles();
log.info("Starting all bundles ({} bundles installed)", bundles.length);
int started = 0;
int failed = 0;
for (Bundle b : bundles) {
if (isFragment(b)) {
started++;
} else {
try {
b.start();
started++;
} catch (BundleException be) {
failed++;
log.warn("Error starting bundle " + b.getSymbolicName(), be);
}
}
cfg.maybeConfigure();
}
if (failed == 0) {
log.info("All {} bundles started.", started);
} else {
log.info("{} bundles started, {} failed to start, total {}", started, failed, bundles.length);
}
log.info("OSGi setup done, waiting for framework to stop");
if (listener != null) {
listener.onStartup(started, failed, bundles.length);
}
framework.waitForStop(0);
if (listener != null) {
listener.onShutdown();
}
return null;
}
Aggregations