oracle.otnsamples.cmsxdb.admin.CMSAdminBean (Java2HTML)
package oracle.otnsamples.cmsxdb.admin;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.CallableStatement;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Collection;
import java.util.StringTokenizer;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import oracle.xdb.XMLType;
import oracle.jdbc.OracleTypes;
import oracle.AQ.AQQueue;
import oracle.AQ.AQMessage;
import oracle.AQ.AQSession;
import oracle.AQ.AQException;
import oracle.AQ.AQOracleQueue;
import oracle.AQ.AQObjectPayload;
import oracle.AQ.AQDriverManager;
import oracle.AQ.AQEnqueueOption;
import oracle.AQ.AQDequeueOption;
import oracle.otnsamples.cmsxdb.ConnParams;
import oracle.otnsamples.cmsxdb.exception.CMSAdminException;
public class CMSAdminBean {
private DataSource datasource = null;
private Externalizer externalizer = null;
private static final int MAXLIST = 10;
private byte[][] privMsgId = new byte[MAXLIST][16];
private byte[][] externMsgId = new byte[MAXLIST][16];
public CMSAdminBean()
throws CMSAdminException {
try {
Class.forName( "oracle.AQ.AQOracleDriver" );
externalizer = new Externalizer( ConnParams.siteRootPath );
} catch ( ClassNotFoundException clsNtFdEx ) {
throw new CMSAdminException( "Error Loading AQDriver :" + clsNtFdEx.toString( ) );
}
}
public PrivilegeRequest enqueuePrivRequest( HttpServletRequest req )
throws CMSAdminException {
String resPath = req.getParameter("ResourcePath");
String[] privReq = req.getParameterValues("Privilege");
String user = req.getParameter("User");
Connection conn = null;
PrivilegeRequest privreq = null;
try {
conn = this.getConnection( );
privreq = new PrivilegeRequest(user, resPath, true,privReq, null, null );
this.enqueue("privilege_queue", privreq.toXMLType(conn), conn );
} finally {
try {
if( conn != null ) conn.close();
} catch(SQLException sqlEx) {
throw new CMSAdminException(" Error closing connection : "+sqlEx.getMessage() );
}
}
return privreq;
}
public Collection processPrivilegeRequest( HttpServletRequest req )
throws CMSAdminException, SQLException {
Collection processedRequest = new ArrayList( );
Connection conn = null;
CallableStatement cstmt = null;
String[] messageId = req.getParameterValues( "MessageId" );
String[] principal = req.getParameterValues( "Principal" );
String[] resPath = req.getParameterValues( "ResourcePath" );
String[] grant = req.getParameterValues( "Grant" );
String[] privileges = req.getParameterValues( "Privileges" );
boolean accept = false;
boolean isGrant = false;
String status = null;
String[] priv = null;
if( messageId != null ) {
try {
conn = this.getConnection( );
cstmt = conn.prepareCall( " begin ? := OTNCMS_ADMIN.Process_Message( ?, ? ); end; " );
for( int ctr = 0; ctr < messageId.length; ctr++ ) {
String grantRequest = req.getParameter( messageId[ctr] );
isGrant = grant[ctr].equals( "Y" )?true:false;
int msgidx = Integer.parseInt(messageId[ctr]);
if( grantRequest != null ) {
cstmt.registerOutParameter( 1, OracleTypes.INTEGER );
cstmt.setBytes( 2, privMsgId[msgidx] );
cstmt.setString(3, grantRequest );
try {
cstmt.execute( );
int result = cstmt.getInt(1);
if( result > 0 ) {
if( isGrant ) {
status = "Granted";
} else {
status = "Denied";
}
} else {
status = " Could not change privilege, user might already have the requested privilege ";
}
} catch ( SQLException sqlEx ) {
status = "ERROR";
System.out.println(" Error executing processing privilege : " + sqlEx.toString( ));
}
StringTokenizer tokens = new StringTokenizer( privileges[ctr] );
priv = new String[ tokens.countTokens( ) ];
for(int i = 0; i < priv.length; i++ ) {
priv[i] = tokens.nextToken();
}
processedRequest.add( new PrivilegeRequest( principal[ctr], resPath[ctr],
true, priv, null, status ) );
}
}
} catch( SQLException sqlEx ) {
System.out.println(" SQL Error while processing privileges : "+
sqlEx.toString( ));
} catch( Exception ex ) {
System.out.println(" Error while processing privileges : "+
ex.toString( ));
} finally {
try {
if( cstmt != null ) cstmt.close();
if( conn != null ) conn.close();
} catch(Exception ex) {
System.out.println(" Error closing Statement/Connection : "+ex.toString());
}
}
}
return processedRequest;
}
public Collection processExternRequest( HttpServletRequest req )
throws CMSAdminException, SQLException {
Collection processedRequest = new ArrayList( );
String[] messageId = req.getParameterValues( "MessageId" );
String[] grant = req.getParameterValues( "Grant" );
boolean accept = false;
int msgidx = 0;
String status = null;
AQQueue queue = null;
AQSession aqSession = null;
AQMessage message = null;
AQObjectPayload payload = null;
AQDequeueOption dqOption = null;
Connection conn = null;
try {
conn = this.getConnection( );
aqSession = this.getAQSession( conn );
queue = aqSession.getQueue(ConnParams.dbUsername, "extern_queue");
dqOption = new AQDequeueOption();
dqOption.setWaitTime(AQDequeueOption.WAIT_NONE);
if( messageId != null ) {
for( int ctr = 0; ctr < messageId.length; ctr++ ) {
String grantRequest = req.getParameter( messageId[ctr] );
msgidx = Integer.parseInt( messageId[ctr] );
if( grantRequest != null ) {
dqOption.setMessageId( externMsgId[ msgidx ] );
message = ((AQOracleQueue)queue).dequeue(dqOption, XMLType.getORADataFactory() );
payload = message.getObjectPayload();
ExternRequest ereq = new ExternRequest( null, (XMLType) payload.getPayloadData( ) );
if( grantRequest.equalsIgnoreCase("Y") ) {
externalizer.generateFile( ereq.getResourceUrl(), ereq.getXSLLocation() ,
ereq.getContentType() );
status = "Generated";
} else {
status = "Rejected";
}
conn.commit();
ereq.setStatus( status );
processedRequest.add( ereq );
}
}
}
} catch ( AQException aqEx ) {
System.out.println("Error Dequeuing extern requests : " + aqEx.toString( ));
throw new CMSAdminException( "Error Dequeuing extern requests :" + aqEx.getMessage() );
} catch ( Exception ex ) {
System.out.println(" Error during externalization : " + ex.toString());
ex.printStackTrace();
throw new CMSAdminException( "Error during externalization : " + ex.getMessage() );
} finally {
if( aqSession != null ) aqSession.close();
if( conn != null ) conn.close();
}
return processedRequest;
}
public byte[] enqueue( String queueName, XMLType xmlmsg,Connection conn )
throws CMSAdminException {
AQQueue queue = null;
AQMessage message = null;
AQSession aqSession = null;
AQObjectPayload payload = null;
AQEnqueueOption eqOption = null;
byte[] msgid = null;
try {
conn = this.getConnection( );
aqSession = this.getAQSession( conn );
queue = aqSession.getQueue(ConnParams.dbUsername , queueName );
message = queue.createMessage();
payload = message.getObjectPayload();
payload.setPayloadData(xmlmsg);
eqOption = new AQEnqueueOption();
queue.enqueue(eqOption, message);
msgid = message.getMessageId();
conn.commit( );
} catch( AQException aqEx ) {
System.out.println(" AQ Error enqueuing into queue "+queueName+
" : "+aqEx.toString( ));
} catch( SQLException sqlEx ) {
System.out.println(" SQL Error enqueuing into queue "+queueName+
" : "+sqlEx.toString( ));
} finally {
try {
if( aqSession != null ) aqSession.close();
} catch(Exception ex) {
System.out.println(" Error closing AQ Session : "+ex.toString());
}
}
if( msgid == null )
throw new CMSAdminException ( " Error enqueuing message into " + queueName );
return msgid;
}
public Collection getPendingRequests( String queueName )
throws CMSAdminException {
AQSession aqSession = null;
AQQueue reqQueue = null;
AQMessage msg = null;
AQObjectPayload payload = null;
AQDequeueOption dqOption = null;
XMLType requestXML = null;
LinkedList pendingRequests = null;
Connection conn = null;
pendingRequests = new LinkedList( );
try {
conn = this.getConnection( );
aqSession = this.getAQSession( conn );
reqQueue = aqSession.getQueue( ConnParams.dbUsername, queueName );
dqOption = new AQDequeueOption( );
dqOption.setWaitTime( AQDequeueOption.WAIT_NONE );
dqOption.setDequeueMode( AQDequeueOption.DEQUEUE_BROWSE );
dqOption.setNavigationMode( AQDequeueOption.NAVIGATION_FIRST_MESSAGE );
for( int msgcnt = 0; msgcnt < MAXLIST ; msgcnt++) {
try {
msg = ( (AQOracleQueue) reqQueue).dequeue( dqOption, XMLType.getORADataFactory( ) );
} catch (AQException aqEx) {
break;
}
payload = msg.getObjectPayload( );
requestXML = ( XMLType ) payload.getPayloadData( );
if( queueName.equals("privilege_queue")) {
pendingRequests.addLast( new PrivilegeRequest (new String( msg.getMessageId( ) ),requestXML));
privMsgId[msgcnt] = msg.getMessageId( );
} else if( queueName.equals("extern_queue")) {
pendingRequests.addLast( new ExternRequest(new String( msg.getMessageId( )),requestXML));
externMsgId[msgcnt] = msg.getMessageId( );
}
dqOption.setMessageId(null);
dqOption.setNavigationMode(AQDequeueOption.NAVIGATION_NEXT_MESSAGE);
}
} catch( AQException aqEx ) {
System.out.println(" AQ Error browsing queue "+queueName+
" : "+aqEx.toString( ));
} catch( SQLException sqlEx ) {
System.out.println(" SQL Error browsing queue "+queueName+
" : "+sqlEx.toString( ));
} finally {
try {
if( aqSession != null ) aqSession.close();
if( conn != null ) conn.close();
} catch(Exception ex) {
System.out.println("Error closing session/connection : "+ex.toString());
}
}
return pendingRequests;
}
private AQSession getAQSession( Connection conn )
throws CMSAdminException, SQLException {
AQSession aqSession = null;
try {
conn.setAutoCommit( false );
aqSession = AQDriverManager.createAQSession( conn );
} catch ( AQException aqEx ) {
throw new CMSAdminException( "Error creating AQSession: " + aqEx.toString( ) );
}
return aqSession;
}
private Connection getConnection( ) throws CMSAdminException {
Connection conn = null;
try {
if ( datasource == null ) {
datasource = ( DataSource ) new InitialContext( ).lookup( ConnParams.datasourceName );
}
conn = datasource.getConnection( );
} catch ( NamingException nameEx ) {
throw new CMSAdminException( "Error during DataSource lookup :" + nameEx.toString( ) );
} catch ( SQLException sqlEx ) {
throw new CMSAdminException( "Error Connecting to Datasource :" + sqlEx.toString( ) );
}
return conn;
}
}
|