// Home | Go Back //
package agent;
import java.io.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.Properties;
import com.ireasoning.protocol.snmp.DefaultAgentConfig;
import com.ireasoning.protocol.snmp.ProxyEntry;
import com.ireasoning.util.Logger;
public class DbConfig extends DefaultAgentConfig
{
public static final String TABLE_PROPERTIES = "properties";
public static final String TABLE_TRAPSINK = "trapsink";
public static final String TABLE_SNMPV3_TRAPSINK = "snmpv3trapsink";
public static final String TABLE_PROXY = "proxy";
public static final String TABLE_TRAP_PROXY = "trapproxy";
public static final String TABLE_USER = "user";
public static final String TABLE_GROUP = "v3group";
public static final String TABLE_VIEW = "view";
public static final String DB_URL = "jdbc:mysql:///test";
public static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private Connection conn = null;
private Statement stmt = null;
public DbConfig () throws Exception
{
super();
init();
}
private void init() throws Exception
{
Class.forName( DB_DRIVER ).newInstance();
this.conn = DriverManager.getConnection( DB_URL );
this.stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
}
private Map[] selectTable( String tableName )
{
ResultSet rs = null;
try
{
rs = stmt.executeQuery( "SELECT * from " + tableName );
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
ArrayList maps = new ArrayList();
while ( rs.next() )
{
Map map = new HashMap();
for ( int i = 1; i <= columnCount ; i++ )
{
String str = rs.getString( i );
if(str == null) str = "";
str = str.trim();
String colname = rsmd.getColumnName(i);
if(colname.indexOf("_") > 0)
{
colname = colname.replace('_', '.');
}
map.put(colname, str );
}
maps.add(map);
}
rs.close();
if(maps.size() == 0) return null;
Map[] ret = new Map[maps.size()];
for (int i = 0; i < ret.length ; i++)
{
ret[i] = (Map) maps.get(i);
}
return ret;
}
catch ( SQLException e )
{
Logger.error( e );
}
return null;
}
private boolean insertRow(String tableName, Map pairs)
{
ResultSet rs = null;
try
{
if(pairs == null || pairs.size() == 0) return false;
String columns = "";
String values = "";
Set s = pairs.entrySet();
Iterator it = s.iterator();
boolean isFirst = true;
while(it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next();
if(!isFirst)
{
columns += " , ";
values += " , ";
}
columns += entry.getKey();
values += "'" + entry.getValue() + "'";
isFirst = false;
}
int updateCount = stmt.executeUpdate( "INSERT INTO " + tableName + " " +
columns + " values (" + values + " )");
if(updateCount == 1) return true;
}
catch ( SQLException e )
{
Logger.error( e );
}
return false;
}
private boolean removeRow(String tableName, Map pairs)
{
ResultSet rs = null;
try
{
if(pairs == null || pairs.size() == 0) return false;
String conditions = "";
Set s = pairs.entrySet();
Iterator it = s.iterator();
boolean isFirst = true;
while(it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next();
if(!isFirst)
{
conditions += " AND ";
}
conditions += entry.getKey() + "= '" + entry.getValue() + "'";
isFirst = false;
}
int updateCount = stmt.executeUpdate( "DELETE FROM " + tableName
+ " WHERE " + conditions);
if(updateCount >= 1) return true;
}
catch ( SQLException e )
{
Logger.error( e );
}
return false;
}
public void close()
{
if ( this.stmt != null )
{
try
{
this.stmt.close();
}
catch ( SQLException SQLE )
{
}
stmt = null;
}
if ( this.conn != null )
{
try
{
this.conn.close();
}
catch ( SQLException SQLE )
{
}
conn = null;
}
}
protected boolean setAttributeValue(String tableName, Map keyvals, String attributeName, String attributeValue)
{
ResultSet rs = null;
try
{
String conditions = "";
if(keyvals != null && keyvals.size() > 0)
{
Set s = keyvals.entrySet();
Iterator it = s.iterator();
boolean isFirst = true;
while(it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next();
if(!isFirst)
{
conditions += " AND ";
}
conditions += entry.getKey() + "= '" + entry.getValue() + "'";
isFirst = false;
}
}
if(attributeName.indexOf(".") > 0)
{
attributeName = attributeName.replace('.', '_');
}
String sql = "UPDATE " + tableName +
" SET " + attributeName + "='" + attributeValue + "'" ;
if(conditions.length() > 0)
{
sql += " WHERE " + conditions;
}
int updateCount = stmt.executeUpdate( sql );
if(updateCount >= 1) return true;
}
catch ( SQLException e )
{
Logger.error( e );
}
return false;
}
protected Map doGetPropertiesProps()
{
return selectTable(TABLE_PROPERTIES)[0];
}
protected boolean doAddTrapSink(Map attributes)
{
return insertRow(TABLE_TRAPSINK, attributes);
}
protected boolean doRemoveTrapSink(String trapSinkHostName, int trapSinkPort, int trapSinkVersion)
{
HashMap map = new HashMap();
map.put("port", "" + trapSinkPort);
map.put("version", "" + trapSinkVersion);
return removeRow(TABLE_TRAPSINK, map);
}
public Map[] getViews()
{
return selectTable(TABLE_VIEW);
}
protected boolean doAddView(Map attrs)
{
return insertRow(TABLE_VIEW, attrs);
}
protected boolean doRemoveView(String viewName)
{
Map map = new HashMap();
map.put("name", viewName);
return removeRow(TABLE_VIEW, map);
}
public Map[] getGroups()
{
return selectTable(TABLE_GROUP);
}
protected boolean doAddGroup(Map attrs)
{
return insertRow(TABLE_GROUP, attrs);
}
protected boolean doRemoveGroup(String groupName)
{
Map map = new HashMap();
map.put("name", groupName);
return removeRow(TABLE_GROUP, map);
}
protected boolean doAddProxy(ProxyEntry entry)
{
return insertRow(TABLE_PROXY, entry.getAttributes());
}
protected boolean doRemoveProxy(ProxyEntry entry)
{
return removeRow(TABLE_GROUP, entry.getAttributes());
}
public Map[] getUsers()
{
return selectTable(TABLE_USER);
}
protected Map[] doGetTrapSinkProps()
{
return selectTable(TABLE_TRAPSINK);
}
protected Map[] doGetSnmpV3TrapSinkProps()
{
return selectTable(TABLE_SNMPV3_TRAPSINK);
}
protected Map [] doGetProxyProps()
{
return selectTable(TABLE_PROXY);
}
protected Map doGetTrapProxyProps()
{
Map [] ret = selectTable(TABLE_TRAP_PROXY);
if(ret == null || ret.length == 0) return null;
else return ret[0];
}
protected boolean doAddUser(Map attrs)
{
return insertRow(TABLE_USER, attrs);
}
protected boolean doRemoveUser(String userName)
{
Map map = new HashMap();
map.put("name", userName);
return removeRow(TABLE_USER, map);
}
protected void doSave() throws IOException
{
}
protected void doReloadConfig() throws IOException
{
}
}