Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Sunday, February 14, 2016

How to generate alert mail to find Unchecked Exception in your JSF application (ExceptionHandlingActionListener)

Past few days I had a dilemma of how to generate email alert if my JSF application has any RuntimeException. Finally I found a solution.

ActionListenerImpl class   help me for that.

No need to talk, let's start the party.

First step:
We need to create ExceptionHandlingActionListener  class that implements  ActionListenerImpl.
in the ExceptionHandlingActionListener classwe should override the supper class processAction(ActionEvent event) method. And write our email alert code inside the catch block.

So in my sysout " Typing email " section, we can call to a email generation method and pass the StackTrace to email body. Email formatting is not in my radar.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.imesh.test;

import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;


import com.sun.faces.application.ActionListenerImpl;

public class ExceptionHandlingActionListener extends ActionListenerImpl
  implements ActionListener {
 
 
 @Override
 public void processAction (ActionEvent event) {
  try {
   super.processAction (event);
  } catch (Throwable e) {
   e.printStackTrace();
   try {
                System.out.println("----------------------- SYSTEM EXCEPTION EMAIL SEND BEGIN. -----------------------");
                
                System.out.println("Typing email");
                
                System.out.println("----------------------- SYSTEM EXCEPTION EMAIL SEND END. -----------------------");
            } catch (Exception f) {
                System.out.println("----------------------- SYSTEM EXCEPTION EMAIL NOT ABLE TO SENT. -----------------------");
                f.printStackTrace();
            }
            
   
  }
 }

}


Second step : 
Add this listener to the faces-config.xml


1
2
3
4
<application>
<action-listener> com.imesh.test.ExceptionHandlingActionListener 
</action-listener>
</application>


Awesome!!!,  We are done. Try create any RuntimeException in your application. Magically it will handle by our ExceptionHandlingActionListener  and generate the email for you.

Thank you, Comments are welcome !!.

Thursday, December 2, 2010

Jaxp Error 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Content is not allowed in prolog.'

I had the above error when I tried to transform a xml in to another xml.
The mistake was, I have pass the xml file as a ByteArrayInputStream instead of InputStream. I was searching every where about the error but no clue with my code.


            xsltFile = getInputStream(xslPath);
            xmlFile = getInputStream(inputXML);
           
            Source xmlSource = new StreamSource(xmlFile);
            Source xsltSource = new StreamSource(xsltFile);

            // the factory pattern supports different XSLT processors
            TransformerFactory transFact = TransformerFactory.newInstance();               
            Templates cachedXSLT = transFact.newTemplates(xsltSource);
            Transformer trans = cachedXSLT.newTransformer();

            writer = new StringWriter();
           
            trans.transform(xmlSource, new StreamResult(writer));

            System.out.println(writer.toString());


Try to chenge the input type and it would help for you.
Aalso I saw some info in Wrox site.

http://p2p.wrox.com/xslt/48696-error-content-not-allowed-prolog-sos.html