📄 remoteindyserver.java~1~
字号:
/*
* @(#)RemoteIndyServer
*
* Copyright (c) 1998 Karl Moss. All Rights Reserved.
*
* You may study, use, modify, and distribute this software for any
* purpose provided that this copyright notice appears in all copies.
*
* This software is provided WITHOUT WARRANTY either expressed or
* implied.
*
* @author Karl Moss
* @version 1.0
* @date 17Apr98
*
*/
package javaservlets.tunnel;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javaservlets.tunnel.server.*;
import javaservlets.db.*;
import javaservlets.jdbc.*;
import javaservlets.timer.*;
import java.util.List;
import java.util.ArrayList;
/**
* <p>This class implements the server for tunneling
* remote Indy method calls
*/
public class RemoteIndyServer
extends TunnelServer
{ List array=new ArrayList();
/**
* <p>Creates a new instance of the server object.
*
* @return Instance of the server object
*/
public Object _getNewInstance()
throws ServletException
{
return new Indy();
}
/**
* <p>Invokes the method for the ordinal given. If the method
* throws an exception it will be sent to the client.
*
* @param Object Server object
* @param ordinal Method ordinal
* @param in Input stream to read additional parameters
* @param out Output stream to write return values
*/
public void _invokeMethod(Object serverObject, int ordinal,
DataInput in, DataOutput out)
throws Exception
{ // The JDBC Connection object
Connection con = null;
// The JDBC Statement object
Statement stmt = null;
ServletConfig config = getServletConfig();
ServletContext context = config.getServletContext();
Object o = context.getAttribute(ConnectionServlet_21.KEY);
if (o == null) {
throw new ServletException("ConnectionServlet not started");
}
ConnectionServlet_21 conServlet = (ConnectionServlet_21) o;
// Cast the server object
Indy indy = (Indy) serverObject;
// Cast the input/output streams
ObjectInputStream objectIn = (ObjectInputStream) in;
ObjectOutputStream objectOut = (ObjectOutputStream) out;
// Evaluate the ordinal
switch (ordinal) {
case 0: // connect
System.out.println("case 0");
boolean b0 = indy.connect();
objectOut.writeObject(new Boolean(b0));
break;
case 1: // close
System.out.println("case 1");
indy.close();
break;
case 2: // query
System.out.println("case 2");
//////////////////////////////////////////////////
// Integer i2 = (Integer) objectIn.readObject();
// There are problems!!!!
IndyRecord record[] = indy.query();
System.out.println("at RemoteIndyServer:"+record[0].taskID);
objectOut.writeObject(record);
break;
case 3: // delete
System.out.println("case 3");
String s = (String) objectIn.readObject();
System.out.println("I want to delete: "+s);
indy.delete(s);
break;
case 4: // insert
System.out.println("case 4");
IndyRecord records = (IndyRecord)objectIn.readObject();
System.out.println("I want to insert: "+records.taskID);
indy.insert(records);
break;
case 5: // update
System.out.println("case 5");
IndyRecord re = (IndyRecord)objectIn.readObject();
System.out.println("I want to update: "+re.taskID);
indy.update(re);
break;
////////////////////////////////////////////////////////////
// Get the ConnectionServlet that holds the
// connection pool
case 6://schedule
List list=new ArrayList();
String[] output=new String[50];
try {
// Get an available connection from our connection pool
con = conServlet.getConnection();
/////////////////////////////////////////////////////////////
try
{
array.clear();
}
catch(UnsupportedOperationException exc)
{ System.out.println("can't clear the list!!!");
}
record[] res=find();
insert(res);
while(!array.isEmpty())
{ String id=((record)array.get(0)).taskID;
String nextid[]=((record)array.get(0)).nextID;
list.add(id);
delete(nextid);
array.remove(0);
find();
}
System.out.println("toop include: ");
for(int i=0;i<list.size();i++)
{ System.out.println((String)list.get(i));
output[i]=(String)list.get(i);
}
String query="update toop set isnew=true,newdu=0";
stmt=con.createStatement();
stmt.executeUpdate(query);
}
catch (Exception ex) {
}
finally {
try {
// Always close properly
if (stmt != null) {
stmt.close();
}
if (con != null) {
// Put the connection back into the pool
conServlet.close(con);
}
}
catch (Exception ex) {
// Ignore any errors here
}
}
if(output.length!=0)
objectOut.writeObject(output);
break;
/////////////////////////////////////////////////////////////
default:
throw new Exception("Invalid ordinal: " + ordinal);
}
}
private record[] find()
{ record[] re;
List rec=new ArrayList();
int locate=-1;
// The JDBC Connection object
Connection con = null;
Connection con1=null;
// The JDBC Statement object
Statement stmt = null;
Statement stmt1=null;
ServletConfig config = getServletConfig();
ServletContext context = config.getServletContext();
Object o = context.getAttribute(ConnectionServlet_21.KEY);
if (o == null) {
//throw new ServletException("ConnectionServlet not started");
}
ConnectionServlet_21 conServlet = (ConnectionServlet_21) o;
// The JDBC ResultSet object
ResultSet rs = null;
try {
// Get an available connection from our connection pool
con = conServlet.getConnection();
con1=conServlet.getConnection();
// Create a statement object that we can execute queries
// with
stmt = con.createStatement();
stmt1=con1.createStatement();
String query1="select taskID,nextID from toop where indu=newdu and isnew=true";
rs=stmt.executeQuery(query1);
while(rs.next())
{
String a=rs.getString(1);
String b=rs.getString(2);
System.out.println("in find( ) "+a);
System.out.println("in find( ) "+b);
boolean isinsert=false;
for(int i=0;i<rec.size();i++)
{if(a==((record)rec.get(i)).taskID)
{isinsert=true;
locate=i;
}
}
if(isinsert)
{ //int l=(((record)rec.get(locate)).nextID).length;
//((record)rec.get(locate)).nextID[l]=b;
System.out.println("I come here2 ");
}
else
{ record c=new record();
c.taskID=a;
c.nextID[0]=b;
rec.add(c);
System.out.println("I come here3 ");
}
}
System.out.println("I come here4 ");
String query2="update toop set isnew=false where taskID in"+
"(select taskID from toop where indu=newdu and isnew=true)";
System.out.print("execute query2");
stmt1.executeUpdate(query2);
}
catch (Exception ex)
{ System.out.println("I come here5 ");
System.out.println("find() mathod error: "+ex.toString());
}
finally {
try {
// Always close properly
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if(stmt1!=null)
{stmt1.close();}
if (con != null) {
// Put the connection back into the pool
conServlet.close(con);
}
if (con1!= null) {
// Put the connection back into the pool
conServlet.close(con1);
}
}
catch (Exception ex) {
// Ignore any errors here
}
}
record copy[]=new record[rec.size()];
for(int i=0;i<rec.size();i++)
{ copy[i]=(record)rec.get(i); }
re=copy;
return re;
}
////////////////////////////////////////////////////////////////
private void insert(record[] re)
{ for(int i=0;i<re.length;i++)
{ array.add(re[i]);
}
}
////////////////////////////////////////////////////////////////
private void delete(String[] nextid)
{ // The JDBC Connection object
Connection con = null;
ServletConfig config = getServletConfig();
ServletContext context = config.getServletContext();
Object o = context.getAttribute(ConnectionServlet_21.KEY);
if (o == null) {
//throw new ServletException("ConnectionServlet not started");
}
ConnectionServlet_21 conServlet = (ConnectionServlet_21) o;
// The JDBC Statement object
PreparedStatement stmt = null;
try {
// Get an available connection from our connection pool
con = conServlet.getConnection();
// Create a statement object that we can execute queries
// with
try {String query="update toop set newdu=newdu+1 "+
"where taskID=?";
stmt = con.prepareStatement(query );
for(int i=0;i<nextid.length;i++)
{ stmt.setString(1,nextid[i]);
stmt.executeUpdate();
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
catch (Exception ex)
{
System.out.println("delete() mathod error: "+ex.toString());
}
finally {
try {
// Always close properly
if (stmt != null) {
stmt.close();
}
if (con != null) {
// Put the connection back into the pool
conServlet.close(con);
}
}
catch (Exception ex) {
// Ignore any errors here
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -