Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  jDTAUS - DTAUS fileformat.
   *  Copyright (c) 2005 Christian Schulte <cs@schulte.it>
   *
   *  This library is free software; you can redistribute it and/or
   *  modify it under the terms of the GNU Lesser General Public
   *  License as published by the Free Software Foundation; either
   *  version 2.1 of the License, or any later version.
   *
  *  This library is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  *  Lesser General Public License for more details.
  *
  *  You should have received a copy of the GNU Lesser General Public
  *  License along with this library; if not, write to the Free Software
  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 package org.jdtaus.core.lang.util;
 
 import java.io.File;
 import java.net.URL;
 import  org.jdtaus.core.container.ContainerFactory;
 import  org.jdtaus.core.container.ContextFactory;
 import  org.jdtaus.core.container.ContextInitializer;
 import  org.jdtaus.core.container.Implementation;
 import  org.jdtaus.core.container.ModelFactory;
 import  org.jdtaus.core.container.Properties;
 import  org.jdtaus.core.container.Property;
 import  org.jdtaus.core.container.Specification;
 import  org.jdtaus.core.lang.ExceptionEvent;
 import  org.jdtaus.core.lang.ExceptionListener;
 import  org.jdtaus.core.messages.BugReportMessage;
 import  org.jdtaus.core.messages.ExceptionMessage;
 import  org.jdtaus.core.messages.UndefinedApplicationStateMessage;
 import  org.jdtaus.core.text.Message;
 import  org.jdtaus.core.text.MessageEvent;
 import  org.jdtaus.core.text.Messages;
 import  org.jdtaus.core.text.spi.ApplicationLogger;

ExceptionListener resolving exceptions to corresponding application messages.

