Author: adietish
Date: 2011-04-07 09:50:14 -0400 (Thu, 07 Apr 2011)
New Revision: 30416
Modified:
workspace/adietish/org.jboss.ide.eclipse.as7.deployment.tests/src/org/jboss/ide/eclipse/as7/deployment/tests/DeploymentBuilderTest.java
workspace/adietish/org.jboss.ide.eclipse.as7.deployment/src/org/jboss/ide/eclipse/as7/deployment/DeploymentBuilder.java
Log:
[JBIDE-8690]
Modified:
workspace/adietish/org.jboss.ide.eclipse.as7.deployment/src/org/jboss/ide/eclipse/as7/deployment/DeploymentBuilder.java
===================================================================
---
workspace/adietish/org.jboss.ide.eclipse.as7.deployment/src/org/jboss/ide/eclipse/as7/deployment/DeploymentBuilder.java 2011-04-07
13:00:32 UTC (rev 30415)
+++
workspace/adietish/org.jboss.ide.eclipse.as7.deployment/src/org/jboss/ide/eclipse/as7/deployment/DeploymentBuilder.java 2011-04-07
13:50:14 UTC (rev 30416)
@@ -1,3 +1,13 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc..
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ *
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat Incorporated - initial API and implementation
+ *******************************************************************************/
package org.jboss.ide.eclipse.as7.deployment;
import java.io.File;
@@ -3,10 +13,7 @@
import java.io.IOException;
import java.net.InetAddress;
-import java.net.URISyntaxException;
-import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
-import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@@ -15,26 +22,20 @@
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.standalone.DeploymentPlan;
import org.jboss.as.controller.client.helpers.standalone.DeploymentPlanBuilder;
-import
org.jboss.as.controller.client.helpers.standalone.DuplicateDeploymentNameException;
import org.jboss.as.controller.client.helpers.standalone.ServerDeploymentManager;
import org.jboss.as.controller.client.helpers.standalone.ServerDeploymentPlanResult;
import org.jboss.as.protocol.StreamUtils;
-import org.jboss.shrinkwrap.api.Archive;
-import org.jboss.shrinkwrap.api.ArchivePath;
-import org.jboss.shrinkwrap.api.ArchivePaths;
-import org.jboss.shrinkwrap.api.ShrinkWrap;
-import org.jboss.shrinkwrap.api.container.ResourceContainer;
-import org.jboss.shrinkwrap.api.exporter.ZipExporter;
-import org.jboss.shrinkwrap.api.spec.JavaArchive;
-import org.jboss.shrinkwrap.api.spec.WebArchive;
+/**
+ * @author André Dietisheim
+ */
public class DeploymentBuilder {
- public static final long DEFAULT_TIMEOUT = 15000;
+ public static final long DEFAULT_TIMEOUT = 15 * 1000;
private ModelControllerClient client;
private ServerDeploymentManager manager;
- private final List<AbstractDeployment> deployments = new
ArrayList<AbstractDeployment>();
+ private final List<File> warFiles = new ArrayList<File>();
private long timeout = DEFAULT_TIMEOUT;
public DeploymentBuilder(String host, int port) throws UnknownHostException {
@@ -42,23 +43,16 @@
this.manager = ServerDeploymentManager.Factory.create(client);
}
- public DeploymentBuilder add(String name, boolean show, Package... pkgs) {
- deployments.add(new WarDeployment(name, pkgs, show));
- return this;
- }
-
public DeploymentBuilder add(File file) {
- deployments.add(new WarFileDeployment(file));
+ warFiles.add(file);
return this;
}
public void deploy() {
try {
- DeploymentPlanBuilder builder = manager.newDeploymentPlan();
- for (AbstractDeployment deployment : deployments) {
- deployment.addDeployment(manager, builder);
- }
try {
+ DeploymentPlanBuilder builder = manager.newDeploymentPlan();
+ addWarFiles(builder);
DeploymentPlan plan = builder.build();
Future<ServerDeploymentPlanResult> planResult =
manager.execute(plan);
planResult.get(timeout, TimeUnit.MILLISECONDS);
@@ -70,183 +64,13 @@
}
}
- private void cleanup() {
- StreamUtils.safeClose(client);
- }
-
- private abstract class AbstractDeployment {
-
- boolean deployed;
- String deploymentName;
-
- public synchronized DeploymentPlanBuilder addDeployment(ServerDeploymentManager
manager, DeploymentPlanBuilder builder)
- throws DuplicateDeploymentNameException, IOException, ExecutionException,
InterruptedException {
- deploymentName = getFile().getName();
- System.out.println("Deploying " + deploymentName);
- return builder.add(deploymentName, getFile()).deploy(deploymentName);
+ private void addWarFiles(DeploymentPlanBuilder builder) throws IOException {
+ for (File warFile : warFiles) {
+ builder.add(warFile.getName(), warFile).andDeploy();
}
-
- public synchronized DeploymentPlanBuilder removeDeployment(DeploymentPlanBuilder
builder) {
- if (deployed) {
- System.out.println("Undeploying " + deploymentName);
- return builder.undeploy(deploymentName).remove(deploymentName);
- } else {
- return builder;
- }
- }
-
- protected void addFiles(ResourceContainer<?> archive, File dir, ArchivePath
dest) {
- for (String name : dir.list()) {
- File file = new File(dir, name);
- if (file.isDirectory()) {
- addFiles(archive, file, ArchivePaths.create(dest, name));
- } else {
- archive.addResource(file, ArchivePaths.create(dest, name));
- }
- }
- }
-
- protected File getSourceMetaInfDir(String archiveName) {
- String name = "archives/" + archiveName +
"/META-INF/MANIFEST.MF";
-
- URL url = Thread.currentThread().getContextClassLoader().getResource(name);
- if (url == null) {
- throw new IllegalArgumentException("No resource called " +
name);
- }
- try {
- File file = new File(url.toURI());
- return file.getParentFile();
- } catch (URISyntaxException e) {
- throw new RuntimeException("Could not get file for " + url);
- }
- }
-
- protected File getSourceWebInfDir(String archiveName) {
- String name = "archives/" + archiveName + "/WEB-INF";
-
- URL url = Thread.currentThread().getContextClassLoader().getResource(name);
- if (url == null) {
- return null;
- }
- try {
- return new File(url.toURI());
- } catch (URISyntaxException e) {
- throw new RuntimeException("Could not get file for " + url);
- }
- }
-
- protected File getOutputDir() {
- File file = new File("target");
- if (!file.exists()) {
- throw new IllegalStateException("target/ does not exist");
- }
- if (!file.isDirectory()) {
- throw new IllegalStateException("target/ is not a directory");
- }
- file = new File(file, "archives");
- if (file.exists()) {
- if (!file.isDirectory()) {
- throw new IllegalStateException("target/archives/ already exists
and is not a directory");
- }
- } else {
- file.mkdir();
- }
- return file.getAbsoluteFile();
- }
-
- protected File createFile(Archive<?> archive) {
- File file = new File(getOutputDir(), archive.getName());
- archive.as(ZipExporter.class).exportZip(file, true);
- return file;
- }
-
- protected abstract File getFile();
}
- private class Deployment extends AbstractDeployment {
- final File realArchive;
-
- public Deployment(String archiveName, Package[] pkgs, boolean show) {
-
- ArchivePath metaInf = ArchivePaths.create("META-INF");
-
- JavaArchive archive = ShrinkWrap.create(JavaArchive.class, archiveName);
- for (Package pkg : pkgs) {
- archive.addPackage(pkg);
- }
-
- File sourceMetaInf = getSourceMetaInfDir(archiveName);
- addFiles(archive, sourceMetaInf, metaInf);
-
- System.out.println(archive.toString(show));
- realArchive = createFile(archive);
- }
-
- @Override
- protected File getFile() {
- return realArchive;
- }
+ private void cleanup() {
+ StreamUtils.safeClose(client);
}
-
- private class WarDeployment extends AbstractDeployment {
- final File file;
-
- public WarDeployment(String archiveName, Package[] pkgs, boolean show) {
- WebArchive archive = createArchive(archiveName, pkgs);
- file = createFile(archive);
- }
-
- private WebArchive createArchive(String archiveName, Package[] pkgs) {
- ArchivePath metaInf = ArchivePaths.create("META-INF");
-
- WebArchive archive = ShrinkWrap.create(WebArchive.class, archiveName);
- for (Package pkg : pkgs) {
- archive.addPackage(pkg);
- }
-
- File sourceMetaInf = getSourceMetaInfDir(archiveName);
- addFiles(archive, sourceMetaInf, metaInf);
-
- File sourceWebInf = getSourceWebInfDir(archiveName);
- if (sourceWebInf != null) {
- addFiles(archive, sourceWebInf,
ArchivePaths.create("WEB-INF"));
- }
- return archive;
- }
-
- @Override
- protected File getFile() {
- return file;
- }
- }
-
- private class WarFileDeployment extends AbstractDeployment {
- final File file;
-
- public WarFileDeployment(File file) {
- this.file = file;
- }
-
- @Override
- protected File getFile() {
- return file;
- }
- }
-
- private class ArbitraryDeployment extends AbstractDeployment {
- final File realArchive;
-
- public ArbitraryDeployment(Archive archive, boolean show) {
-
- ArchivePath metaInf = ArchivePaths.create("META-INF");
-
- System.out.println(archive.toString(show));
- realArchive = createFile(archive);
- }
-
- @Override
- protected File getFile() {
- return realArchive;
- }
- }
}
Modified:
workspace/adietish/org.jboss.ide.eclipse.as7.deployment.tests/src/org/jboss/ide/eclipse/as7/deployment/tests/DeploymentBuilderTest.java
===================================================================
---
workspace/adietish/org.jboss.ide.eclipse.as7.deployment.tests/src/org/jboss/ide/eclipse/as7/deployment/tests/DeploymentBuilderTest.java 2011-04-07
13:00:32 UTC (rev 30415)
+++
workspace/adietish/org.jboss.ide.eclipse.as7.deployment.tests/src/org/jboss/ide/eclipse/as7/deployment/tests/DeploymentBuilderTest.java 2011-04-07
13:50:14 UTC (rev 30416)
@@ -1,3 +1,13 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat Inc..
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ *
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat Incorporated - initial API and implementation
+ *******************************************************************************/
package org.jboss.ide.eclipse.as7.deployment.tests;
import java.io.File;
@@ -2,16 +12,41 @@
import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.Socket;
import java.net.URISyntaxException;
import java.net.URL;
+import java.net.UnknownHostException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.jboss.ide.eclipse.as7.deployment.DeploymentBuilder;
+import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.Bundle;
+import static junit.framework.Assert.assertTrue;
+
+/**
+ * @author André Dietisheim
+ */
public class DeploymentBuilderTest {
+ private static final String WAR_FOLDER = "wars/";
+
+ private static final String BUNDLE_ID =
"org.jboss.ide.eclipse.as7.deployment.tests";
+
private static final String HOST = "localhost";
private static final int PORT = 9999;
+ @Before
+ public void setUp() throws UnknownHostException, IOException {
+ assertAs7IsRunning();
+ }
+
+ private void assertAs7IsRunning() throws UnknownHostException, IOException {
+ Socket socket = new Socket();
+ socket.connect(new InetSocketAddress(HOST, PORT));
+ assertTrue(socket.isConnected());
+ socket.close();
+ }
+
@Test
@@ -22,10 +57,10 @@
new DeploymentBuilder(HOST, PORT).add(file).deploy();
}
- private File getWarFile(String name) {
- Bundle bundle =
Platform.getBundle("org.jboss.ide.eclipse.as7.deployment.tests");
- URL entryUrl = bundle.getEntry("wars/" + name);
- File file = new File(FileLocator.resolve(entryUrl).toURI());
+ private File getWarFile(String name) throws URISyntaxException, IOException {
+ Bundle bundle = Platform.getBundle(BUNDLE_ID);
+ URL entryUrl = bundle.getEntry(WAR_FOLDER + name);
+ return new File(FileLocator.resolve(entryUrl).toURI());
}
}