📄 dbcomponentmanager.java
字号:
/*
* DbComponentManager.java
*
* Created on 2001年7月13日, 下午2:24
*/
package com.gs.db.dbimp;
import com.gs.db.*;
import java.sql.*;
import java.util.*;
import java.io.*;
/**
*
* @author administrator
* @version
*/
public class DbComponentManager implements com.gs.db.ComponentManager {
public static final String GET_COMPONENTS =
"SELECT * FROM iofficecomponent ORDER BY weight DESC";
public static final String INSERT_COMPONENT =
"INSERT INTO iofficecomponent( componentname, agentclass, entryurl,displayname,weight ,desktop ,status, isdefault)"
+"VALUES(?,?,?,?,?,?,?,?)";
private DbIofficeFactory factory;
ArrayList compList = new ArrayList();
/** Creates new DbComponentManager */
public DbComponentManager(DbIofficeFactory factory) {
this.factory = factory;
loadComponents();
}
/**
* Register a new component into Inter-Office System
* @param componentName the name of the component, must be unique
* @param componentAgentClassName the name of the component agent class
* @param entryURL the URL used to entry the component services
* @param weight the importance of the component
*
* @throw UnauthorizedException if not authorized to do so,
* @ throw ComponentRegistrationException if registration fails
*/
// "INSERT INTO iofficeComponent( componentName, agentClass, entryURL,displayName,weight ,desktop ,status)"
// +"VALUES(?,?,?,?,?,?,?)";
public IofficeComponent registerComponent(String componentName,String componentAgentClassName,
String entryURL, String displayName, int weight, int desktop)
throws UnauthorizedException, ComponentRegistrationException
{
//try to install the component
IofficeComponentAgent agent = null;
try {
Class ac = Class.forName(componentAgentClassName);
agent = (IofficeComponentAgent)ac.newInstance();
}
catch ( Exception e )
{
//throw new ComponentRegistrationException("Cannot instantiate agent class");
}
//call the install method of the agent
if ( agent != null )
{
try {
agent.onInstall();
}
catch ( ComponentAgentException cae )
{
System.err.println( "Exception in DbComponentManager.java registerComponent() "+cae );
throw new ComponentRegistrationException("Agent::onInstall() fails");
}
}
//insert component into database table
IofficeComponent cp = null;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_COMPONENT);
pstmt.setString(1,componentName);
pstmt.setString(2,componentAgentClassName);
pstmt.setString(3,entryURL);
pstmt.setString(4,displayName);
pstmt.setInt(5,weight);
pstmt.setInt(6,desktop);
pstmt.setInt(7, 0);
pstmt.setInt(8, 0 ); // not defaultly accessible by default
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
sqle.printStackTrace();
throw new ComponentRegistrationException("cannot insert record to component table");
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
//new a IofficeComponent object and the insert it to listComp -- which is order by weight
cp = new DbIofficeComponent( componentName,
displayName, entryURL, componentAgentClassName,
0, weight, desktop, 0);
int i;
for ( i=0; i<compList.size()-1; i++)
{
if ( i+1 >= compList.size()) break;
IofficeComponent c1 = (IofficeComponent) compList.get(i);
IofficeComponent c2 = (IofficeComponent) compList.get(i+1);
if ( c1.getWeight() > weight && weight >= c2.getWeight() )
{
break;
}
}
// i is the place that I should insert the IofficeComponent into the list
compList.add( i, cp );
//return the new IofficeComponent
return cp;
}
/**
* Delete a new component off Inter-Office System
* @param componentName the name of the component, must be unique
* might throw UnauthorizedException if not authorized to do so,
* might throw ComponentNotFoundException if name not found
*/
//public void unregisterComponent(IofficeComponent cp) throws UnauthorizedException, ComponentNotFoundException {
//}
/**
* Return all the component names
*/
public IofficeComponent getComponent(String componentName) throws ComponentNotFoundException {
IofficeComponent cp = null;
for ( int i=0; i< compList.size(); i++)
{
cp = (IofficeComponent) compList.get(i );
if ( cp.getName().equals( componentName ) ) return cp;
}
throw new ComponentNotFoundException("No Such IofficeComponent"+componentName );
}
public int getComponentCount()
{
return compList.size();
}
public Iterator getComponents()
{
return compList.iterator();
}
private void loadComponents()
{
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_COMPONENTS);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String name = rs.getString("componentname");
String agentClass = rs.getString("agentclass");
String entryURL = rs.getString("entryurl");
String displayName = rs.getString("displayname");
int weight = rs.getInt("weight");
int desktop = rs.getInt("desktop");
int status = rs.getInt("status");
int isDefault = rs.getInt("isdefault");
this.compList.add( new DbIofficeComponent( name,
displayName, entryURL, agentClass,
status, weight, desktop, isDefault) );
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -