📄 requestbrokerservlet.java
字号:
if (logger.isDebugEnabled()) {
logger.debug("Creating ejb ...");
}
String jndiName = request.getJndiName();
Object[] params = request.getParams();
String methodName = request.getMethodName();
Object remoteInterface = null;
Object homeInterface = null;
Response response = new Response();
try {
EJBData ejbData =
ServerResourceManager.instance().getEJBData(jndiName);
if (ejbData == null) {
throw new Exception(
"EJB with jndi name "
+ jndiName
+ " is not "
+ "configured!! Please add an entry in your jkf server configuration file.");
}
JndiProperties props = ejbData.getJndiProperties();
Context context = new InitialContext(props.getProperties());
Object ref = context.lookup(jndiName);
if (ref == null) {
throw new Exception(
"Couldn't get a reference to ejb with jndi name "
+ jndiName);
}
// Get a reference from this to the Bean's Home interface
homeInterface =
PortableRemoteObject.narrow(
ref,
Class.forName(ejbData.getHomeInterface()));
// create a array containing the parameter classes
Method method =
ReflectionUtils.findMethod(
homeInterface.getClass(),
methodName,
params);
// invoke the method
remoteInterface = method.invoke(homeInterface, params);
// store the new object in the uses HttpSession
RemoteObjectID remoteId = generateUniqueId();
session.setAttribute(remoteId.toString(), remoteInterface);
response.setRemoteObjectId(remoteId);
} catch (Exception ex) {
logger.error("Error creating EJB: ", ex);
response.setRemoteException(
new JKFCommunicationException(ex.getMessage()));
} finally {
return response;
}
}
private Response handleEJBFindByPrimKey(
EJBFindByPrimKeyRequest request,
HttpSession session) {
if (logger.isDebugEnabled()) {
logger.debug("Trying to find ejb by primary key");
}
String jndiName = request.getJndiName();
Object[] params = request.getParams();
String methodName = request.getMethodName();
Object remoteInterface = null;
Object homeInterface = null;
Response response = new Response();
try {
EJBData ejbData =
ServerResourceManager.instance().getEJBData(jndiName);
if (ejbData == null) {
throw new Exception(
"EJB with jndi name "
+ jndiName
+ " is not "
+ "configured!! Please add an entry in your jkf server configuration file.");
}
JndiProperties props = ejbData.getJndiProperties();
Context context = new InitialContext(props.getProperties());
Object ref = context.lookup(jndiName);
if (ref == null) {
throw new Exception(
"Couldn't get a reference to ejb with jndi name "
+ jndiName);
}
// Get a reference from this to the Bean's Home interface
homeInterface =
PortableRemoteObject.narrow(
ref,
Class.forName(ejbData.getHomeInterface()));
// create a array containing the parameter classes
Method method =
ReflectionUtils.findMethod(
homeInterface.getClass(),
methodName,
params);
// invoke the method
remoteInterface = method.invoke(homeInterface, params);
// store the new object in the uses HttpSession
RemoteObjectID remoteId = generateUniqueId();
session.setAttribute(remoteId.toString(), remoteInterface);
response.setRemoteObjectId(remoteId);
} catch (Exception ex) {
logger.error("Error finding ejb by primary key : ", ex);
response.setRemoteException(
new JKFCommunicationException(ex.getMessage()));
} finally {
return response;
}
}
private Response handleEJBFindByXXX(
EJBFindByXXXRequest request,
HttpSession session) {
if (logger.isDebugEnabled()) {
logger.debug("Trying to find ejbs ...");
}
String jndiName = request.getJndiName();
Object[] params = request.getParams();
String methodName = request.getMethodName();
Collection remoteInterfaces = null;
Object homeInterface = null;
EJBFindByXXXResponse response = new EJBFindByXXXResponse();
try {
EJBData ejbData =
ServerResourceManager.instance().getEJBData(jndiName);
if (ejbData == null) {
throw new Exception(
"EJB with jndi name "
+ jndiName
+ " is not "
+ "configured!! Please add an entry in your jkf server configuration file.");
}
JndiProperties props = ejbData.getJndiProperties();
Context context = new InitialContext(props.getProperties());
Object ref = context.lookup(jndiName);
if (ref == null) {
throw new Exception(
"Couldn't get a reference to ejb with jndi name "
+ jndiName);
}
// Get a reference from this to the Bean's Home interface
homeInterface =
PortableRemoteObject.narrow(
ref,
Class.forName(ejbData.getHomeInterface()));
// create a array containing the parameter classes
Method method =
ReflectionUtils.findMethod(
homeInterface.getClass(),
methodName,
params);
// invoke the method
remoteInterfaces =
(Collection) method.invoke(homeInterface, params);
// store the new objects in the uses HttpSession
RemoteObjectID remoteId = null;
Iterator it = remoteInterfaces.iterator();
Object remoteInterface = null;
while (it.hasNext()) {
remoteInterface = it.next();
remoteId = generateUniqueId();
session.setAttribute(remoteId.toString(), remoteInterface);
response.addRemoteObjectId(remoteId);
}
if (logger.isDebugEnabled()) {
logger.debug("Leaving ...");
}
} catch (Exception ex) {
logger.error("Error finding ejbs : ", ex);
response.setRemoteException(
new JKFCommunicationException(ex.getMessage()));
} finally {
return response;
}
}
/*
* Method handleQuery. This method is called, if a client application
* wants to invoke any methods of a remote object.
*
* @param datagramm - QueryRequest that holds all necessary information
* for querying the object(id, method name, parameters)
*
* @return Object - The QueryRequest filled with informations from
* the method call.
*/
private Response handleQuery(QueryRequest datagramm, HttpSession session) {
DataResponse response = new DataResponse();
try {
// retrieve the object from the session by the specified id
Object ob =
session.getAttribute(datagramm.getRemoteObjectId().toString());
// get the name of the method, that should be called
String methodName = datagramm.getMethodName();
// get the parameters for the method call and create an array
// of class objects, to determin the correct method that should
// be called. Method itself is not serializable and declared as
// final. Therefore we'll have to do some workaround.
Object[] params = datagramm.getParams();
Method method =
ReflectionUtils.findMethod(ob.getClass(), methodName, params);
// invoke the method and store the result in the given
// QueryRequest
Object result = method.invoke(ob, params);
response.setData(result);
} catch (Exception ex) {
// set the exception field of the response, if any exception
// occures
logger.error("Error handling query of a remote object", ex);
response.setRemoteException(
new JKFCommunicationException(ex.getMessage()));
} finally {
return response;
}
}
/*
* Method for generating a unique RemoteObjectID
* that is used to identify Objets.
*/
private RemoteObjectID generateUniqueId() {
return new RemoteObjectID(new UID().toString());
}
/*
* Method for handling logging messages from clients.
* A separate Thread is started if there are any
* messages available.
*/
private void handleClientLogging(Request wrapper, HttpSession session) {
Vector buffer = wrapper.getLogMessages();
// if there are no existing messages, exit method
if (buffer == null) {
if (logger.isInfoEnabled()) {
logger.info("no client log messages available");
}
return;
}
// start a Thread, that logs all messages
ClientLoggerThread thread =
new ClientLoggerThread(
buffer,
(Logger) session.getAttribute(USER_LOGGER));
thread.start();
}
/*
* Method for handling garbage collection of remote
* Objects. Before the Object is removed from the
* specified HttpSession, it's tried to invoke the
* finalize() Method of the Object if anyone
* exists.
*/
private void handleGarbageCollection(
Request wrapper,
HttpSession session) {
String id = null;
if (logger.isInfoEnabled()) {
logger.info("handling remote gc ...");
}
// get all RemoteObjectIDs for the Objects
// that should be garbage collected
Vector gcObjects = wrapper.getGCMessages();
// if no remote objects needs to be garbage collected
// exit method
if (gcObjects == null) {
if (logger.isInfoEnabled()) {
logger.info("no gc messages available");
}
return;
}
for (int i = 0; i < gcObjects.size(); i++) {
Object o = gcObjects.elementAt(i);
if(o == null) {
System.out.println("Object that should be RemoteObjectID is null in gc thread");
}else {
id = ((RemoteObjectID) gcObjects.elementAt(i)).toString();
if (logger.isInfoEnabled()) {
logger.info("removing " + id + " from session ...");
}
// remove the object from session
session.removeAttribute(id);
}
}
}
/**
* This method is called if a client makes an inital registration request.
* This request is performed automatically by the jkf client framework,
* before the user can work with the server communication framework.
* In the RegisterRequest is transfered a user name. The user name
* is needed to generate a logger object, that is responsible for
* logging messages, that are retrieved from this user and only from
* this user. The user name defines the file name to which this logger
* is logging. e.g. user name = foobar --> logging to foobar.log
* Therefore it would be good if the user name is unique. If not, users
* with the same user name log to the same logfile.
*
* @param request the Request object, that stores all necessary information
* for registration
* @param session the HttpSession in which the user name and the generated
* Logger object is stored
*/
private void registerUser(RegisterRequest request, HttpSession session) {
session.setAttribute(USER_NAME, request.getUserName());
session.setAttribute(
USER_LOGGER,
ServerLoggingManager.instance().generateClientLogger(
request.getUserName()));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -