Java实验: 基于CORBA的分布式议程服务 (2)

1.1  关键代码

绑定远程对象:

 

public static void main(String[] args) {

       try {

           // 8步  : 初始化---根---策略----子POA---伺服---激活---激活---监听

           //初始化

           org.omg.CORBA.ORB orb=org.omg.CORBA.ORB.init(args, null);

           

           //根

           org.omg.PortableServer.POA rootPOA=

POAHelper.narrow(orb.resolve_initial_references("RootPOA"));

           

           //策略

           org.omg.CORBA.Policy[] policies=

{rootPOA.create_lifespan_policy(

LifespanPolicyValue.PERSISTENT)};

           

           //有 根和策略 创建子POA

           org.omg.PortableServer.POA agendaPOA=

rootPOA.create_POA("AgendaPOA", 

rootPOA.the_POAManager(), policies);

           

           //伺服

           AgendaCORBAImpl agendaServant=new AgendaCORBAImpl();

           

           //激活 伺服

           agendaPOA.activate_object_with_id(

"AgendaManager".getBytes(), agendaServant);

           

           //激活 POA管理器

           rootPOA.the_POAManager().activate();

            

           System.out.println("Server has ready ...");

           //循环监听

           orb.run();

       } catch (Exception e) {

           e.printStackTrace();

       }

}

 

 

 

 

 

 

获取远程对象

private static IAgenda getAgenda(String[] args) {

       try {

           //初始化

           org.omg.CORBA.ORB orb=org.omg.CORBA.ORB.init(args,null);

           //查找

           IAgenda agenda=

IAgendaHelper.bind(

orb,"/AgendaPOA","AgendaManager".getBytes());

           return agenda;

       } catch (Exception e) {

           e.printStackTrace();

           return null;

       }

    }

  

 

获取数据库连接:

DBConnectionFacade.java

/**

* Gets the database connection from current active connection provider. 

* @return the connection

*/

public static Connection getConnection() throws DBConnectionException{

       if (connProvider==null) {

           synchronized (initLock) {

              if (connProvider==null) {                                                     resourceBundleUtil=

ResourceBundleUtil.getResourceBundleUtil(Lables);

                  String providerClassName=

resourceBundleUtil.getString("provider.calss");

                  if (providerClassName!=null

&&providerClassName.length()==0) {

                     try {

                         Class providerClass=

Class.forName(providerClassName);

                      connProvider=(DBConnectionProvider)

providerClass.newInstance();//动态装载

                     } catch (Exception e) {}//这里删去处理异常的代码

                  }

                  else {

connProvider = new DBConnectionDefaultPool(resourceBundleUtil);

                  }

                  connProvider.initProvider();

              }

           }

       }

return Connection conn=connProvider.getConnection();//这里删去处理异常的代码

}

 

 

 

 

接口DBConnectionProvider的默认实现类DBConnectionDefaultPool.java

/**
  * initial the connection pool.
  * 
  * @see Database.DBConnectionProvider#initProvider()
  */
 public void initProvider() throws DBConnectionException {
  // set Lock to prevent a connection be returned.
  synchronized (initLock) {
   // Get properties
   String driver = resourceBundleUtil.getString("driver");
   String server = resourceBundleUtil.getString("server");
   String username = resourceBundleUtil.getString("username");
   String password = resourceBundleUtil.getString("password");

   // System.out.println(driver+ server+ username+password);

   int minConnections = 0, maxConnections = 0;
   double connectionTimeOut = 0.0;
   try {
    minConnections = Integer.parseInt(resourceBundleUtil.getString("minConnections"));
    maxConnections = Integer.parseInt(resourceBundleUtil
      .getString("maxConnections"));
    connectionTimeOut = Double.parseDouble(resourceBundleUtil
      .getString("connectionTimeOut"));
   } catch (Exception e) {
    throw new ResourceBundleNotFoundException(
      "Could not parse default pool properties. "
        + "Make sure the values exist and are correct.",
      e);
   }
   String logPath = resourceBundleUtil.getString("logPath");

   try {
    connectionPool = new ConnectionPool(driver, server, username,
      password, minConnections, maxConnections, logPath,
      connectionTimeOut);
   } catch (IOException e) {
    // "Error starting DbConnectionDefaultPool: "
    // throw new ConnectionPoolInitPool("Cannt found properties");
    e.printStackTrace();
   }
  }
 }


 

    public Connection getConnection() throws DBConnectionException {

       if (connectionPool == null) {

           // Block until the init has been done.

           synchronized (initLock) {// waitting for init.

              if (connectionPool == null) {

                  // Something has gone wrong.

                  return null;

              
}

 

 

你可能感兴趣的:(java)