com.adventnet.services.database
Class DatabaseAdaptor

java.lang.Object
  |
  +--com.adventnet.services.database.DatabaseAdaptor

public class DatabaseAdaptor
extends java.lang.Object
implements DynamicMBean, MBeanRegistration

The DatabaseAdaptor Service helps to make sql queries to retrieve tables in JMX. This can be used in combination with User Storage Model. Example: DatabaseAdaptor dbservice = new DatabaseAdaptor(); dbservice.setDatabaseURL("jdbc:oracle:thin:@murali:1521:murali"); dbservice.setUserName("scott"); dbservice.setPassword("tiger"); dbservice.setTableName("Empl"); dbservice.setDriverName("oracle.jdbc.driver.OracleDriver"); dbservice.setColumnNames(new String[]{"name","dept","employeeId","age","designation", "salary","contactEmail","addr"}); dbservice.setIndexNames(new String[]{"dept","employeeId"}); dbservice.setColumnTypes(new String[]{DatabaseAdaptor.STRING,DatabaseAdaptor.STRING, DatabaseAdaptor.STRING,DatabaseAdaptor.INTEGER, DatabaseAdaptor.STRING,DatabaseAdaptor.INTEGER, DatabaseAdaptor.STRING,DatabaseAdaptor.STRING}); dbservice.setDebugLevel(agentName.debugLevel); server = agentName.getServer(); String name = server.getDefaultDomain()+":type=com.adventnet.services.database,tableName=emplTable"; try{ server.registerMBean(dbservice,new ObjectName(name)); } catch(Exception e) { e.printStackTrace(); } dbservice.startService();


Field Summary
(package private)  boolean cacheFlag
           
(package private)  int cacheNumber
           
(package private)  int cachingTime
           
(package private)  int[] columnAccess
           
(package private)  java.util.Hashtable columnMap
           
(package private)  java.lang.String[] columnNames
           
(package private)  java.lang.String[] columnTypes
           
(package private)  java.lang.String databaseURL
           
(package private)  int debugLevel
           
(package private)  java.lang.String driverName
           
static java.lang.String FLOAT
          Denotes Float data type for the column in the database.
(package private)  java.lang.String[] indexNames
           
static java.lang.String INTEGER
          Denotes Integer data type for the column in the database.
static java.lang.String LONG
          Denotes Long data type for the column in the database.
(package private)  java.lang.String passwd
           
static int READ_ONLY
          Specifies the access of the column as READ_ONLY
static int READ_WRITE
          Specifies the access of the column as READ_WRITE
(package private)  MBeanServer server
          the MBeanServer
static java.lang.String STRING
          Denotes String data type for the column in the database.
(package private)  java.lang.String tableName
           
(package private)  java.lang.String userName
           
 
Constructor Summary
DatabaseAdaptor()
          The NoArg constructor used to initialize the DatabaseAdaptor in forming the MBeanInfo and getting the reference of the MBeanServer using the MBeanServer.findJMXAgent(...) method.
DatabaseAdaptor(MBeanServer server)
          Constructor that takes MBeanServer reference.
 
