diff --git a/jetty-plugins/pom.xml b/jetty-plugins/pom.xml new file mode 100644 index 00000000000..52fdd62e811 --- /dev/null +++ b/jetty-plugins/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + jetty-project + org.eclipse.jetty + 9.0.0-SNAPSHOT + + jetty-plugins + Jetty :: Plugin Support + The jetty plugin artifact. + + + + org.apache.maven.plugins + maven-jar-plugin + + + org.apache.maven.plugins + maven-assembly-plugin + + + jar-with-dependencies + + + + org.eclipse.jetty.plugins.Main + + + + + + package + + single + + + + + + + + + junit + junit + test + + + org.hamcrest + hamcrest-all + 1.0 + test + + + org.mockito + mockito-core + test + + + diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/Main.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/Main.java new file mode 100644 index 00000000000..a35f09f3e23 --- /dev/null +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/Main.java @@ -0,0 +1,129 @@ +// ======================================================================== +// Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== + +package org.eclipse.jetty.plugins; + +import java.util.List; +import java.util.Map; + +import org.eclipse.jetty.plugins.impl.HttpMavenServiceImpl; +import org.eclipse.jetty.plugins.impl.PluginManagerImpl; + +/* ------------------------------------------------------------ */ +/** + */ +public class Main { + private static final String JETTY_HOME = "JETTY_HOME"; + + private MavenService _mavenService = new HttpMavenServiceImpl(); + private PluginManager _pluginManager; + private String _jettyHome; + private String _installPlugin; + private boolean _listPlugins; + private String _repositoryUrl; + private String _groupId; + private String _version; + + /* ------------------------------------------------------------ */ + /** + * @param args + */ + public static void main(String[] args) { + Main main = new Main(); + main.execute(args); + } + + private void execute(String[] args) { + parseEnvironmentVariables(); + parseCommandline(args); + configureMavenService(); + + _pluginManager = new PluginManagerImpl(_mavenService, _jettyHome); + + if (_listPlugins) { + listPlugins(); + } else if (_installPlugin != null) { + installPlugin(); + } + } + + private void configureMavenService() { + if (_repositoryUrl != null) { + _mavenService.setRepositoryUrl(_repositoryUrl); + } + if (_groupId != null) { + _mavenService.setGroupId(_groupId); + } + if (_version != null) { + _mavenService.setVersion(_version); + } + } + + private void listPlugins() { + List availablePlugins = _pluginManager.listAvailablePlugins(); + for (String pluginName : availablePlugins) { + System.out.println(pluginName); + } + } + + private void installPlugin() { + _pluginManager.installPlugin(_installPlugin); + System.out.println("Successfully installed plugin: " + _installPlugin + + " to " + _jettyHome); + } + + private void parseEnvironmentVariables() { + Map env = System.getenv(); + if (env.containsKey(JETTY_HOME)) { + _jettyHome = env.get(JETTY_HOME); + } + } + + private void parseCommandline(String[] args) { + int i = 0; + for (String arg : args) { + i++; + + if (arg.startsWith("--jettyHome=")) { + _jettyHome = arg.substring(12); + } + if (arg.startsWith("--repositoryUrl=")) { + _repositoryUrl = arg.substring(16); + } + if (arg.startsWith("--groupId=")) { + _groupId = arg.substring(10); + } + if (arg.startsWith("--version=")) { + _version = arg.substring(10); + } + if (arg.startsWith("install")) { + _installPlugin = args[i]; + } + if ("list".equals(arg)) { + _listPlugins = true; + } + } + + // TODO: Usage instead of throwing exceptions + if (_jettyHome == null && _installPlugin != null) + throw new IllegalArgumentException( + "No --jettyHome commandline option specified and no \"JETTY_HOME\" environment variable found!"); + if (_installPlugin == null && _listPlugins == false) + throw new IllegalArgumentException( + "Neither install nor list commandline option specified. Nothing to do for me!"); + if (_installPlugin != null && _listPlugins) + throw new IllegalArgumentException( + "Please specify either install or list commandline options, but not both at the same time!"); + } + +} diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/MavenService.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/MavenService.java new file mode 100644 index 00000000000..a1db631bee2 --- /dev/null +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/MavenService.java @@ -0,0 +1,36 @@ +// ======================================================================== +// Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== + + +package org.eclipse.jetty.plugins; + +import java.util.List; + +import org.eclipse.jetty.plugins.model.Plugin; + + +/* ------------------------------------------------------------ */ +/** + */ +public interface MavenService +{ + public List listAvailablePlugins(); + + public Plugin getPlugin(String pluginName); + + public void setGroupId(String groupId); + + public void setRepositoryUrl(String repositoryUrl); + + public void setVersion(String version); +} diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/PluginManager.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/PluginManager.java new file mode 100644 index 00000000000..33409e9cd72 --- /dev/null +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/PluginManager.java @@ -0,0 +1,28 @@ +// ======================================================================== +// Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== + + +package org.eclipse.jetty.plugins; + +import java.util.List; + + +/* ------------------------------------------------------------ */ +/** + */ +public interface PluginManager +{ + public List listAvailablePlugins(); + + public void installPlugin(String pluginName); +} diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceImpl.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceImpl.java new file mode 100644 index 00000000000..582d7386b9c --- /dev/null +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceImpl.java @@ -0,0 +1,126 @@ +package org.eclipse.jetty.plugins.impl; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jetty.plugins.MavenService; +import org.eclipse.jetty.plugins.model.Plugin; +import org.eclipse.jetty.plugins.util.RepositoryParser; +import org.eclipse.jetty.plugins.util.StreamUtils; + +public class HttpMavenServiceImpl implements MavenService { + private static final String REPOSITORY_URL = "http://repo2.maven.org/maven2/"; + private static final String GROUP_ID = "org/eclipse/jetty"; + private static final String VERSION = "7.6.0.v20120127"; // TODO: should be + // automatically + // set + private String _repositoryUrl = REPOSITORY_URL; + private String _groupId = GROUP_ID; + private String _version = VERSION; + + public List listAvailablePlugins() { + List availablePlugins = new ArrayList(); + + String moduleListing = fetchDirectoryListingOfJettyModules(); + List modules = RepositoryParser + .parseLinksInDirectoryListing(moduleListing); + + for (String module : modules) { + String listing = fetchModuleDirectoryListing(module); + if (RepositoryParser.isModuleAPlugin(listing)) { + availablePlugins.add(module); + } + } + + return availablePlugins; + } + + private String fetchDirectoryListingOfJettyModules() { + try { + URL url = new URL(_repositoryUrl + _groupId); + URLConnection connection = url.openConnection(); + InputStream inputStream = connection.getInputStream(); + return StreamUtils.inputStreamToString(inputStream); + } catch (MalformedURLException e) { + throw new IllegalStateException(e); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + private String fetchModuleDirectoryListing(String module) { + try { + URL configJar = new URL(getModuleDirectory(module)); + URLConnection connection = configJar.openConnection(); + InputStream inputStream = connection.getInputStream(); + return StreamUtils.inputStreamToString(inputStream); + } catch (MalformedURLException e) { + throw new IllegalStateException(e); + } catch (IOException e) { + // Honestly, I'm not a friend of ignoring exceptions as it might + // hide something important. In this case however it "usually" + // just means: THIS IS NOT A PLUGIN! However it still might hide + // things. If that'll be the case, I hope I'm not the one who + // has to debug my own code. ;) + return "not a plugin"; + } + } + + public Plugin getPlugin(String pluginName) { + File configJar = getFile(getModulePrefix(pluginName) + "-plugin.jar"); + return new Plugin(pluginName, configJar); + } + + private String getModuleDirectory(String pluginName) { + return _repositoryUrl + _groupId + "/" + pluginName + "/" + _version + + "/"; + } + + private String getModulePrefix(String pluginName) { + return getModuleDirectory(pluginName) + pluginName + "-" + _version; + } + + private File getFile(String urlString) { + String fileName = urlString.substring(urlString.lastIndexOf("/") + 1); + try { + URL url = new URL(urlString); + URLConnection connection = url.openConnection(); + InputStream inputStream = connection.getInputStream(); + File tempFile = new File(System.getProperty("java.io.tmpdir"), + fileName); + OutputStream out = new FileOutputStream(tempFile); + byte buf[] = new byte[1024]; + int len; + while ((len = inputStream.read(buf)) > 0) + out.write(buf, 0, len); + out.close(); + inputStream.close(); + return tempFile; + } catch (MalformedURLException e) { + throw new IllegalStateException(e); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + public void setGroupId(String groupId) { + this._groupId = groupId.replace(".", "/"); + } + + public void setRepositoryUrl(String repositoryUrl) { + this._repositoryUrl = repositoryUrl; + } + + public void setVersion(String version) { + this._version = version; + } + +} diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/PluginManagerImpl.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/PluginManagerImpl.java new file mode 100644 index 00000000000..6b00fd66891 --- /dev/null +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/impl/PluginManagerImpl.java @@ -0,0 +1,110 @@ +// ======================================================================== +// Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== + +package org.eclipse.jetty.plugins.impl; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import org.eclipse.jetty.plugins.MavenService; +import org.eclipse.jetty.plugins.PluginManager; +import org.eclipse.jetty.plugins.model.Plugin; + +/* ------------------------------------------------------------ */ +/** + */ +public class PluginManagerImpl implements PluginManager { + private String _jettyHome; + private MavenService _mavenService; + + private static List excludes = Arrays.asList("META-INF"); + + public PluginManagerImpl(MavenService mavenService, String jettyHome) { + this._mavenService = mavenService; + this._jettyHome = jettyHome; + } + + /* ------------------------------------------------------------ */ + /** + * @see org.eclipse.jetty.plugins.PluginManager#listAvailablePlugins() + */ + public List listAvailablePlugins() { + return _mavenService.listAvailablePlugins(); + } + + /* ------------------------------------------------------------ */ + /** + * @see org.eclipse.jetty.plugins.PluginManager#installPlugin(String) + */ + public void installPlugin(String pluginName) { + Plugin plugin = _mavenService.getPlugin(pluginName); + installPlugin(plugin); + } + + private void installPlugin(Plugin plugin) { + try { + JarFile pluginJar = new JarFile(plugin.getPluginJar()); + extractJar(pluginJar); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + private void extractJar(JarFile file) { + Enumeration entries = file.entries(); + while (entries.hasMoreElements()) { + extractFileFromJar(file, entries.nextElement()); + } + } + + private void extractFileFromJar(JarFile jarFile, JarEntry jarEntry) { + for (String exclude : excludes) + if (jarEntry.getName().startsWith(exclude)) + return; + + System.out.println("Extracting: " + jarEntry.getName()); + File f = new File(_jettyHome + File.separator + jarEntry.getName()); + if (jarEntry.isDirectory()) { // if its a directory, create it + f.mkdir(); + return; + } + InputStream is = null; + FileOutputStream fos = null; + try { + is = jarFile.getInputStream(jarEntry); + fos = new FileOutputStream(f); + while (is.available() > 0) { + fos.write(is.read()); + } + } catch (IOException e) { + throw new IllegalStateException( + "IOException while extracting plugin jar: ", e); + } finally { + try { + fos.close(); + is.close(); + } catch (IOException e) { + throw new IllegalStateException( + "Couldn't close InputStream or FileOutputStream. This might be a file leak!", + e); + } + } + } +} diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/model/Plugin.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/model/Plugin.java new file mode 100644 index 00000000000..a9a06defe90 --- /dev/null +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/model/Plugin.java @@ -0,0 +1,22 @@ +package org.eclipse.jetty.plugins.model; + +import java.io.File; + +public class Plugin { + private String name; + + private File pluginJar; + + public Plugin(String name, File configJar) { + this.name = name; + this.pluginJar = configJar; + } + + public String getName() { + return name; + } + + public File getPluginJar() { + return pluginJar; + } +} diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/RepositoryParser.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/RepositoryParser.java new file mode 100644 index 00000000000..e7b70381bc2 --- /dev/null +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/RepositoryParser.java @@ -0,0 +1,46 @@ +/** + * + */ +package org.eclipse.jetty.plugins.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author tbecker + * + */ +public class RepositoryParser { + private final static List EXCLUDES = Arrays.asList(".."); + + public static List parseLinksInDirectoryListing(String listing) { + List modules = new ArrayList(); + List lines = Arrays.asList(listing.split("\n")); + for (String line : lines) { + Pattern p = Pattern.compile(".*?]+>(?=([^ lines = Arrays.asList(listing.split("\n")); + for (String line : lines) { + Pattern p = Pattern.compile("-plugin\\.jar"); + Matcher m = p.matcher(line); + if (m.find()) { + return true; + } + } + return false; + } + +} diff --git a/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/StreamUtils.java b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/StreamUtils.java new file mode 100644 index 00000000000..9de4241611c --- /dev/null +++ b/jetty-plugins/src/main/java/org/eclipse/jetty/plugins/util/StreamUtils.java @@ -0,0 +1,21 @@ +package org.eclipse.jetty.plugins.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class StreamUtils { + public static String inputStreamToString(InputStream inputStream) throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + StringBuilder stringBuilder = new StringBuilder(); + String line = null; + + while ((line = bufferedReader.readLine()) != null) { + stringBuilder.append(line + "\n"); + } + + bufferedReader.close(); + return stringBuilder.toString(); + } +} diff --git a/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceTest.java b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceTest.java new file mode 100644 index 00000000000..be4df02e9dd --- /dev/null +++ b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/HttpMavenServiceTest.java @@ -0,0 +1,65 @@ +package org.eclipse.jetty.plugins.impl; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import org.eclipse.jetty.plugins.MavenService; +import org.eclipse.jetty.plugins.model.Plugin; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; + +/** + * This is currently more an integration test downloading real stuff from real + * maven repositories. Actually it's preferred to have a real unit test or at + * least a local repository server. But since HttpClient.send(exchange) has an + * api which is really hard to mock, I will leave that excercise for later. + * + * However this tests should be disabled for the general build and ci. + * + * @author tbecker + * + */ +public class HttpMavenServiceTest { + private MavenService _mavenService = new HttpMavenServiceImpl(); + + private static final String JETTY_JMX_PLUGIN_NAME = "jetty-jmx"; + private static final String PRIVATE_NEXUS_REPOSITORY_URL = "http://gravity-design.de:8080/nexus/content/repositories/releases/"; + private static final String MAVEN_CENTRAL_URL = "http://repo2.maven.org/maven2/"; + + @Before + public void setUp() throws Exception { + _mavenService.setRepositoryUrl(PRIVATE_NEXUS_REPOSITORY_URL); + } + + @Test + @Ignore("requires online repo") + public void testListAvailablePlugins() { + List pluginNames = _mavenService.listAvailablePlugins(); + assertThat(pluginNames.size(), is(2)); + } + + @Test + @Ignore("requires online repo") + public void testGetPluginJar() throws IOException { + Plugin plugin = _mavenService.getPlugin(JETTY_JMX_PLUGIN_NAME); + assertThat("jetty-jmx should contain a plugin-jar", + plugin.getPluginJar(), is(notNullValue())); + } + + @Test + @Ignore("requires online repo") + public void testGetConfigJar() throws IOException { + Plugin plugin = _mavenService.getPlugin(JETTY_JMX_PLUGIN_NAME); + File configJar = plugin.getPluginJar(); + assertThat(configJar, is(not(nullValue()))); + } + +} diff --git a/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/PluginManagerTest.java b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/PluginManagerTest.java new file mode 100644 index 00000000000..5d7c32542b7 --- /dev/null +++ b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/impl/PluginManagerTest.java @@ -0,0 +1,156 @@ +// ======================================================================== +// Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== + +package org.eclipse.jetty.plugins.impl; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URL; +import java.nio.channels.FileChannel; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jetty.plugins.MavenService; +import org.eclipse.jetty.plugins.model.Plugin; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +/* ------------------------------------------------------------ */ +/** + */ +@RunWith(MockitoJUnitRunner.class) +public class PluginManagerTest { + @Mock + private MavenService _mavenService; + + private PluginManagerImpl _pluginManager; + + private List availablePlugins = createAvailablePluginsTestData(); + private ClassLoader _classLoader = this.getClass().getClassLoader(); + private String _tmpDir; + private File _javaTmpDir = new File(System.getProperty("java.io.tmpdir")); + + /* ------------------------------------------------------------ */ + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + URL resource = this.getClass().getResource("/jetty_home"); + _tmpDir = resource.getFile(); + _pluginManager = new PluginManagerImpl(_mavenService, _tmpDir); + } + + @Test + public void testListAvailablePlugins() { + when(_mavenService.listAvailablePlugins()).thenReturn(availablePlugins); + List availablePlugins = _pluginManager.listAvailablePlugins(); + assertThat("jetty-jmx not found", + availablePlugins.contains("jetty-jmx"), is(true)); + assertThat("jetty-jta not found", + availablePlugins.contains("jetty-jta"), is(true)); + } + + @Test + public void testInstallPluginJar() { + String pluginName = "jetty-plugin-with-plugin-jar"; + String pluginJar = _classLoader.getResource("example-plugin.jar") + .getFile(); + File pluginJarFile = new File(pluginJar); + Plugin plugin = createTestPlugin(pluginName, pluginJarFile); + + when(_mavenService.getPlugin(pluginName)).thenReturn(plugin); + + _pluginManager.installPlugin(pluginName); + + File someJar = new File(_tmpDir + File.separator + "lib" + + File.separator + "someJar.jar"); + assertThat("someJar.jar does not exist", someJar.exists(), is(true)); + File someOtherJar = new File(_tmpDir + File.separator + "lib" + + File.separator + "someOtherJar.jar"); + assertThat("someOtherJar.jar does not exist", someOtherJar.exists(), + is(true)); + } + + @Test + public void testInstallPlugins() throws IOException { + + String pluginName = "jetty-jmx"; + String jmxPluginConfigJar = _classLoader.getResource( + "jetty-jmx-7.6.0.v20120127-plugin.jar").getFile(); + File jmxPluginConfigJarFile = new File(jmxPluginConfigJar); + + // Need to copy it to a temp file since the implementation will move the + // file and we need to keep the test files where they are. + File jmxPluginConfigTempCopy = copyToTempFile(jmxPluginConfigJarFile); + + Plugin plugin = new Plugin(pluginName, jmxPluginConfigTempCopy); + + when(_mavenService.getPlugin(pluginName)).thenReturn(plugin); + + _pluginManager.installPlugin(pluginName); + + File metaInf = new File(_tmpDir + File.separator + "META-INF"); + File jettyXmlConfigFile = new File(_tmpDir + File.separator + "start.d" + + File.separator + "20-jetty-jmx.xml"); + File jettyJmxJarFile = new File(_tmpDir + File.separator + "lib" + + File.separator + "jetty-jmx-7.6.0.v20120127.jar"); + assertThat("META-INF should be skipped", metaInf.exists(), not(true)); + assertThat("20-jetty-jmx.xml does not exist", + jettyXmlConfigFile.exists(), is(true)); + assertThat("jetty-jmx-7.6.0.v20120127.jar does not exist", + jettyJmxJarFile.exists(), is(true)); + } + + public File copyToTempFile(File sourceFile) throws IOException { + File destFile = new File(_javaTmpDir + File.separator + + sourceFile.getName()); + FileChannel source = null; + FileChannel destination = null; + try { + source = new FileInputStream(sourceFile).getChannel(); + destination = new FileOutputStream(destFile).getChannel(); + destination.transferFrom(source, 0, source.size()); + } finally { + if (source != null) { + source.close(); + } + if (destination != null) { + destination.close(); + } + } + return destFile; + } + + private List createAvailablePluginsTestData() { + List availablePlugins = new ArrayList(); + availablePlugins.add("jetty-jmx"); + availablePlugins.add("jetty-jta"); + return availablePlugins; + } + + private Plugin createTestPlugin(String name, File jar) { + Plugin plugin = new Plugin(name, jar); + return plugin; + } + +} diff --git a/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/util/RepositoryParserTest.java b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/util/RepositoryParserTest.java new file mode 100644 index 00000000000..c8f27aea977 --- /dev/null +++ b/jetty-plugins/src/test/java/org/eclipse/jetty/plugins/util/RepositoryParserTest.java @@ -0,0 +1,35 @@ +package org.eclipse.jetty.plugins.util; + +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class RepositoryParserTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testParseLinksInDirectoryListing() throws IOException { + String listing = StreamUtils.inputStreamToString(this.getClass().getClassLoader().getResourceAsStream("mavenRepoJettyDirectoryListing.html")); + List modules = RepositoryParser.parseLinksInDirectoryListing(listing); + assertThat("At least ten jetty modules expected",modules.size(), greaterThan(10)); + assertThat("jetty-jmx module expected", modules.contains("jetty-jmx"), is(true)); + } + + @Test + public void testIsPlugin() throws IOException{ + String listing = StreamUtils.inputStreamToString(this.getClass().getClassLoader().getResourceAsStream("mavenRepoJettyJMXDirectoryListing.html")); + assertThat("listing describes a plugin", RepositoryParser.isModuleAPlugin(listing), is(true)); + String nonPluginListing = StreamUtils.inputStreamToString(this.getClass().getClassLoader().getResourceAsStream("mavenRepoJettyJNDIDirectoryListing.html")); + assertThat("listing doesn't describe a plugin", RepositoryParser.isModuleAPlugin(nonPluginListing), is(false)); + } + +} diff --git a/jetty-plugins/src/test/resources/example-plugin.jar b/jetty-plugins/src/test/resources/example-plugin.jar new file mode 100644 index 00000000000..31cd1e04d4b Binary files /dev/null and b/jetty-plugins/src/test/resources/example-plugin.jar differ diff --git a/jetty-plugins/src/test/resources/jetty-jmx-7.6.0.v20120127-plugin.jar b/jetty-plugins/src/test/resources/jetty-jmx-7.6.0.v20120127-plugin.jar new file mode 100644 index 00000000000..ed99c27732a Binary files /dev/null and b/jetty-plugins/src/test/resources/jetty-jmx-7.6.0.v20120127-plugin.jar differ diff --git a/jetty-plugins/src/test/resources/jetty_home/.donotdelete b/jetty-plugins/src/test/resources/jetty_home/.donotdelete new file mode 100644 index 00000000000..e69de29bb2d diff --git a/jetty-plugins/src/test/resources/mavenRepoJettyDirectoryListing.html b/jetty-plugins/src/test/resources/mavenRepoJettyDirectoryListing.html new file mode 100644 index 00000000000..cd6549cac38 --- /dev/null +++ b/jetty-plugins/src/test/resources/mavenRepoJettyDirectoryListing.html @@ -0,0 +1,55 @@ + +Index of /maven2/org/eclipse/jetty/ + +

