Skip to content

Commit 6a83589

Browse files
committed
Fixes #47 - Adds ability to read repository map from plugin dependencies.
1 parent b7cf520 commit 6a83589

File tree

2 files changed

+59
-33
lines changed

2 files changed

+59
-33
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@
164164
<version>1.16.0</version>
165165
<scope>test</scope>
166166
</dependency>
167+
<dependency>
168+
<groupId>org.codehaus.plexus</groupId>
169+
<artifactId>plexus-resources</artifactId>
170+
<version>1.1.0</version>
171+
</dependency>
167172
</dependencies>
168173

169174
<build>

src/main/java/com/lazerycode/selenium/SeleniumServerMojo.java

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
package com.lazerycode.selenium;
22

33
import com.lazerycode.selenium.download.DownloadHandler;
4-
import com.lazerycode.selenium.repository.*;
4+
import com.lazerycode.selenium.repository.DriverContext;
5+
import com.lazerycode.selenium.repository.DriverDetails;
6+
import com.lazerycode.selenium.repository.DriverMap;
7+
import static com.lazerycode.selenium.repository.FileRepository.buildDownloadableFileRepository;
8+
import com.lazerycode.selenium.repository.OperatingSystem;
9+
import static com.lazerycode.selenium.repository.OperatingSystem.getOperatingSystem;
10+
import com.lazerycode.selenium.repository.SystemArchitecture;
11+
import static com.lazerycode.selenium.repository.SystemArchitecture.getCurrentSystemArcitecture;
12+
import com.lazerycode.selenium.repository.XMLParser;
13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.net.URISyntaxException;
17+
import java.net.URL;
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.HashSet;
21+
import java.util.Map;
22+
import java.util.Set;
23+
import javax.xml.XMLConstants;
24+
import javax.xml.bind.JAXBException;
25+
import javax.xml.transform.Source;
26+
import javax.xml.transform.stream.StreamSource;
27+
import javax.xml.validation.Schema;
28+
import javax.xml.validation.SchemaFactory;
29+
import javax.xml.validation.Validator;
30+
import javax.xml.xpath.XPathExpressionException;
531
import org.apache.log4j.BasicConfigurator;
632
import org.apache.log4j.Logger;
733
import org.apache.maven.plugin.AbstractMojo;
834
import org.apache.maven.plugin.MojoExecutionException;
935
import org.apache.maven.plugin.MojoFailureException;
10-
import org.apache.maven.plugins.annotations.Execute;
36+
import org.apache.maven.plugins.annotations.Component;
1137
import org.apache.maven.plugins.annotations.LifecyclePhase;
1238
import org.apache.maven.plugins.annotations.Mojo;
1339
import org.apache.maven.plugins.annotations.Parameter;
1440
import org.apache.maven.project.MavenProject;
41+
import org.codehaus.plexus.resource.ResourceManager;
42+
import org.codehaus.plexus.resource.loader.FileResourceCreationException;
43+
import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
1544
import org.xml.sax.SAXException;
1645

17-
import javax.xml.XMLConstants;
18-
import javax.xml.bind.JAXBException;
19-
import javax.xml.transform.Source;
20-
import javax.xml.transform.stream.StreamSource;
21-
import javax.xml.validation.Schema;
22-
import javax.xml.validation.SchemaFactory;
23-
import javax.xml.validation.Validator;
24-
import javax.xml.xpath.XPathExpressionException;
25-
import java.io.File;
26-
import java.io.IOException;
27-
import java.io.InputStream;
28-
import java.net.URISyntaxException;
29-
import java.net.URL;
30-
import java.util.*;
31-
32-
import static com.lazerycode.selenium.repository.FileRepository.buildDownloadableFileRepository;
33-
import static com.lazerycode.selenium.repository.OperatingSystem.getOperatingSystem;
34-
import static com.lazerycode.selenium.repository.SystemArchitecture.getCurrentSystemArcitecture;
35-
3646
/**
3747
* Selenium Standalone Server Maven Plugin
3848
*
@@ -57,11 +67,11 @@ public class SeleniumServerMojo extends AbstractMojo {
5767
protected File downloadedZipFileDirectory;
5868

5969
/**
60-
* <h3>Absolute path to the XML RepositoryMap</h3>
70+
* <h3>Path to the XML RepositoryMap</h3>
6171
* <p>&lt;xmlRepositoryMap&gt;${project.basedir}/src/main/resources/RepositoryMap.xml&lt;/xmlRepositoryMap&gt;</p>
6272
*/
6373
@Parameter
64-
protected File customRepositoryMap;
74+
protected String customRepositoryMap;
6575

