Class IP2LocationAdmin
- All Implemented Interfaces:
IServiceStatusListener
co.mindus.ip2location library: the status of the running service as the NetPhantom IP2Location page shows it (getStatus(ServerSettings)), the actions of its buttons and of the context menu of its status indicator, the recent events, and a IP2LocationAdmin.Listener told when something changed. ServerAdmin's IP2Location dialog is built on it (CC-IP2LOCATION-SERVERADMIN); NetPhantom's AdminIP2LocationPanel read the manager and the event log directly.One instance per JVM, like the manager it wraps. It registers itself with the manager as a status listener once, at first use, and keeps the last download error the way the NetPhantom panel did, so that the status indicator can say "Download failed: ..." until a download succeeds or the counters are cleared. The listeners of this class are called on the library's threads; a UI posts to its own worker.
- Author:
- Christopher Mindus
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceTold about the service by the library's threads.static final recordThe status of the service at one moment, the fields of the Status section of the NetPhantom page: everything a UI shows, nothing it must compute.Nested classes/interfaces inherited from interface IServiceStatusListener
IServiceStatusListener.ErrorTypeMethod Summary
Modifier and TypeMethodDescriptionvoidaddListener(IP2LocationAdmin.Listener listener) Adds a listener.booleanapply(ServerSettings settings) Applies the server settings to the running service: the module reconfigures the manager from them (IP2LocationModule.reconfigure(ServerSettings, java.io.File)).booleanChecks if a recovery can be triggered (the service is configured, none is running).booleanChecks if the databases can be updated now (the service is enabled and ready, no update is running).voidResets the download failure counter and the last download error.voidResets every error counter, the recovery recommendation and the last download error.voidResets the initialization failure counter.voidResets the lookup error counter.voidClears the recovery recommendation without recovering.static IP2LocationAdminGets the instance, registered with the manager.getRecentEvents(int max) Gets the recent events of the JSON-lines history, newest last, as one line each.getStatus(ServerSettings settings) Reads the status of the service.voidonDatabaseLoaded(LocalDate date, boolean isIPv6) Called when a database file has been loaded into memory.voidonDatabaseUpdated(boolean ipv4Updated, boolean ipv6Updated) Called when databases are reloaded into memory.voidonDownloadCompleted(boolean success) Called when a database download completes.voidCalled when a database download is about to start.voidonError(IServiceStatusListener.ErrorType type, String message, Throwable error) Called when an error occurs in the service.voidonInitialized(boolean success, Throwable error) Called when service initialization completes.voidonReconfigurationCompleted(boolean success, Throwable error) Called when the manager completes reconfiguration.voidonRecoveryCompleted(boolean success, Throwable error) Called when automatic recovery completes.voidonRecoveryRecommended(String reason, boolean autoRecoveryTriggered) Called when recovery is recommended or automatic recovery is triggered.voidonStatusChanged(String newStatus) --- IServiceStatusListener (the manager's threads) ---booleanremoveListener(IP2LocationAdmin.Listener listener) Removes a listener.voidTriggers a recovery: the database files are cleared and downloaded again.voidStarts an immediate database update check.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface IServiceStatusListener
onReconfigurationStarted, onServiceDisposed, onUpdateCheckPerformed
Method Details
getInstance
Gets the instance, registered with the manager.- Returns:
- The instance.
getStatus
Reads the status of the service.- Parameters:
settings- The server settings, for the switch.- Returns:
- The status.
getRecentEvents
apply
Applies the server settings to the running service: the module reconfigures the manager from them (IP2LocationModule.reconfigure(ServerSettings, java.io.File)). The caller has validated and written the settings and saved the configuration file.- Parameters:
settings- The server settings.- Returns:
- false when there is no module (no server) or the manager is shut down, true otherwise.
canUpdateNow
public boolean canUpdateNow()Checks if the databases can be updated now (the service is enabled and ready, no update is running).- Returns:
- true when
updateNow()would start an update.
updateNow
public void updateNow()Starts an immediate database update check.canTriggerRecovery
public boolean canTriggerRecovery()Checks if a recovery can be triggered (the service is configured, none is running).- Returns:
- true when
triggerRecovery()would start one.
triggerRecovery
public void triggerRecovery()Triggers a recovery: the database files are cleared and downloaded again.clearRecoveryRecommendation
public void clearRecoveryRecommendation()Clears the recovery recommendation without recovering.clearErrorCounters
public void clearErrorCounters()Resets every error counter, the recovery recommendation and the last download error.clearInitFailures
public void clearInitFailures()Resets the initialization failure counter.clearLookupErrors
public void clearLookupErrors()Resets the lookup error counter.clearDownloadError
public void clearDownloadError()Resets the download failure counter and the last download error.addListener
Adds a listener.- Parameters:
listener- The listener.
removeListener
Removes a listener.- Parameters:
listener- The listener.- Returns:
- true if it was registered.
onStatusChanged
--- IServiceStatusListener (the manager's threads) ---- Specified by:
onStatusChangedin interfaceIServiceStatusListener- Parameters:
newStatus- The new status description string- See Also:
onDownloadStarted
public void onDownloadStarted()Description copied from interface:IServiceStatusListenerCalled when a database download is about to start.This is called before each download attempt, including automatic scheduled updates and forced updates.
- Specified by:
onDownloadStartedin interfaceIServiceStatusListener
onDownloadCompleted
public void onDownloadCompleted(boolean success) Description copied from interface:IServiceStatusListenerCalled when a database download completes.This is called after each download attempt finishes, whether successful or not.
- Specified by:
onDownloadCompletedin interfaceIServiceStatusListener- Parameters:
success-trueif at least one database was downloaded
onDatabaseLoaded
Description copied from interface:IServiceStatusListenerCalled when a database file has been loaded into memory.This is called after each individual database file is successfully opened, during initial load, reconfiguration, download, or recovery. It provides the publication date embedded in the database file.
- Specified by:
onDatabaseLoadedin interfaceIServiceStatusListener- Parameters:
date- The publication date of the loaded database, ornullif the date could not be determinedisIPv6-trueif the IPv6 database was loaded,falsefor IPv4
onDatabaseUpdated
public void onDatabaseUpdated(boolean ipv4Updated, boolean ipv6Updated) Description copied from interface:IServiceStatusListenerCalled when databases are reloaded into memory.This is called after new database files are loaded, either from a fresh download or when the service starts with existing files.
- Specified by:
onDatabaseUpdatedin interfaceIServiceStatusListener- Parameters:
ipv4Updated-trueif the IPv4 database was reloadedipv6Updated-trueif the IPv6 database was reloaded
onReconfigurationCompleted
Description copied from interface:IServiceStatusListenerCalled when the manager completes reconfiguration.This is called after reconfiguration finishes, whether successful or not. The old service has been disposed at this point.
- Specified by:
onReconfigurationCompletedin interfaceIServiceStatusListener- Parameters:
success-trueif the new service initialized successfullyerror- The error that occurred, ornullif successful- See Also:
onInitialized
Description copied from interface:IServiceStatusListenerCalled when service initialization completes.This is called once when the service finishes its initial startup, whether successful or not.
- Specified by:
onInitializedin interfaceIServiceStatusListener- Parameters:
success-trueif initialization succeeded,falseif it failederror- The error that occurred, ornullif successful
onError
Description copied from interface:IServiceStatusListenerCalled when an error occurs in the service.This provides detailed error information for logging, monitoring, or automated handling. The error type can be used to determine appropriate responses:
IServiceStatusListener.ErrorType.isRecoverable()- Recovery might helpIServiceStatusListener.ErrorType.requiresUserAction()- User intervention neededIServiceStatusListener.ErrorType.isTransient()- May resolve on retry
Example
@Override public void onError(ErrorType type, String message, Throwable error) { // Log the error logger.error("IP2Location [" + type + "]: " + message, error); // Alert administrator for critical errors if (type.requiresUserAction()) { alertAdmin("IP2Location requires attention: " + message); } }- Specified by:
onErrorin interfaceIServiceStatusListener- Parameters:
type- The category of errormessage- A human-readable error messageerror- The underlying exception, ornullif not applicable- See Also:
onRecoveryRecommended
Description copied from interface:IServiceStatusListenerCalled when recovery is recommended or automatic recovery is triggered.This event is fired when error thresholds are exceeded and recovery action should be considered. The event indicates whether automatic recovery has been triggered (if enabled) or if manual intervention is needed.
Example
@Override public void onRecoveryRecommended(String reason, boolean autoRecoveryTriggered) { if (autoRecoveryTriggered) { logger.info("Automatic recovery initiated: " + reason); } else { // Prompt user for action SwingUtilities.invokeLater(() -> { boolean confirmed = showConfirmDialog( "Recovery Recommended", reason + "\n\nAttempt recovery now?"); if (confirmed) { manager.triggerRecovery(); } }); } }- Specified by:
onRecoveryRecommendedin interfaceIServiceStatusListener- Parameters:
reason- Human-readable explanation of why recovery is recommendedautoRecoveryTriggered-trueif automatic recovery has been initiated,falseif manual action is needed- See Also:
onRecoveryCompleted
Description copied from interface:IServiceStatusListenerCalled when automatic recovery completes.This is only called when automatic recovery is enabled and was triggered. It indicates whether the recovery attempt succeeded.
- Specified by:
onRecoveryCompletedin interfaceIServiceStatusListener- Parameters:
success-trueif recovery succeeded and service is operationalerror- The error that occurred during recovery, ornullif successful