Index of /maven2/org/eclipse/jetty/


../
+aggregate/                                         22-Dec-2011 21:20                   -
+example-async-rest/                                09-Mar-2012 16:06                   -
+example-jetty-embedded/                            09-Mar-2012 16:06                   -
+jetty-ajp/                                         09-Mar-2012 16:06                   -
+jetty-annotations/                                 09-Mar-2012 16:06                   -
+jetty-client/                                      09-Mar-2012 16:06                   -
+jetty-continuation/                                09-Mar-2012 16:06                   -
+jetty-deploy/                                      09-Mar-2012 16:06                   -
+jetty-distribution/                                09-Mar-2012 16:06                   -
+jetty-embedded-examples/                           21-Sep-2009 15:50                   -
+jetty-http/                                        09-Mar-2012 16:06                   -
+jetty-http-spi/                                    09-Mar-2012 16:06                   -
+jetty-io/                                          09-Mar-2012 16:06                   -
+jetty-jaspi/                                       09-Mar-2012 16:06                   -
+jetty-jmx/                                         09-Mar-2012 16:06                   -
+jetty-jndi/                                        09-Mar-2012 16:06                   -
+jetty-jsp/                                         09-Mar-2012 16:06                   -
+jetty-jsp-2.1/                                     25-Oct-2011 01:05                   -
+jetty-monitor/                                     09-Mar-2012 16:06                   -
+jetty-nested/                                      09-Mar-2012 16:06                   -
+jetty-nosql/                                       09-Mar-2012 16:06                   -
+jetty-overlay-deployer/                            09-Mar-2012 16:06                   -
+jetty-parent/                                      20-Sep-2011 16:54                   -
+jetty-plus/                                        09-Mar-2012 16:06                   -
+jetty-policy/                                      09-Mar-2012 16:06                   -
+jetty-project/                                     09-Mar-2012 16:06                   -
+jetty-rewrite/                                     09-Mar-2012 16:06                   -
+jetty-security/                                    09-Mar-2012 16:06                   -
+jetty-server/                                      09-Mar-2012 16:06                   -
+jetty-servlet/                                     09-Mar-2012 16:06                   -
+jetty-servlet-tester/                              21-Sep-2009 15:52                   -
+jetty-servlets/                                    09-Mar-2012 16:06                   -
+jetty-start/                                       09-Mar-2012 16:06                   -
+jetty-test-webapp/                                 21-Sep-2009 15:50                   -
+jetty-util/                                        09-Mar-2012 16:06                   -
+jetty-webapp/                                      09-Mar-2012 16:06                   -
+jetty-websocket/                                   09-Mar-2012 16:06                   -
+jetty-xml/                                         09-Mar-2012 16:06                   -
+npn/                                               09-Mar-2012 16:05                   -
+orbit/                                             24-Jan-2012 23:22                   -
+osgi/                                              27-May-2011 08:34                   -
+spdy/                                              09-Mar-2012 16:05                   -
+test-continuation/                                 01-Apr-2010 13:30                   -
+test-continuation-jetty6/                          01-Apr-2010 13:30                   -
+test-jetty-nested/                                 09-Mar-2012 16:06                   -
+test-jetty-servlet/                                09-Mar-2012 16:06                   -
+test-jetty-webapp/                                 09-Mar-2012 16:06                   -
+tests/                                             30-Nov-2011 02:16                   -
+toolchain/                                         06-Dec-2011 23:32                   -
+

+ \ No newline at end of file diff --git a/jetty-plugins/src/test/resources/mavenRepoJettyJMXDirectoryListing.html b/jetty-plugins/src/test/resources/mavenRepoJettyJMXDirectoryListing.html new file mode 100644 index 00000000000..ac33b1ac140 --- /dev/null +++ b/jetty-plugins/src/test/resources/mavenRepoJettyJMXDirectoryListing.html @@ -0,0 +1,36 @@ + +Index of /maven2/org/eclipse/jetty/jetty-jmx/7.6.0.v20120127/ + +

Index of /maven2/org/eclipse/jetty/jetty-jmx/7.6.0.v20120127/


../
+jetty-jmx-7.6.0.v20120127-plugin.jar               27-Jan-2012 14:24                1873
+jetty-jmx-7.6.0.v20120127-plugin.jar.asc           27-Jan-2012 14:24                 198
+jetty-jmx-7.6.0.v20120127-plugin.jar.asc.md5       27-Jan-2012 14:24                  32
+jetty-jmx-7.6.0.v20120127-plugin.jar.asc.sha1      27-Jan-2012 14:24                  40
+jetty-jmx-7.6.0.v20120127-plugin.jar.md5           27-Jan-2012 14:24                  32
+jetty-jmx-7.6.0.v20120127-plugin.jar.sha1          27-Jan-2012 14:24                  40
+jetty-jmx-7.6.0.v20120127-javadoc.jar              27-Jan-2012 14:24               52590
+jetty-jmx-7.6.0.v20120127-javadoc.jar.asc          27-Jan-2012 14:24                 198
+jetty-jmx-7.6.0.v20120127-javadoc.jar.asc.md5      27-Jan-2012 14:24                  32
+jetty-jmx-7.6.0.v20120127-javadoc.jar.asc.sha1     27-Jan-2012 14:24                  40
+jetty-jmx-7.6.0.v20120127-javadoc.jar.md5          27-Jan-2012 14:24                  32
+jetty-jmx-7.6.0.v20120127-javadoc.jar.sha1         27-Jan-2012 14:24                  40
+jetty-jmx-7.6.0.v20120127-sources.jar              27-Jan-2012 14:24               16675
+jetty-jmx-7.6.0.v20120127-sources.jar.asc          27-Jan-2012 14:24                 198
+jetty-jmx-7.6.0.v20120127-sources.jar.asc.md5      27-Jan-2012 14:24                  32
+jetty-jmx-7.6.0.v20120127-sources.jar.asc.sha1     27-Jan-2012 14:24                  40
+jetty-jmx-7.6.0.v20120127-sources.jar.md5          27-Jan-2012 14:24                  32
+jetty-jmx-7.6.0.v20120127-sources.jar.sha1         27-Jan-2012 14:24                  40
+jetty-jmx-7.6.0.v20120127.jar                      27-Jan-2012 14:24               23187
+jetty-jmx-7.6.0.v20120127.jar.asc                  27-Jan-2012 14:24                 198
+jetty-jmx-7.6.0.v20120127.jar.asc.md5              27-Jan-2012 14:24                  32
+jetty-jmx-7.6.0.v20120127.jar.asc.sha1             27-Jan-2012 14:24                  40
+jetty-jmx-7.6.0.v20120127.jar.md5                  27-Jan-2012 14:24                  32
+jetty-jmx-7.6.0.v20120127.jar.sha1                 27-Jan-2012 14:24                  40
+jetty-jmx-7.6.0.v20120127.pom                      27-Jan-2012 14:24                2655
+jetty-jmx-7.6.0.v20120127.pom.asc                  27-Jan-2012 14:24                 198
+jetty-jmx-7.6.0.v20120127.pom.asc.md5              27-Jan-2012 14:24                  32
+jetty-jmx-7.6.0.v20120127.pom.asc.sha1             27-Jan-2012 14:24                  40
+jetty-jmx-7.6.0.v20120127.pom.md5                  27-Jan-2012 14:24                  32
+jetty-jmx-7.6.0.v20120127.pom.sha1                 27-Jan-2012 14:24                  40
+

+ \ No newline at end of file diff --git a/jetty-plugins/src/test/resources/mavenRepoJettyJNDIDirectoryListing.html b/jetty-plugins/src/test/resources/mavenRepoJettyJNDIDirectoryListing.html new file mode 100644 index 00000000000..e1e6bf82ae8 --- /dev/null +++ b/jetty-plugins/src/test/resources/mavenRepoJettyJNDIDirectoryListing.html @@ -0,0 +1,30 @@ + +Index of /maven2/org/eclipse/jetty/jetty-jndi/7.6.0.v20120127/ + +

Index of /maven2/org/eclipse/jetty/jetty-jndi/7.6.0.v20120127/


../
+jetty-jndi-7.6.0.v20120127-javadoc.jar             27-Jan-2012 14:37              127695
+jetty-jndi-7.6.0.v20120127-javadoc.jar.asc         27-Jan-2012 14:37                 198
+jetty-jndi-7.6.0.v20120127-javadoc.jar.asc.md5     27-Jan-2012 14:37                  32
+jetty-jndi-7.6.0.v20120127-javadoc.jar.asc.sha1    27-Jan-2012 14:37                  40
+jetty-jndi-7.6.0.v20120127-javadoc.jar.md5         27-Jan-2012 14:37                  32
+jetty-jndi-7.6.0.v20120127-javadoc.jar.sha1        27-Jan-2012 14:37                  40
+jetty-jndi-7.6.0.v20120127-sources.jar             27-Jan-2012 14:37               26645
+jetty-jndi-7.6.0.v20120127-sources.jar.asc         27-Jan-2012 14:37                 198
+jetty-jndi-7.6.0.v20120127-sources.jar.asc.md5     27-Jan-2012 14:37                  32
+jetty-jndi-7.6.0.v20120127-sources.jar.asc.sha1    27-Jan-2012 14:37                  40
+jetty-jndi-7.6.0.v20120127-sources.jar.md5         27-Jan-2012 14:37                  32
+jetty-jndi-7.6.0.v20120127-sources.jar.sha1        27-Jan-2012 14:37                  40
+jetty-jndi-7.6.0.v20120127.jar                     27-Jan-2012 14:36               38073
+jetty-jndi-7.6.0.v20120127.jar.asc                 27-Jan-2012 14:37                 198
+jetty-jndi-7.6.0.v20120127.jar.asc.md5             27-Jan-2012 14:37                  32
+jetty-jndi-7.6.0.v20120127.jar.asc.sha1            27-Jan-2012 14:37                  40
+jetty-jndi-7.6.0.v20120127.jar.md5                 27-Jan-2012 14:36                  32
+jetty-jndi-7.6.0.v20120127.jar.sha1                27-Jan-2012 14:36                  40
+jetty-jndi-7.6.0.v20120127.pom                     27-Jan-2012 14:36                2810
+jetty-jndi-7.6.0.v20120127.pom.asc                 27-Jan-2012 14:37                 198
+jetty-jndi-7.6.0.v20120127.pom.asc.md5             27-Jan-2012 14:37                  32
+jetty-jndi-7.6.0.v20120127.pom.asc.sha1            27-Jan-2012 14:37                  40
+jetty-jndi-7.6.0.v20120127.pom.md5                 27-Jan-2012 14:37                  32
+jetty-jndi-7.6.0.v20120127.pom.sha1                27-Jan-2012 14:37                  40
+

+ \ No newline at end of file diff --git a/pom.xml b/pom.xml index f087c77d06b..6d832614bff 100644 --- a/pom.xml +++ b/pom.xml @@ -377,7 +377,7 @@ jetty-deploy jetty-start - + jetty-plugins