Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Revert "trying to fix race condition"
Browse files Browse the repository at this point in the history
This reverts commit 47e8ba46ef324f6e254487df09239fab992982f2.

git-svn-id: file:///opt/svn/repositories/sonatype.org/nexus/trunk/nexus-indexer@5077 2aa8b3fc-8ebb-4439-a84f-95066eaea8ab
  • Loading branch information
jdcasey committed Sep 24, 2009
1 parent bcd6d55 commit cee066f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 126 deletions.
Expand Up @@ -11,6 +11,7 @@
import org.eclipse.jetty.client.security.Realm;
import org.eclipse.jetty.client.security.RealmResolver;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpMethods;
import org.sonatype.nexus.index.updater.ResourceFetcher;

import java.io.File;
Expand All @@ -23,7 +24,7 @@ public class JettyResourceFetcher
{

// configuration fields.
private int maxConnections = 1;
private int maxConnections;

private int connectionTimeout;

Expand Down Expand Up @@ -52,35 +53,27 @@ public void retrieve( final String name, final File targetFile )
throws IOException, FileNotFoundException
{
HttpFields exchangeHeaders = buildHeaders();
ResourceExchange exchange = new ResourceExchange( targetFile, exchangeHeaders, maxRedirects, listenerSupport );

exchange.setMethod( HttpMethods.GET );

StringBuilder getUrl = new StringBuilder( url );
if ( getUrl.charAt( getUrl.length() - 1 ) != '/' && !name.startsWith( "/" ) )
{
getUrl.append( '/' );
}
getUrl.append( name );

ResourceExchange exchange =
new ResourceExchange( targetFile, exchangeHeaders, getUrl.toString(), listenerSupport );

get( exchange );

int redirCount = 0;
while ( exchange.isRedirectRequested() && redirCount < maxRedirects )
{
String url = exchange.getRedirectUrl();

exchange = new ResourceExchange( targetFile, exchangeHeaders, getUrl.toString(), url, listenerSupport );

get( exchange );
}
exchange.setURL( getUrl.toString() );

if ( redirCount == maxRedirects )
exchange = get( exchange );
while ( exchange.prepareForRedirect() )
{
throw new IOException( "Maximum redirection count (" + maxRedirects + ") exceeded." );
exchange = get( exchange );
}
}

private void get( final ResourceExchange exchange )
private ResourceExchange get( final ResourceExchange exchange )
throws IOException
{
httpClient.send( exchange );
Expand All @@ -96,15 +89,6 @@ private void get( final ResourceExchange exchange )
throw err;
}

try
{
Thread.sleep( 500 );
}
catch ( InterruptedException e )
{
e.printStackTrace();
}

int responseStatus = exchange.getResponseStatus();
switch ( responseStatus )
{
Expand All @@ -115,7 +99,7 @@ private void get( final ResourceExchange exchange )
break;

case ServerResponse.SC_FORBIDDEN:
throw new IOException( "Transfer failed: [" + responseStatus + "] " + exchange.getOriginalUrl() );
throw new IOException( "Transfer failed: [" + responseStatus + "] " + url );

case ServerResponse.SC_UNAUTHORIZED:
throw new IOException( "Transfer failed: Not authorized" );
Expand All @@ -124,17 +108,18 @@ private void get( final ResourceExchange exchange )
throw new IOException( "Transfer failed: Not authorized by proxy" );

case ServerResponse.SC_NOT_FOUND:
throw new IOException( "Transfer failed: " + exchange.getOriginalUrl() + " does not exist" );
throw new IOException( "Transfer failed: " + url + " does not exist" );

default:
{
IOException ex =
new IOException( "Transfer failed: [" + responseStatus + "] " + exchange.getOriginalUrl() );
IOException ex = new IOException( "Transfer failed: [" + responseStatus + "] " + url );
listenerSupport.fireTransferError( url, ex, TransferEvent.REQUEST_GET );

throw ex;
}
}

return exchange;
}

public void connect( final String id, final String url )
Expand Down
Expand Up @@ -5,7 +5,6 @@
import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.BufferUtil;
import org.eclipse.jetty.io.nio.NIOBuffer;
Expand All @@ -30,12 +29,14 @@ public class ResourceExchange

private final File targetFile;

private final String originalUrl;
private final int maxRedirects;

private final TransferListenerSupport listenerSupport;

protected long lastModified;

private int redirectCount = 0;

private boolean redirectionRequested;

private String redirectionUrl;
Expand All @@ -44,6 +45,8 @@ public class ResourceExchange

private File tmpFile;

private String originalUrl;

private TransferEvent transferEvent;

private String contentEncoding;
Expand All @@ -52,22 +55,12 @@ public class ResourceExchange

private int contentLength;

