Author: [email protected] Date: Tue Aug 2 17:17:29 2011 New Revision: 1240
Log: AMDATU-399 first drop of ace rest client and mojo Added: sandbox/bdekruijff/amdatu-maven-plugin/ sandbox/bdekruijff/amdatu-maven-plugin/pom.xml sandbox/bdekruijff/amdatu-maven-plugin/src/ sandbox/bdekruijff/amdatu-maven-plugin/src/main/ sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/ sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/ sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/ sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/ sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClient.java sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClientException.java sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClientObject.java sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClientWorkspace.java sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/DeployBundleMojo.java sandbox/bdekruijff/amdatu-maven-plugin/src/test/ sandbox/bdekruijff/amdatu-maven-plugin/src/test/java/ sandbox/bdekruijff/amdatu-maven-plugin/src/test/java/org/ sandbox/bdekruijff/amdatu-maven-plugin/src/test/java/org/amdatu/ sandbox/bdekruijff/amdatu-maven-plugin/src/test/java/org/amdatu/maven/ sandbox/bdekruijff/amdatu-maven-plugin/src/test/java/org/amdatu/maven/MyMojoTest.java Added: sandbox/bdekruijff/amdatu-maven-plugin/pom.xml ============================================================================== --- (empty file) +++ sandbox/bdekruijff/amdatu-maven-plugin/pom.xml Tue Aug 2 17:17:29 2011 @@ -0,0 +1,85 @@ +<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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.amdatu</groupId> + <artifactId>amdatu</artifactId> + <version>4-SNAPSHOT</version> + </parent> + + <groupId>org.amdatu.maven</groupId> + <artifactId>amdatu-maven-plugin</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>maven-plugin</packaging> + + <name>Amdatu Maven Plugin</name> + <description>Collection of Amdatu specific mojo</description> + + <properties> + <mavenVersion>2.2.1</mavenVersion> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <version>${mavenVersion}</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-project</artifactId> + <version>${mavenVersion}</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-model</artifactId> + <version>${mavenVersion}</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-artifact-manager</artifactId> + <version>${mavenVersion}</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-artifact</artifactId> + <version>${mavenVersion}</version> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.8.2</version> + <type>jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>1.7.1</version> + <type>jar</type> + <scope>compile</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-plugin-plugin</artifactId> + <version>2.5.1</version> + <configuration> + <goalPrefix>amdatu-maven-plugin</goalPrefix> + </configuration> + <executions> + <execution> + <id>generated-helpmojo</id> + <goals> + <goal>helpmojo</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> Added: sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClient.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClient.java Tue Aug 2 17:17:29 2011 @@ -0,0 +1,48 @@ +package org.amdatu.maven; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +public class AceClient { + + private final String m_clientEndpoint; + + public AceClient(String clientEndpoint) { + m_clientEndpoint = clientEndpoint; + } + + public String getClientEndpoint() { + return m_clientEndpoint; + } + + public AceClientWorkspace createNewWorkspace() throws AceClientException { + try { + URL url = new URL(m_clientEndpoint); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDoInput(true); + connection.setDoOutput(false); + connection.setInstanceFollowRedirects(false); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + + if (HttpURLConnection.HTTP_MOVED_TEMP != connection.getResponseCode()) { + throw new AceClientException("Failed to create workspace for client: " + m_clientEndpoint + + ". Response code is " + connection.getResponseCode() + ", response message is " + + connection.getResponseMessage()); + } + String clientworkspaceEndpoint = connection.getHeaderField("Location"); + connection.disconnect(); + return new AceClientWorkspace(this, clientworkspaceEndpoint); + } + catch (MalformedURLException e) { + throw new AceClientException("Failed to create workspace for client: " + m_clientEndpoint + + ". URL is malformed.", e); + } + catch (IOException e) { + throw new AceClientException("Failed to create workspace for client: " + m_clientEndpoint + + ". Failed to open connection.", e); + } + } +} Added: sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClientException.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClientException.java Tue Aug 2 17:17:29 2011 @@ -0,0 +1,12 @@ +package org.amdatu.maven; + +public class AceClientException extends Exception { + + public AceClientException(String message) { + super(message); + } + + public AceClientException(String message, Throwable cause) { + super(message, cause); + } +} Added: sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClientObject.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClientObject.java Tue Aug 2 17:17:29 2011 @@ -0,0 +1,38 @@ +package org.amdatu.maven; + +import java.util.HashMap; +import java.util.Map; + +public class AceClientObject { + + private final Map<String, String> attributes = new HashMap<String, String>(); + private final Map<String, String> tags = new HashMap<String, String>(); + + public AceClientObject() { + putTag("generated", "true"); + } + + public void putAttribute(String key, String value) { + attributes.put(key, value); + } + + public void putAllAttributes(Map<String, String> allAttributes) { + if (allAttributes == null) + return; + for (String key : allAttributes.keySet()) { + attributes.put(key, allAttributes.get(key)); + } + } + + public void putTag(String key, String value) { + tags.put(key, value); + } + + public void putAllTags(Map<String, String> allTags) { + if (allTags == null) + return; + for (String key : allTags.keySet()) { + attributes.put(key, allTags.get(key)); + } + } +} \ No newline at end of file Added: sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClientWorkspace.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/AceClientWorkspace.java Tue Aug 2 17:17:29 2011 @@ -0,0 +1,220 @@ +package org.amdatu.maven; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLDecoder; +import java.util.Map; + +import com.google.gson.Gson; + +public class AceClientWorkspace { + + private final AceClient m_aceClient; + private final String m_clientWorkspaceEndpoint; + + public AceClientWorkspace(AceClient aceClient, String clientWorkspaceEndpoint) { + m_aceClient = aceClient; + m_clientWorkspaceEndpoint = clientWorkspaceEndpoint; + } + + public String createArtifact(String artifactName, String artifactDescription, String artifactUrl, String mimetype, + String processorPid, Map<String, String> attributes, Map<String, String> tags) throws AceClientException { + + AceClientObject object = new AceClientObject(); + object.putAttribute("artifactName", artifactName); + object.putAttribute("artifactDescription", artifactDescription); + object.putAttribute("url", artifactUrl); + object.putAttribute("mimetype", mimetype); + object.putAttribute("processorPid", processorPid); + object.putAllAttributes(attributes); + object.putAllTags(tags); + return createResource(m_clientWorkspaceEndpoint + "/artifact", object); + } + + public String createBundleArtifact(String artifactName, String artifactDescription, String artifactUrl, + String processorPid, Map<String, String> attributes, Map<String, String> tags, String bundleName, + String bundleSymbolicName, String bundleVersion) throws AceClientException { + + AceClientObject object = new AceClientObject(); + object.putAttribute("artifactName", artifactName); + object.putAttribute("artifactDescription", artifactDescription); + object.putAttribute("url", artifactUrl); + object.putAttribute("mimetype", "application/vnd.osgi.bundle"); + object.putAttribute("processorPid", processorPid); + object.putAttribute("Bundle-Name", bundleName); + object.putAttribute("Bundle-SymbolicName", bundleSymbolicName); + object.putAttribute("Bundle-Version", bundleVersion); + object.putAllAttributes(attributes); + object.putAllTags(tags); + return createResource(m_clientWorkspaceEndpoint + "/artifact", object); + } + + public String createConfigurationArtifact(String artifactName, String artifactDescription, String artifactUrl, + String fileName, String processorPid, Map<String, String> extraAttributes, Map<String, String> extraTags) + throws AceClientException { + + AceClientObject object = new AceClientObject(); + object.putAttribute("artifactName", artifactName); + object.putAttribute("artifactDescription", artifactDescription); + object.putAttribute("url", artifactUrl); + object.putAttribute("mimetype", "application/xml:osgi-autoconf"); + object.putAttribute("processorPid", processorPid); + object.putAttribute("filename", fileName); + object.putAllAttributes(extraAttributes); + object.putAllTags(extraTags); + return createResource(m_clientWorkspaceEndpoint + "/artifact", object); + } + + public String createFeature(String featureName, String featureDescription, Map<String, String> attributes, + Map<String, String> tags) throws AceClientException { + AceClientObject object = new AceClientObject(); + object.putAttribute("name", featureName); + object.putAttribute("description", featureDescription); + object.putAllAttributes(attributes); + object.putAllTags(tags); + return createResource(m_clientWorkspaceEndpoint + "/feature", object); + } + + public String createDistribution(String distributionName, String distributionDescription, + Map<String, String> attributes, Map<String, String> tags) throws AceClientException { + AceClientObject object = new AceClientObject(); + object.putAttribute("name", distributionName); + object.putAttribute("description", distributionDescription); + object.putAllAttributes(attributes); + object.putAllTags(tags); + return createResource(m_clientWorkspaceEndpoint + "/distribution", object); + } + + public String createTarget(String targetId, Map<String, String> attributes, Map<String, String> tags) + throws AceClientException { + AceClientObject object = new AceClientObject(); + object.putAttribute("id", targetId); + object.putAttribute("autoapprove", "true"); + object.putAllAttributes(attributes); + object.putAllTags(tags); + return createResource(m_clientWorkspaceEndpoint + "/target", object); + } + + public String createArtifact2Feature(String leftEndpoint, String leftCardinality, String rightEndpoint, + String rightCardinality, Map<String, String> attributes, Map<String, String> tags) + throws AceClientException { + AceClientObject object = new AceClientObject(); + object.putAttribute("leftEndpoint", leftEndpoint); + object.putAttribute("leftCardinality", leftCardinality); + object.putAttribute("rightEndpoint", rightEndpoint); + object.putAttribute("rightCardinality", rightCardinality); + object.putAllAttributes(attributes); + object.putAllTags(tags); + return createResource(m_clientWorkspaceEndpoint + "/artifact2feature", object); + } + + public String createFeature2Distribution(String leftEndpoint, String leftCardinality, String rightEndpoint, + String rightCardinality, Map<String, String> attributes, Map<String, String> tags) + throws AceClientException { + AceClientObject object = new AceClientObject(); + object.putAttribute("leftEndpoint", leftEndpoint); + object.putAttribute("leftCardinality", leftCardinality); + object.putAttribute("rightEndpoint", rightEndpoint); + object.putAttribute("rightCardinality", rightCardinality); + object.putAllAttributes(attributes); + object.putAllTags(tags); + return createResource(m_clientWorkspaceEndpoint + "/feature2distribution", object); + } + + public String createDistribution2Target(String leftEndpoint, String leftCardinality, String rightEndpoint, + String rightCardinality, Map<String, String> attributes, Map<String, String> tags) + throws AceClientException { + AceClientObject object = new AceClientObject(); + object.putAttribute("leftEndpoint", leftEndpoint); + object.putAttribute("leftCardinality", leftCardinality); + object.putAttribute("rightEndpoint", rightEndpoint); + object.putAttribute("rightCardinality", rightCardinality); + object.putAllAttributes(attributes); + object.putAllTags(tags); + return createResource(m_clientWorkspaceEndpoint + "/distribution2target", object); + } + + public void commitChanges() throws AceClientException { + try { + URL url = new URL(m_clientWorkspaceEndpoint); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDoInput(true); + connection.setDoOutput(false); + connection.setInstanceFollowRedirects(false); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + + if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) { + throw new AceClientException("Failed to commit workspace for endpoint: " + m_clientWorkspaceEndpoint + + ". Response code is " + connection.getResponseCode() + ", response message is " + + connection.getResponseMessage()); + } + connection.disconnect(); + return; + } + catch (MalformedURLException e) { + throw new AceClientException("Failed to commit workspace: " + m_clientWorkspaceEndpoint + + ". URL is malformed.", e); + } + catch (IOException e) { + throw new AceClientException("Failed to commit workspace: " + m_clientWorkspaceEndpoint + + ". Failed to open connection.", e); + } + } + + private String createResource(String endpoint, AceClientObject object) throws AceClientException { + String data = new Gson().toJson(object); + HttpURLConnection connection = null; + try { + URL endpointUrl = new URL(endpoint); + connection = (HttpURLConnection) endpointUrl.openConnection(); + connection.setDoInput(true); + connection.setDoOutput(true); + connection.setInstanceFollowRedirects(false); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes().length)); + + DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); + wr.writeBytes(data); + wr.flush(); + wr.close(); + + InputStream is = connection.getInputStream(); + BufferedReader rd = new BufferedReader(new InputStreamReader(is)); + String line; + StringBuffer response = new StringBuffer(); + while ((line = rd.readLine()) != null) { + response.append(line); + response.append('\r'); + } + rd.close(); + + int responseCode = connection.getResponseCode(); + if (responseCode != HttpURLConnection.HTTP_MOVED_TEMP) { + throw new AceClientException("Error submiting data: " + responseCode + " " + + connection.getResponseMessage()); + } + + return URLDecoder.decode(connection.getHeaderField("Location").substring( + connection.getHeaderField("Location").lastIndexOf("/") + 1), "UTF-8"); + } + catch (MalformedURLException e) { + throw new AceClientException("Failed to submit data: " + e.getMessage(), e); + } + catch (IOException e) { + throw new AceClientException("Failed to submit data: " + e.getMessage(), e); + } + finally { + if (connection != null) { + connection.disconnect(); + } + } + } +} Added: sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/DeployBundleMojo.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/amdatu-maven-plugin/src/main/java/org/amdatu/maven/DeployBundleMojo.java Tue Aug 2 17:17:29 2011 @@ -0,0 +1,119 @@ +package org.amdatu.maven; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +import java.net.MalformedURLException; +import java.util.Map; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; + +/** + * Goal which deploys a bundle to Amdatu ACE + * + * @goal deployBundle + */ +public class DeployBundleMojo extends AbstractMojo { + + /** + * @parameter default-value="${project.artifact}" + * @required + * @readonly + */ + private Artifact artifact; + + /** + * Amdatu REST client endpoint + * + * @parameter + * @required + */ + private String amdatuServer; + + /** + * Artifact name + * + * @parameter default-value="${project.name} + * @required + */ + private String artifactName; + + /** + * Artifact description + * + * @parameter default-value="${project.description} + * @required + */ + private String artifactDescription; + + /** + * Bundle-Name + * + * @parameter default-value="${project.name} + * @required + */ + private String bundleName; + + /** + * Bundle-SymbolicName + * + * @parameter default-value="${project.artifactId} + * @required + */ + private String bundleSymbolicName; + + /** + * Bundle-Version + * + * @parameter default-value="${project.version} + * @required + */ + private String bundleVersion; + + /** + * Additional attributes + * + * @parameter + */ + private Map attributes; + + /** + * Additional attributes + * + * @parameter + */ + private Map tags; + + public void execute() throws MojoExecutionException { + + AceClient c = new AceClient(amdatuServer); + try { + AceClientWorkspace w = c.createNewWorkspace(); + w.createBundleArtifact(artifactName, artifactDescription, artifact.getFile().toURI().toURL().toString(), + "", attributes, tags, bundleName, bundleSymbolicName, + bundleVersion.replace("-SNAPSHOT", "." + System.currentTimeMillis())); + w.commitChanges(); + } + catch (AceClientException e) { + throw new MojoExecutionException(e.getMessage(), e); + } + catch (MalformedURLException e) { + throw new MojoExecutionException(e.getMessage(), e); + } + } +} Added: sandbox/bdekruijff/amdatu-maven-plugin/src/test/java/org/amdatu/maven/MyMojoTest.java ============================================================================== --- (empty file) +++ sandbox/bdekruijff/amdatu-maven-plugin/src/test/java/org/amdatu/maven/MyMojoTest.java Tue Aug 2 17:17:29 2011 @@ -0,0 +1,70 @@ +package org.amdatu.maven; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +import java.util.Collections; + +import org.junit.Ignore; +import org.junit.Test; + +public class MyMojoTest { + + @Ignore + @Test + public void testGSON() throws Exception { + + AceClient c = new AceClient("http://localhost:8080/client/work"); + AceClientWorkspace w = c.createNewWorkspace(); + String artifactId1 = + w.createBundleArtifact( + "Felix Shell Service", + "The Felix Shell Service", + "http://repo1.maven.org/maven2/org/apache/felix/org.apache.felix.shell/1.4.2/org.apache.felix.shell-1.4.2.jar", + "", + Collections.EMPTY_MAP, + Collections.EMPTY_MAP, + "Felix Bundle", + "org.apache.felix.shell", + "1.4.2"); + + String artifactId2 = + w.createBundleArtifact( + "Felix Shell TUI", + "the felix shell TUI", + "http://repo1.maven.org/maven2/org/apache/felix/org.apache.felix.shell.tui/1.4.1/org.apache.felix.shell.tui-1.4.1.jar", + "", + Collections.EMPTY_MAP, + Collections.EMPTY_MAP, + "Felix Bundle TUI", + "org.apache.felix.shell.tui", + "1.4.1"); + + String featureId = + w.createFeature("AmdatuCore", "Amdatu Core feature", Collections.EMPTY_MAP, Collections.EMPTY_MAP); + String distributionId = + w.createDistribution("AmdatuPlatform", "Amdatu Platform Distribution", Collections.EMPTY_MAP, + Collections.EMPTY_MAP); + String targetId = w.createTarget("ama-1", Collections.EMPTY_MAP, Collections.EMPTY_MAP); + + w.createArtifact2Feature(artifactId1, "1", featureId, "1", Collections.EMPTY_MAP, Collections.EMPTY_MAP); + w.createArtifact2Feature(artifactId2, "1", featureId, "1", Collections.EMPTY_MAP, Collections.EMPTY_MAP); + w.createFeature2Distribution(featureId, "1", distributionId, "1", Collections.EMPTY_MAP, Collections.EMPTY_MAP); + w.createDistribution2Target(distributionId, "1", targetId, "1", Collections.EMPTY_MAP, Collections.EMPTY_MAP); + + w.commitChanges(); + } +} _______________________________________________ Amdatu-commits mailing list [email protected] http://lists.amdatu.org/mailman/listinfo/amdatu-commits
