⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 topologymanagerbean.java

📁 Sun公司Dream项目
💻 JAVA
字号:
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 *
 * You can obtain a copy of the license at
 * http://www.opensource.org/licenses/cddl1.php
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * http://www.opensource.org/licenses/cddl1.php.  If 
 * applicable, add the following below this CDDL HEADER, 
 * with the fields enclosed by brackets "[]" replaced 
 * with your own identifying information: 
 * Portions Copyright [yyyy]
 * [name of copyright owner]
 */ 

/*
 * $(@)TopologyManagerBean.java $Revision: 1.1.1.1 $ $Date: 2006/03/15 13:12:10 $
 * 
 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
 */
/**

 *

 * @version: 1.0

 * @date: Jan 28, 2003

 * 

 * Modified: Kelly.Kishore@sun.com

 *

 */

package com.sun.sjc.idtv.vod.server.topologymanager;



import java.util.*;

import java.sql.*;

import javax.ejb.*;

import javax.sql.*;

import javax.naming.*;

import java.rmi.*;



import com.sun.sjc.idtv.vod.shared.data.*;



/**

 * Implementation bean for the <code>TopologyManager</code> interface, <code>TopologyManager</code>

 * defines all possible business methods for the bean.

 *

 * @see TopologyManager

 * @see TopologyManagerHome

 */

public class TopologyManagerBean implements SessionBean {



    private  javax.ejb.SessionContext m_ctx = null; 

    public static final int QUERYTIMEOUT = 60;

    private DataSource dataSource;

   

    /**

     * Sets the session context. Required by EJB spec.

     * @param ctx A SessionContext object.

     */

    public void setSessionContext(javax.ejb.SessionContext ctx) { 

        m_ctx = ctx; 

	try {

	    InitialContext ic = new InitialContext();

	    //dataSource = (DataSource) ic.lookup("java:comp/env/jdbc/vod");

	    dataSource = (DataSource) ic.lookup("jdbc/sample");

	} catch (Exception ex) {

	    throw new EJBException("Unable to connect to database. " +

				   ex.getMessage());

	}

    } 



    /**

     * Creates a bean. Required by EJB spec.

     * @exception throws CreateException.

     */

    public void ejbCreate() throws java.rmi.RemoteException, javax.ejb.CreateException { 

    } 



    /**

     * Removes the bean. Required by EJB spec.

     */

    public void ejbRemove() { 

    } 



    /**

     * Loads the state of the bean from secondary storage. Required by EJB spec.

     */

    public void ejbActivate() { 

    } 



    /**

     * Serializes the state of the bean to secondary storage. Required by EJB spec.

     */

    public void ejbPassivate() { 

    } 



    /**

     * Required by EJB spec.

     */

    public void TopologyManager() { 

    } 





    /**

     * Allocate QAM resources for a particular CPE

     * @param cpeId customer premise equiment id

     * @return qam ip, port, tsid, rf channel

     * @exception RemoteException, SQLException

     */

     

    public QamResource allocate(String cpeId) throws java.rmi.RemoteException, SQLException {



       // get db connection from pool

       Connection conn = dataSource.getConnection();



       // get the next free port from reliable dataset (no live cache)

       PreparedStatement stmt = conn.prepareStatement("SELECT * FROM qamresources WHERE free = ? ORDER BY qamport"); // FOR UPDATE



       stmt.setQueryTimeout(QUERYTIMEOUT);

       stmt.setInt(1, 1);

       ResultSet rs = stmt.executeQuery();

       QamResource resource = null;

       if (rs.next()) {

	   resource = new QamResource(cpeId, 

				      rs.getString(2),

				      rs.getString(3),

				      rs.getString(4),

				      rs.getString(5));

       }

       rs.close();

       stmt.close();



       if (resource != null) {

	   stmt = conn.prepareStatement("UPDATE qamresources SET cpeid = ?, free = ?, timest = ? WHERE qamip = ? AND qamport = ?");

	   

	   Timestamp now = new Timestamp(System.currentTimeMillis());

	   stmt.setQueryTimeout(QUERYTIMEOUT);

	   stmt.setString(1, cpeId);

	   stmt.setInt(2, 0);

	   stmt.setTimestamp(3, now);

	   stmt.setString(4, resource.getIP());

	   stmt.setString(5, resource.getPort());

	   stmt.executeUpdate();

	   stmt.close();

       }

       conn.close();



       System.out.println("TopologyManager.allocate: reserving resource fpr cpeid " + cpeId + ": " + resource);

       return resource;

    }



    /**

     * Free QAM resources for a particular CPE

     * @param cpeId customer premise equiment id

     * @return true if ok, false if an exception occured

     * @exception RemoteException

     */

     

    public boolean release(String cpeId) throws java.rmi.RemoteException, SQLException {



	boolean status = true;



       // get db connection from pool

       Connection conn = dataSource.getConnection();



       // free the resource allocated to this cpeid

       PreparedStatement stmt = conn.prepareStatement("UPDATE qamresources SET cpeid = NULL, free = ?, timest = NULL WHERE cpeId = ?");

	   

       stmt.setQueryTimeout(QUERYTIMEOUT);

       stmt.setBoolean(1, true);

       stmt.setString(2, cpeId);

       stmt.executeUpdate();

       stmt.close();

       conn.close();



       System.out.println("TopologyManager.release: releasing resource for " + cpeId);

       return status;

    }



    /* TODO: merge allocateIpResource and allocate together to return a

     * ResourceObject instead of multiple different objects. */



    /**

     * 

     * Allocate IP resource for a particular CPE

     * @param cpeId (customer premise equipment id)

     * @return IPResource: server ip

     * @exception RemoteException

     */

    public IpResource allocateIpResource(String cpeId, String serverIp) throws RemoteException, SQLException {

       

       // get db connection from pool

       Connection conn = dataSource.getConnection();



       // get the next free port from reliable dataset (no live cache)

       PreparedStatement stmt = conn.prepareStatement("SELECT * FROM ipresourcesvod WHERE free = ? AND serverip = ? ORDER BY slotnum"); // FOR UPDATE



       stmt.setQueryTimeout(QUERYTIMEOUT);

       stmt.setInt(1, 1);

       stmt.setString(2,serverIp);

       ResultSet rs = stmt.executeQuery();

       IpResource ipResource = null;

       String slotnum = null;

       System.out.println("allocateIpResource: cpeId="+cpeId+" serverIp="+serverIp);

       if (rs.next()) {

	       ipResource = new IpResource(cpeId, serverIp);

           slotnum = rs.getString(1);

       }

       rs.close();

       stmt.close();



       if (ipResource != null) {

	   stmt = conn.prepareStatement("UPDATE ipresourcesvod SET cpeid = ?, free = ?, timest = ? WHERE slotnum = ? AND serverip = ?");

	   

	   Timestamp now = new Timestamp(System.currentTimeMillis());

	   stmt.setQueryTimeout(QUERYTIMEOUT);

	   stmt.setString(1, cpeId);

	   stmt.setInt(2, 0);

	   stmt.setTimestamp(3, now);

	   stmt.setString(4, slotnum);

       stmt.setString(5, serverIp);

	   stmt.executeUpdate();

	   stmt.close();

       }

       conn.close();



       System.out.println("TopologyManager.allocate: reserving ipresource for cpeid " + cpeId + ": " + ipResource);

       return ipResource;

    }

          

    /**

     * 

     * Releasing IP resource for a particular CPE

     * @param cpeId (customer premise equipment id)

     * @return boolean

     * @exception RemoteException

     */

    public boolean releaseIpResource(String cpeId) throws RemoteException, SQLException {

            

	   boolean status = true;



       // get db connection from pool

       Connection conn = dataSource.getConnection();



       // free the resource allocated to this cpeid

       PreparedStatement stmt = conn.prepareStatement("UPDATE ipresourcesvod SET cpeid = NULL, free = ?, timest = NULL WHERE cpeId = ?");

	   

       stmt.setQueryTimeout(QUERYTIMEOUT);

       stmt.setInt(1, 1);

       stmt.setString(2, cpeId);

       stmt.executeUpdate();

       stmt.close();

       conn.close();



       System.out.println("TopologyManager.release: releasing resource for " + cpeId);

       return status;

    }



} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -