Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add a numberThreads parameter. #133

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ public abstract class AbstractLicenseMojo extends AbstractMojo {
@Parameter(property = "license.concurrencyFactor", defaultValue = "1.5")
public float concurrencyFactor = 1.5f;


/**
* Maven license plugin uses concurrency to check license headers. With this
* option the number of threads used to check can be specified. If given
* it take precedence over <code>concurrencyFactor</code>
*
* The default is 0 which implies that the default for <code>concurrencyFactor</code>
* is used.
*/
@Parameter(property = "license.numberThreads", defaultValue = "0")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be documented in the README

public int numberThreads;

/**
* Whether to skip the plugin execution
*/
Expand Down Expand Up @@ -263,7 +275,7 @@ public abstract class AbstractLicenseMojo extends AbstractMojo {
*/
@Parameter(property = "license.skipExistingHeaders", defaultValue = "false")
public boolean skipExistingHeaders = false;

@Component
public MavenProject project;

Expand All @@ -277,22 +289,22 @@ public abstract class AbstractLicenseMojo extends AbstractMojo {
*/
@Component
private SettingsDecrypter settingsDecrypter;

private ResourceFinder finder;

protected abstract class AbstractCallback implements Callback {

/**
* Related to {@link #failIfUnknown}.
*/
private final Collection<File> unknownFiles = new ConcurrentLinkedQueue<File>();

@Override
public void onUnknownFile(Document document, Header header) {
warn("Unknown file extension: %s", document.getFilePath());
unknownFiles.add(document.getFile());
}

public void checkUnknown() throws MojoExecutionException {
if (!unknownFiles.isEmpty()) {
String msg = "Unable to find a comment style definition for some "
Expand All @@ -303,9 +315,9 @@ public void checkUnknown() throws MojoExecutionException {
getLog().warn(msg);
}
}

}

@SuppressWarnings({"unchecked"})
public final void execute(final Callback callback) throws MojoExecutionException, MojoFailureException {
if (!skip) {
Expand All @@ -317,7 +329,7 @@ public final void execute(final Callback callback) throws MojoExecutionException
warn("Property 'strictCheck' is not enabled. Please consider adding <strictCheck>true</strictCheck> in your pom.xml file.");
warn("See http://mycila.github.io/license-maven-plugin for more information.");
}

finder = new ResourceFinder(basedir);
try {
finder.setCompileClassPath(project.getCompileClasspathElements());
Expand All @@ -338,7 +350,7 @@ public final void execute(final Callback callback) throws MojoExecutionException
final HeaderSource validHeaderSource = HeaderSource.of(null, validHeader, this.encoding, this.finder);
validHeaders.add(new Header(validHeaderSource, headerSections));
}

final List<PropertiesProvider> propertiesProviders = new LinkedList<PropertiesProvider>();
for (PropertiesProvider provider : ServiceLoader.load(PropertiesProvider.class, Thread.currentThread().getContextClassLoader())) {
propertiesProviders.add(provider);
Expand All @@ -347,7 +359,7 @@ public final void execute(final Callback callback) throws MojoExecutionException
@Override
public Properties load(Document document) {
Properties props = new Properties();

for (Map.Entry<String, String> entry : mergeProperties(document).entrySet()) {
if (entry.getValue() != null) {
props.setProperty(entry.getKey(), entry.getValue());
Expand Down Expand Up @@ -375,15 +387,15 @@ public Properties load(Document document) {
return props;
}
};

final DocumentFactory documentFactory = new DocumentFactory(basedir, buildMapping(), buildHeaderDefinitions(), encoding, keywords, propertiesLoader);

int nThreads = (int) (Runtime.getRuntime().availableProcessors() * concurrencyFactor);
int nThreads = getNumberOfExecutorThreads();
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
CompletionService completionService = new ExecutorCompletionService(executorService);
int count = 0;
debug("Number of execution threads: %s", nThreads);

try {
for (final String file : listSelectedFiles()) {
completionService.submit(new Runnable() {
Expand Down Expand Up @@ -413,7 +425,7 @@ public void run() {
}, null);
count++;
}

while (count-- > 0) {
try {
completionService.take().get();
Expand All @@ -436,13 +448,25 @@ public void run() {
throw new RuntimeException(cause.getMessage(), cause);
}
}

} finally {
executorService.shutdownNow();
}
}
}


private int getNumberOfExecutorThreads() {
int nThreads = numberThreads;
if (nThreads != 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you validating against negative numbers?

return nThreads;
}
nThreads = (int) (Runtime.getRuntime().availableProcessors() * concurrencyFactor);
if (nThreads != 0) {
return nThreads;
}
return 1;
}

private Map<String, String> mergeProperties(Document document) {
// first put systen environment
Map<String, String> props = new LinkedHashMap<String, String>(System.getenv());
Expand All @@ -466,15 +490,15 @@ private Map<String, String> mergeProperties(Document document) {
}
return props;
}

private String[] listSelectedFiles() {
Selection selection = new Selection(basedir, includes, buildExcludes(), useDefaultExcludes);
debug("From: %s", basedir);
debug("Including: %s", deepToString(selection.getIncluded()));
debug("Excluding: %s", deepToString(selection.getExcluded()));
return selection.getSelectedFiles();
}

private String[] buildExcludes() {
List<String> ex = new ArrayList<String>();
ex.addAll(asList(this.excludes));
Expand All @@ -485,25 +509,25 @@ private String[] buildExcludes() {
}
return ex.toArray(new String[ex.size()]);
}

public final void info(String format, Object... params) {
if (!quiet) {
getLog().info(format(format, params));
}
}

public final void debug(String format, Object... params) {
if (!quiet) {
getLog().debug(format(format, params));
}
}

public final void warn(String format, Object... params) {
if (!quiet) {
getLog().warn(format(format, params));
}
}

private Map<String, String> buildMapping() {
Map<String, String> extensionMapping = new LinkedHashMap<String, String>();
// force inclusion of unknow item to manage unknown files
Expand All @@ -520,7 +544,7 @@ private Map<String, String> buildMapping() {
}
return extensionMapping;
}

private Map<String, HeaderDefinition> buildHeaderDefinitions() throws MojoFailureException {
// like mappings, first get default definitions
final Map<String, HeaderDefinition> headers = new HashMap<String, HeaderDefinition>(HeaderType.defaultDefinitions());
Expand Down Expand Up @@ -563,18 +587,18 @@ List<Server> getDecryptedServers() {
*/
public Credentials findCredentials(String serverID) {
List<Server> decryptedServers = getDecryptedServers();

for (Server ds : decryptedServers) {
if (ds.getId().equals(serverID)) {
getLog().debug("credentials have been found for server: " + serverID + ", login:" + ds.getUsername() + ", password:" + starEncrypt(ds.getPassword()));
return new Credentials(ds.getUsername(), ds.getPassword());
}
}

getLog().debug("no credentials found for server: " + serverID);
return null;
}

static String starEncrypt(String str) {
if (str == null) {
return null;
Expand Down