public ResourceExchange( final File targetFile, final HttpFields httpHeaders, final String targetUrl,
public ResourceExchange( final File targetFile, final HttpFields httpHeaders, final int maxRedirects,
final TransferListenerSupport listenerSupport )
{
this( targetFile, httpHeaders, targetUrl, targetUrl, listenerSupport );
}

public ResourceExchange( final File targetFile, final HttpFields httpHeaders, final String originalUrl,
final String targetUrl, final TransferListenerSupport listenerSupport )
{
super( false );

setMethod( HttpMethods.GET );
setURL( targetUrl );

this.targetFile = targetFile;
this.originalUrl = originalUrl;
this.maxRedirects = maxRedirects;
this.listenerSupport = listenerSupport;

addRequestHeaders( httpHeaders );
Expand Down Expand Up @@ -114,6 +107,10 @@ public void onResponseHeader( final Buffer name, final Buffer value )
case HttpHeaders.LOCATION_ORDINAL:
{
redirectionUrl = value.toString();

System.out.println( redirectionUrl );
redirectCount++;

break;
}
}
Expand All @@ -130,6 +127,38 @@ public void onResponseStatus( final Buffer version, final int status, final Buff
}
}

public int getMaxRedirects()
{
return maxRedirects;
}

public int getRedirectCount()
{
return redirectCount;
}

public boolean prepareForRedirect()
{
if ( redirectionRequested && redirectCount < maxRedirects )
{
setURL( redirectionUrl );

reset();
return true;
}

return false;
}

@Override
public void reset()
{
super.reset();

redirectionRequested = false;
redirectionUrl = null;
}

@Override
protected synchronized void onResponseContent( final Buffer content )
throws IOException
Expand Down Expand Up @@ -171,10 +200,6 @@ protected synchronized void onResponseContent( final Buffer content )
transferEvent.setTimestamp( System.currentTimeMillis() );
listenerSupport.fireTransferProgress( transferEvent, content.array(), contentLength );
}
else
{
System.out.println( "Redirection requested for location: " + redirectionUrl + "\nSkipping response body." );
}
}

private void newTempFile()
Expand All @@ -192,8 +217,6 @@ protected void onResponseComplete()

if ( "gzip".equals( contentEncoding ) )
{
System.out.println( "Unpacking: " + tmpFile + "\nto: " + targetFile );

InputStream in = null;
OutputStream out = null;
try
Expand All @@ -212,25 +235,33 @@ protected void onResponseComplete()
}
else
{
System.out.println( "Moving: " + tmpFile + "\nto: " + targetFile );
if ( targetFile.exists() )
{
targetFile.delete();
}

tmpFile.renameTo( targetFile );
System.out.println( "Move target exists? " + targetFile.exists() );
targetFile.setLastModified( lastModified );
}

tmpFile = null;

listenerSupport.fireGetCompleted( originalUrl, targetFile );
transferEvent = null;
redirectionRequested = false;
}
}

@Override
public void setURL( final String url )
{
if ( originalUrl == null )
{
originalUrl = url;
}

super.setURL( url );
}

void setResponseContentStream( final InputStream in )
throws IOException
{
Expand Down Expand Up @@ -308,19 +339,4 @@ void setContentLength( final int contentLength )
this.contentLength = contentLength;
}

public String getOriginalUrl()
{
return originalUrl;
}

public String getRedirectUrl()
{
return redirectionUrl;
}

public boolean isRedirectRequested()
{
return redirectionRequested;
}

}
@@ -1,6 +1,5 @@
package org.sonatype.nexus.index.updater;

import org.apache.maven.wagon.authentication.AuthenticationInfo;
import org.apache.maven.wagon.events.TransferEvent;
import org.apache.maven.wagon.events.TransferListener;
import org.codehaus.plexus.DefaultPlexusContainer;
Expand All @@ -23,53 +22,10 @@ public class FullBootProofOfConcept
{

public static void main( final String[] args )
throws IOException
{
for ( int i = 0; i < 2; i++ )
{
File basedir = File.createTempFile( "nexus-indexer.", ".dir" );

try
{
run( basedir );
}
catch ( IOException e )
{
e.printStackTrace();
}
catch ( ComponentLookupException e )
{
e.printStackTrace();
}
catch ( PlexusContainerException e )
{
e.printStackTrace();
}
catch ( ParseException e )
{
e.printStackTrace();
}
catch ( UnsupportedExistingLuceneIndexException e )
{
e.printStackTrace();
}
finally
{
try
{
FileUtils.forceDelete( basedir );
}
catch ( IOException e )
{
}
}
}
}

public static void run( final File basedir )
throws IOException, ComponentLookupException, PlexusContainerException, ParseException,
UnsupportedExistingLuceneIndexException
{
File basedir = File.createTempFile( "nexus-indexer.", ".dir" );
try
{
FileUtils.forceDelete( basedir );
Expand All @@ -91,25 +47,14 @@ public static void run( final File basedir )
creators.add( jar );

String repositoryId = "test";
String repositoryUrl = "http://repository.sonatype.org/content/groups/sonatype/";
// String repositoryUrl = "http://repo1.maven.org/maven2/";
String repositoryUrl = "http://repo1.maven.org/maven2/";
String indexUrl = repositoryUrl + ".index";

IndexingContext ctx =
new DefaultIndexingContext( repositoryId, repositoryId, basedir, basedir, repositoryUrl, indexUrl,
creators, true );

IndexUpdateRequest updateRequest = new IndexUpdateRequest( ctx );
updateRequest.setAuthenticationInfo( new AuthenticationInfo()
{
private static final long serialVersionUID = 1L;

{
setUserName( "deployment" );
setPassword( "mj21op1" );
}
} );

updateRequest.setTransferListener( new TransferListener()
{

Expand Down

0 comments on commit cee066f

Please sign in to comment.