6676
/**
6777
* <h3>Only get drivers that are compatible with the operating system running the plugin</h3>
@@ -178,6 +188,9 @@ public class SeleniumServerMojo extends AbstractMojo {
178188
@Parameter(defaultValue = "${project}", required = true, readonly = true)
179189
protected MavenProject project;
180190

191+
@Component
192+
private ResourceManager locator;
193+
181194
protected InputStream xmlRepositoryMap = null;
182195
private static final Logger LOG = Logger.getLogger(SeleniumServerMojo.class);
183196

@@ -270,23 +283,31 @@ protected void setSystemProperties(DriverMap driverRepository) {
270283
}
271284
}
272285

286+
private File getRepositoryMapFile(String customRepositoryMap) {
287+
File repositoryMap = null;
288+
try {
289+
repositoryMap = locator.getResourceAsFile(customRepositoryMap);
290+
} catch (ResourceNotFoundException e) {
291+
LOG.info("Unable to access the specified custom repository map, defaulting to bundled version...\n");
292+
} catch (FileResourceCreationException e) {
293+
}
294+
return repositoryMap;
295+
}
296+
273297
/**
274298
* Set the RepositoryMap used to get file information.
275299
* If the supplied map is invalid it will default to the pre-packaged one here.
276300
*
277301
* @throws MojoExecutionException
278302
*/
279303
private void setRepositoryMapFile() throws MojoExecutionException {
280-
if (this.customRepositoryMap == null || !this.customRepositoryMap.exists()) {
281-
if (this.customRepositoryMap != null) {
282-
LOG.info("Unable to access the specified custom repository map, defaulting to bundled version...");
283-
LOG.info(" ");
284-
}
304+
File repositoryMap = getRepositoryMapFile(this.customRepositoryMap);
305+
if (repositoryMap == null || !repositoryMap.exists()) {
285306
this.xmlRepositoryMap = this.getClass().getResourceAsStream("/RepositoryMap.xml");
286307
} else {
287-
checkRepositoryMapIsValid();
308+
checkRepositoryMapIsValid(repositoryMap);
288309
try {
289-
this.xmlRepositoryMap = this.customRepositoryMap.toURI().toURL().openStream();
310+
this.xmlRepositoryMap = repositoryMap.toURI().toURL().openStream();
290311
} catch (IOException ioe) {
291312
throw new MojoExecutionException(ioe.getLocalizedMessage());
292313
}
@@ -300,9 +321,9 @@ private void setRepositoryMapFile() throws MojoExecutionException {
300321
*
301322
* @throws MojoExecutionException thrown if customRepositoryMap is not valid
302323
*/
303-
protected void checkRepositoryMapIsValid() throws MojoExecutionException {
324+
protected void checkRepositoryMapIsValid(File repositoryMap) throws MojoExecutionException {
304325
URL schemaFile = this.getClass().getResource("/RepositoryMap.xsd");
305-
Source xmlFile = new StreamSource(this.customRepositoryMap);
326+
Source xmlFile = new StreamSource(repositoryMap);
306327
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
307328
try {
308329
Schema schema = schemaFactory.newSchema(schemaFile);
@@ -311,7 +332,7 @@ protected void checkRepositoryMapIsValid() throws MojoExecutionException {
311332
LOG.info(" " + xmlFile.getSystemId() + " is valid");
312333
LOG.info(" ");
313334
} catch (SAXException saxe) {
314-
throw new MojoExecutionException(this.customRepositoryMap.getName() + " is not valid: " + saxe.getLocalizedMessage());
335+
throw new MojoExecutionException(repositoryMap.getName() + " is not valid: " + saxe.getLocalizedMessage());
315336
} catch (IOException ioe) {
316337
//Assume it doesn't exist, set to null so that we default to packaged version
317338
this.customRepositoryMap = null;

0 commit comments

Comments
 (0)