You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:15:36 UTC
[sling-maven-sling-plugin] 12/48: - Abstract bundle symbolic name
extraction - Install: merge directory and file name parameter to
bundleFileName
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag maven-sling-plugin-2.0.2-incubator
in repository https://gitbox.apache.org/repos/asf/sling-maven-sling-plugin.git
commit cc9903a65ff64bea46f9d4d5e2b098fdce1d0ac8
Author: Felix Meschberger <fm...@apache.org>
AuthorDate: Mon Sep 24 07:45:52 2007 +0000
- Abstract bundle symbolic name extraction
- Install: merge directory and file name parameter to bundleFileName
git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk/maven-sling-plugin@578690 13f79535-47bb-0310-9956-ffa450edef68
---
.../bundlesupport/AbstractBundlePostMojo.java | 89 ++++++++++++++++++++++
.../maven/bundlesupport/BundleDeployMojo.java | 62 ++-------------
.../maven/bundlesupport/BundleInstallMojo.java | 74 +++---------------
3 files changed, 106 insertions(+), 119 deletions(-)
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java
new file mode 100644
index 0000000..93b5886
--- /dev/null
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java
@@ -0,0 +1,89 @@
+/*
+ * 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.maven.bundlesupport;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+import org.apache.maven.plugin.AbstractMojo;
+
+public abstract class AbstractBundlePostMojo extends AbstractMojo {
+
+ /**
+ * Returns the symbolic name of the given bundle. If the
+ * <code>jarFile</code> does not exist or does not contain a manifest with
+ * a <code>Bundle-SymbolicName</code> header <code>null</code> is
+ * returned. Otherwise the value of the <code>Bundle-SymbolicName</code>
+ * header is returned.
+ * <p>
+ * This method may also be used to check whether the file is a bundle at all
+ * as it is assumed, that only if the file contains an OSGi bundle will the
+ * <code>Bundle-SymbolicName</code> manifest header be set.
+ *
+ * @param jarFile The file providing the bundle whose symbolic name is
+ * requested.
+ * @return The bundle's symbolic name from the
+ * <code>Bundle-SymbolicName</code> manifest header or
+ * <code>null</code> if the file does not exists or no manifest
+ * exists in the file or the header is not contained in the
+ * manifest. However, if <code>null</code> is returned, the file
+ * may be assumed to not contain an OSGi bundle.
+ */
+ protected String getBundleSymbolicName(File jarFile) {
+ if (!jarFile.exists()) {
+ getLog().debug("getBundleSymbolicName: " + jarFile + " does not exist");
+ return null;
+ }
+
+ JarFile jaf = null;
+ try {
+ jaf = new JarFile(jarFile);
+ Manifest manif = jaf.getManifest();
+ if (manif == null) {
+ getLog().debug("getBundleSymbolicName: Missing manifest in " + jarFile);
+ return null;
+ }
+
+ String symbName = manif.getMainAttributes().getValue(
+ "Bundle-SymbolicName");
+ if (symbName == null) {
+ getLog().debug("getBundleSymbolicName: No Bundle-SymbolicName in " + jarFile);
+ return null;
+ }
+
+ return symbName;
+ } catch (IOException ioe) {
+ getLog().warn("getBundleSymbolicName: Problem checking " + jarFile, ioe);
+ } finally {
+ if (jaf != null) {
+ try {
+ jaf.close();
+ } catch (IOException ignore) {
+ // don't care
+ }
+ }
+ }
+
+ // fall back to not being a bundle
+ return null;
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java
index e9c8b12..f6764e6 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java
@@ -21,7 +21,6 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.OutputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
@@ -41,7 +40,6 @@ import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.io.IOUtils;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
-import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
@@ -55,7 +53,7 @@ import org.apache.maven.project.MavenProject;
* @phase deploy
* @description deploy an OSGi bundle jar to the Day OBR
*/
-public class BundleDeployMojo extends AbstractMojo {
+public class BundleDeployMojo extends AbstractBundlePostMojo {
/**
* The directory for the generated JAR.
@@ -97,7 +95,8 @@ public class BundleDeployMojo extends AbstractMojo {
public void execute() throws MojoExecutionException {
// only upload if packaging as an osgi-bundle
File jarFile = new File(this.buildDirectory, this.jarName);
- if (!this.isBundle(jarFile)) {
+ String bundleName = getBundleSymbolicName(jarFile);
+ if (bundleName == null) {
this.getLog().info(jarFile + " is not an OSGi Bundle, not uploading");
return;
}
@@ -134,14 +133,13 @@ public class BundleDeployMojo extends AbstractMojo {
// we ignore this and don't append "final"!
}
}
+
+ getLog().info("Deploying Bundle " + bundleName + "(" + jarFile + ") to " + obr);
this.post(this.obr, jarFile);
}
private void post(String targetURL, File file) {
- this.getLog().info("Uploading " + file + " to " + targetURL);
-
PostMethod filePost = new PostMethod(targetURL);
-
try {
Part[] parts = { new FilePart(file.getName(), new FilePartSource(file.getName(), file)),
new StringPart("_noredir_", "_noredir_") };
@@ -152,16 +150,10 @@ public class BundleDeployMojo extends AbstractMojo {
5000);
int status = client.executeMethod(filePost);
if (status == HttpStatus.SC_OK) {
- InputStream res = filePost.getResponseBodyAsStream();
- String response = "";
- if (res != null) {
- response = IOUtils.toString(res,
- filePost.getResponseCharSet());
- }
- this.getLog().info("Upload complete: " + response);
+ getLog().info("Bundle deployed");
} else {
this.getLog().error(
- "Upload failed, cause: " + HttpStatus.getStatusText(status));
+ "Deployment failed, cause: " + HttpStatus.getStatusText(status));
}
} catch (Exception ex) {
this.getLog().error(ex.getClass().getName() + " " + ex.getMessage());
@@ -171,46 +163,6 @@ public class BundleDeployMojo extends AbstractMojo {
}
}
- private boolean isBundle(File jarFile) {
- if (!jarFile.exists()) {
- this.getLog().debug("isBundle: " + jarFile + " does not exist");
- return false;
- }
-
- JarFile jaf = null;
- try {
- jaf = new JarFile(jarFile);
- Manifest manif = jaf.getManifest();
- if (manif == null) {
- this.getLog().debug("isBundle: Missing manifest in " + jarFile);
- return false;
- }
-
- String symbName =
- manif.getMainAttributes().getValue("Bundle-SymbolicName");
- if (symbName == null) {
- this.getLog().debug("isBundle: No Bundle-SymbolicName in " + jarFile);
- return false;
- }
-
- this.getLog().info("isBundle: " + jarFile + " contains Bundle " + symbName);
- return true;
- } catch (IOException ioe) {
- // TODO
- } finally {
- if (jaf != null) {
- try {
- jaf.close();
- } catch (IOException ignore) {
- // don't care
- }
- }
- }
-
- // fall back to not being a bundle
- return false;
- }
-
/**
* Change the version in jar
* @param newVersion
diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
index 09884d9..4b9d588 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
@@ -18,13 +18,9 @@
package org.apache.sling.maven.bundlesupport;
import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;
-import java.util.jar.JarFile;
-import java.util.jar.Manifest;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
@@ -37,9 +33,6 @@ import org.apache.commons.httpclient.methods.multipart.FilePartSource;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
-import org.apache.commons.io.IOUtils;
-import org.apache.maven.plugin.AbstractMojo;
-import org.apache.maven.project.MavenProject;
/**
* Install an OSGi bundle to a running Sling instance.
@@ -48,7 +41,7 @@ import org.apache.maven.project.MavenProject;
* @phase install
* @description install an OSGi bundle jar to a running Sling instance
*/
-public class BundleInstallMojo extends AbstractMojo {
+public class BundleInstallMojo extends AbstractBundlePostMojo {
/**
* Whether to skip this step even though it has been configured in the
@@ -61,21 +54,13 @@ public class BundleInstallMojo extends AbstractMojo {
*/
private boolean skip;
- /**
- * The directory for the generated JAR.
- *
- * @parameter expression="${project.build.directory}"
- * @required
- */
- private String buildDirectory;
-
/**
* The name of the generated JAR file.
*
- * @parameter alias="jarName" expression="${project.build.finalName}.jar"
+ * @parameter expression="${sling.file}" default-value="${project.build.directory}/${project.build.finalName}.jar"
* @required
*/
- private String jarName;
+ private String bundleFileName;
/**
* The URL of the running Sling instance.
@@ -128,15 +113,15 @@ public class BundleInstallMojo extends AbstractMojo {
}
// only upload if packaging as an osgi-bundle
- File jarFile = new File(buildDirectory, jarName);
- String bundleName = isBundle(jarFile);
+ File bundleFile = new File(bundleFileName);
+ String bundleName = getBundleSymbolicName(bundleFile);
if (bundleName == null) {
- getLog().info(jarFile + " is not an OSGi Bundle, not uploading");
+ getLog().info(bundleFile + " is not an OSGi Bundle, not uploading");
return;
}
- getLog().info("Installing Bundle " + bundleName + "(" + jarFile + ") to " + slingUrl);
- post(slingUrl, jarFile);
+ getLog().info("Installing Bundle " + bundleName + "(" + bundleFile + ") to " + slingUrl);
+ post(slingUrl, bundleFile);
}
private void post(String targetURL, File file) {
@@ -174,10 +159,10 @@ public class BundleInstallMojo extends AbstractMojo {
getLog().info("Bundle installed");
} else {
getLog().error(
- "Install failed, cause: " + HttpStatus.getStatusText(status));
+ "Installation failed, cause: " + HttpStatus.getStatusText(status));
}
} catch (ConnectException ce) {
- getLog().info("Install on " + targetURL + " failed, cause: " + ce.getMessage());
+ getLog().info("Installation on " + targetURL + " failed, cause: " + ce.getMessage());
getLog().debug(ce); // dump on debug
} catch (Exception ex) {
getLog().error(ex.getClass().getName() + " " + ex.getMessage());
@@ -186,43 +171,4 @@ public class BundleInstallMojo extends AbstractMojo {
filePost.releaseConnection();
}
}
-
- private String isBundle(File jarFile) {
- if (!jarFile.exists()) {
- getLog().debug("isBundle: " + jarFile + " does not exist");
- return null;
- }
-
- JarFile jaf = null;
- try {
- jaf = new JarFile(jarFile);
- Manifest manif = jaf.getManifest();
- if (manif == null) {
- getLog().debug("isBundle: Missing manifest in " + jarFile);
- return null;
- }
-
- String symbName =
- manif.getMainAttributes().getValue("Bundle-SymbolicName");
- if (symbName == null) {
- getLog().debug("isBundle: No Bundle-SymbolicName in " + jarFile);
- return null;
- }
-
- return symbName;
- } catch (IOException ioe) {
- getLog().warn("isBundle: Problem checking " + jarFile, ioe);
- } finally {
- if (jaf != null) {
- try {
- jaf.close();
- } catch (IOException ignore) {
- // don't care
- }
- }
- }
-
- // fall back to not being a bundle
- return null;
- }
}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.