Method Summary
 void addRow(java.lang.Object[] indexObjects, CompositeData entry)
          When the manager is trying to add a row in the database table, this method can be called.
 void deleteRow(java.lang.Object[] indexObjects)
          When the manager is trying to delete a row in the table, this method can be called.
 TabularData executeQueryToDB(java.lang.String query)
          This method is used to get the TabularData from the database
 java.lang.Object getAttribute(java.lang.String attribute)
          Gets the value of a specific attribute of this MBean.
 AttributeList getAttributes(java.lang.String[] attributes)
          Enables the values of several attributes of this MBean.
 int getCachingNumber()
          Getter for the number of rows which will be cached.
 int getCachingTime()
          Getter for caching Time.
 int[] getColumnAccess()
          Getter for the column access.
 java.util.Hashtable getColumnMap()
          Getter for the column mapping.
 java.lang.String[] getColumnNames()
          Getter for the column names of the table present in the mib.
 java.lang.String[] getColumnTypes()
          Getter for the column types of the table present in the mib.
 java.sql.Connection getConnection()
          This method gives the Connection instance with the Database
 java.lang.String getDatabaseURL()
          Getter for the database URL.
 int getDebugLevel()
          Getter for the debug level.
 java.lang.String getDriverName()
          Getter for the JDBC driver name.
 java.util.ArrayList getEntries(int startIndex, int endIndex)
          This method gives an ArrayList of the CompositeData instances in this table starting from the startIndex through till the EndIndex.
 CompositeData getEntry(java.lang.Object[] indexObjects)
          This method is used to get the entry from the database.
 CompositeData getFirstEntry()
          To get the first entry in the table from the database.
 java.lang.String[] getIndexNames()
          Getter for the index names of the table present in the mib.
 MBeanInfo getMBeanInfo()
          This method discovers the attributes and operations this MBean exposes for management.
 CompositeData getNextEntry(java.lang.Object[] indexObjects)
          To get the next entry for the given row (identified by the instance)
 java.lang.String getPassword()
          Getter for the Password.
 java.lang.Integer getState()
          Returns the state, whether ON_STATE (1) / OFF_STATE (0), of this service.
 java.lang.String getTableName()
          Getter for the table name.
 java.lang.String getUserName()
          Getter for the user name.
 java.lang.Object invoke(java.lang.String actionName, java.lang.Object[] params, java.lang.String[] signature)
          Invokes an action on the original MBean associated with this MBean.
 boolean isCachingEnabled()
          Used to see whether the table is cached or not.
 void modifyRow(java.lang.Object[] indexObjects, CompositeData entry)
          When the manager is trying to modify a row in the database table, this method can be called.
 void postDeregister()
          Allows this MBean to perform any operations needed after having been de-registered in the MBeanServer.
 void postRegister(java.lang.Boolean registrationDone)
          Allows this MBean to perform any operations needed after having been registered in the MBeanServer or after the registration has failed.
 void preDeregister()
          Allows this MBean to perform any operations it needs before being de-registered by the MBeanServer.
 ObjectName preRegister(MBeanServer server, ObjectName name)
          Allows the MBean to perform any operations it needs before being registered in the MBeanServer.
 void setAttribute(Attribute attribute)
          Sets the value of a specific attribute of this MBean.
 AttributeList setAttributes(AttributeList attributes)
          Sets the values of several attributes of this MBean.
 void setCachingEnabled(boolean cacheFlag)
          Used to set whether the table needs to be cached or not.
 void setCachingNumber(int noOfRows)
          Setter for the number of rows that will be cached.
 void setCachingTime(int cachingTime)
          Setter for Caching Time for Time in seconds to cache the Table Entries By default 60 seconds
 void setColumnAccess(int[] access)
          Setter for the column access.
 void setColumnMap(java.util.Hashtable columnMap)
          Setter for the column mapping.
 void setColumnNames(java.lang.String[] columnNames)
          Setter for the column names of the table present in the mib.
 void setColumnTypes(java.lang.String[] columnTypes)
          Setter for the column types of the table present in the mib.
 void setDatabaseURL(java.lang.String url)
          Setter for the database URL.
 void setDebugLevel(int debugLevel)
          Setter for the debug level.
 void setDriverName(java.lang.String driverName)
          Setter for the driver name.
 void setIndexNames(java.lang.String[] indexNames)
          Setter for the index names of the table present in the mib.
 void setPassword(java.lang.String password)
          Setter for the password.
 void setTableName(java.lang.String tableName)
          Setter for the table name.
 void setUserName(java.lang.String name)
          Setter for the User name.
 void startService()
          Allows the service to get started.
 void stopService()
          Allows the service to get stopped.
 int totalRows()
          This method gives the total number of rows in the table present in the database.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INTEGER

public static final java.lang.String INTEGER
Denotes Integer data type for the column in the database.

LONG

public static final java.lang.String LONG
Denotes Long data type for the column in the database.

STRING

public static final java.lang.String STRING
Denotes String data type for the column in the database.

FLOAT

public static final java.lang.String FLOAT
Denotes Float data type for the column in the database.

READ_ONLY

