Package gov.usgs.earthquake.distribution
Class JDBCNotificationIndex
java.lang.Object
gov.usgs.util.DefaultConfigurable
gov.usgs.earthquake.util.JDBCConnection
gov.usgs.earthquake.distribution.JDBCNotificationIndex
- All Implemented Interfaces:
NotificationIndex
,Configurable
,AutoCloseable
Stores and retrieves Notifications.
This is typically used by a NotificationReceiver to track its Notifications,
but may also be used by NotificationListeners. Each object should maintain a
separate NotificationIndex.
This implementation uses a SQLite Database as the index.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final String
This is the property key used in the configuration file to specify a different SQLite database file.Fields inherited from class gov.usgs.earthquake.util.JDBCConnection
DATABASE_SECRET_ARN_PROPERTY
-
Constructor Summary
ConstructorsConstructorDescriptionDefault, no-arg constructor.JDBCNotificationIndex
(Config config) Constructor called from the config object conforming to theConfigurable
interface specification.JDBCNotificationIndex
(String filename) Constructor call from filename, where filename is jdbc index file If null, then index file defaults -
Method Summary
Modifier and TypeMethodDescriptionvoid
addNotification
(Notification notification) Add a notification to the index.void
Reads the givenconfig
object and sets values appropriately.protected Connection
connect()
Connect to the database.Search the index for expired notifications.Search the index for notifications matching id.findNotifications
(String source, String type, String code) Search the index for notifications matching the sources, types, and codes.Search the index for notifications matching the sources, types, and codes.protected PreparedStatement
protected List<Notification>
Executes a prepared statement and parses the result set into a list of notifications.protected Notification
parseNotification
(String source, String type, String code, Date update, Date expires, String download) Creates and returns aNotification
based on the provided data.void
removeNotification
(Notification notification) Remove a notification from the index.void
removeNotifications
(List<Notification> notifications) Remove notifications from the index.void
shutdown()
Closes the JDBC connection and all it's associated prepared statements.void
startup()
Connects to the JDBC DB index and prepares the DML/Query statements that will execute at runtime.Methods inherited from class gov.usgs.earthquake.util.JDBCConnection
beginTransaction, buildUrl, close, commitTransaction, getConnection, getDriver, getUrl, rollbackTransaction, setDriver, setUrl, verifyConnection
Methods inherited from class gov.usgs.util.DefaultConfigurable
getName, setName
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface gov.usgs.util.Configurable
getName, setName
-
Field Details
-
JDBC_FILE_PROPERTY
This is the property key used in the configuration file to specify a different SQLite database file. If this file doesn't exist it will be created at startup time- See Also:
-
-
Constructor Details
-
JDBCNotificationIndex
Default, no-arg constructor. This just ensures the JDBC SQLite driver is appropriately on the classpath for proper runtime execution. This probably will not get called directly in favor of the configurable constructor.- Throws:
Exception
- If the JDBC driver class is not found.- See Also:
-
JDBC_DRIVER_CLASS
-
JDBCNotificationIndex
Constructor call from filename, where filename is jdbc index file If null, then index file defaults- Parameters:
filename
- String - What will be the index file- Throws:
Exception
- if error occurs
-
JDBCNotificationIndex
Constructor called from the config object conforming to theConfigurable
interface specification. This internally calls its no-arg constructor then configures itself.- Parameters:
config
- The config object from which this instance will be configured.- Throws:
Exception
- If the JDBC driver class is not found.- See Also:
-
Configurable
JDBC_DRIVER_CLASS
-
-
Method Details
-
configure
Reads the givenconfig
object and sets values appropriately.- Specified by:
configure
in interfaceConfigurable
- Overrides:
configure
in classJDBCConnection
- Parameters:
config
- The config object from which this instance will be configured.- Throws:
Exception
- Exception- See Also:
-
connect
Description copied from class:JDBCConnection
Connect to the database. Sub-classes determine how connection is made.- Overrides:
connect
in classJDBCConnection
- Returns:
- the connection.
- Throws:
Exception
- if unable to connect.
-
startup
Connects to the JDBC DB index and prepares the DML/Query statements that will execute at runtime. If the JDBC DB index file does not exist then an empty schema will be copied out of the executing JAR file to be used.- Specified by:
startup
in interfaceConfigurable
- Overrides:
startup
in classJDBCConnection
- Throws:
Exception
- if error occurs- See Also:
-
shutdown
Closes the JDBC connection and all it's associated prepared statements.- Specified by:
shutdown
in interfaceConfigurable
- Overrides:
shutdown
in classJDBCConnection
- Throws:
Exception
- if error occurs- See Also:
-
addNotification
Add a notification to the index. If an identical notification is already in the index, the implementation may choose whether or not to store the duplicate information.- Specified by:
addNotification
in interfaceNotificationIndex
- Parameters:
notification
- the notification to add.- Throws:
Exception
- if an error occurs while storing the notification.- See Also:
-
removeNotification
Remove a notification from the index. All matching notifications should be removed from the index.- Specified by:
removeNotification
in interfaceNotificationIndex
- Parameters:
notification
- the notification to remove.- Throws:
Exception
- if an error occurs while removing the notification.- See Also:
-
removeNotifications
Remove notifications from the index. All matching notifications should be removed from the index.- Specified by:
removeNotifications
in interfaceNotificationIndex
- Parameters:
notifications
- the notifications to remove.- Throws:
Exception
- if an error occurs while removing the notifications.- See Also:
-
findNotifications
Search the index for notifications matching id. If more than one notification matches, all should be returned.- Specified by:
findNotifications
in interfaceNotificationIndex
- Parameters:
id
- the ProductId to find.- Returns:
- a list of matching notifications.
- Throws:
Exception
- if an error occurs while searching the index.- See Also:
-
findNotifications
public List<Notification> findNotifications(String source, String type, String code) throws Exception Search the index for notifications matching the sources, types, and codes. Only one notification for each unique ProductId (source+type+code+updateTime) should be returned. If sources, types, and/or codes are null, that parameter should be considered a wildcard. If sources, types, and codes are all null, a notification for each unique ProductId in the index should be returned.- Specified by:
findNotifications
in interfaceNotificationIndex
- Parameters:
source
- sources to include, or all if null.type
- types to include, or all if null.code
- codes to include, or all if null.- Returns:
- a list of matching notifications.
- Throws:
Exception
- if an error occurs while searching the index.- See Also:
-
findNotifications
public List<Notification> findNotifications(List<String> sources, List<String> types, List<String> codes) throws Exception Search the index for notifications matching the sources, types, and codes. Only one notification for each unique ProductId (source+type+code+updateTime) should be returned. If sources, types, and/or codes are null, that parameter should be considered a wildcard. If sources, types, and codes are all null, a notification for each unique ProductId in the index should be returned. This implementation require synchronization to prevent SQLExceptions caused by concurrent access. SQLite locks the database whenever there is an open ResultSet resource. So even read queries can end up causing SQL concurrent access problems.- Specified by:
findNotifications
in interfaceNotificationIndex
- Parameters:
sources
- sources to include, or all if null.types
- types to include, or all if null.codes
- codes to include, or all if null.- Returns:
- a list of matching notifications.
- Throws:
Exception
- if an error occurs while searching the index.
-
findExpiredNotifications
Search the index for expired notifications. All expired notifications, even if duplicate, should be returned.- Specified by:
findExpiredNotifications
in interfaceNotificationIndex
- Returns:
- a list of expired notifications.
- Throws:
Exception
- if an error occurs while searching the index.- See Also:
-
getNotifications
Executes a prepared statement and parses the result set into a list of notifications. The prepared statement can have any set of criteria and all required parameters should be bound before calling this method. The result set of the prepared statement must include at least: - PRODUCT_SOURCE_COLUMN
- PRODUCT_TYPE_COLUMN
- PRODUCT_CODE_COLUMN
- PRODUCT_UPDATE_COLUMN
- EXPIRATION_DATE_COLUMN
- PRODUCT_URL_COLUMN- Parameters:
ps
- The prepared statement to execute.- Returns:
- A list of notifications returned by executing the statement.
- Throws:
Exception
- If aSQLException
occurs.
-
parseNotification
protected Notification parseNotification(String source, String type, String code, Date update, Date expires, String download) Creates and returns aNotification
based on the provided data. If thedownload
string references a valid URL, then aURLNotification
is created, otherwise aDefaultNotification
is created.- Parameters:
source
- The product source string.type
- The product type string.code
- The product code string.update
- The latest update date/time for the product.expires
- The date/time when this notification expires.download
- A reference to a URL where one can download this product, ornull
if this is not aURLNotification
.- Returns:
- The generated notification, or
null
if one could not be created (but an exception did not occur).
-
getCorrectStatement
protected PreparedStatement getCorrectStatement(List<String> sources, List<String> types, List<String> codes) throws Exception - Parameters:
sources
- List string of sourcestypes
- List string of typescodes
- List string of codes- Returns:
- prepared query based on what is/is not null
- Throws:
Exception
- if error occurs
-