Package org.openxdas.log4j

Source Code of org.openxdas.log4j.XDasAppender

//------------------------------------------------------------------------------
// Desc:   
//
// Copyright (c) 2007 Novell, Inc. All Rights Reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Novell nor the names of its contributors
//       may be used to endorse or promote products derived from this
//       software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//------------------------------------------------------------------------------
package org.openxdas.log4j;

import java.io.IOException;
import java.net.SocketException;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;
import org.openxdas.client.XDasException;
import org.openxdas.client.XDasRecord;
import org.openxdas.client.XDasSession;

/**
* XDasAppender send OpenXDAS event to OpenXDAS client through log4j logging methods.
*/
public class XDasAppender extends AppenderSkeleton
{
    XDasSession m_xdasSession = null;

    String m_originator = null;

    String m_originatorLocationName = null;

    String m_originatorLocationAddress = null;

    String m_originatorServiceType = null;

    String m_originatorAuthAuthority = null;

    String m_originatorPrincipalName = null;

    String m_originatorPrincipalIdentity = null;

    //boolean m_enableXDas = false;

    /**
     * Set OpenXDAS session.
     *
     * @param sOriginator
     * @throws XDasException
     * @throws SocketException
     * @throws IOException
     */
    public synchronized void setXDasSession(String sOriginator)
            throws XDasException, SocketException, IOException
    {
        reset();
        m_xdasSession = new XDasSession(sOriginator);
        m_originator = sOriginator;
    }

    /**
     * Initialize OpenXDAS session.
     *
     * @param sOriginatorLocationName
     * @param sOriginatorLocationAddress
     * @param sOriginatorServiceType
     * @param sOriginatorAuthAuthority
     * @param sOriginatorPrincipalName
     * @param sOriginatorPrincipalIdentity
     * @throws XDasException
     * @throws SocketException
     * @throws IOException
     */
    public synchronized void setXDasSession(String sOriginatorLocationName,
            String sOriginatorLocationAddress, String sOriginatorServiceType,
            String sOriginatorAuthAuthority, String sOriginatorPrincipalName,
            String sOriginatorPrincipalIdentity) throws XDasException,
            SocketException, IOException
    {
        reset();
        m_xdasSession = new XDasSession(sOriginatorLocationName,
                sOriginatorLocationAddress, sOriginatorServiceType,
                sOriginatorAuthAuthority, sOriginatorPrincipalName,
                sOriginatorPrincipalIdentity);
        m_originatorLocationName = sOriginatorLocationName;
        m_originatorLocationAddress = sOriginatorLocationAddress;
        m_originatorServiceType = sOriginatorServiceType;
        m_originatorAuthAuthority = sOriginatorAuthAuthority;
        m_originatorPrincipalName = sOriginatorPrincipalName;
        m_originatorPrincipalIdentity = sOriginatorPrincipalIdentity;
    }

    /**
     * Append a message to the log.
     *
     * @see <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/AppenderSkeleton.html#append(org.apache.log4j.spi.LoggingEvent)">org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)</a>
     */
    protected void append(LoggingEvent event)
    {
        LogLog.debug("In XDasAppender:append");
        System.out.println("In XDasAppender:append");
        if (!isXDasEnabled()) {
            LogLog.debug("XDas was not enabled");
            LogLog.debug("m_enableXDas is false");
            return;
        } else if (m_xdasSession == null) {
            LogLog.debug("m_xdasSession is null... reconnect");
            System.out.println("m_xdasSession is null... reconnect");
            reconnect();
            if (!isXDasEnabled())
                return;
        }

        XDasRecord xdasRecord = null;

        Object o = event.getMessage();

        if (o instanceof XDasRecord) {
            try {
                xdasRecord = (XDasRecord) o;
                if (xdasRecord.getXDasSession() == null)
                    xdasRecord.setXDasSession(m_xdasSession);
                xdasRecord.commit();
            } catch (XDasException e) {
                String errorMsg = "Error appending to OpenXDAS."
                        + "XDas Error: " + e.getStatus() + ", Minor Code: "
                        + e.getMinorStatus();
                LogLog.error(errorMsg, e);
                errorHandler.error(errorMsg, e, 0);
            } catch (IOException e) {
                String errorMsg = "Error appending to OpenXDAS.";
                LogLog.error(errorMsg, e);
                errorHandler.error(errorMsg, e, 0);
            }
        }
    }

    /**
     * Reconnect to OpenXDAS client if the OpenXDAS session is lost.
     */
    public void reconnect()
    {
      if (m_xdasSession != null)
            return;
      //m_enableXDas = true;
        try {
            if (m_originator != null) {
                setXDasSession(m_originator);
            } else if (m_originatorLocationName != null) {
                setXDasSession(m_originatorLocationName,
                        m_originatorLocationAddress, m_originatorServiceType,
                        m_originatorAuthAuthority, m_originatorPrincipalName,
                        m_originatorPrincipalIdentity);
            } else {
              // session has not been setup
              //m_enableXDas = false;
                m_xdasSession = null;
            }
           
        } catch (Exception e) {
            LogLog.error("Could not set XDasSession " + e);
            errorHandler.error("Could not set XDasSession ", e, 0);
           // m_enableXDas = false;
        }
    }

    /**
     * Close the OpenXDAS session.
     *
     * @see <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Appender.html#close()">org.apache.log4j.Appender#close()</a>
     */
    public void close()
    {
        reset();
    }

    /**
     * Determine if this appender requires a layout.
     *
     * @return true if layout is required.
     * @see <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Appender.html#requiresLayout()">org.apache.log4j.Appender#requiresLayout()</a>
     */
    public boolean requiresLayout()
    {
        return false;
    }

    void reset()
    {
        if (m_xdasSession == null)
            return;
        try {
            m_xdasSession.close();
        } catch (SocketException se) {
            LogLog.error("Could not close XDasSession " + se);
        } catch (IOException ie) {
            LogLog.error("Could not close XDasSession" + ie);
        }
        m_xdasSession = null;
        m_originator = null;

        m_originatorLocationName = null;
        m_originatorLocationAddress = null;
        m_originatorServiceType = null;
        m_originatorAuthAuthority = null;
        m_originatorPrincipalName = null;
        m_originatorPrincipalIdentity = null;
    }
   
   
    /**
     * Get the current OpenXDAS session.
     *
     * @return openxdas session.
     */
    public XDasSession getXDasSession() {
        return m_xdasSession;
    }

    /**
     * Check if OpenXDAS session is valid.
     *
     * @return true if session is valid
     */
  public boolean isXDasEnabled() {
    return (m_xdasSession != null);
  }
}
TOP

Related Classes of org.openxdas.log4j.XDasAppender

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.