You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2007/09/24 09:45:53 UTC
svn commit: r578690 - in
/incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport:
AbstractBundlePostMojo.java BundleDeployMojo.java BundleInstallMojo.java
Author: fmeschbe
Date: Mon Sep 24 00:45:52 2007
New Revision: 578690
URL: http://svn.apache.org/viewvc?rev=578690&view=rev
Log:
- Abstract bundle symbolic name extraction
- Install: merge directory and file name parameter to bundleFileName
Added:
incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java
Modified:
incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java
incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
Added: incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java?rev=578690&view=auto
==============================================================================
--- incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java (added)
+++ incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundlePostMojo.java Mon Sep 24 00:45:52 2007
@@ -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
Modified: incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java?rev=578690&r1=578689&r2=578690&view=diff
==============================================================================
--- incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java (original)
+++ incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleDeployMojo.java Mon Sep 24 00:45:52 2007
@@ -21,7 +21,6 @@
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.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 @@
* @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 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 @@
// 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 @@
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());
@@ -169,46 +161,6 @@
} finally {
filePost.releaseConnection();
}
- }
-
- 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;
}
/**
Modified: incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java?rev=578690&r1=578689&r2=578690&view=diff
==============================================================================
--- incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java (original)
+++ incubator/sling/trunk/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallMojo.java Mon Sep 24 00:45:52 2007
@@ -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.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 @@
* @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 @@
*/
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 @@
}
// 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 @@
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());
@@ -185,44 +170,5 @@
} finally {
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;
}
}