Skip to content

Commit

Permalink
added code around maven settings.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
mkristian committed Jan 24, 2013
1 parent 7b8dda9 commit 177fe8f
Showing 1 changed file with 302 additions and 0 deletions.
302 changes: 302 additions & 0 deletions src/main/java/jbundler/AetherSettings.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,302 @@
/*******************************************************************************
* Copyright (c) 2010, 2012 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Sonatype, Inc. - initial API and implementation
*******************************************************************************/

/**
* this basically the file wth some inlines and strips here and there
*
* http://git.eclipse.org/c/aether/aether-ant.git/tree/src/main/java/org/eclipse/aether/ant/AntRepoSys.java
*
*/
package jbundler;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Server;
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.SettingsBuilder;
import org.apache.maven.settings.building.SettingsBuildingException;
import org.sonatype.aether.repository.Authentication;
import org.sonatype.aether.repository.AuthenticationSelector;
import org.sonatype.aether.repository.LocalRepository;
import org.sonatype.aether.repository.MirrorSelector;
import org.sonatype.aether.repository.Proxy;
import org.sonatype.aether.repository.ProxySelector;
import org.sonatype.aether.util.repository.ConservativeAuthenticationSelector;
import org.sonatype.aether.util.repository.DefaultAuthenticationSelector;
import org.sonatype.aether.util.repository.DefaultMirrorSelector;
import org.sonatype.aether.util.repository.DefaultProxySelector;

public class AetherSettings {

private static final SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
//private static SettingsDecrypter settingsDecrypter;// = new DefaultSettingsDecryptorFactory().newInstance();

private File globalSettings;
private File userSettings;
private List<Proxy> proxies = new LinkedList<Proxy>();;
private Settings settings;
private List<Mirror> mirrors = new LinkedList<Mirror>();
// private List<Authentication> authentications;
private Boolean offline;
private LocalRepository localRepository;

LocalRepository getLocalRepository()
{
if ( localRepository != null && localRepository.getBasedir() != null )
{
return localRepository;
}
else
{
return new LocalRepository( getDefaultLocalRepoDir() );
}
}

private File getDefaultLocalRepoDir()
{

Settings settings = getSettings();
if ( settings.getLocalRepository() != null )
{
return new File( settings.getLocalRepository() );
}

return new File( new File( System.getProperty( "user.home" ), ".m2" ), "repository" );
}

void setLocalRepository( LocalRepository localRepository )
{
this.localRepository = localRepository;
}

synchronized Settings getSettings()
{
if ( settings == null )
{
DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
request.setUserSettingsFile( getUserSettings() );
request.setGlobalSettingsFile( getGlobalSettings() );
request.setSystemProperties( getSystemProperties() );
request.setUserProperties( getUserProperties() );

try
{
settings = settingsBuilder.build( request ).getEffectiveSettings();
}
catch ( SettingsBuildingException e )
{
//log( "Could not process settings.xml: " + e.getMessage(), e );
}

// SettingsDecryptionResult result =
// settingsDecrypter.decrypt( new DefaultSettingsDecryptionRequest( settings ) );
// settings.setServers( result.getServers() );
// settings.setProxies( result.getProxies() );
}
return settings;
}

ProxySelector getProxySelector()
{
DefaultProxySelector selector = new DefaultProxySelector();

for ( Proxy proxy : proxies )
{
selector.add( proxy, proxy.getHost() );
}

Settings settings = getSettings();
for ( org.apache.maven.settings.Proxy proxy : settings.getProxies() )
{
Authentication auth = new Authentication( proxy.getUsername(), proxy.getPassword() );

selector.add( new Proxy( proxy.getProtocol(), proxy.getHost(),
proxy.getPort(), auth ),
proxy.getNonProxyHosts() );
}
return selector;
}

AuthenticationSelector getAuthSelector()
{
DefaultAuthenticationSelector selector = new DefaultAuthenticationSelector();

// Collection<String> ids = new HashSet<String>();
// for ( Authentication auth : authentications )
// {
// List<String> servers = auth.getServers();
// if ( !servers.isEmpty() )
// {
// org.eclipse.aether.repository.Authentication a = ConverterUtils.toAuthentication( auth );
// for ( String server : servers )
// {
// if ( ids.add( server ) )
// {
// selector.add( server, a );
// }
// }
// }
// }

Settings settings = getSettings();
for ( Server server : settings.getServers() )
{
Authentication auth = new Authentication( server.getUsername(), server.getPassword(),
server.getPrivateKey(), server.getPassphrase() );
selector.add( server.getId(), auth );
}

return new ConservativeAuthenticationSelector( selector );
}

String getUserAgent()
{
StringBuilder buffer = new StringBuilder( 128 );

buffer.append( "JBundler/" );//.append( getProperty( "jbundler.version" ) );
buffer.append( " (" );
buffer.append( "Java " ).append( System.getProperty( "java.version" ) );
buffer.append( "; " );
buffer.append( System.getProperty( "os.name" ) ).append( " " ).append( System.getProperty( "os.version" ) );
buffer.append( ")" );
buffer.append( " Aether" );

return buffer.toString();
}

void setOffline( Boolean offline )
{
this.offline = offline;
}

boolean isOffline()
{
return offline == null ? getSettings().isOffline() : offline;
}

Properties getSystemProperties()
{
Properties props = new Properties();
getEnvProperties( props );
props.putAll( System.getProperties() );
return props;
}

Properties getUserProperties()
{
Properties props = new Properties();
return props;
}

private Properties getEnvProperties( Properties props )
{
if ( props == null )
{
props = new Properties();
}
boolean envCaseInsensitive = false;//Os.isFamily( "windows" );
for ( Map.Entry<String, String> entry : System.getenv().entrySet() )
{
String key = entry.getKey();
if ( envCaseInsensitive )
{
key = key.toUpperCase( Locale.ENGLISH );
}
key = "env." + key;
props.put( key, entry.getValue() );
}
return props;
}

void addMirror( Mirror mirror )
{
mirrors.add( mirror );
}

MirrorSelector getMirrorSelector()
{
DefaultMirrorSelector selector = new DefaultMirrorSelector();

for ( Mirror mirror : mirrors )
{
selector.add( mirror.getId(), mirror.getUrl(), "default", true, mirror.getMirrorOf(), null );
}

Settings settings = getSettings();
for ( org.apache.maven.settings.Mirror mirror : settings.getMirrors() )
{
selector.add( String.valueOf( mirror.getId() ), mirror.getUrl(), mirror.getLayout(), true,
mirror.getMirrorOf(), mirror.getMirrorOfLayouts() );
}

return selector;
}

synchronized void setUserSettings( File file )
{
// if ( !eq( this.userSettings, file ) )
// {
// settings = null;
// }
this.userSettings = file;
}

File getUserSettings()
{
if ( userSettings == null )
{
userSettings = new File( new File( System.getProperty( "user.home" ), ".m2" ), "settings.xml" );
}
return userSettings;
}

// public void setGlobalSettings( File file )
// {
// if ( !eq( this.globalSettings, file ) )
// {
// settings = null;
// }
// this.globalSettings = file;
// }

private String getMavenHome()
{
String mavenHome = System.getProperty( "maven.home" );
if ( mavenHome != null )
{
return mavenHome;
}
return System.getenv( "M2_HOME" );
}

File getGlobalSettings()
{
if ( globalSettings == null )
{
globalSettings = new File( new File( getMavenHome(), "conf" ), "settings.xml" );
}
return globalSettings;
}

void addProxy( Proxy proxy )
{
proxies.add( proxy );
}

}

0 comments on commit 177fe8f

Please sign in to comment.