public static final int READ_ONLY
Specifies the access of the column as READ_ONLY

READ_WRITE

public static final int READ_WRITE
Specifies the access of the column as READ_WRITE

server

transient MBeanServer server
the MBeanServer

databaseURL

java.lang.String databaseURL

userName

java.lang.String userName

passwd

java.lang.String passwd

tableName

java.lang.String tableName

driverName

java.lang.String driverName

columnNames

java.lang.String[] columnNames

indexNames

java.lang.String[] indexNames

columnTypes

java.lang.String[] columnTypes

columnAccess

int[] columnAccess

columnMap

java.util.Hashtable columnMap

debugLevel

int debugLevel

cacheFlag

boolean cacheFlag

cachingTime

int cachingTime

cacheNumber

int cacheNumber
Constructor Detail

DatabaseAdaptor

public DatabaseAdaptor()
The NoArg constructor used to initialize the DatabaseAdaptor in forming the MBeanInfo and getting the reference of the MBeanServer using the MBeanServer.findJMXAgent(...) method.

DatabaseAdaptor

public DatabaseAdaptor(MBeanServer server)
Constructor that takes MBeanServer reference.
Parameters:
server - the MBeanServer where the Database Adaptor will be registered.
Method Detail

getState

public java.lang.Integer getState()
Returns the state, whether ON_STATE (1) / OFF_STATE (0), of this service.

getMBeanInfo

public MBeanInfo getMBeanInfo()
This method discovers the attributes and operations this MBean exposes for management.
Specified by:
getMBeanInfo in interface DynamicMBean
Returns:
An instance of MBeanInfo allowing to retrieve all attributes and operations of this MBean.

getAttribute

public java.lang.Object getAttribute(java.lang.String attribute)
                              throws AttributeNotFoundException,
                                     MBeanException,
                                     ReflectionException
Gets the value of a specific attribute of this MBean.
Specified by:
getAttribute in interface DynamicMBean
Parameters:
attribute - A String specifying the name of the attribute to be retrieved.
Returns:
The value of the retrieved attribute.

getAttributes

public AttributeList getAttributes(java.lang.String[] attributes)
Enables the values of several attributes of this MBean.
Specified by:
getAttributes in interface DynamicMBean
Parameters:
attributes - A list of attributes to be retrieved.
Returns:
The value of the retrieved attributes as attributeList.

invoke

public java.lang.Object invoke(java.lang.String actionName,
                               java.lang.Object[] params,
                               java.lang.String[] signature)
                        throws MBeanException,
                               ReflectionException
Invokes an action on the original MBean associated with this MBean.
Specified by:
invoke in interface DynamicMBean
Parameters:
actionName - The name of the action to be invoked.
params - An array containing the parameters to be set when the action is invoked
signature - An array containing the signature of the action. The class objects will be loaded using the same class loader as the one used for loading the MBean on which the action was invoked.
Returns:
The object returned by the action, which represents the result ofinvoking the action on the specified MBean.

setAttribute

public void setAttribute(Attribute attribute)
                  throws AttributeNotFoundException,
                         InvalidAttributeValueException,
                         MBeanException,
                         ReflectionException
Sets the value of a specific attribute of this MBean.
Specified by:
setAttribute in interface DynamicMBean
Parameters:
attribute - The identification of the attribute to be set and the value it is to be set to.
Returns:
The value of the attribute that has been set.

setAttributes

public AttributeList setAttributes(AttributeList attributes)
Sets the values of several attributes of this MBean.
Specified by:
setAttributes in interface DynamicMBean
Parameters:
attributes - A list of attributes: The identification of the attributes to be set and the values they are to be set to.
Returns:
The list of attributes that were set, with their new values.

startService

public void startService()
Allows the service to get started.

stopService

public void stopService()
Allows the service to get stopped.

preRegister

public ObjectName preRegister(MBeanServer server,
                              ObjectName name)
                       throws java.lang.Exception
