Skip to content

Instantly share code, notes, and snippets.

@huogerac
Created October 7, 2011 12:16
Show Gist options
  • Save huogerac/1270152 to your computer and use it in GitHub Desktop.
Save huogerac/1270152 to your computer and use it in GitHub Desktop.
code smell sample
package sample;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.ejb.SessionBean;
/**
* EJB motor de processo.
*
* @ejb.bean type="Stateless" name="Engine" view-type="local"
* jndi-name="EJB/Session/Engine"
* local-jndi-name="EJB/Session/Local/Engine"
* transaction-type="Container"
* @ejb.transaction type="Never"
* @ejb.ejb-ref ejb-name="Repository" view-type="local"
* ref-name="ejb/Repository"
* @ejb.ejb-ref ejb-name="Connector" view-type="local" ref-name="ejb/Connector"
* @ejb.ejb-ref ejb-name="Saf" view-type="local" ref-name="ejb/Saf"
* @ejb.ejb-ref ejb-name="Sonda" view-type="local" ref-name="ejb/Sonda"
* @ejb.ejb-ref ejb-name="RepositoryContainer" view-type="local"
* ref-name="ejb/RepositoryContainer"
*/
public class EngineBean extends AbstractSessionBean implements SessionBean {
private static final long serialVersionUID = -1222050961590109955L;
/**
* Verifica se uma transacao foi originada pelo componente SAF.
*
* @param request
* entrada do processo
* @return <b>true</b> se a transacao foi originada no SAF <b>false</b> caso
* contrario
*/
private static boolean isSAFProcess(ProcessRequest request) {
boolean flagSAF = false;
Properties properties = request.getProperties();
if (properties != null) {
String propertyValue = properties
.getProperty(Constants.IS_SAF_PROCESS);
if (propertyValue != null
&& Constants.YES.equalsIgnoreCase(propertyValue)) {
flagSAF = true;
}
}
return flagSAF;
}
/**
* Verifica se uma transacao foi originada pelo componente Sonda.
*
* @param request
* entrada do processo
* @return <b>true</b> se a transacao foi originada no Sonda <b>false</b>
* caso contrario
*/
private static boolean isSondaProcess(ProcessRequest request) {
boolean flagSonda = false;
Properties properties = request.getProperties();
if (properties != null) {
String propertyValue = properties
.getProperty(Constants.IS_SONDA_PROCESS);
if (propertyValue != null
&& Constants.YES.equalsIgnoreCase(propertyValue)) {
flagSonda = true;
}
}
return flagSonda;
}
/**
* Verifica se uma transacao deve ser convertida para o formato ISO8583.
*
* @param request
* entrada do processo
* @return <b>true</b> se a transacao deve ser convertida para o formato
* ISO8583 <b>false</b> caso contrario
*/
private static boolean formatToISO(ProcessRequest request) {
boolean flagISO = false;
Properties properties = request.getProperties();
if (properties != null) {
String propertyValue = properties
.getProperty(Constants.FORMAT_TO_ISO);
if (propertyValue != null
&& Constants.YES.equalsIgnoreCase(propertyValue)) {
flagISO = true;
}
}
return flagISO;
}
/**
* Verifica se a entrada do processo deve ser convertida para o formato XML.
*
* @param request
* entrada do processo
* @return <b>true</b> se a entrada do processo deve ser convertida para o
* formato XML <b>false</b> caso contrario
*/
private static boolean formatToXML(ProcessRequest request) {
boolean flagISO = false;
Properties properties = request.getProperties();
if (properties != null) {
String propertyValue = properties
.getProperty(Constants.FORMAT_TO_XML);
if (propertyValue != null
&& Constants.YES.equalsIgnoreCase(propertyValue)) {
flagISO = true;
}
}
return flagISO;
}
/**
* Criador.
*
* @ejb.create-method
*/
public void ejbCreate() {
// setMonitorComponent(IMonitorService.COMPONENT_ENGINE);
}
/**
* Obtem a definicao de um processo
*
* @param logger
* Gestor de logs
* @param session
* Sessao
* @param engineRequest
* Container de entrada
* @return Container de saida
* @ejb.interface-method view-type="local"
*/
public GetDefinitionResponse getDefinition(ILogManager logger,
Session session, GetDefinitionRequest engineRequest)
throws PDCTechnicalException, EngineException {
GetDefinitionResponse engineResponse = new GetDefinitionResponse();
try {
// obtem os parametros
String processName = engineRequest.getName();
logger.debug(this, "Parametro 'processName' = '" + processName
+ "'");
String processVersion = engineRequest.getVersion();
logger.debug(this, "Parametro 'processVersion' = '"
+ processVersion + "'");
// cria o repositorio
ProcessRepository repository = new ProcessRepository(logger,
session.getProductName());
// obtem a definicao do processo
ProcessDefinition process = repository.getProcessDefinition(
engineRequest.getName(), engineRequest.getVersion());
// obtem o resultado e atualiza a resposta
String processRequest = process.getRequest();
engineResponse.setRequest(processRequest);
String processResponse = process.getResponse();
engineResponse.setResponse(processResponse);
} catch (PDCTechnicalException pdctEx) {
// reportMonitor(true);
throw pdctEx;
} catch (EngineException eEx) {
// reportMonitor(true);
throw eEx;
}
return engineResponse;
}
/**
* Executa um processo
*
* @param logger
* Gestor de logs
* @param session
* Sessao
* @param engineRequest
* Container de entrada
* @return Container de saida
* @ejb.interface-method view-type="local"
*/
public ExecuteResponse execute(ILogManager logger, Session session,
ExecuteRequest engineRequest) throws PDCTechnicalException,
EngineException {
ExecuteResponse engineResponse = new ExecuteResponse();
try {
// obtem os parametros
String processName = engineRequest.getName();
logger.debug(this, "Parametro 'processName' = '" + processName
+ "'");
String processVersion = engineRequest.getVersion();
logger.debug(this, "Parametro 'processVersion' = '"
+ processVersion + "'");
// cria o contexto do motor de processos
EngineContext engineContext = new EngineContext(session, logger);
// limpa o cache se necessario
if (engineRequest.isFlushCache()) {
engineContext.getRepository().flushCache();
}
// obtem o modelo do processo
ProcessModel model = engineContext.getRepository().getProcessModel(
engineRequest.getName(), engineRequest.getVersion());
try {
model = model.clone();
} catch (CloneNotSupportedException e) {
logger.error(this, "Erro ao efetuar o clone do objeto ProcessModel.", e);
throw new ProcessCloneException(processName, processVersion, e);
}
// carga o executor do interceptador
String interceptorClassName = session
.getProductProperty(Product.INTERCEPTOR);
InterceptorRunner interceptorRunner = null;
if ((interceptorClassName != null)
&& (interceptorClassName.trim().length() > 0)) {
interceptorRunner = new InterceptorRunner(interceptorClassName,
engineContext);
// seta o metodo
interceptorRunner.runPreBuild(engineRequest, model);
}
// analisa a mensagem de entrada
ProcessRequest request = new ProcessRequest();
try {
request = engineContext.getProcessMessageFactory()
.toProcessRequest(model.getRequest(),
engineRequest.getRequest());
} catch (ProcessMessageException pmEx) {
throw new RequestParseException(pmEx);
}
request.setErrorType(engineRequest.getErrorType());
request.setOperationId(engineRequest.getOperationId());
request.setLocalTime(engineRequest.getLocalTime());
request.setMoreData(engineRequest.isMoreData());
request.setTicket(engineRequest.getTicket());
request.setProperties(engineRequest.getProperties());
request.setFormatToXML(formatToXML(request));
// inicializa a mensagem de saida
ProcessResponse response = engineContext.getProcessMessageFactory()
.getProcessResponse(model.getResponse());
response.setSafProcess(isSAFProcess(request));
response.setSondaProcess(isSondaProcess(request));
response.setFormatToISO(formatToISO(request));
if (interceptorRunner != null) {
// seta o metodo
interceptorRunner.runPre(request, response, model);
}
// executa o processo
ProcessRunner processRunner = new ProcessRunner(engineContext);
// seta o metodo
processRunner.runProcess(model, request, response);
if (interceptorRunner != null) {
// seta o metodo
interceptorRunner.runPost(engineResponse, response, model);
}
// Se nao foi especificado um retorno, faz o parser padrao...
if (engineResponse.getResponse() == null) {
// converte o xml a mensagem de saida
String processResponse = null;
processResponse = engineContext.getProcessMessageFactory()
.fromProcessResponse(model.getResponse(), response);
engineResponse.setResponse(processResponse);
}
if (response.getInputStreams() != null
&& !response.getInputStreams().isEmpty()) {
engineResponse.setInputStreams(response.getInputStreams());
}
List processResponseMessages = response.getMessages();
Iterator itProcessResponseMessages = processResponseMessages
.iterator();
while (itProcessResponseMessages.hasNext()) {
ProcessResponseMessage processResponseMessage = (ProcessResponseMessage) itProcessResponseMessages
.next();
if (processResponseMessage.isError()) {
logger.debug(this,
"Resultado 'processResponseMessage' = ERRO - "
+ processResponseMessage.getCode() + ": "
+ processResponseMessage.getMessage());
} else {
logger.debug(this,
"Resultado do 'processResponseMessage' = AVISO - "
+ processResponseMessage.getCode() + ": "
+ processResponseMessage.getMessage());
}
}
engineResponse.setMessages(response.getMessages());
engineResponse.setMoreData(response.isMoreData());
engineResponse.setTicket(response.getTicket());
engineResponse.setTimes(response.getTimes());
engineResponse.setProperties(response.getProperties());
} catch (PDCTechnicalException pdctEx) {
// Lanca evento de monitoracao
getMonitorService().reportMonitoracao(this.getClass().getSimpleName(), "execute", true);
throw pdctEx;
} catch (EngineException eEx) {
// Lanca evento de monitoracao
getMonitorService().reportMonitoracao(this.getClass().getSimpleName(), "execute", true);
throw eEx;
}
// reportMonitor(false);
return engineResponse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment