This is an automated email from the ASF dual-hosted git repository. kwin pushed a commit to branch feature/new-tooling-support-bundles in repository https://gitbox.apache.org/repos/asf/sling-ide-tooling.git
commit 00cb42b60fca691e4ba447b153881f17f087a414 Author: Konrad Windszus <[email protected]> AuthorDate: Tue Nov 22 13:45:45 2022 +0100 SLING-11694 Embed new Tooling Support Install/Source bundles WIP --- .../core/internal/SlingLaunchpadBehaviour.java | 8 +-- shared/api/pom.xml | 5 +- .../java/org/apache/sling/ide/osgi/OsgiClient.java | 29 ++++++----- .../apache/sling/ide/osgi/impl/HttpOsgiClient.java | 34 ++++--------- .../sling/ide/osgi/impl/HttpOsgiClientIT.java | 5 +- shared/artifacts/bnd.bnd | 1 - shared/artifacts/pom.xml | 59 ++++++++++++++++++++-- .../ide/artifacts/EmbeddedArtifactLocator.java | 6 ++- .../ide/artifacts/impl/ArtifactsLocatorImpl.java | 41 ++++++++------- .../artifacts/impl/ArtifactsLocatorImplTest.java | 55 ++++++++++++++++++++ shared/impl-vlt/pom.xml | 6 +-- shared/parent/pom.xml | 2 + 12 files changed, 171 insertions(+), 80 deletions(-) diff --git a/eclipse/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/SlingLaunchpadBehaviour.java b/eclipse/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/SlingLaunchpadBehaviour.java index 81dd8928..974bd67d 100644 --- a/eclipse/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/SlingLaunchpadBehaviour.java +++ b/eclipse/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/SlingLaunchpadBehaviour.java @@ -110,7 +110,7 @@ public class SlingLaunchpadBehaviour extends ServerBehaviourDelegateWithModulePu EmbeddedArtifactLocator artifactLocator = Activator.getDefault().getArtifactLocator(); installBundle(monitor,client, artifactLocator.loadSourceSupportBundle(), SUPPORT_SOURCE_BUNDLE_SYMBOLIC_NAME); // 15/50 done - installBundle(monitor,client, artifactLocator.loadToolingSupportBundle(), SUPPORT_BUNDLE_SYMBOLIC_NAME); // 20/50 done + installBundle(monitor,client, artifactLocator.loadInstallSupportBundle(), SUPPORT_BUNDLE_SYMBOLIC_NAME); // 20/50 done } catch ( IOException | OsgiClientException e) { Activator.getDefault().getPluginLogger() @@ -355,8 +355,8 @@ public class SlingLaunchpadBehaviour extends ServerBehaviourDelegateWithModulePu //TODO SLING-3767: //osgiClient must have a timeout!!! - if ( installLocally ) { - osgiClient.installLocalBundle(outputLocation.toFile().toPath()); + if (installLocally) { + osgiClient.installBundle(outputLocation.toFile().toPath()); monitor.worked(3); } else { @@ -364,7 +364,7 @@ public class SlingLaunchpadBehaviour extends ServerBehaviourDelegateWithModulePu InputStream bundle = builder.buildJar(outputFolder); monitor.worked(1); - osgiClient.installLocalBundle(bundle, outputFolder.getLocation().toOSString()); + osgiClient.installBundle(bundle, outputFolder.getLocation().toOSString()); monitor.worked(2); } diff --git a/shared/api/pom.xml b/shared/api/pom.xml index 9fc914c5..e18dd2ee 100644 --- a/shared/api/pom.xml +++ b/shared/api/pom.xml @@ -46,12 +46,12 @@ <artifactItem> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.tooling.support.install</artifactId> - <version>1.0.6</version> + <version>${toolingInstallVersion}</version> </artifactItem> <artifactItem> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.tooling.support.source</artifactId> - <version>1.0.4</version> + <version>${toolingSourceVersion}</version> </artifactItem> <artifactItem> <groupId>org.apache.sling</groupId> @@ -126,6 +126,7 @@ <startTimeoutSeconds>${startTimeoutSeconds}</startTimeoutSeconds> </launch> </launches> + </configuration> <executions> <execution> diff --git a/shared/api/src/main/java/org/apache/sling/ide/osgi/OsgiClient.java b/shared/api/src/main/java/org/apache/sling/ide/osgi/OsgiClient.java index 00b58b88..bfb0861d 100644 --- a/shared/api/src/main/java/org/apache/sling/ide/osgi/OsgiClient.java +++ b/shared/api/src/main/java/org/apache/sling/ide/osgi/OsgiClient.java @@ -37,33 +37,33 @@ public interface OsgiClient extends AutoCloseable { Version getBundleVersion(String bundleSymbolicName) throws OsgiClientException; - void installBundle(InputStream in, String fileName) throws OsgiClientException; - /** - * Installs a bundle from a local directory - * + * Installs a bundle from a JAR. * <p> - * The Sling launchpad instance must have filesystem access to the specified <tt>explodedBundleLocation</tt> - * </p> + * Leverages the <a href="https://felix.apache.org/documentation/subprojects/apache-felix-web-console/web-console-restful-api.html">Felix Web Console ReST endpoint</a>. + * @param in the contents of the jarred bundle + * @param sourceLocation the source location, for informative purposes only * - * @param explodedBundleLocation * @throws OsgiClientException */ - void installLocalBundle(Path explodedBundleLocation) throws OsgiClientException; + void installBundle(InputStream in, String sourceLocation) throws OsgiClientException; /** - * Installs a local bundle from an already-built jar file + * Installs a bundle from a local filesystem directory. * - * @param jarredBundle the contents of the jarred bundle - * @param sourceLocation the source location, for informative purposes only + * <strong>The Sling launchpad instance must have filesystem access to the specified <tt>explodedBundleLocation</tt></strong> + * <p> + * Leverages the <a href="https://github.com/apache/sling-org-apache-sling-tooling-support-install">Sling Tooling Support Install bundle</a>. * + * @param explodedBundleLocation the local path of the directory containing the exploded bundle * @throws OsgiClientException */ - void installLocalBundle(InputStream jarredBundle, String sourceLocation) throws OsgiClientException; + void installBundle(Path explodedBundleLocation) throws OsgiClientException; /** * Finds source references for all bundles deployed in the Sling instance - * + * <p> + * Leverages the <a href="https://github.com/apache/sling-org-apache-sling-tooling-support-source">Sling Tooling Support Source bundle</a>. * @return the source references, possibly empty * @throws OsgiClientException */ @@ -71,7 +71,8 @@ public interface OsgiClient extends AutoCloseable { /** * Uninstalls the bundle with the specified Bundle-SymbolicName, if present - * + * <p> + * Leverages the <a href="https://felix.apache.org/documentation/subprojects/apache-felix-web-console/web-console-restful-api.html">Felix Web Console ReST endpoint</a>. * @param bundleSymbolicName The Bundle-SymbolicName * @return true in case a bundle with that BSN was found and uninstalled, false in case the BSN was not found * @throws OsgiClientException error when trying to uninstall the bundle diff --git a/shared/api/src/main/java/org/apache/sling/ide/osgi/impl/HttpOsgiClient.java b/shared/api/src/main/java/org/apache/sling/ide/osgi/impl/HttpOsgiClient.java index 16375121..10cc40e1 100644 --- a/shared/api/src/main/java/org/apache/sling/ide/osgi/impl/HttpOsgiClient.java +++ b/shared/api/src/main/java/org/apache/sling/ide/osgi/impl/HttpOsgiClient.java @@ -296,7 +296,7 @@ public class HttpOsgiClient implements OsgiClient, AutoCloseable { } @Override - public void installLocalBundle(final Path explodedBundleLocation) throws OsgiClientException { + public void installBundle(final Path explodedBundleLocation) throws OsgiClientException { if (explodedBundleLocation == null) { throw new IllegalArgumentException("explodedBundleLocation may not be null"); @@ -305,24 +305,19 @@ public class HttpOsgiClient implements OsgiClient, AutoCloseable { List<? extends NameValuePair> parameters = Collections .singletonList(new BasicNameValuePair("dir", explodedBundleLocation.toString())); try { - installLocalBundle(new UrlEncodedFormEntity(parameters), explodedBundleLocation.toString()); + HttpPost request = new HttpPost(repositoryInfo.getUrl().resolve("system/sling/tooling/install")); + request.setEntity(new UrlEncodedFormEntity(parameters)); + BundleInstallerResult result = executeJsonRequest(request, BundleInstallerResult.class, "install local bundle from " + explodedBundleLocation.toString()); + if (!result.isSuccessful()) { + String errorMessage = !result.hasMessage() ? "Bundle deployment failed, please check the Sling logs" + : result.getMessage(); + throw new OsgiClientException(errorMessage); + } } catch (UnsupportedEncodingException e) { throw new OsgiClientException("Cannot install local bundle due to unsupported encoding", e); } } - @Override - public void installLocalBundle(final InputStream jarredBundle, String sourceLocation) throws OsgiClientException { - - if (jarredBundle == null) { - throw new IllegalArgumentException("jarredBundle may not be null"); - } - - MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); - entityBuilder.addBinaryBody("bundle", jarredBundle, ContentType.DEFAULT_BINARY, "bundle.jar"); - installLocalBundle(entityBuilder.build(), sourceLocation); - } - @Override public List<SourceReference> findSourceReferences() throws OsgiClientException { SourceBundleData[] sourceBundleData = executeJsonGetRequest("system/sling/tooling/sourceReferences.json", SourceBundleData[].class, "find source references"); @@ -449,17 +444,6 @@ public class HttpOsgiClient implements OsgiClient, AutoCloseable { } - void installLocalBundle(HttpEntity httpEntity, String bundleSource) throws OsgiClientException { - HttpPost request = new HttpPost(repositoryInfo.getUrl().resolve("system/sling/tooling/install")); - request.setEntity(httpEntity); - BundleInstallerResult result = executeJsonRequest(request, BundleInstallerResult.class, "install local bundle from " + bundleSource); - if (!result.isSuccessful()) { - String errorMessage = !result.hasMessage() ? "Bundle deployment failed, please check the Sling logs" - : result.getMessage(); - throw new OsgiClientException(errorMessage); - } - } - private <T> T executeJsonGetRequest(String relativePath, Class<T> jsonObjectClass, String requestLabel) throws OsgiClientException { HttpGet request = new HttpGet(repositoryInfo.getUrl().resolve(relativePath)); return executeJsonRequest(request, jsonObjectClass, requestLabel); diff --git a/shared/api/src/test/java/org/apache/sling/ide/osgi/impl/HttpOsgiClientIT.java b/shared/api/src/test/java/org/apache/sling/ide/osgi/impl/HttpOsgiClientIT.java index d5466263..131ca5b9 100644 --- a/shared/api/src/test/java/org/apache/sling/ide/osgi/impl/HttpOsgiClientIT.java +++ b/shared/api/src/test/java/org/apache/sling/ide/osgi/impl/HttpOsgiClientIT.java @@ -88,11 +88,8 @@ public class HttpOsgiClientIT { osgiClient.installBundle(input, "org.apache.sling.api"); } osgiClient.waitForComponentRegistered("org.apache.sling.tooling.support.install.impl.InstallServlet", 20000, 500); - try (InputStream input = Objects.requireNonNull(this.getClass().getResourceAsStream("/org.apache.sling.commons.messaging.jar"))) { - osgiClient.installLocalBundle(input, "commons-messaging.jar"); - } - osgiClient.installLocalBundle(explodedJarFolder); + osgiClient.installBundle(explodedJarFolder); osgiClient.uninstallBundle("org.apache.sling.tooling.support.install"); } diff --git a/shared/artifacts/bnd.bnd b/shared/artifacts/bnd.bnd index 09df554a..e69de29b 100644 --- a/shared/artifacts/bnd.bnd +++ b/shared/artifacts/bnd.bnd @@ -1 +0,0 @@ --includeresource: org.apache.sling.tooling.support*.jar diff --git a/shared/artifacts/pom.xml b/shared/artifacts/pom.xml index 667caef4..844541d9 100644 --- a/shared/artifacts/pom.xml +++ b/shared/artifacts/pom.xml @@ -48,19 +48,70 @@ <scope>provided</scope> </dependency> - <!-- will be embedded --> + <!-- embedded via Bnd--> <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.tooling.support.install</artifactId> - <version>1.0.6</version> + <version>${toolingInstallVersion}</version> <scope>provided</scope> </dependency> - <!-- will be embedded --> + <!-- embedded via Bnd--> <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.tooling.support.source</artifactId> - <version>1.0.4</version> + <version>${toolingSourceVersion}</version> <scope>provided</scope> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.11.0</version> + <scope>test</scope> + </dependency> </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <executions> + <execution> + <id>embed-tooling-jars</id> + <phase>process-sources</phase> + <goals> + <goal>copy</goal> + </goals> + <configuration> + <artifactItems> + <artifactItem> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.tooling.support.install</artifactId> + <version>${toolingInstallVersion}</version> + </artifactItem> + <artifactItem> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.tooling.support.source</artifactId> + <version>${toolingSourceVersion}</version> + </artifactItem> + </artifactItems> + <outputDirectory>${project.build.outputDirectory}</outputDirectory> + <overWriteReleases>true</overWriteReleases> + <overWriteSnapshots>true</overWriteSnapshots> + <stripVersion>true</stripVersion> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> </project> diff --git a/shared/artifacts/src/main/java/org/apache/sling/ide/artifacts/EmbeddedArtifactLocator.java b/shared/artifacts/src/main/java/org/apache/sling/ide/artifacts/EmbeddedArtifactLocator.java index 6982de98..eb440faf 100644 --- a/shared/artifacts/src/main/java/org/apache/sling/ide/artifacts/EmbeddedArtifactLocator.java +++ b/shared/artifacts/src/main/java/org/apache/sling/ide/artifacts/EmbeddedArtifactLocator.java @@ -16,13 +16,15 @@ */ package org.apache.sling.ide.artifacts; +import java.io.IOException; + public interface EmbeddedArtifactLocator { public static final String SUPPORT_BUNDLE_SYMBOLIC_NAME = "org.apache.sling.tooling.support.install"; public static final String SUPPORT_SOURCE_BUNDLE_SYMBOLIC_NAME = "org.apache.sling.tooling.support.source"; - EmbeddedArtifact loadToolingSupportBundle(); + EmbeddedArtifact loadInstallSupportBundle() throws IOException; - EmbeddedArtifact loadSourceSupportBundle(); + EmbeddedArtifact loadSourceSupportBundle() throws IOException; } diff --git a/shared/artifacts/src/main/java/org/apache/sling/ide/artifacts/impl/ArtifactsLocatorImpl.java b/shared/artifacts/src/main/java/org/apache/sling/ide/artifacts/impl/ArtifactsLocatorImpl.java index 3dc74968..e9cba7e9 100644 --- a/shared/artifacts/src/main/java/org/apache/sling/ide/artifacts/impl/ArtifactsLocatorImpl.java +++ b/shared/artifacts/src/main/java/org/apache/sling/ide/artifacts/impl/ArtifactsLocatorImpl.java @@ -16,7 +16,10 @@ */ package org.apache.sling.ide.artifacts.impl; +import java.io.IOException; import java.net.URL; +import java.util.jar.Attributes; +import java.util.jar.JarInputStream; import org.apache.sling.ide.artifacts.EmbeddedArtifact; import org.apache.sling.ide.artifacts.EmbeddedArtifactLocator; @@ -26,37 +29,33 @@ import org.osgi.service.component.annotations.Component; public class ArtifactsLocatorImpl implements EmbeddedArtifactLocator { public ArtifactsLocatorImpl() { - } @Override - public EmbeddedArtifact loadToolingSupportBundle() { - - String version = "1.0.6"; // TODO - remove version hardcoding - String artifactId = "org.apache.sling.tooling.support.install"; - String extension = "jar"; + public EmbeddedArtifact loadInstallSupportBundle() throws IOException { + return getArtifactFromResource("org.apache.sling.tooling.support.install"); + } - URL jarUrl = loadResource(artifactId + "-" + version - + "." + extension); + @Override + public EmbeddedArtifact loadSourceSupportBundle() throws IOException { + return getArtifactFromResource("org.apache.sling.tooling.support.source"); + } - return new EmbeddedArtifact(artifactId + "-" + version + "." + extension, version, jarUrl); + private EmbeddedArtifact getArtifactFromResource(String artifactId) throws IOException { + URL jarUrl = loadResource(artifactId + ".jar"); + // extract version from it + String version = getVersionFromJarUrl(jarUrl); + return new EmbeddedArtifact(artifactId + "-" + version + ".jar", version, jarUrl); } - @Override - public EmbeddedArtifact loadSourceSupportBundle() { - - String version = "1.0.4"; // TODO - remove version hardcoding - String artifactId = "org.apache.sling.tooling.support.source"; - String extension = "jar"; - - URL jarUrl = loadResource(artifactId + "-" + version - + "." + extension); - - return new EmbeddedArtifact(artifactId + "-" + version + "." + extension, version, jarUrl); + private String getVersionFromJarUrl(URL url) throws IOException { + // extract version from it + try (JarInputStream jarInput = new JarInputStream(url.openStream())) { + return jarInput.getManifest().getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION); + } } private URL loadResource(String resourceLocation) { - URL resourceUrl = this.getClass().getClassLoader().getResource(resourceLocation); if (resourceUrl == null) { throw new RuntimeException("Unable to locate bundle resource " + resourceLocation); diff --git a/shared/artifacts/src/test/java/org/apache/sling/ide/artifacts/impl/ArtifactsLocatorImplTest.java b/shared/artifacts/src/test/java/org/apache/sling/ide/artifacts/impl/ArtifactsLocatorImplTest.java new file mode 100644 index 00000000..74a6867d --- /dev/null +++ b/shared/artifacts/src/test/java/org/apache/sling/ide/artifacts/impl/ArtifactsLocatorImplTest.java @@ -0,0 +1,55 @@ +/* + * 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.ide.artifacts.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; +import org.apache.sling.ide.artifacts.EmbeddedArtifact; +import org.junit.Test; + +public class ArtifactsLocatorImplTest { + + @Test + public void testLoadSourceSupportBundle() throws IOException { + ArtifactsLocatorImpl artifactsLocator = new ArtifactsLocatorImpl(); + EmbeddedArtifact embeddedArtifact = artifactsLocator.loadSourceSupportBundle(); + assertEmbeddedArtifact(embeddedArtifact, "org.apache.sling.tooling.support.source.jar"); + assertEquals("1.1.0-SNAPSHOT", embeddedArtifact.getVersion()); + assertEquals("org.apache.sling.tooling.support.source-1.1.0-SNAPSHOT.jar", embeddedArtifact.getName()); + } + + @Test + public void testLoadInstallSupportBundle() throws IOException { + ArtifactsLocatorImpl artifactsLocator = new ArtifactsLocatorImpl(); + EmbeddedArtifact embeddedArtifact = artifactsLocator.loadInstallSupportBundle(); + assertEmbeddedArtifact(embeddedArtifact, "org.apache.sling.tooling.support.install.jar"); + assertEquals("1.1.0-SNAPSHOT", embeddedArtifact.getVersion()); + assertEquals("org.apache.sling.tooling.support.install-1.1.0-SNAPSHOT.jar", embeddedArtifact.getName()); + } + + private void assertEmbeddedArtifact(EmbeddedArtifact embeddedArtifact, String expectedInputResourceName) throws IOException { + try (InputStream expectedInput = this.getClass().getClassLoader().getResourceAsStream(expectedInputResourceName); + InputStream actualInput = embeddedArtifact.openInputStream()) { + assertTrue(IOUtils.contentEquals(expectedInput, actualInput)); + } + } +} diff --git a/shared/impl-vlt/pom.xml b/shared/impl-vlt/pom.xml index 79b9b47f..bff3593a 100644 --- a/shared/impl-vlt/pom.xml +++ b/shared/impl-vlt/pom.xml @@ -66,9 +66,9 @@ <dependencyManagement> <dependencies> <dependency> - <groupId>commons-io</groupId> - <artifactId>commons-io</artifactId> - <version>2.11.0</version> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.11.0</version> </dependency> </dependencies> </dependencyManagement> diff --git a/shared/parent/pom.xml b/shared/parent/pom.xml index 69fef0ce..eefc3e04 100644 --- a/shared/parent/pom.xml +++ b/shared/parent/pom.xml @@ -133,5 +133,7 @@ <jackrabbit.version>2.20.6</jackrabbit.version> <oak.version>1.44.0</oak.version> <vault.version>3.6.4</vault.version> + <toolingInstallVersion>1.1.0-SNAPSHOT</toolingInstallVersion> + <toolingSourceVersion>1.1.0-SNAPSHOT</toolingSourceVersion> </properties> </project>