Allows the MBean to perform any operations it needs before being registered in the MBeanServer. If the name of the MBean is not specified, the MBean can provide a name for its registration. If any exception is raised, the MBean will not be registered in the MBeanServer.
Specified by:
preRegister in interface MBeanRegistration
Parameters:
server - The MBeanServer in which the MBean will be registered.
name - The object name of the MBean.
Returns:
The name of the MBean registered.
Throws:
java.lang.Exception - - This exception should be caught by the MBeanServer and re-thrown as an MBeanRegistrationException.

postRegister

public void postRegister(java.lang.Boolean registrationDone)
Allows this MBean to perform any operations needed after having been registered in the MBeanServer or after the registration has failed.
Specified by:
postRegister in interface MBeanRegistration
Parameters:
registrationDone - Indicates whether or not the MBean has been successfully registered in the MBeanServer. The value false means that either the registration phase has failed.
registrationDone - indicates whether the mbean is registered properly with the server.

preDeregister

public void preDeregister()
                   throws java.lang.Exception
Allows this MBean to perform any operations it needs before being de-registered by the MBeanServer.
Specified by:
preDeregister in interface MBeanRegistration
Throws:
java.langException - This exception should be caught by the MBeanServer and re-thrown as an MBeanRegistrationException.

postDeregister

public void postDeregister()
Allows this MBean to perform any operations needed after having been de-registered in the MBeanServer.
Specified by:
postDeregister in interface MBeanRegistration

getFirstEntry

public CompositeData getFirstEntry()
To get the first entry in the table from the database.
Returns:
instance of javax.jmx.openmbean.CompositeData with the entry information. Otherwise null.

getNextEntry

public CompositeData getNextEntry(java.lang.Object[] indexObjects)
To get the next entry for the given row (identified by the instance)
Parameters:
indexObjects - the object array with index objects which identifies the row
Returns:
instance of javax.jmx.openmbean.CompositeData with the entry information. Otherwise null.

getEntry

public CompositeData getEntry(java.lang.Object[] indexObjects)
This method is used to get the entry from the database.
Parameters:
The - indexes which identifies the row as an Object Array.
Returns:
The entry as a CompositeData

executeQueryToDB

public TabularData executeQueryToDB(java.lang.String query)
                             throws java.lang.Exception
This method is used to get the TabularData from the database
Parameters:
String - The query need to execute to the database
Returns:
TabularData return parameter from the database

totalRows

public int totalRows()
This method gives the total number of rows in the table present in the database.
Returns:
int specifying the number of rows in the Table.

getEntries

public java.util.ArrayList getEntries(int startIndex,
                                      int endIndex)
This method gives an ArrayList of the CompositeData instances in this table starting from the startIndex through till the EndIndex. For example: If the 1st 5 rows have to be retrieved, then the start index will be 1 and the end index will be 5.
Returns:
ArrayList of the instances of the CompositeData.

addRow

public void addRow(java.lang.Object[] indexObjects,
                   CompositeData entry)
            throws java.lang.Exception
When the manager is trying to add a row in the database table, this method can be called.
Parameters:
indexObjects - the object array with index objects which identifies the row to be added.
entry - the entry value as javax.jmx.openmbean.CompositeData instance.

modifyRow

public void modifyRow(java.lang.Object[] indexObjects,
                      CompositeData entry)
               throws java.lang.Exception
When the manager is trying to modify a row in the database table, this method can be called.
Parameters:
indexObjects - the object array with index objects which identifies the row to be modified.
entry - the entry value as javax.jmx.openmbean.CompositeData instance.

deleteRow

public void deleteRow(java.lang.Object[] indexObjects)
               throws java.lang.Exception
When the manager is trying to delete a row in the table, this method can be called.
Parameters:
indexObjects - the object array with index objects which identifies the row to be deleted.
entry - the entry value as javax.jmx.openmbean.CompositeData instance.

getDatabaseURL

public java.lang.String getDatabaseURL()
Getter for the database URL.
Returns:
The Database URL of the connection.

setDatabaseURL

public void setDatabaseURL(java.lang.String url)
Setter for the database URL.
Parameters:
The - Database URL of the connection.

getUserName

public java.lang.String getUserName()
Getter for the user name.
Returns:
The User name of the connection.

setUserName

