Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev-sjones-euca-12622'
Browse files Browse the repository at this point in the history
  • Loading branch information
sjones4 committed Oct 19, 2016
2 parents b6734dc + 653ab43 commit 3b204e4
Showing 1 changed file with 14 additions and 9 deletions.
Expand Up @@ -99,7 +99,7 @@
public class VmMetadata {
private static//
Logger LOG = Logger.getLogger( VmMetadata.class );

private static//
Function<MetadataRequest, ByteArray> dynamicFunc = new Function<MetadataRequest, ByteArray>( ) {
public ByteArray apply( MetadataRequest arg0 ) {
Expand Down Expand Up @@ -146,7 +146,7 @@ public ByteArray apply( MetadataRequest arg0 ) {
throw new NoSuchElementException( "Failed to lookup path: " + arg0.getLocalPath( ) );
}
};

private static//
ConcurrentMap<String, Function<MetadataRequest, ByteArray>> publicMetadataEndpoints = new ConcurrentSkipListMap<String, Function<MetadataRequest, ByteArray>>( ) {
{
Expand All @@ -166,11 +166,15 @@ public ByteArray apply( MetadataRequest arg0 ) {
new Function<MetadataRequest, ByteArray>( ) {
public ByteArray apply( MetadataRequest arg0 ) {
String listing = "";
for ( String key : keySet( ) ) {
for ( String key : keySet( ) ) try {
if ( !"".equals( key )
&& get( key ).apply( arg0 ) != null ) {
listing += key + "\n";
}
} catch ( final RuntimeException e ) {
if ( !Exceptions.isCausedBy( e, NoSuchElementException.class ) ) {
throw e;
}
}
listing = listing.replaceAll( "\n$", "" );
return ByteArray.newInstance( listing );
Expand All @@ -181,7 +185,7 @@ && get( key ).apply( arg0 ) != null ) {
put( "meta-data", cache( metaDataFunc, VmInstances.VM_METADATA_INSTANCE_CACHE ) );
}
};

private static//
ConcurrentMap<String, Function<MetadataRequest, ByteArray>> //
systemMetadataEndpoints = new ConcurrentSkipListMap<String, Function<MetadataRequest, ByteArray>>( ) {
Expand Down Expand Up @@ -239,7 +243,10 @@ public Optional<String> apply( final String requestIp ) {
}


public byte[] handle( final String path ) {
/**
* @return byte[] for content or String for expected error
*/
public Object handle( final String path ) {
final String[] parts = path.split( ":", 2 );
try {
final String requestIpOrInstanceId = ResourceIdentifiers.tryNormalize( ).apply( parts[0] );
Expand All @@ -262,10 +269,8 @@ public byte[] handle( final String path ) {
} else if ( publicMetadataEndpoints.containsKey( request.getMetadataName( ) ) ) {
return publicMetadataEndpoints.get( request.getMetadataName( ) ).apply( request ).getBytes( );
} else {
throw new NoSuchElementException( "Metadata request failed: " + path );
return "Metadata request failed: " + path;
}
} catch ( NoSuchElementException ex ) {
throw ex;
} catch ( Exception ex ) {
NoSuchElementException noSuchElementException = Exceptions.findCause( ex, NoSuchElementException.class );
if ( noSuchElementException != null ) throw noSuchElementException;
Expand All @@ -276,5 +281,5 @@ public byte[] handle( final String path ) {
throw Exceptions.toUndeclared( ex );
}
}

}

0 comments on commit 3b204e4

Please sign in to comment.