📄 requestbrokerservlet.java
字号:
/**
* $Log: RequestBrokerServlet.java,v $
* Revision 1.6 2003/02/12 16:54:17 mwulff
* no deals with JndiProperties instead of LookupHandler
*
* Revision 1.5 2003/02/03 13:04:26 mwulff
* changed sequence of Request testing
*
* Revision 1.4 2003/02/01 17:43:43 willaxt
* removed unnecessary instance variables
*
* Revision 1.3 2003/01/31 16:54:38 willaxt
* removed unused SingelThreadModel import
*
* Revision 1.2 2003/01/30 16:31:22 mwulff
* bugfix:
* CompressionHandler is nolonger an instance variable
*
* Revision 1.1 2003/01/16 13:11:41 mwulff
* initial version
*
* Revision 1.10 2003/01/02 12:01:19 mwulff
* no message
*
* Revision 1.9 2003/01/02 11:59:55 mwulff
* no message
*
* Revision 1.8 2002/12/20 18:51:18 mwulff
* no message
*
* Revision 1.7 2002/12/15 16:53:53 mwulff
* no message
*
* Revision 1.6 2002/12/14 22:43:08 mwulff
* no message
*
* Revision 1.5 2002/12/13 20:07:42 mwulff
* no message
*
* Revision 1.4 2002/12/09 18:05:56 mwulff
* added support for ejb finder methods
*
* Revision 1.3 2002/12/05 21:29:55 mwulff
* reengineering of the complete communication component
*
* Revision 1.2 2002/12/01 21:39:27 mwulff
* accomodated to ServerResourceManager that is now implemented as singleton
*
* Revision 1.1 2002/11/22 16:41:50 mwulff
* moved
*
* Revision 1.8 2002/11/21 11:10:30 mwulff
* removed some logging statements
*
* Revision 1.7 2002/11/17 17:52:37 mwulff
* before an object is garbage collected, the RequestBrokerServlet tries to
* invoke a finalize method, if one is existing
*
* Revision 1.6 2002/11/15 18:22:44 mwulff
* - addes support for ejb creation
* - major code reorganisation and adjustments to other changed classes
*
* Revision 1.5 2002/11/08 19:24:32 mwulff
* now deals with remote garbage collection
*
* Revision 1.4 2002/11/07 16:26:38 mwulff
* configuration for jkf and logging is now handled by JKFBroker.java
*
* Revision 1.3 2002/11/05 19:56:21 mwulff
* added a rudimentary support for logging client logs
*
* Revision 1.2 2002/11/03 21:04:08 mwulff
* RequestBrokerServlet now stores the remote objects in a HttpSession
* corresponding to the clients request. The servlet also deals with compressed
* and uncompressed data stream.
*
* Revision 1.1.1.1 2002/10/23 14:13:23 mwulff
* initial checkin
*
*/
package de.fhm.jkf.comm.sv;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.rmi.server.UID;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import de.fhm.jkf.comm.clsv.CompressionHandler;
import de.fhm.jkf.comm.clsv.CreationRequest;
import de.fhm.jkf.comm.clsv.DataResponse;
import de.fhm.jkf.comm.clsv.EJBCreationRequest;
import de.fhm.jkf.comm.clsv.EJBFindByPrimKeyRequest;
import de.fhm.jkf.comm.clsv.EJBFindByXXXRequest;
import de.fhm.jkf.comm.clsv.EJBFindByXXXResponse;
import de.fhm.jkf.comm.clsv.JKFCommunicationException;
import de.fhm.jkf.comm.clsv.KeepAliveRequest;
import de.fhm.jkf.comm.clsv.QueryRequest;
import de.fhm.jkf.comm.clsv.RegisterRequest;
import de.fhm.jkf.comm.clsv.RemoteObjectID;
import de.fhm.jkf.comm.clsv.Request;
import de.fhm.jkf.comm.clsv.Response;
import de.fhm.jkf.resource.sv.JKFServer;
import de.fhm.jkf.resource.sv.RemoteManager;
import de.fhm.jkf.resource.sv.ServerLoggingManager;
import de.fhm.jkf.resource.sv.ServerResourceManager;
import de.fhm.jkf.utils.clsv.ReflectionUtils;
/**
* @author marten wulff
*
* <br><br><center><table border="1" width="80%"><hr>
* <strong><a href="http://jkf.sourceforge.net">The JKF Project</a></strong>
* <p>
* Copyright (C) 2002 by Marten Wulff
* <p>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* <p>
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* <p>
* You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
* GNU Lesser General Public License</a> along with this library; if not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
* <hr></table></center>
*
*
* This Servlet is used as Controller, that receives all the requests from
* client applications.
*/
public class RequestBrokerServlet extends HttpServlet {
// Logging
private static Logger logger = Logger.getLogger(RequestBrokerServlet.class);
// keys under which informations are stored in the session
private static final String USER_NAME = "userName";
private static final String USER_LOGGER = "userLogger";
private static final String LAST_CONNECT = "lastConnect";
/**
* Limit from the ServerResourceManager. Stored for convenience.
*/
private int compressLimit;
/**
* Buffersize from the ServerResourceManager. Stored for convenience.
*/
private int communicationBufferSize;
/**
* Initalizes the server side of the jkf framework.
*
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
// configure the JKF Framework
JKFServer.configure(config);
compressLimit = ServerResourceManager.instance().getCompressLimit();
communicationBufferSize =
ServerResourceManager.instance().getCommunicationBufferSize();
}
/*
* @see HttpServlet#service(HttpServletRequest, HttpServletResponse)
*/
public void service(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Request requestObject = null;
Response responseObject = null;
try {
if (logger.isDebugEnabled()) {
logger.debug("----- handling new request --- ");
}
// check for a session
HttpSession session = request.getSession(false);
if (session == null) {
if (logger.isInfoEnabled()) {
logger.info("Creating new Session");
}
session = request.getSession(true);
} else {
if (logger.isInfoEnabled()) {
logger.info("Session already exists");
}
}
// get the request data
CompressionHandler compressionHandler =
new ServerCompressionHandler(
this.compressLimit,
this.communicationBufferSize);
compressionHandler.setStreamWrapper(
new ServletStreamWrapper(request, response));
requestObject = (Request) compressionHandler.receiveObject();
// deal with client loggings
handleClientLogging(requestObject, session);
// deal with gc
handleGarbageCollection(requestObject, session);
/* depending on the object type, choose an action
be careful to look for the most specific types
first. for example EJBFindByPrimKeyRequest and
EJBFindByXXXRequest are Subclasses of EJBCreationRequest.
Therefore first test EJBFindByXXXRequest and
EJBFindByPrimKeyRequest, before testing EJBCreationRequest */
if (requestObject instanceof QueryRequest) {
if (logger.isDebugEnabled()) {
logger.debug("Handling QueryRequest");
}
responseObject =
handleQuery((QueryRequest) requestObject, session);
} else if (requestObject instanceof CreationRequest) {
if (logger.isDebugEnabled()) {
logger.debug("Handling CreationRequest");
}
responseObject =
handleCreation((CreationRequest) requestObject, session);
} else if (requestObject instanceof EJBFindByXXXRequest) {
if (logger.isDebugEnabled()) {
logger.debug("Handling EJBFindByXXXRequest");
}
responseObject =
handleEJBFindByXXX(
(EJBFindByXXXRequest) requestObject,
session);
} else if (requestObject instanceof EJBFindByPrimKeyRequest) {
if (logger.isDebugEnabled()) {
logger.debug("Handling EJBFindByPrimKeyRequest");
}
responseObject =
handleEJBFindByPrimKey(
(EJBFindByPrimKeyRequest) requestObject,
session);
} else if (requestObject instanceof EJBCreationRequest) {
if (logger.isDebugEnabled()) {
logger.debug("Handling EJBCreationRequest");
}
responseObject =
handleEJBCreation(
(EJBCreationRequest) requestObject,
session);
} else if (requestObject instanceof RegisterRequest) {
if (logger.isDebugEnabled()) {
logger.debug("Handling RegisterRequest");
}
registerUser((RegisterRequest) requestObject, session);
responseObject = new Response();
} else if (requestObject instanceof KeepAliveRequest) {
if (logger.isDebugEnabled()) {
logger.debug("Handling KeepAliveRequest");
}
// do nothing
responseObject = new Response();
}
// set response commands, that should be send to the clients.
responseObject.setResponseCommands(
RemoteManager.instance().getResponseCommands(
(Timestamp) session.getAttribute(LAST_CONNECT)));
// update the timestamp at which the user made his last connection
session.setAttribute(
LAST_CONNECT,
new Timestamp(new Date().getTime()));
// send the response
/* if(responseObject instanceof Externalizable) {
System.out.println("Is Externalizable");
}else {
System.out.println(responseObject.toString());
}*/
compressionHandler.sendObject(responseObject);
} catch (Exception ex) {
logger.error("Error handling post request", ex);
}
}
/*
* Method handleCreation. This method is called, if a client
* application wants to instantiate a new object.
*
* @param datagramm - CreationRequest with all necessary Information
* (ClassName of the class the should be created)
*
* @return Response - the response containing id of the
* remote object.
*/
private Response handleCreation(
CreationRequest request,
HttpSession session) {
if (logger.isDebugEnabled()) {
logger.debug("Creating new remote object ...");
}
Response response = new Response();
try {
String className = request.getClassName();
Object[] params = request.getParams();
Object generatedObject = null;
Class generatedClass = Class.forName(className);
Constructor constructor =
ReflectionUtils.findConstructor(generatedClass, params);
generatedObject = constructor.newInstance(params);
// store the new object in the uses HttpSession
RemoteObjectID remoteId = generateUniqueId();
session.setAttribute(remoteId.toString(), generatedObject);
response.setRemoteObjectId(remoteId);
if (logger.isDebugEnabled()) {
logger.debug("Creation of remote object done.");
}
} catch (Exception ex) {
logger.error("Error creatin new remote object:", ex);
response.setRemoteException(
new JKFCommunicationException(ex.getMessage()));
} finally {
return response;
}
}
private Response handleEJBCreation(
EJBCreationRequest request,
HttpSession session) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -