jetty-9 jetty-plugins is now able to handle multiple repositoriesmci

This commit is contained in:
Thomas Becker 2012-09-04 16:53:56 +02:00
parent 0b77a69571
commit 1b3d32e337
7 changed files with 112 additions and 88 deletions

View File

@ -89,7 +89,8 @@ public class DefaultPluginManager implements PluginManager
if (zipEntry.isDirectory())
{
// if its a directory, create it
f.mkdir(); // TODO: check the result: what if the directory cannot be created ?
if(!f.mkdir()) // TODO: what if the directory cannot be created?
System.out.println("Can't create directory: " + f);
return;
}

View File

@ -38,24 +38,23 @@ import org.eclipse.jetty.plugins.util.StreamUtils;
public class HttpMavenService implements MavenService
{
private static final String REPOSITORY_URL = "http://repo2.maven.org/maven2/";
// autodetect...without maven deps
private static final String GROUP_ID = "org/eclipse/jetty";
private static final String[] GROUP_IDS = new String[]{"org/eclipse/jetty"};
private static final String VERSION = "9.0.0-SNAPSHOT"; // TODO: should be automatically set
private boolean _searchRemoteRepository = true;
private boolean _searchLocalRepository = false;
private String _localRepository = MavenUtils.getLocalRepositoryLocation();
private String _repositoryUrl = REPOSITORY_URL;
private String _groupId = GROUP_ID;
private String[] _groupIds = GROUP_IDS;
private String _version = VERSION;
public Set<String> listAvailablePlugins()
{
System.out.println("Using local repo: " + _searchLocalRepository + " remote repo: " + _searchRemoteRepository);
Set<String> availablePlugins = new HashSet<>();
if(_searchRemoteRepository)
if (_searchRemoteRepository)
availablePlugins.addAll(getListOfRemotePlugins());
if(_searchLocalRepository)
if (_searchLocalRepository)
availablePlugins.addAll(getListOfLocalPlugins());
return availablePlugins;
@ -64,21 +63,28 @@ public class HttpMavenService implements MavenService
private Set<String> getListOfLocalPlugins()
{
Set<String> availablePlugins = new HashSet<>();
File localMavenRepository = new File(_localRepository + _groupId);
if(!localMavenRepository.exists())
File localMavenRepository = new File(_localRepository);
if (!localMavenRepository.exists())
{
System.out.println("Can't find local repo: " + localMavenRepository);
return availablePlugins;
}
System.out.println("Using local repository: " + localMavenRepository);
String[] localMavenModuleList = localMavenRepository.list();
for (String potentialPlugin : localMavenModuleList)
for (String groupId : _groupIds)
{
File pluginFile = new File(_localRepository + getPluginPath(potentialPlugin));
if(pluginFile.exists())
availablePlugins.add(potentialPlugin);
File file = new File(_localRepository + groupId);
if (!file.exists())
break;
String[] localMavenModuleList = file.list();
for (String potentialPlugin : localMavenModuleList)
{
File pluginFile = new File(_localRepository + getPluginPath(groupId,potentialPlugin));
if (pluginFile.exists())
availablePlugins.add(potentialPlugin);
}
}
return availablePlugins;
@ -107,10 +113,15 @@ public class HttpMavenService implements MavenService
{
try
{
URL url = new URL(_repositoryUrl + _groupId);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
return StreamUtils.inputStreamToString(inputStream);
StringBuilder directoryListing = new StringBuilder();
for (String groupId : _groupIds)
{
URL url = new URL(_repositoryUrl + groupId);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
directoryListing.append(StreamUtils.inputStreamToString(inputStream));
}
return directoryListing.toString();
}
catch (IOException e)
{
@ -120,83 +131,99 @@ public class HttpMavenService implements MavenService
private String fetchModuleDirectoryListing(String module)
{
try
for (String groupId : _groupIds)
{
URL configJar = new URL(getRemoteModuleDirectory(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";
try
{
URL configJar = new URL(_repositoryUrl + getModulePath(groupId, 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. ;)
System.out.println(e); //TODO:
}
}
return "not a plugin";
}
public Plugin getPlugin(String pluginName)
{
File configJar = getFile(getRemotePluginLocation(pluginName));
return new Plugin(pluginName, configJar);
File pluginJar = getPluginFile(pluginName);
return new Plugin(pluginName, pluginJar);
}
private String getRemoteModuleDirectory(String pluginName)
private String getPluginPath(String groupId, String pluginName)
{
return _repositoryUrl + getModulePath(pluginName);
return getModulePath(groupId, pluginName) + pluginName + "-" + _version + "-plugin.zip";
}
private String getRemotePluginLocation(String pluginName)
private String getModulePath(String groupId, String pluginName)
{
return _repositoryUrl + getPluginPath(pluginName);
}
private String getPluginPath(String pluginName)
{
return getModulePath(pluginName) + pluginName + "-" + _version + "-plugin.zip";
}
private String getModulePath(String pluginName)
{
return _groupId + "/" + pluginName + "/" + _version
return groupId + "/" + pluginName + "/" + _version
+ "/";
}
private File getFile(String urlString)
/**
* Tries to find the plugin in the local repo first and then tries the remote repositories in the order they're
* stored in _repositoryUrls
*
* @param pluginName the name of the plugin to get the plugin file for
* @return the plugin file
*/
private File getPluginFile(String pluginName)
{
String fileName = urlString.substring(urlString.lastIndexOf("/") + 1);
try
for (String groupId : _groupIds)
{
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 (IOException e)
{
throw new IllegalStateException(e);
File pluginFile = new File(MavenUtils.getLocalRepositoryLocation() + getPluginPath(groupId, pluginName));
if (pluginFile.exists())
return pluginFile;
String urlString = _repositoryUrl + getPluginPath(groupId, pluginName);
String fileName = urlString.substring(urlString.lastIndexOf("/") + 1);
try
{
return getPluginFileFromRemoteLocation(urlString, fileName);
}
catch (IOException e)
{
System.out.println("Couldn't find plugin: " + pluginName + " at repo: " + _repositoryUrl + ". " +
"Probably trying other repo. Reason: " + e.getMessage());
}
}
throw new IllegalStateException("Plugin: " + pluginName + " not found at any configured repo.");
}
private File getPluginFileFromRemoteLocation(String urlString, String fileName) throws IOException
{
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;
}
public void setGroupId(String groupId)
{
this._groupId = groupId.replace(".", "/");
this._groupIds = new String[] { groupId.replace(".", "/") };
}
public void setLocalRepository(String localRepository)

View File

@ -106,9 +106,9 @@ public class Main
if (arg.startsWith("--version="))
_version = arg.substring(10);
if (arg.startsWith("--useLocalRepo="))
_searchLocalRepository = new Boolean(arg.substring(15));
_searchLocalRepository = Boolean.valueOf(arg.substring(15));
if (arg.startsWith("--useRemoteRepo="))
_searchRemoteRepository = new Boolean(arg.substring(15));
_searchRemoteRepository = Boolean.valueOf(arg.substring(15));
if (arg.startsWith("install"))
_installPlugin = args[i];
if ("list".equals(arg))

View File

@ -25,10 +25,10 @@ public class Plugin
private final String name;
private final File pluginJar;
public Plugin(String name, File configJar)
public Plugin(String name, File pluginJar)
{
this.name = name;
this.pluginJar = configJar;
this.pluginJar = pluginJar;
}
public String getName()

View File

@ -26,7 +26,7 @@ import org.xml.sax.helpers.XMLReaderFactory;
public class MavenUtils
{
private final static String DEFAULT_REPOSITORY_LOCATION = System.getProperty("user.home") + "/.m2/repository";
private final static String DEFAULT_REPOSITORY_LOCATION = System.getProperty("user.home") + "/.m2/repository/";
/**
* Looks for maven's settings.xml in $M2_HOME/conf/settings.xml
@ -36,8 +36,7 @@ public class MavenUtils
static File findGlobalSettingsXml()
{
String m2Home = System.getenv("M2_HOME");
File settingsXml = new File(m2Home + "/conf/settings.xml");
return settingsXml;
return new File(m2Home + "/conf/settings.xml");
}
/**
@ -48,8 +47,7 @@ public class MavenUtils
static File findUserSettingsXml()
{
String userHome = System.getProperty("user.home");
File settingsXml = new File(userHome + "/.m2/settings.xml");
return settingsXml;
return new File(userHome + "/.m2/settings.xml");
}
/**
@ -146,7 +144,7 @@ public class MavenUtils
}
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
}

View File

@ -34,9 +34,8 @@ 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
* 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 exercise for later.
* <p/>
* However this tests should be disabled for the general build and ci.
@ -52,7 +51,7 @@ public class HttpMavenServiceIntegrationTest
@Before
public void setUp() throws Exception
{
_mavenService.setRepositoryUrl(PRIVATE_NEXUS_REPOSITORY_URL);
_mavenService.setRepositoryUrls(new String[]{PRIVATE_NEXUS_REPOSITORY_URL});
}
@Test

View File

@ -32,14 +32,13 @@ public class HttpMavenServiceTest
{
private HttpMavenService _mavenService = new HttpMavenService();
private static final String JETTY_JMX_PLUGIN_NAME = "jetty-jmx";
private static final String MAVEN_CENTRAL_URL = "http://repo2.maven.org/maven2/";
@Before
public void setUp() throws Exception
{
_mavenService.setLocalRepository(this.getClass().getClassLoader().getResource("maven_repo").getFile() + "/");
_mavenService.setRepositoryUrl(MAVEN_CENTRAL_URL);
_mavenService.setRepositoryUrls(new String[]{MAVEN_CENTRAL_URL});
_mavenService.setVersion("version");
_mavenService.setSearchRemoteRepository(false);
_mavenService.setSearchLocalRepository(true);