You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/04/27 09:58:42 UTC

[sling-org-apache-sling-feature-karaf] 01/11: Move feature model to whiteboard git

This is an automated email from the ASF dual-hosted git repository.

davidb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-karaf.git

commit ec3240adcf8df0dce4044e90968dffb10cc70194
Author: Carsten Ziegeler <cz...@adobe.com>
AuthorDate: Fri Nov 3 15:06:50 2017 +0100

    Move feature model to whiteboard git
---
 pom.xml                                            |  72 ++++++++
 .../sling/feature/karaf/KarafFeatureWriter.java    | 195 +++++++++++++++++++++
 .../apache/sling/feature/karaf/package-info.java   |  23 +++
 3 files changed, 290 insertions(+)

diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..4591599
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+    <!--
+        Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+        agreements. See the NOTICE file distributed with this work for additional information
+        regarding copyright ownership. The ASF licenses this file to you under the Apache License,
+        Version 2.0 (the "License"); you may not use this file except in compliance with the
+        License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
+
+        Unless required by applicable law or agreed to in writing, software distributed under the
+        License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+        either express or implied. See the License for the specific language governing permissions
+        and limitations under the License.
+    -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>32</version>
+        <relativePath />
+    </parent>
+
+    <artifactId>org.apache.sling.feature.karaf</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <packaging>bundle</packaging>
+
+    <name>Apache Sling Feature Karaf</name>
+    <description>
+        A feature describes an OSGi system
+    </description>
+
+    <properties>
+        <sling.java.version>8</sling.java.version>
+    </properties>
+
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/tooling/support/feature-karaf</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/tooling/support/feature-karaf</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/trunk/tooling/support/feature-karaf</url>
+    </scm>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+            </plugin>
+        </plugins>
+    </build>
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature.support</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.core</artifactId>
+            <version>6.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+</project>
diff --git a/src/main/java/org/apache/sling/feature/karaf/KarafFeatureWriter.java b/src/main/java/org/apache/sling/feature/karaf/KarafFeatureWriter.java
new file mode 100644
index 0000000..7402f04
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/karaf/KarafFeatureWriter.java
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.sling.feature.karaf;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Configuration;
+import org.apache.sling.feature.Configurations;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.support.ArtifactHandler;
+import org.apache.sling.feature.support.ArtifactManager;
+import org.apache.sling.feature.support.ConfigurationUtil;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.nio.file.Files;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+
+/**
+ * This writer writes out a Karaf feature XML.
+ *
+ */
+public class KarafFeatureWriter {
+
+    public static void writeKAR(final OutputStream os,
+            final Feature feature,
+            final ArtifactManager artifactManager)
+    throws IOException {
+        // check for repoinit extension
+        final Extension repoinitExt = feature.getExtensions().getByName(Extension.NAME_REPOINIT);
+        File configurationBundleFile = null;
+        Artifact configuratorBundle = null;
+        try {
+            final Configurations configs = new Configurations();
+            configs.addAll(feature.getConfigurations());
+            if ( repoinitExt != null ) {
+                final Configuration cfg = new Configuration("org.apache.sling.jcr.repoinit.RepositoryInitializer",
+                        "feature-" + feature.getId().getArtifactId());
+                cfg.getProperties().put("scripts", repoinitExt.getText());
+                configs.add(cfg);
+            }
+            if ( !configs.isEmpty() ) {
+                configurationBundleFile = Files.createTempFile(null, null).toFile();
+
+                try ( final FileOutputStream fos = new FileOutputStream(configurationBundleFile)) {
+                    final Map<String, String> map;
+                    if ( repoinitExt == null ) {
+                        map = null;
+                    } else {
+                        map = Collections.singletonMap("Require-Capability",
+                                ConfigurationUtil.REQUIRE_REPOINIT_CAPABILITY);
+                    }
+                    ConfigurationUtil.createConfiguratorBundle(os,
+                            configs,
+                            feature.getId().getGroupId() + "." + feature.getId().getArtifactId(),
+                            feature.getId().getOSGiVersion().toString(),
+                            map);
+                    configuratorBundle = new Artifact(new ArtifactId(feature.getId().getGroupId(),
+                            feature.getId().getArtifactId(),
+                            feature.getId().getVersion(), "configurator", null));
+                }
+            }
+
+            try ( final ZipOutputStream jos = new ZipOutputStream(os) ) {
+                // repository/features.xml
+                // repository/{maven-path-to-bundle}
+                final ZipEntry xmlEntry = new ZipEntry("repository/features.xml");
+                jos.putNextEntry(xmlEntry);
+
+                final Writer writer = new OutputStreamWriter(jos);
+                writeFeaturesXML(writer, feature, configuratorBundle);
+                writer.flush();
+
+                jos.closeEntry();
+
+                for(final Map.Entry<Integer, List<Artifact>> entry : feature.getBundles().getBundlesByStartLevel().entrySet()) {
+                    for(final Artifact artifact : entry.getValue()) {
+                        final ArtifactHandler handler = artifactManager.getArtifactHandler(artifact.getId().toMvnUrl());
+
+                        addEntry(jos, artifact, handler.getFile());
+                    }
+                }
+
+                if ( configuratorBundle != null ) {
+                    addEntry(jos, configuratorBundle, configurationBundleFile);
+                }
+            }
+        } finally {
+            if ( configurationBundleFile != null ) {
+                configurationBundleFile.delete();
+            }
+        }
+    }
+
+    private static void addEntry(final ZipOutputStream jos, final Artifact artifact, final File file)
+    throws IOException {
+        final ZipEntry bundleEntry = new ZipEntry("repository/" + artifact.getId().toMvnPath());
+        jos.putNextEntry(bundleEntry);
+
+        final byte[] buffer = new byte[16384];
+        try ( final FileInputStream fis = new FileInputStream(file)) {
+            int l = 0;
+            while ( (l = fis.read(buffer)) > 0 ) {
+                jos.write(buffer, 0, l);
+            }
+        }
+
+        jos.closeEntry();
+    }
+
+    /**
+     * Writes the feature XML to the writer.
+     * The writer is not closed.
+     * @param writer Writer
+     * @param model Model
+     * @throws IOException
+     */
+    private static void writeFeaturesXML(final Writer writer,
+            final Feature feature,
+            final Artifact configuratorBundle)
+    throws IOException {
+        final PrintWriter w = new PrintWriter(writer);
+
+        w.print("<features name=\"");
+        w.print(feature.getId().getArtifactId());
+        w.print("-repo-");
+        w.print(feature.getId().getVersion());
+        w.println("\" xmlns=\"http://karaf.apache.org/xmlns/features/v1.4.0\">");
+
+        write(w, feature, configuratorBundle);
+
+        w.println("</features>");
+        w.flush();
+    }
+
+    private static void write(final PrintWriter w, final Feature feature, final Artifact configuratorBundle)
+    throws IOException {
+        w.print("  <feature name=\"");
+        w.print(feature.getId().getGroupId());
+        w.print('.');
+        w.print(feature.getId().getArtifactId());
+        if ( feature.getId().getClassifier() != null ) {
+            w.print('.');
+            w.print(feature.getId().getClassifier());
+        }
+        w.print("\" version=\"");
+        w.print(feature.getId().getVersion());
+        w.println("\">");
+
+        if ( configuratorBundle != null ) {
+            w.print("     <bundle start-level=\"1\">");
+            w.print(configuratorBundle.getId().toMvnUrl());
+            w.println("</bundle>");
+        }
+
+        // bundles
+        for(final Map.Entry<Integer, List<Artifact>> entry : feature.getBundles().getBundlesByStartLevel().entrySet()) {
+            for(final Artifact artifact : entry.getValue()) {
+                w.print("     <bundle start-level=\"");
+                w.print(entry.getKey().toString());
+                w.print("\">");
+                w.print(artifact.getId().toMvnUrl());
+                w.println("</bundle>");
+            }
+        }
+
+        w.println("  </feature>");
+    }
+}
diff --git a/src/main/java/org/apache/sling/feature/karaf/package-info.java b/src/main/java/org/apache/sling/feature/karaf/package-info.java
new file mode 100644
index 0000000..76136a3
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/karaf/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+@org.osgi.annotation.versioning.Version("1.0.0")
+package org.apache.sling.feature.karaf;
+
+

-- 
To stop receiving notification emails like this one, please contact
davidb@apache.org.