I created a seam component to send all errors raised in the application by email,
just create a seam component as described below:
package br.com.sanepar.sns.mbean;
import java.io.Serializable;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.exception.Exceptions;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.faces.Renderer;
import br.com.sanepar.sns.util.ServerUtils;
@SuppressWarnings("serial")
@Name("error")
@Scope(ScopeType.APPLICATION)
public class ErrorAction extends Exceptions implements Serializable {
@In
private Renderer renderer;
@In(value = "org.jboss.seam.handledException", required = false)
public Exception exception;
@In
FacesMessages facesMessages;
@Observer(value = "org.jboss.seam.exceptionHandled")
public void sendMail() {
sendErrorMail(exception, "Exception no servidor "+ ServerUtils.getServerAddress());
facesMessages.add("Ocorreram erros ao executar a operação.");
}
public void sendErrorMail(Throwable throwable, String message) {
StringBuffer err = new StringBuffer();
if (throwable != null) {
err.append(throwable.getLocalizedMessage()).append("\n");
err.append(throwable.getMessage()).append("\n");
for (StackTraceElement elem : throwable.getStackTrace()) {
err.append(elem.getFileName()).append(":")
.append(elem.getLineNumber()).append(":")
.append(elem.getClassName()).append(":")
.append(elem.getMethodName()).append("\n");
}
}
Contexts.getEventContext().set("subject", message);
Contexts.getEventContext().set("body", err.toString());
Contexts.getEventContext().set("to", "leandrog@sanepar.com.br");
Contexts.getEventContext().set("to", "klerisonk@sanepar.com.br");
renderer.render("/acaoMail.xhtml");
}
}
create a page for sending email
<?xml version="1.0" encoding="ISO-8859-1" ?>
<m:message xmlns="http://www.w3.org/1999/xhtml"
xmlns:m="http://jboss.com/products/seam/mail"
xmlns:h="http://java.sun.com/jsf/html">
<m:from name="SNS - Sistema Normativo da Sanepar" address="sns@sns.com" />
<m:to name="Leandro">leandrog@sanepar.com.br</m:to>
<m:subject>
#{subject}
</m:subject>
<p>
<m:body>
#{body}
</m:body>
</p>
</m:message>
and add the e-mail configuration in components.xml
<mail:mail-session host="10.100.1.70" auto-create="true" />
the ServerUtils
package br.com.sanepar.sns.util;
/**
* @author Leandro de Godoy
*18/06/2012
*/
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
public class ServerUtils {
public static final String SERVER_SEPARATOR=":";
public static final String FILE_SEPARATOR="/";
//mudar para o endereco do servidor alvo
public static final String HOST_STATIC_PATH ="http://localhost:8080/";
public static String getServerAddress() {
HttpServletRequest req = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
String address = req.getScheme() +SERVER_SEPARATOR+FILE_SEPARATOR+FILE_SEPARATOR+ req.getServerName()
+ SERVER_SEPARATOR+req.getServerPort()+FILE_SEPARATOR;
return address;
}
}
Oh my god, seam is fantastic
Comments