Skip to content

Commit

Permalink
[BZ-1323310] create a temp file when the user maven settings doesn't …
Browse files Browse the repository at this point in the history
…come from a file
  • Loading branch information
mariofusco committed Apr 20, 2016
1 parent f42976b commit ffd7709
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 48 deletions.
38 changes: 28 additions & 10 deletions drools-core/src/main/java/org/drools/core/util/IoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.nio.channels.FileChannel;
Expand All @@ -35,6 +36,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand Down Expand Up @@ -83,7 +85,7 @@ public static String readFileAsString(File file) {
StringBuffer sb = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), IoUtils.UTF8_CHARSET));
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), UTF8_CHARSET));
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
sb.append(line).append("\n");
}
Expand Down Expand Up @@ -131,7 +133,25 @@ public static void copyFile(File sourceFile, File destFile) {
}
}
}


public static long copy( InputStream input, OutputStream output ) throws IOException {
byte[] buffer = createBytesBuffer( input );
long count = 0;
int n = 0;
while ((n = input.read(buffer)) != -1) {
output.write(buffer, 0, n);
count += n;
}
return count;
}

public static File copyInTempFile( InputStream input, String fileExtension ) throws IOException {
File tempFile = File.createTempFile( UUID.randomUUID().toString(), "." + fileExtension );
tempFile.deleteOnExit();
copy(input, new FileOutputStream(tempFile));
return tempFile;
}

