Author: jgallimore
Date: Sun Oct 2 14:13:13 2011
New Revision: 1178192
URL: http://svn.apache.org/viewvc?rev=1178192&view=rev
Log:
OPENEJB-1687 get remote adapter to download Tomcat/OpenEJB individually as well
as being able to download the whole TomEE zip
Added:
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/DownloadException.java
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/MavenCache.java
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/ZipExtractor.java
Modified:
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/SimpleMavenBuilderImpl.java
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEConfiguration.java
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEContainer.java
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/test/resources/arquillian.xml
Added:
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/DownloadException.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/DownloadException.java?rev=1178192&view=auto
==============================================================================
---
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/DownloadException.java
(added)
+++
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/DownloadException.java
Sun Oct 2 14:13:13 2011
@@ -0,0 +1,20 @@
+package org.apache.openejb.arquillian.common;
+
+public class DownloadException extends Exception {
+
+ public DownloadException() {
+ }
+
+ public DownloadException(String message) {
+ super(message);
+ }
+
+ public DownloadException(Throwable throwable) {
+ super(throwable);
+ }
+
+ public DownloadException(String message, Throwable throwable) {
+ super(message, throwable);
+ }
+
+}
Added:
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/MavenCache.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/MavenCache.java?rev=1178192&view=auto
==============================================================================
---
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/MavenCache.java
(added)
+++
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/MavenCache.java
Sun Oct 2 14:13:13 2011
@@ -0,0 +1,169 @@
+/**
+ * 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.openejb.arquillian.common;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+
+import org.apache.maven.repository.internal.MavenRepositorySystemSession;
+import org.codehaus.plexus.DefaultPlexusContainer;
+import org.codehaus.plexus.PlexusContainerException;
+import
org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+import org.jboss.shrinkwrap.resolver.impl.maven.MavenRepositorySettings;
+import org.sonatype.aether.RepositorySystem;
+import org.sonatype.aether.RepositorySystemSession;
+import org.sonatype.aether.artifact.Artifact;
+import org.sonatype.aether.graph.Dependency;
+import org.sonatype.aether.installation.InstallRequest;
+import org.sonatype.aether.installation.InstallationException;
+import org.sonatype.aether.resolution.ArtifactRequest;
+import org.sonatype.aether.resolution.ArtifactResolutionException;
+import org.sonatype.aether.resolution.ArtifactResult;
+import org.sonatype.aether.util.artifact.DefaultArtifact;
+
+/**
+ * This class resolves artifacts in Maven. If an artifact (such as the Tomcat
+ * zip) isn't available in Maven, this class can obtain it from an alternative
+ * URL and store it in the local repository.
+ *
+ * @author jon
+ *
+ */
+public class MavenCache {
+
+ private final MavenRepositorySettings settings;
+ private final RepositorySystem system;
+ private final RepositorySystemSession session;
+
+ public MavenCache() {
+ this.settings = new MavenRepositorySettings();
+ this.system = getRepositorySystem();
+ this.session = getSession();
+ }
+
+ public RepositorySystemSession getSession() {
+ MavenRepositorySystemSession session = new
MavenRepositorySystemSession();
+
session.setLocalRepositoryManager(system.newLocalRepositoryManager(settings.getLocalRepository()));
+ session.setTransferListener(settings.getTransferListener());
+ session.setRepositoryListener(settings.getRepositoryListener());
+
+ return session;
+ }
+
+ private RepositorySystem getRepositorySystem() {
+ try {
+ return new
DefaultPlexusContainer().lookup(RepositorySystem.class);
+ } catch (ComponentLookupException e) {
+ throw new RuntimeException("Unable to lookup component
RepositorySystem, cannot establish Aether dependency resolver.", e);
+ } catch (PlexusContainerException e) {
+ throw new RuntimeException("Unable to load
RepositorySystem component by Plexus, cannot establish Aether dependency
resolver.", e);
+ }
+ }
+
+ public Artifact getArtifact(String coords, String altUrl) {
+ return getArtifact(getDependency(coords), altUrl);
+ }
+
+ public Artifact getArtifact(Dependency dependency, String altUrl) {
+ Artifact artifact = null;
+
+ try {
+ artifact = resolve(dependency);
+ } catch (Exception e) {
+ // so lets try and download and install it instead
+ try {
+ if (altUrl != null) {
+ File file = download(altUrl);
+
+ InstallRequest request = new
InstallRequest();
+ artifact =
dependency.getArtifact().setFile(file);
+ request.addArtifact(artifact);
+ system.install(session, request);
+ artifact = resolve(dependency);
+ }
+ } catch (InstallationException e1) {
+ e1.printStackTrace();
+ } catch (DownloadException e1) {
+ e1.printStackTrace();
+ } catch (ArtifactResolutionException e1) {
+ e1.printStackTrace();
+ }
+ }
+
+ return artifact;
+ }
+
+ public Dependency getDependency(String coords) {
+ return new Dependency(new DefaultArtifact(coords), "");
+ }
+
+ public Artifact resolve(Dependency dependency) throws
ArtifactResolutionException {
+ ArtifactRequest artifactRequest = new
ArtifactRequest(dependency.getArtifact(), settings.getRemoteRepositories(),
null);
+ ArtifactResult artifactResult = system.resolveArtifact(session,
artifactRequest);
+ Artifact artifact = artifactResult.getArtifact();
+ return artifact;
+ }
+
+ public File download(String source) throws DownloadException {
+ File file = null;
+ InputStream is = null;
+ OutputStream os = null;
+ try {
+ is = new URL(source).openStream();
+ file = File.createTempFile("dload", ".fil");
+ file.deleteOnExit();
+ os = new FileOutputStream(file);
+
+ int bytesRead = -1;
+ byte[] buffer = new byte[8192];
+
+ while ((bytesRead = is.read(buffer)) > -1) {
+ os.write(buffer, 0, bytesRead);
+ }
+
+ is.close();
+ os.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new DownloadException("Unable to download " +
source + " to " + file.getAbsolutePath());
+ } finally {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (Exception e) {
+ }
+ }
+
+ if (os != null) {
+ try {
+ os.close();
+ } catch (Exception e) {
+ }
+ }
+ }
+
+ return file;
+ }
+
+ public static void main(String[] args) {
+ File file = new
MavenCache().getArtifact("org.apache.openejb:tomcat:zip:6.0.33",
"http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.21/bin/apache-tomcat-7.0.21.zip").getFile();
+ System.out.println(file.getAbsolutePath());
+ }
+}
Modified:
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/SimpleMavenBuilderImpl.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/SimpleMavenBuilderImpl.java?rev=1178192&r1=1178191&r2=1178192&view=diff
==============================================================================
---
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/SimpleMavenBuilderImpl.java
(original)
+++
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/SimpleMavenBuilderImpl.java
Sun Oct 2 14:13:13 2011
@@ -10,25 +10,17 @@ import org.jboss.shrinkwrap.resolver.api
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolutionFilter;
import org.jboss.shrinkwrap.resolver.impl.maven.MavenBuilderImpl;
import org.jboss.shrinkwrap.resolver.impl.maven.MavenConverter;
-import org.jboss.shrinkwrap.resolver.impl.maven.MavenRepositorySettings;
-import org.jboss.shrinkwrap.resolver.impl.maven.MavenRepositorySystem;
import org.jboss.shrinkwrap.resolver.impl.maven.Validate;
-import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.graph.Dependency;
-import org.sonatype.aether.resolution.ArtifactRequest;
-import org.sonatype.aether.resolution.ArtifactResult;
public class SimpleMavenBuilderImpl extends MavenBuilderImpl {
private static final File[] FILE_CAST = new File[0];
- private final MavenRepositorySystem system;
- private final RepositorySystemSession session;
+ private String altUrl = null;
public SimpleMavenBuilderImpl() {
super();
- this.system = new MavenRepositorySystem(new
MavenRepositorySettings());
- this.session = system.getSession();
}
public File[] resolveAsFiles(MavenResolutionFilter filter) throws
ResolutionException {
@@ -42,9 +34,7 @@ public class SimpleMavenBuilderImpl exte
List<Dependency> dependencies =
MavenConverter.asDependencies(super.getDependencies());
for (Dependency dependency : dependencies) {
try {
- ArtifactRequest artifactRequest = new
ArtifactRequest(dependency.getArtifact(), system.getRemoteRepositories(), null);
- ArtifactResult artifactResult =
system.resolveArtifact(session, artifactRequest);
- Artifact artifact =
artifactResult.getArtifact();
+ Artifact artifact = new
MavenCache().getArtifact(dependency, altUrl);
files.add(artifact.getFile());
} catch (Exception e) {
throw new ResolutionException("Unable to
resolve an artifact", e);
@@ -53,4 +43,9 @@ public class SimpleMavenBuilderImpl exte
return files.toArray(FILE_CAST);
}
+
+ public MavenBuilderImpl alternativeUrl(String altUrl) {
+ this.altUrl = altUrl;
+ return this;
+ }
}
Modified:
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEConfiguration.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEConfiguration.java?rev=1178192&r1=1178191&r2=1178192&view=diff
==============================================================================
---
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEConfiguration.java
(original)
+++
openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEConfiguration.java
Sun Oct 2 14:13:13 2011
@@ -25,6 +25,8 @@ public class TomEEConfiguration implemen
private int stopPort = 8005;
private String dir = System.getProperty("java.io.tmpdir") +
"/arquillian-apache-tomee";
private boolean plusContainer = true;
+ private String tomcatVersion = null;
+ private String openejbVersion = "1.0.0-beta-1";
public int getHttpPort() {
return httpPort;
@@ -57,7 +59,23 @@ public class TomEEConfiguration implemen
public void setPlusContainer(boolean plusContainer) {
this.plusContainer = plusContainer;
}
+
+ public String getTomcatVersion() {
+ return tomcatVersion;
+ }
+
+ public void setTomcatVersion(String tomcatVersion) {
+ this.tomcatVersion = tomcatVersion;
+ }
+
+ public String getOpenejbVersion() {
+ return openejbVersion;
+ }
+
+ public void setOpenejbVersion(String openejbVersion) {
+ this.openejbVersion = openejbVersion;
+ }
- public void validate() throws ConfigurationException {
+ public void validate() throws ConfigurationException {
}
}
Modified:
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEContainer.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEContainer.java?rev=1178192&r1=1178191&r2=1178192&view=diff
==============================================================================
---
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEContainer.java
(original)
+++
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEContainer.java
Sun Oct 2 14:13:13 2011
@@ -17,21 +17,23 @@
package org.apache.openejb.arquillian.remote;
import java.io.File;
+import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Collection;
+import org.apache.openejb.arquillian.common.MavenCache;
import org.apache.openejb.arquillian.common.SimpleMavenBuilderImpl;
import org.apache.openejb.arquillian.common.TomEEContainer;
import org.apache.openejb.config.RemoteServer;
import org.jboss.arquillian.spi.client.container.LifecycleException;
import org.jboss.shrinkwrap.api.GenericArchive;
import org.jboss.shrinkwrap.api.exporter.ExplodedExporter;
+import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.resolver.impl.maven.filter.StrictFilter;
+import org.sonatype.aether.artifact.Artifact;
public class RemoteTomEEContainer extends TomEEContainer {
- private static final String OPENEJB_VERSION = "4.0.0-beta-1-SNAPSHOT";
-
private RemoteServer container;
private boolean needsStart = false;
@@ -47,38 +49,23 @@ public class RemoteTomEEContainer extend
return;
}
- File catalinaDirectory = new File(configuration.getDir());
- catalinaDirectory.mkdirs();
-
- String artifactName;
- if (configuration.isPlusContainer()) {
- artifactName =
"org.apache.openejb:apache-tomee:zip:plus:" + OPENEJB_VERSION;
- } else {
- artifactName =
"org.apache.openejb:apache-tomee:zip:webprofile:" + OPENEJB_VERSION;
- }
-
- Collection<GenericArchive> archives = new SimpleMavenBuilderImpl()
- .artifact(artifactName)
- .resolveAs(GenericArchive.class, new StrictFilter());
+ File workingDirectory = new File(configuration.getDir());
+ workingDirectory.mkdirs();
- GenericArchive archive = archives.iterator().next();
- archive.as(ExplodedExporter.class).exportExploded(catalinaDirectory);
-
- File parent = new File(catalinaDirectory, archive.getName());
- if ((!parent.exists()) || (!parent.isDirectory())) {
- throw new LifecycleException("Unable to unpack TomEE zip file");
- }
-
File openejbHome = null;
- for (File directory : parent.listFiles()) {
- if (".".equals(directory.getName()) ||
"..".equals(directory.getName())) continue;
-
- if (directory.isDirectory()) {
- openejbHome = directory;
- break;
- }
+ if (configuration.getTomcatVersion() == null ||
configuration.getTomcatVersion().length() == 0) {
+ downloadTomEE(workingDirectory);
+ openejbHome = findOpenEJBHome(workingDirectory);
+ } else {
+ downloadTomcat(workingDirectory,
configuration.getTomcatVersion());
+ openejbHome = findOpenEJBHome(workingDirectory);
+ File webappsOpenEJB = new File(openejbHome, "webapps/openejb");
+ webappsOpenEJB.mkdirs();
+ downloadOpenEJBWebapp(webappsOpenEJB);
}
+
+ // TODO: then we need to use the Installer to fix up the ports we want
to use.
if (openejbHome == null || (! openejbHome.exists())) {
throw new LifecycleException("Error finding OPENEJB_HOME");
@@ -97,6 +84,78 @@ public class RemoteTomEEContainer extend
container.start();
}
+ private File findOpenEJBHome(File directory) {
+ File conf = new File(directory, "conf");
+ File webapps = new File(directory, "webapps");
+
+ if (conf.exists() && conf.isDirectory() && webapps.exists() &&
webapps.isDirectory()) {
+ return directory;
+ }
+
+ for (File file : directory.listFiles()) {
+ if (".".equals(file.getName()) ||
"..".equals(file.getName())) continue;
+
+ File found = findOpenEJBHome(file);
+ if (found != null) {
+ return found;
+ }
+ }
+
+ return null;
+ }
+
+ protected void downloadTomEE(File catalinaDirectory) throws
LifecycleException {
+ String artifactName;
+ if (configuration.isPlusContainer()) {
+ artifactName =
"org.apache.openejb:apache-tomee:zip:plus:" + configuration.getOpenejbVersion();
+ } else {
+ artifactName =
"org.apache.openejb:apache-tomee:zip:webprofile:" +
configuration.getOpenejbVersion();
+ }
+
+ File zipFile = downloadFile(artifactName, null);
+ ZipExtractor.unzip(zipFile, catalinaDirectory);
+ }
+
+ protected File downloadFile(String artifactName, String altUrl) {
+ Artifact artifact = new MavenCache().getArtifact(artifactName, altUrl);
+ return artifact.getFile();
+ }
+
+ protected void downloadOpenEJBWebapp(File targetDirectory) throws
LifecycleException {
+ String artifactName;
+ if (configuration.isPlusContainer()) {
+ artifactName =
"org.apache.openejb:openejb-tomcat-plus-webapp:war:" +
configuration.getOpenejbVersion();
+ } else {
+ artifactName =
"org.apache.openejb:openejb-tomcat-webapp:war:" +
configuration.getOpenejbVersion();
+ }
+
+ File zipFile = downloadFile(artifactName, null);
+ ZipExtractor.unzip(zipFile, targetDirectory);
+ }
+
+ protected void downloadTomcat(File catalinaDirectory, String
tomcatVersion) throws LifecycleException {
+ String source = null;
+
+ if (tomcatVersion.startsWith("7.")) {
+ source =
"http://archive.apache.org/dist/tomcat/tomcat-7/v" + tomcatVersion +
"/bin/apache-tomcat-" + tomcatVersion + ".zip";
+ }
+
+ if (tomcatVersion.startsWith("6.")) {
+ source =
"http://archive.apache.org/dist/tomcat/tomcat-6/v" + tomcatVersion +
"/bin/apache-tomcat-" + tomcatVersion + ".zip";
+ }
+
+ if (tomcatVersion.startsWith("5.5")) {
+ source =
"http://archive.apache.org/dist/tomcat/tomcat-5/v" + tomcatVersion +
"/bin/apache-tomcat-" + tomcatVersion + ".zip";
+ }
+
+ if (source == null) {
+ throw new LifecycleException("Unable to find URL for
Tomcat " + tomcatVersion);
+ }
+
+ File zipFile = downloadFile("org.apache.openejb:tomcat:zip:" +
tomcatVersion, source);
+ ZipExtractor.unzip(zipFile, catalinaDirectory);
+ }
+
public void stop() throws LifecycleException {
// only stop the container if we started it
if (needsStart) {
@@ -109,4 +168,7 @@ public class RemoteTomEEContainer extend
OutputStream out = socket.getOutputStream();
out.close();
}
+
+
+
}
Added:
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/ZipExtractor.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/ZipExtractor.java?rev=1178192&view=auto
==============================================================================
---
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/ZipExtractor.java
(added)
+++
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/ZipExtractor.java
Sun Oct 2 14:13:13 2011
@@ -0,0 +1,57 @@
+package org.apache.openejb.arquillian.remote;
+
+import java.io.*;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+public class ZipExtractor {
+
+ public static void unzip(File source, File targetDirectory) {
+ OutputStream os = null;
+ ZipInputStream is = null;
+
+ try {
+ is = new ZipInputStream(new FileInputStream(source));
+ ZipEntry entry;
+
+ while ((entry = is.getNextEntry()) != null) {
+ String name = entry.getName();
+ File file = new File(targetDirectory, name);
+
+ if (name.endsWith("/")) {
+ file.mkdir();
+ } else {
+ file.createNewFile();
+
+ int bytesRead;
+ byte data[] = new byte[8192];
+
+ os = new FileOutputStream(file);
+ while ((bytesRead = is.read(data)) != -1) {
+ os.write(data, 0, bytesRead);
+ }
+
+ is.closeEntry();
+ }
+
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (Exception e) {
+ }
+ }
+
+ if (os != null) {
+ try {
+ os.close();
+ } catch (Exception e) {
+ }
+ }
+
+ }
+ }
+}
Modified:
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/test/resources/arquillian.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/test/resources/arquillian.xml?rev=1178192&r1=1178191&r2=1178192&view=diff
==============================================================================
---
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/test/resources/arquillian.xml
(original)
+++
openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/test/resources/arquillian.xml
Sun Oct 2 14:13:13 2011
@@ -25,6 +25,8 @@
<property name="dir">/tmp/arquillian-apache-tomee</property>
<property name="httpPort">8080</property>
<property name="stopPort">8005</property>
+ <property name="tomcatVersion"></property>
+ <property name="openejbVersion">1.0.0-beta-1</property>
</configuration>
</container>
</arquillian>
\ No newline at end of file