Author(s):
Christian Schulte
Version:
$Id: ExceptionMessageProducer.java 4521 2007-12-25 21:36:55Z schulte2005 $
See also:
onException(ExceptionEvent)
org.jdtaus.core.text.util.SwingMessagePane
org.jdtaus.core.text.util.MessageLogger
 
 public final class ExceptionMessageProducer implements ExceptionListener
 {
     //--Implementation----------------------------------------------------------
 
 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausImplementation
     // This section is managed by jdtaus-container-mojo.
 
    
Meta-data describing the implementation.
 
     private static final Implementation META =
         ModelFactory.getModel().getModules().
         getImplementation(ExceptionMessageProducer.class.getName());
 // </editor-fold>//GEN-END:jdtausImplementation
 
     //----------------------------------------------------------Implementation--
     //--Constructors------------------------------------------------------------
 
 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausConstructors
     // This section is managed by jdtaus-container-mojo.
 
    
Initializes the properties of the instance.

Parameters:
meta the property values to initialize the instance with.
Throws:
NullPointerException if meta is null.
 
     private void initializeProperties(final Properties meta)
     {
         Property p;
 
         if(meta == null)
         {
             throw new NullPointerException("meta");
         }
 
     }
 // </editor-fold>//GEN-END:jdtausConstructors
 
     //------------------------------------------------------------Constructors--
     //--Dependencies------------------------------------------------------------
 
 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausDependencies
     // This section is managed by jdtaus-container-mojo.
 
    
Configured ApplicationLogger implementation.
 
     private transient ApplicationLogger _dependency0;

    
Gets the configured ApplicationLogger implementation.

Returns:
the configured ApplicationLogger implementation.
    private ApplicationLogger getApplicationLogger()
    {
        ApplicationLogger ret = null;
        if(this. != null)
        {
            ret = this.;
        }
        else
        {
            ret = (ApplicationLogger) ContainerFactory.getContainer().
                getDependency(ExceptionMessageProducer.class,
                "ApplicationLogger");
            if(ModelFactory.getModel().getModules().
                getImplementation(ExceptionMessageProducer.class.getName()).
                getDependencies().getDependency("ApplicationLogger").
                isBound())
            {
                this. = ret;
            }
        }
        if(ret instanceof ContextInitializer && !((ContextInitializer) ret).
            isInitialized(ContextFactory.getContext()))
        {
            ((ContextInitializer) ret).initialize(ContextFactory.getContext());
        }
        return ret;
    }
// </editor-fold>//GEN-END:jdtausDependencies
    //------------------------------------------------------------Dependencies--
    //--Properties--------------------------------------------------------------
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties
    // This section is managed by jdtaus-container-mojo.
// </editor-fold>//GEN-END:jdtausProperties
    //--------------------------------------------------------------Properties--
    //--ExceptionListener-------------------------------------------------------

    

This method resolves exceptions provided by an applications's ExceptionEventSource to corresponding MessageEvents which an application may react to by registering a listener to its MessageEventSource. Messages for checked exceptions are resolved by using any available ExceptionMessageResolver implementation stopping at the first implementation not returning null. For exceptions which are instances of RuntimeException an additional message informing the user that the application's state is undefined and that a restart is recommended is added to the produced MessageEvent. Optionally an additional message informing the user on how to report a bug is added to the produced MessageEvent if the instance got constructed using the constructor taking that information.

Parameters:
event the event holding the exception.
See also:
ExceptionMessageResolver.resolve(Exception)
    public void onException(final ExceptionEvent event)
    {
        if(event != null)
        {
            final Throwable exception = event.getException();
            final Throwable rootCause = event.getRootCause();
            final Messages messages = new Messages();
            messages.addMessage(new ExceptionMessage(exception));
            if(rootCause instanceof RuntimeException)
            {
                messages.addMessage(new UndefinedApplicationStateMessage());
                if(this. != null)
                {
                    messages.addMessage(new BugReportMessage(this.,
                        this.this.));
                }
            }
            else if(rootCause instanceof Exception)
            {
                final Message[] resolved =
                    this.resolveMessages((ExceptionrootCause);
                if(resolved != null)
                {
                    messages.addMessages(resolved);
                }
            }
            this.getApplicationLogger().log(new MessageEvent(this,
                messages.getMessages(), MessageEvent.ERROR));
        }
    }
    //-------------------------------------------------------ExceptionListener--
    //--ExceptionReporter-------------------------------------------------------

    
Directory holding the application's log files.

Serial:
    private File logDirectory;

    
URL of the online bugtracking system.

Serial:
    private URL trackerUrl;

    
Mail address to send the bugreport to.

Serial:
    private String reportAddress;

    
Creates a new ExceptionMessageProducer instance.
    public ExceptionMessageProducer()
    {
        super();
        this.initializeProperties(.getProperties());
        this. = null;
        this. = null;
        this. = null;
    }

    
Creates a new ExceptionMessageProducer instance taking the application's logfile directory, an URL to the application's online bugtracking system, and an email address where to send bugreports to alternatively.

Parameters:
logDirectory the directory holding the application's logfiles.
trackerUrl an URL to the application's online bugtracking system.
reportAddress an email address to alternatively send bugreports to.
Throws:
NullPointerException if either logDirectory, trackerUrl or reportAddress is null.
IllegalArgumentException if logDirectory is not a directory.
    public ExceptionMessageProducer(final File logDirectory,
        final URL trackerUrlfinal String reportAddress)
    {
        super();
        if(logDirectory == null)
        {
            throw new NullPointerException("logDirectory");
        }
        if(!logDirectory.isDirectory())
        {
            throw new IllegalArgumentException(logDirectory.getAbsolutePath());
        }
        if(trackerUrl == null)
        {
            throw new NullPointerException("trackerUrl");
        }
        if(reportAddress == null)
        {
            throw new NullPointerException("reportAddress");
        }
        this.initializeProperties(.getProperties());
        this. = logDirectory;
        this. = trackerUrl;
        this. = reportAddress;
    }

    
Resolves application messages for a given exception by querying any available ExceptionMessageResolver implementation stopping at the first implementation not returning null.

Parameters:
exception the exception to resolve application messages for.
Throws:
NullPointerException if exception is null.
    private Message[] resolveMessages(final Exception exception)
    {
        if(exception == null)
        {
            throw new NullPointerException("exception");
        }
        Message[] messages = null;
        final Specification spec = ModelFactory.getModel().getModules().
            getSpecification(ExceptionMessageResolver.class.getName());
        final Implementation[] resolvers = spec.getImplementations().
            getImplementations();
        for(int i = resolvers.length - 1; i >= 0 && messages == nulli--)
        {
            final ExceptionMessageResolver resolver =
                (ExceptionMessageResolver) ContainerFactory.getContainer().
                getImplementation(ExceptionMessageResolver.class,
                resolvers[i].getName());
            messages = resolver.resolve(exception);
        }
        return messages;
    }
    //-------------------------------------------------------ExceptionReporter--
New to GrepCode? Check out our FAQ X