Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013-2015 Amazon Technologies, Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at:
   *
   *    http://aws.amazon.com/apache2.0
   *
  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  * OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.amazonaws.regions;
 
 import java.io.File;
 import java.net.URI;
 import java.util.List;
 
 
Utilities for working with regions.
 
 public class RegionUtils {
 
     private static final String REGIONS_FILE_OVERRIDE =
 
     private static final String BUNDLED_ENDPOINTS_RESOURCE_PATH =
         "/com/amazonaws/regions/regions.xml";
 
     private static final String OVERRIDE_ENDPOINTS_RESOURCE_PATH =
         "/com/amazonaws/regions/override/regions.xml";
 
 
     private static final Log log = LogFactory.getLog(RegionUtils.class);
 
 
     private static String source;
     private static volatile RegionMetadata regionMetadata;

    
For test purposes.

Returns:
the source from which the region metadata singleton was loaded
 
     protected static String getSource() {
         return ;
     }
 
     /*
      * Convenience methods that access the singleton RegionMetadata instance
      * maintained by this class, lazily initializing it if need be.
      */

    
Returns the current set of region metadata for this process, initializing it if it has not yet been explicitly initialized before.

Returns:
the current set of region metadata
 
     public static RegionMetadata getRegionMetadata() {
         RegionMetadata rval = ;
         if (rval != null) {
             return rval;
         }
 
         initialize();
         return ;
     }

    
Returns a list of the available AWS regions.
 
     public static List<RegiongetRegions() {
         return getRegionMetadata().getRegions();
     }

    
Returns a list of the regions that support the service given.

    public static List<RegiongetRegionsForService(
            String serviceAbbreviation) {
        return getRegionMetadata().getRegionsForService(serviceAbbreviation);
    }

    
Returns the region with the id given, if it exists. Otherwise, returns null.
    public static Region getRegion(String regionName) {
        return getRegionMetadata().getRegion(regionName);
    }

    
Searches through all known regions to find one with any service at the specified endpoint. If no region is found with a service at that endpoint, an exception is thrown.

Parameters:
endpoint The endpoint for any service residing in the desired region.
Returns:
The region containing any service running at the specified endpoint, otherwise an exception is thrown if no region is found with a service at the specified endpoint.
Throws:
java.lang.IllegalArgumentException If the given URL is malformed, or if the one of the service URLs on record is malformed.
    public static Region getRegionByEndpoint(String endpoint) {
        return getRegionMetadata().getRegionByEndpoint(endpoint);
    }
    /*
     * Methods for loading a RegionMetadata object from various locations.
     */

    
Loads a set of region metadata by downloading an XML file from the given URI and parsing it.

Parameters:
uri the uri of the XML file to parse
Returns:
the parsed region metadata
Throws:
java.io.IOException on error fetching or parsing the XML file
    public static RegionMetadata loadMetadataFromURI(final URI uri)
            throws IOException {
        return loadMetadataFromURI(urinull);
    }

    
Loads a set of region metadata by downloading an XML file from the given URI and parsing it.

Parameters:
uri the uri of the XML file to parse
config configuration for the HTTP client to use to fetch the file
Returns:
the parsed region metadata
Throws:
java.io.IOException on error fetching or parsing the XML file
    public static RegionMetadata loadMetadataFromURI(
            final URI uri,
            final ClientConfiguration configthrows IOException {
    
        InputStream stream = HttpUtils.fetchFile(uriconfig);
        try {
            return loadMetadataFromInputStream(stream);
        } finally {
            stream.close();
        }
    }

    
Loads a set of region metadata from an XML file on disk.

Parameters:
file the file to load from
Returns:
the loaded region metadata
Throws:
java.io.IOException on error opening or reading from the file
    public static RegionMetadata loadMetadataFromFile(final File file)
            throws IOException {
        InputStream stream =
            new BufferedInputStream(new FileInputStream(file));
        try {
            return loadMetadataFromInputStream(stream);
        } finally {
            stream.close();
        }
    }

    
Loads a set of region metadata from an XML file stored as a resource of the classloader used to load the RegionUtils class.

Parameters:
name the path of the resource, relative to the RegionUtils class
Returns:
the parsed region metadata
Throws:
java.io.IOException if the resource is not found or cannot be parsed
    public static RegionMetadata loadMetadataFromResource(final String name)
            throws IOException {
        return loadMetadataFromResource(RegionUtils.classname);
    }

    
Loads a set of region metadata from an XML file stored as a resource of the classloader used to load the given class.

Parameters:
clazz the class to use as a base for the resource
name the path to the resource, relative to the given class
Returns:
the parsed region metadata
Throws:
java.io.IOException if the resource is not found or cannot be parsed
            final Class<?> clazz,
            final String namethrows IOException {
        InputStream stream = clazz.getResourceAsStream(name);
        if (stream == null) {
            throw new FileNotFoundException(
                    "No resource '" + name + "' found.");
        }
        try {
            return loadMetadataFromInputStream(stream);
        } finally {
            stream.close();
        }
    }

    
Loads a set of region metadata from an XML file stored as a resource of the given classloader.

Parameters:
classLoader the class loader to load the resource from
name the path to the resource
Returns:
the parsed region metadata
Throws:
java.io.IOException if the resource is not found or cannot be parsed
            final ClassLoader classLoader,
            final String namethrows IOException {
        InputStream stream = classLoader.getResourceAsStream(name);
        if (stream == null) {
            throw new FileNotFoundException(
                    "No resource '" + name + "' found.");
        }
        try {
            return loadMetadataFromInputStream(stream);
        } finally {
            stream.close();
        }
    }

    
Loads a set of region metadata from an arbitrary InputStream containing an XML file.

Parameters:
stream the stream to load from
Returns:
the loaded region metadata
Throws:
java.io.IOException on error reading from the stream
            final InputStream streamthrows IOException {
        return RegionMetadataParser.parse(stream);
    }
    /*
     * Methods for initializing the process-wide singleton RegionMetadata
     * instance.
     */

    
This method no longer attempts to retrieve region metadata from CloudFront, as that file is no longer being maintained and the version bundled with the SDK is likely to be more up-to-date.

It's deprecated to signal that it no longer has any possibility of retrieving a newer set of metadata than was previously loaded. If you are simply wanting to reinitialize from the bundled region metadata, call initialize. If you want to maintain your own remote copy of the region metadata and periodically refresh it at runtime, call initializeFromURI.

Deprecated:
in favor of initialize()
    @Deprecated
    public static void init() {
        initialize();
    }

    
Initializes the region metadata by loading from the default hierarchy of region metadata locations.
    public static synchronized void initialize() {
        String overrideFilePath = System.getProperty();
        if (overrideFilePath != null) {
            doInitializeFromFile(new File(overrideFilePath));
             = overrideFilePath;
            return;
        }
        InputStream override = RegionUtils.class
                .getResourceAsStream();
        if (override != null) {
            try {
                doInitializeFromInputStream(override);
                 = ;
                return;
            } finally {
                IOUtils.closeQuietly(override);
            }
        }
                                 );
    }

    
Loads a set of region metadata by downloading an XML file from the given URI and parsing it.

Parameters:
uri the uri of the XML file to parse
Throws:
com.amazonaws.AmazonClientException on error
    public static synchronized void initializeFromURI(final URI uri) {
        doInitializeFromURI(urinull);
         = uri.toASCIIString();
    }

    
Loads a set of region metadata by downloading an XML file from the given URI and parsing it.

Parameters:
uri the uri of the XML file to parse
config configuration for the HTTP client to use to fetch the file
Throws:
com.amazonaws.AmazonClientException on error
    public static synchronized void initializeFromURI(
            final URI uri,
            final ClientConfiguration config) {
        doInitializeFromURI(uriconfig);
         = uri.toASCIIString();
    }

    
Initializes the region metadata singleton from an XML file on disk.

Parameters:
file the file to load from
Throws:
com.amazonaws.AmazonClientException on error opening or reading from the file
    public static synchronized void initializeFromFile(final File file) {
        doInitializeFromFile(file);
         = file.toString();
    }

    
Initializes the region metadata singleton from an XML file stored as a resource of the classloader used to load the RegionUtils class.

Parameters:
name the path of the resource, relative to the RegionUtils class
Throws:
com.amazonaws.AmazonClientException on error
    public static synchronized void initializeFromResource(final String name) {
        doInitializeFromResource(RegionUtils.classname);
         = name;
    }

    
Initializes the region metadata singleton from the given resource.

Parameters:
clazz the class to use as a base for the resource
name the path to the resource, relative to the given class
Throws:
com.amazonaws.AmazonClientException on error
    public static synchronized void initializeFromResource(
            final Class<?> clazz,
            final String name) {
        doInitializeFromResource(clazzname);
         = name;
    }

    
Initializes the region metadata singleton from the given resource.

Parameters:
classLoader the class loader to use to load the resource
name the path to the resource
Throws:
com.amazonaws.AmazonClientException on error
    public static synchronized void initializeFromResource(
            final ClassLoader classLoader,
            final String name) {
        doInitializeFromResource(classLoadername);
         = name;
    }

    
Directly sets the singleton RegionMetadata instance.

Parameters:
metadata the new region metadata object
    public static void initializeWithMetadata(final RegionMetadata metadata) {
        if (metadata == null) {
            throw new IllegalArgumentException("metadata cannot be null");
        }
         = metadata;
         = "RegionUtils.initializeWithMetadata(RegionMetadata)";
    }

    
Private, unsynchronized helper method that initializes the region metadata singleton by loading the given URI.

Parameters:
uri the uri to load
config optional configuration for the HTTP client
Throws:
com.amazonaws.AmazonClientException on error
    private static void doInitializeFromURI(final URI uri,
                                            final ClientConfiguration config) {
        try {
             = loadMetadataFromURI(uriconfig);
        } catch (IOException exception) {
            throw new AmazonClientException(
                    "Error parsing region metadata from " + uri,
                    exception);
        }
    }

    
Private, unsynchronized helper method that initializes the region metadata singleton by loading the given file.

Parameters:
file the file to load
Throws:
com.amazonaws.AmazonClientException on error
    private static void doInitializeFromFile(final File file) {
        try {
             = loadMetadataFromFile(file);
        } catch (IOException exception) {
            throw new AmazonClientException(
                    "Error parsing region metadata from " + file,
                    exception);
        }
    }

    
Private, unsynchronized helper method that initializes the region metadata singleton by loading the given resource from the classpath.

Parameters:
clazz the class to use as a base for the resource path
name the name of the resource to load
Throws:
com.amazonaws.AmazonClientException on error
    private static void doInitializeFromResource(final Class<?> clazz,
                                                 final String name) {
        try {
             = loadMetadataFromResource(clazzname);
        } catch (IOException exception) {
            throw new AmazonClientException(
                    "Error parsing region metadata from resource " + name,
                    exception);
        }
    }

    
Private, unsynchronized helper method that initializes the region metadata singleton by loading the given resource from the given classloader.

Parameters:
classLoader the classloader to use to load the resource
name the name of the resource to load
Throws:
com.amazonaws.AmazonClientException on error
    private static void doInitializeFromResource(
            final ClassLoader classLoader,
            final String name) {
        try {
             = loadMetadataFromResource(classLoadername);
        } catch (IOException exception) {
            throw new AmazonClientException(
                    "Error parsing region metadata from resource " + name,
                    exception);
        }
    }

    
Private, unsynchronized helper method that initializes the region metadata singleton by loading from the given input stream.

Parameters:
stream the input stream to load from
Throws:
com.amazonaws.AmazonClientException on error
    private static void doInitializeFromInputStream(
            final InputStream stream) {
        try {
             = loadMetadataFromInputStream(stream);
        } catch (IOException exception) {
            throw new AmazonClientException(
                    "Error parsing region metadata from input stream",
                    exception);
        }
    }
New to GrepCode? Check out our FAQ X