use of org.apache.openejb.client.ClusterResponse in project tomee by apache.
the class ClusterRequestHandler method processResponse.
@Override
public void processResponse(final Response response, final ObjectOutputStream out, final ProtocolMetaData metaData) throws Exception {
if (null != response) {
if (ClusterResponse.class.isInstance(response)) {
final ClusterResponse res = (ClusterResponse) response;
try {
res.setMetaData(metaData);
res.writeExternal(out);
} catch (IOException e) {
logger.error("Failed to write to ClusterResponse", e);
throw e;
}
} else {
logger.error("ClusterRequestHandler cannot process an instance of: " + response.getClass().getName());
}
}
}
use of org.apache.openejb.client.ClusterResponse in project tomee by apache.
the class ClusterRequestHandler method processRequest.
@Override
public ClusterResponse processRequest(final ObjectInputStream in, final ProtocolMetaData metaData) throws Exception {
final ClusterRequest req = new ClusterRequest();
req.setMetaData(metaData);
final ClusterResponse res = new ClusterResponse();
res.setMetaData(metaData);
try {
req.readExternal(in);
} catch (IOException e) {
res.setFailure(e);
return res;
} catch (ClassNotFoundException e) {
res.setFailure(new IOException().initCause(e));
return res;
}
final ClusterMetaData currentClusterMetaData = data.current();
if (req.getClusterMetaDataVersion() < currentClusterMetaData.getVersion()) {
if (logger.isDebugEnabled()) {
final URI[] locations = currentClusterMetaData.getLocations();
if (locations.length < 10) {
logger.debug("Sending client updated cluster locations: [" + Join.join(", ", locations) + "]");
} else {
logger.debug("Sending client updated cluster locations: " + locations.length + " locations total");
}
}
res.setUpdatedMetaData(currentClusterMetaData);
} else {
res.setCurrent();
}
return res;
}
use of org.apache.openejb.client.ClusterResponse in project tomee by apache.
the class EjbDaemon method service.
public void service(final InputStream rawIn, final OutputStream rawOut) throws IOException {
final ProtocolMetaData clientProtocol = new ProtocolMetaData();
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
RequestType requestType = null;
byte requestTypeByte = RequestType.NOP_REQUEST.getCode();
try {
final RequestInfos.RequestInfo info = RequestInfos.info();
info.setInputStream(countStreams ? new CountingInputStream(rawIn) : rawIn);
// Read client Protocol Version
final InputStream cis = info.getInputStream();
clientProtocol.readExternal(cis);
ois = new EjbObjectInputStream(cis);
// Read ServerMetaData
final ServerMetaData serverMetaData = new ServerMetaData();
serverMetaData.readExternal(ois);
ClientObjectFactory.serverMetaData.set(serverMetaData);
// Read request type
requestTypeByte = ois.readByte();
requestType = RequestType.valueOf(requestTypeByte);
if (requestType == RequestType.NOP_REQUEST) {
return;
}
ClusterResponse clusterResponse = null;
if (requestType == RequestType.CLUSTER_REQUEST) {
clusterResponse = clusterHandler.processRequest(ois, clientProtocol);
//Check for immediate failure
final Throwable failure = clusterResponse.getFailure();
if (null != clusterResponse && null != failure) {
clusterHandler.getLogger().debug("Failed to write to ClusterResponse", failure);
try {
info.setOutputStream(countStreams ? new CountingOutputStream(rawOut) : rawOut);
oos = new ObjectOutputStream(info.getOutputStream());
clusterResponse.setMetaData(clientProtocol);
clusterResponse.writeExternal(oos);
oos.flush();
} catch (IOException ie) {
final String m = "Failed to write to ClusterResponse: " + ie.getMessage();
clusterHandler.getLogger().error(m, ie);
throw Exceptions.newIOException(m, ie);
}
throw failure;
}
}
requestTypeByte = ois.readByte();
requestType = RequestType.valueOf(requestTypeByte);
if (requestType == RequestType.NOP_REQUEST) {
return;
}
// Exceptions should not be thrown from these methods
// They should handle their own exceptions and clean
// things up with the client accordingly.
final Response response;
switch(requestType) {
case EJB_REQUEST:
response = processEjbRequest(ois, clientProtocol);
break;
case JNDI_REQUEST:
response = processJndiRequest(ois, clientProtocol);
break;
case AUTH_REQUEST:
response = processAuthRequest(ois, clientProtocol);
break;
case LOGOUT_REQUEST:
response = processLogoutRequest(ois, clientProtocol);
break;
default:
logger.error("\"" + requestType + " " + clientProtocol.getSpec() + "\" FAIL \"Unknown request type " + requestType);
return;
}
try {
info.setOutputStream(countStreams ? new CountingOutputStream(rawOut) : rawOut);
final OutputStream cos = info.getOutputStream();
//Let client know we are using the requested protocol to respond
clientProtocol.writeExternal(cos);
cos.flush();
oos = new ObjectOutputStream(cos);
clusterHandler.processResponse(clusterResponse, oos, clientProtocol);
oos.flush();
} finally {
switch(requestType) {
case EJB_REQUEST:
processEjbResponse(response, oos, clientProtocol);
break;
case JNDI_REQUEST:
processJndiResponse(response, oos, clientProtocol);
break;
case AUTH_REQUEST:
processAuthResponse(response, oos, clientProtocol);
break;
case LOGOUT_REQUEST:
processLogoutResponse(response, oos, clientProtocol);
break;
default:
//Should never get here...
logger.error("\"" + requestType + " " + clientProtocol.getSpec() + "\" FAIL \"Unknown response type " + requestType);
}
}
} catch (IllegalArgumentException iae) {
final String msg = "\"" + clientProtocol.getSpec() + "\" FAIL \"Unknown request type " + requestTypeByte;
if (logger.isDebugEnabled()) {
logger.debug(msg, iae);
} else {
logger.warning(msg + " - Debug for StackTrace");
}
} catch (SecurityException e) {
final String msg = "\"" + requestType + " " + clientProtocol.getSpec() + "\" FAIL \"Security error - " + e.getMessage() + "\"";
if (logger.isDebugEnabled()) {
logger.debug(msg, e);
} else {
logger.warning(msg + " - Debug for StackTrace");
}
} catch (Throwable e) {
final String msg = "\"" + requestType + " " + clientProtocol.getSpec() + "\" FAIL \"Unexpected error - " + e.getMessage() + "\"";
if (logger.isDebugEnabled()) {
logger.debug(msg, e);
} else {
logger.warning(msg + " - Debug for StackTrace");
}
} finally {
try {
ClientObjectFactory.serverMetaData.remove();
} finally {
if (null != oos) {
try {
oos.flush();
} catch (Throwable e) {
//Ignore
}
try {
oos.close();
} catch (Throwable e) {
//Ignore
}
}
if (null != ois) {
try {
ois.close();
} catch (Throwable e) {
//Ignore
}
}
// enforced in case of exception
securityService.disassociate();
}
}
}
Aggregations