?? rtspstoredsessionenginebean.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]
*/
/*
* $(@)RTSPStoredSessionEngineBean.java $Revision: 1.1.1.1 $ $Date: 2006/03/15 13:12:10 $
*
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
*/
package com.sun.sjc.idtv.vod.server.rtspsession;
import com.sun.sjc.idtv.vod.shared.data.*;
import java.sql.*;
import javax.ejb.*;
import java.rmi.*;
import javax.sql.*;
import javax.naming.*;
import java.util.*;
/**
* Implementation bean for the <code>RTSPStoredSessionEngine</code> interface,
* <code>RTSPStoredSessionEngine</code> defines all possible business methods for
* the bean.
*
* @see RTSPStoredSessionEngine
* @see RTSPStoredSessionEngineHome
*/
/* table information:
SQL> describe rtspsessions
Name Null? Type
----------------------------------------- -------- ----------------------------
USERID NUMBER(38)
MOVIEID VARCHAR2(255)
TIMEST TIMESTAMP(6)
SMPTE VARCHAR2(43)
*/
public class RTSPStoredSessionEngineBean implements SessionBean {
private SessionContext sessionContext = null;
private DataSource dataSource;
/**
* Sets the session context. Required by EJB spec.
* @param sessionContext - a SessionContext object
*/
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
try {
InitialContext ic = new InitialContext();
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 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 RTSPStoredSessionEngine() {
}
/**
* Stores an RTSP session object into the database
* @param rtspSessionObject - information on the RTSP session
* @return void
* @exception RemoteException
* @exception SQLException
*/
public void storeRTSPSession(RTSPSessionObject rtspSessionObject)
throws RemoteException, java.sql.SQLException {
try {
// create database connection and prepare statements
Connection conn = dataSource.getConnection();
PreparedStatement stmt = null;
// find existing sessions
stmt = conn.prepareStatement("select userId from rtspsessions where userId = ? and movieId = ?");
stmt.setQueryTimeout(30);
stmt.setLong(1,Long.parseLong(rtspSessionObject.getUserId()));
stmt.setString(2,rtspSessionObject.getMovieId());
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
// if found, then update the existing session with the current
// rtspsession values
stmt = conn.prepareStatement("update rtspsessions set smpte = ?, timest = ? where userId = ? and movieId = ?");
stmt.setString(1,rtspSessionObject.getSmpte());
stmt.setTimestamp(2,rtspSessionObject.getTimestamp());
stmt.setLong(3,Long.parseLong(rtspSessionObject.getUserId()));
stmt.setString(4,rtspSessionObject.getMovieId());
} else {
// if not found, then insert a new rtspsession
stmt = conn.prepareStatement("insert into rtspsessions values(?,?,?,?)");
stmt.setLong(1,Long.parseLong(rtspSessionObject.getUserId()));
stmt.setString(2,rtspSessionObject.getMovieId());
stmt.setTimestamp(3,rtspSessionObject.getTimestamp());
stmt.setString(4,rtspSessionObject.getSmpte());
}
stmt.executeUpdate();
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Gets all of the stored RTSP sessions from the database for a given user
* @param userId - id of user
* @return rtspSessionObjects - array of RTSPSessionObjects containing all
* of the RTSP sessions of a specified user
* @exception RemoteException
* @exception SQLException
*/
public RTSPSessionObject[] getStoredRTSPSessions(String userId) throws RemoteException,
java.sql.SQLException {
Vector vector = null;
RTSPSessionObject[] rtspSessionObjects = null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// create database connection and prepare statement
conn = dataSource.getConnection();
stmt = conn.prepareStatement("select movieId,timest,smpte from rtspsessions where userId = ?");
stmt.setQueryTimeout(30);
stmt.setLong(1,Long.parseLong(userId));
rs = stmt.executeQuery();
vector = new Vector();
// store all of the RTSPSessionObjects in a vector
while(rs.next()) {
vector.add(new RTSPSessionObject(userId, rs.getString(1), rs.getTimestamp(2), rs.getString(3)));
}
// move all of the RTSPSessionObjects into an array from the vector
rtspSessionObjects = new RTSPSessionObject[vector.size()];
for(int i=0;i<vector.size(); i++) {
rtspSessionObjects[i] = (RTSPSessionObject)vector.elementAt(i);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rs.close();
stmt.close();
conn.close();
return rtspSessionObjects;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -