001/*
002// Name
003//      $RCSfile: EventServiceNotifier.java,v $
004//
005// Copyright
006//      Confidential Property of Kabira Technologies, Inc.
007//      Copyright 2010, 2011 by Kabira Technologies, Inc.
008//      All rights reserved.
009//
010// History
011//      $Revision: 1.1.2.8 $ $Date: 2011/07/29 03:36:11 $
012//
013 */
014
015package com.kabira.platform.logging;
016
017import com.kabira.platform.component.Notifier;
018import com.kabira.platform.swbuiltin.TransactionNotifier;
019import com.kabira.platform.swbuiltin.ObjectServices;
020
021import java.util.logging.Logger;
022import java.util.logging.Handler;
023import java.util.HashMap;
024
025/**
026 * Logging service component notifier
027 * <p>
028 * Installs the default log handler.
029 */
030public class EventServiceNotifier extends Notifier
031{
032    /**
033     * Create the logging service component notifier
034     */
035    public EventServiceNotifier() { }
036
037    /**
038     * Pre configuration initialization
039     * <p>
040     * Removes all currently installed log handlers and installs the default
041     * log handler
042     */
043    @Override
044    public void preConfigurationInitialize()
045    {
046                Logger          log = Logger.getLogger("");
047                Handler[]       hlist = log.getHandlers();
048
049                for (Handler h : hlist)
050                {
051                log.removeHandler(h);
052                }
053                log.addHandler(new com.kabira.platform.logging.Handler());
054
055                // Create transaction notifier which restores the
056                // original log handlers if the transactoin rolls back
057                new EventServiceTransactionNotifier(hlist);
058        }
059
060        static class EventServiceTransactionNotifier extends TransactionNotifier
061        {
062                // Static map to store the log handlers to be restored on 
063                // transaction rollback.  Keyed by the object identifier
064                // of the transaction notifier instance responsible for
065                // restoring the handlers.
066                // 
067                private final static HashMap<String,Handler[]> m_handlerMap = new HashMap<String,Handler[]>();
068
069                EventServiceTransactionNotifier(Handler[] oldHandlers)
070                {
071                        m_handlerMap.put(ObjectServices.objectToString(this), oldHandlers);
072                }
073
074                @Override
075                public void onCommit()
076                {
077                        // Clear the old log handlers saved in case of rollback 
078                        m_handlerMap.remove(ObjectServices.objectToString(this));
079                }
080                
081                //
082                // Restore the log handlers service registration artifacts on rollback
083                //
084                @Override
085                public void onRollback()
086                {
087                        Logger log = Logger.getLogger("");
088                        Handler[]       hlist = log.getHandlers();
089
090                        for (Handler h : hlist)
091                        {
092                        log.removeHandler(h);
093                        }
094
095                        hlist = m_handlerMap.remove(ObjectServices.objectToString(this));
096                        for (Handler h : hlist)
097                        {
098                                log.addHandler(h);
099                        }
100                }
101        }
102}