public void setUserName(java.lang.String name)
Setter for the User name.
Parameters:
The - User name of the connection.

getPassword

public java.lang.String getPassword()
Getter for the Password.
Returns:
The password of the connection.

setPassword

public void setPassword(java.lang.String password)
Setter for the password.
Parameters:
The - Password of the connection.

getTableName

public java.lang.String getTableName()
Getter for the table name.
Returns:
The Table Name in the database which should be queried.

setTableName

public void setTableName(java.lang.String tableName)
Setter for the table name.
Parameters:
The - Table Name in the database which should be queried.

getDriverName

public java.lang.String getDriverName()
Getter for the JDBC driver name.
Returns:
The Database driver of the connection.

setDriverName

public void setDriverName(java.lang.String driverName)
Setter for the driver name.
Parameters:
The - Driver name of the connection.

getColumnNames

public java.lang.String[] getColumnNames()
Getter for the column names of the table present in the mib.
Returns:
The column names as a String array.

setColumnNames

public void setColumnNames(java.lang.String[] columnNames)
Setter for the column names of the table present in the mib.
Parameters:
The - column names as a String array.

getIndexNames

public java.lang.String[] getIndexNames()
Getter for the index names of the table present in the mib.
Returns:
The index names as a String array.

setIndexNames

public void setIndexNames(java.lang.String[] indexNames)
Setter for the index names of the table present in the mib.
Parameters:
The - index names as a String array.

getColumnTypes

public java.lang.String[] getColumnTypes()
Getter for the column types of the table present in the mib.
Returns:
The column types as a String array.

setColumnTypes

public void setColumnTypes(java.lang.String[] columnTypes)
Setter for the column types of the table present in the mib.
Parameters:
The - column types as a String array.

getColumnAccess

public int[] getColumnAccess()
Getter for the column access.
Returns:
The access for the columns as an int[].

setColumnAccess

public void setColumnAccess(int[] access)
Setter for the column access.
Parameters:
The - access for the columns as an int[].

getColumnMap

public java.util.Hashtable getColumnMap()
Getter for the column mapping. This will be used if the table names in the mib differ from that of the database.
Returns:
The column mapping. null if no mapping is set.

setColumnMap

public void setColumnMap(java.util.Hashtable columnMap)
Setter for the column mapping. This will be used if the table names in the mib differ from that of the database.
Parameters:
The - column mapping as a Hashtable.

getDebugLevel

public int getDebugLevel()
Getter for the debug level.
Returns:
The debug level as an int. 1 - FATAL , 2 - CRITICAL , 3 - NON-CRITICAL

setDebugLevel

public void setDebugLevel(int debugLevel)
Setter for the debug level.
Parameters:
The - debug level as an int. 1 - FATAL , 2 - CRITICAL , 3 - NON-CRITICAL

isCachingEnabled

public boolean isCachingEnabled()
Used to see whether the table is cached or not. By default, there will not be any caching.
Returns:
true to specify caching false to specify no caching.

setCachingEnabled

public void setCachingEnabled(boolean cacheFlag)
Used to set whether the table needs to be cached or not. By default, there will not be any caching.
Parameters:
cacheFlag - true to specify caching false to specify no caching.

getCachingTime

public int getCachingTime()
Getter for caching Time. By default 60 seconds.
Returns:
The caching time in seconds

setCachingTime

public void setCachingTime(int cachingTime)
Setter for Caching Time for Time in seconds to cache the Table Entries By default 60 seconds
Parameters:
cachingTime - The caching Time in seconds.

getCachingNumber

public int getCachingNumber()
Getter for the number of rows which will be cached.
Returns:
The no. of rows that will be cached in memory.

setCachingNumber

public void setCachingNumber(int noOfRows)
Setter for the number of rows that will be cached. For example, if the number of rows to be cached is 10, the 10 rows which follow the GET Request / SET request for a particular row will be cached. By default all rows will be cached.
Parameters:
noOfRows - The number of rows that will be cached.

getConnection

public java.sql.Connection getConnection()
                                  throws java.lang.Exception
This method gives the Connection instance with the Database
Returns:
The connection instance of the database.