Package com.iizix.geo
Class RegionLocationService
java.lang.Object
com.iizix.geo.RegionLocationService
Region location service that is used to find the region location information from an IP address.
- Author:
- Christopher Mindus
- Field SummaryFields
- Constructor SummaryConstructorsModifierConstructorDescription- protected- RegionLocationService- (String name, long defaultTimeout) Constructs the service provider for the region location information.
- Method SummaryModifier and TypeMethodDescription- final void- configure- (int localCacheSize, boolean useGlobalCache, boolean doSaveToGlobalCache) Configures the service provider.- final HostNameRegionLocationRequests a region location for a host name or IP address (version 4 or 6).- final HostNameRegionLocation- createRegionLocation- (String host) Requests the region location from a host name or IP address (version 4 or 6).- final HostNameRegionLocation- createRegionLocation- (InetAddress address) Requests the region location from an IP address (version 4 or 6).- static StringGets the default region location provider name.- static RegionLocationService- getInstance- (String name) Gets an instance of a RegionLocationDetectionService.- final String- getName()Gets the name of the region location service.- static String[]- getNames()Gets the available names for the region location providers.- static void- initialize- (RegionLocationService service) Initializes the region location detection service with the service provider.- protected booleanReturns if the service provider is available to use.- booleanChecks if the service provider is disposed of.- protected voidDisposes of the region service provider.- protected final JSONObject- parse- (URLConnection uc) Process a HTTP[S] URL connection and return a JSONObject.- protected abstract RegionLocationRequests region location information from the service provider.- static void- setGlobalCacheSize- (int size) Sets the maximum global cache size.- static void- shutdown()Called when the server is shutting down and disposes all service providers.
- Field Details- ENCODINGAlways UTF-8 encoded.
- ENCODING_NAMEThe "UTF-8" string.
 
- Constructor Details- RegionLocationServiceConstructs the service provider for the region location information. The server will once constructed, register the service with its name.- Parameters:
- name- The service name.
- defaultTimeout- The default timeout.
- Throws:
- NullPointerException- If- nameis- null.
- IllegalArgumentException- If the- defaultTimeoutis less than 5000ms.
 
 
- Method Details- setGlobalCacheSizepublic static void setGlobalCacheSize- (int size) Sets the maximum global cache size.- Parameters:
- size- The size: must be larger than 100.
- Throws:
- IllegalArgumentException- If size is smaller than 100.
- IllegalStateException- If not called from the server.
 
- initializeInitializes the region location detection service with the service provider. This code is called from the server.- Parameters:
- service- The RegionLocationDetectionService instance.
- Throws:
- NullPointerException- If the- serviceis- null.
- IllegalStateException- If- RegionLocationService's service name is already initialized.
 
- shutdownpublic static void shutdown()Called when the server is shutting down and disposes all service providers.- Throws:
- IllegalStateException- If this method is not called from the server.
 
- getNamesGets the available names for the region location providers.- Returns:
- The names.
 
- getDefaultNameGets the default region location provider name.- Returns:
- The name, or null if no providers are initialized.
 
- getInstanceGets an instance of a RegionLocationDetectionService.- Parameters:
- name- The name of the RegionLocationDetectionService,- nullfor default.
- Throws:
- IllegalArgumentException- If the name is invalid.
- IllegalStateException- If the service is not initiated with correct settings (such as keys, etc).
 
- configurepublic final void configure- (int localCacheSize, boolean useGlobalCache, boolean doSaveToGlobalCache) Configures the service provider.- Parameters:
- localCacheSize- Local cache size, zero for none.
- useGlobalCache- Flag to enable usage of global cache prior to asking the service provider.
- doSaveToGlobalCache- Flag to save requests answered by the service provider to the global cache.
- Throws:
- IllegalStateException- If provider is already configured.
 
- isAvailableprotected boolean isAvailable()Returns if the service provider is available to use.- Subclasses override this method to return another value than - true.- Returns:
- true for OK to use, false if not properly configured.
 
- getNameGets the name of the region location service.- Returns:
- The name.
 
- createRegionLocationRequests the region location from an IP address (version 4 or 6).- Parameters:
- address- The IPv4/IPv6 address.
- Throws:
- NullPointerException- If- addressis null.
 
- createRegionLocationRequests the region location from a host name or IP address (version 4 or 6).- Parameters:
- host- The host name or IPv4/IPv6 address.
- Throws:
- NullPointerException- If- hostNameis null.
- UnknownHostException- If- hostName InetAddresscannot be determined.
 
- createRegionLocationRequests a region location for a host name or IP address (version 4 or 6).- Parameters:
- host- The host name or IPv4/IPv6 address.
- Throws:
- NullPointerException- If- hostis null.
 
- parseProcess a HTTP[S] URL connection and return a JSONObject.- Parameters:
- uc- The URLConnection, should be Http(s)UrlConnection.
- Returns:
- A JSONObject.
- Throws:
- IOException- For failures.
 
- isDisposedpublic boolean isDisposed()Checks if the service provider is disposed of.- Returns:
- true if service provider is disposed of, false otherwise.
 
- requestprotected abstract RegionLocation request- (String ipAddress) throws IOException, InterruptedIOException, InterruptedException Requests region location information from the service provider. When the request has completed with or without error, call- Parameters:
- ipAddress- The IP address request.
- Returns:
- The region location information, must always be non-null.
- Throws:
- IOException- For I/O errors.
- InterruptedIOException- If an I/O operation is interrupted.
- InterruptedException- If thread is interrupted.
 
- onDisposedprotected void onDisposed()Disposes of the region service provider. This is done by the server when it is shutting down. Any pending requests that are not yet processed are canceled by the framework.- Subclasses override this method to perform some kind of custom clean-up such as e.g. closing a database.