public static Map<String, byte[]> indexZipFile(java.io.File jarFile) {
Map<String, byte[]> files = new HashMap<String, byte[]>();
ZipFile zipFile = null;
Expand Down Expand Up @@ -204,22 +224,20 @@ private static File[] safeListFiles(final File file) {
}

public static byte[] readBytesFromInputStream(InputStream input) throws IOException {
int length = input.available();
byte[] buffer = new byte[Math.max(length, 8192)];
byte[] buffer = createBytesBuffer( input );
ByteArrayOutputStream output = new ByteArrayOutputStream(buffer.length);

if (length > 0) {
int n = input.read(buffer);
output.write(buffer, 0, n);
}

int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}


private static byte[] createBytesBuffer( InputStream input ) throws IOException {
return new byte[Math.max(input.available(), 8192)];
}

public static byte[] readBytesFromZipEntry(File file, ZipEntry entry) throws IOException {
if ( entry == null ) {
return null;
Expand Down
3 changes: 0 additions & 3 deletions kie-ci/src/main/java/org/kie/scanner/IoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.UUID;

class IoUtils {

public static final Charset UTF8_CHARSET = Charset.forName("UTF-8");

public static File getTmpDirectory() {
File tmp = new File( System.getProperty( "java.io.tmpdir" ) );
File f = new File( tmp, "_kie_repo_" + UUID.randomUUID().toString() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import static org.drools.core.util.ClassUtils.convertResourceToClassName;
import static org.drools.core.util.IoUtils.readBytesFromZipEntry;
import static org.drools.core.util.IoUtils.UTF8_CHARSET;
import static org.kie.scanner.ArtifactResolver.getResolverFor;

public class KieModuleMetaDataImpl implements KieModuleMetaData {
Expand Down Expand Up @@ -186,7 +187,7 @@ private void scanJar(File jarFile) {
ZipEntry entry = entries.nextElement();
String pathName = entry.getName();
if(pathName.endsWith("bpmn2")){
processes.put(pathName, new String(readBytesFromZipEntry(jarFile, entry), IoUtils.UTF8_CHARSET));
processes.put(pathName, new String(readBytesFromZipEntry(jarFile, entry), UTF8_CHARSET));
}
if (!indexClass(pathName)) {
if (pathName.endsWith(KieModuleModelImpl.KMODULE_INFO_JAR_PATH)) {
Expand Down Expand Up @@ -226,7 +227,7 @@ private boolean indexClass(String pathName) {
}

private void indexMetaInfo(byte[] bytes) {
KieModuleMetaInfo info = KieModuleMetaInfo.unmarshallMetaInfos(new String(bytes, IoUtils.UTF8_CHARSET));
KieModuleMetaInfo info = KieModuleMetaInfo.unmarshallMetaInfos(new String(bytes, UTF8_CHARSET));
typeMetaInfos.putAll(info.getTypeMetaInfos());
rulesByPackage.putAll(info.getRulesByPackage());
}
Expand Down
27 changes: 20 additions & 7 deletions kie-ci/src/main/java/org/kie/scanner/embedder/MavenEmbedder.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
import org.apache.maven.settings.building.FileSettingsSource;
import org.apache.maven.settings.building.SettingsBuilder;
import org.apache.maven.settings.building.SettingsBuildingException;
import org.apache.maven.settings.building.SettingsBuildingRequest;
import org.apache.maven.settings.building.SettingsSource;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.Os;
Expand All @@ -56,6 +58,8 @@
import java.util.Map.Entry;
import java.util.Properties;

import static org.drools.core.util.IoUtils.copyInTempFile;


public class MavenEmbedder {

Expand Down Expand Up @@ -111,8 +115,17 @@ protected MavenExecutionRequest buildMavenExecutionRequest( MavenRequest mavenRe
mavenExecutionRequest.setGlobalSettingsFile( new File( mavenRequest.getGlobalSettingsFile() ) );
}

if ( mavenRequest.getUserSettingsFile() != null ) {
mavenExecutionRequest.setUserSettingsFile( new File( mavenRequest.getUserSettingsFile() ) );
SettingsSource userSettings = mavenRequest.getUserSettingsSource();
if ( userSettings != null ) {
if ( userSettings instanceof FileSettingsSource ) {
mavenExecutionRequest.setUserSettingsFile( ( (FileSettingsSource) userSettings ).getSettingsFile() );
} else {
try {
mavenExecutionRequest.setUserSettingsFile( copyInTempFile( userSettings.getInputStream(), "xml" ) );
} catch (IOException ioe) {
log.warn( "Unable to use maven settings defined in " + userSettings, ioe );
}
}
}

try {
Expand Down Expand Up @@ -198,12 +211,12 @@ public Settings getSettings() throws MavenEmbedderException, ComponentLookupExce
} else {
settingsBuildingRequest.setGlobalSettingsFile( DEFAULT_GLOBAL_SETTINGS_FILE );
}
if ( this.mavenRequest.getUserSettingsFile() != null ) {
settingsBuildingRequest.setUserSettingsFile( new File( this.mavenRequest.getUserSettingsFile() ) );
if ( this.mavenRequest.getUserSettingsSource() != null ) {
settingsBuildingRequest.setUserSettingsSource( this.mavenRequest.getUserSettingsSource() );
} else {
File userSettingsFile = MavenSettings.getUserSettingsFile();
if ( userSettingsFile != null ) {
settingsBuildingRequest.setUserSettingsFile( userSettingsFile );
SettingsSource userSettingsSource = MavenSettings.getUserSettingsSource();
if ( userSettingsSource != null ) {
settingsBuildingRequest.setUserSettingsSource( userSettingsSource );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ public static MavenEmbedder newMavenEmbedder(boolean offline) {
private static MavenRequest createMavenRequest(boolean offline) {
MavenRequest mavenRequest = new MavenRequest();
mavenRequest.setLocalRepositoryPath( MavenSettings.getSettings().getLocalRepository() );
if ( MavenSettings.getUserSettingsFile() != null) {
mavenRequest.setUserSettingsFile(MavenSettings.getUserSettingsFile().getAbsolutePath());
}
mavenRequest.setUserSettingsSource(MavenSettings.getUserSettingsSource());

// BZ-1007894: If dependency is not resolvable and maven project builder does not complain about it,
// then a <code>java.lang.NullPointerException</code> is thrown to the client.
// So, the user will se an exception message "null", not descriptive about the real error.
Expand Down
19 changes: 10 additions & 9 deletions kie-ci/src/main/java/org/kie/scanner/embedder/MavenRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@

package org.kie.scanner.embedder;

import java.net.URL;
import java.util.List;
import java.util.Properties;

import org.apache.maven.execution.ExecutionListener;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.settings.building.SettingsSource;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.logging.LoggerManager;
import org.eclipse.aether.repository.WorkspaceReader;
import org.eclipse.aether.transfer.TransferListener;

import java.net.URL;
import java.util.List;
import java.util.Properties;

public class MavenRequest {

private String globalSettingsFile;

private String userSettingsFile;
private SettingsSource userSettingsSource;

private String localRepositoryPath;

Expand Down Expand Up @@ -140,12 +141,12 @@ public MavenRequest setGlobalSettingsFile( String globalSettingsFile ) {
return this;
}

public String getUserSettingsFile() {
return userSettingsFile;
public SettingsSource getUserSettingsSource() {
return userSettingsSource;
}

public MavenRequest setUserSettingsFile( String userSettingsFile ) {
this.userSettingsFile = userSettingsFile;
public MavenRequest setUserSettingsSource( SettingsSource userSettingsSource ) {
this.userSettingsSource = userSettingsSource;
return this;
}

Expand Down
32 changes: 21 additions & 11 deletions kie-ci/src/main/java/org/kie/scanner/embedder/MavenSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.DefaultSettingsBuilderFactory;
import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
import org.apache.maven.settings.building.FileSettingsSource;
import org.apache.maven.settings.building.SettingsBuilder;
import org.apache.maven.settings.building.SettingsBuildingException;
import org.apache.maven.settings.building.SettingsSource;
import org.apache.maven.settings.building.UrlSettingsSource;
import org.kie.scanner.MavenRepositoryConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class MavenSettings {

Expand All @@ -33,13 +38,13 @@ public class MavenSettings {
private static final String CUSTOM_SETTINGS_PROPERTY = "kie.maven.settings.custom";

private static class SettingsHolder {
private static final File userSettingsFile = initUserSettingsFile();
private static final Settings settings = initSettings(userSettingsFile);
private static final SettingsSource userSettingsSource = initUserSettingsSource();
private static final Settings settings = initSettings(userSettingsSource);
private static final MavenRepositoryConfiguration mavenConf = new MavenRepositoryConfiguration(settings);
}

public static File getUserSettingsFile() {
return SettingsHolder.userSettingsFile;
public static SettingsSource getUserSettingsSource() {
return SettingsHolder.userSettingsSource;
}

public static Settings getSettings() {
Expand All @@ -50,12 +55,12 @@ public static MavenRepositoryConfiguration getMavenRepositoryConfiguration() {
return SettingsHolder.mavenConf;
}

private static Settings initSettings(File userSettingsFile) {
private static Settings initSettings(SettingsSource userSettingsSource) {
SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();

if (userSettingsFile != null) {
request.setUserSettingsFile( userSettingsFile );
if (userSettingsSource != null) {
request.setUserSettingsSource( userSettingsSource );
}

String mavenHome = System.getenv( "M2_HOME" );
Expand Down Expand Up @@ -89,22 +94,27 @@ private static Settings initSettings(File userSettingsFile) {
return settings;
}

private static File initUserSettingsFile() {
private static SettingsSource initUserSettingsSource() {
String customSettings = System.getProperty( CUSTOM_SETTINGS_PROPERTY );
if (customSettings != null) {
File customSettingsFile = new File( customSettings );
if (customSettingsFile.exists()) {
return customSettingsFile;
return new FileSettingsSource( customSettingsFile );
} else {
log.warn("Cannot find custom maven settings file: " + customSettings);
try {
return new UrlSettingsSource( new URL( customSettings ) );
} catch (MalformedURLException e) {
// Ignore
}
log.warn("Cannot find custom maven settings: " + customSettings);
}
}

String userHome = System.getProperty( "user.home" );
if (userHome != null) {
File userSettingsFile = new File( userHome + "/.m2/settings.xml" );
if (userSettingsFile.exists()) {
return userSettingsFile;
return new FileSettingsSource( userSettingsFile );
}
} else {
log.warn("User home is not set");
Expand Down

0 comments on commit ffd7709

Please sign in to comment.