/*
 * Copyright 2005 Niclas Hedhman.
 *
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ops4j.pax.logging.internal;

import java.util.Hashtable;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import org.ops4j.pax.logging.PaxLoggingService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ManagedService;
import org.osgi.service.log.LogReaderService;
import org.osgi.service.log.LogService;

/**
 * Starts the Log4j log services.
 */
public class Activator
    implements BundleActivator
{
    /**
     * The Managed Service PID for the log4j configuration
     */
    public static final String CONFIGURATION_PID = "org.ops4j.pax.logging";

    /**
     * Reference to the registered service
     */
    private ServiceRegistration m_RegistrationPaxLogging;
    private JdkHandler m_JdkHandler;
    private ServiceRegistration m_registrationLogReaderService;
    private FrameworkHandler m_frameworkHandler;

    /**
     * Default constructor
     */
    public Activator()
    {
    }

    /**
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    public void start( BundleContext bundleContext )
        throws Exception
    {
        // register the LogReaderService
        LogReaderServiceImpl logReader = new LogReaderServiceImpl();
        m_registrationLogReaderService =
            bundleContext.registerService( LogReaderService.class.getName(), logReader, null );

        // register the Pax Logging service
        Log4jConfiguratorImpl log4jConfigurator = new Log4jConfiguratorImpl();
        PaxLoggingServiceImpl paxLogging = new PaxLoggingServiceImpl( logReader );
        final LoggingServiceConfiguration loggingServiceConfig = new LoggingServiceConfiguration( log4jConfigurator );
        final LoggingServiceFactory loggingServiceFactory =
            new LoggingServiceFactory( loggingServiceConfig, paxLogging );

        String[] services =
            {
                LogService.class.getName(),
                org.knopflerfish.service.log.LogService.class.getName(),
                PaxLoggingService.class.getName()
            };

        Hashtable srProperties = new Hashtable();
        m_RegistrationPaxLogging = bundleContext.registerService( services, loggingServiceFactory, srProperties );
        // Register the logging service configuration
        Hashtable configProperties = new Hashtable();
        configProperties.put( Constants.SERVICE_ID, "org.ops4j.pax.logging.configuration" );
        configProperties.put( Constants.SERVICE_PID, CONFIGURATION_PID );
        bundleContext.registerService( LoggingServiceConfiguration.class.getName(), loggingServiceConfig, configProperties );
        // Add a global handler for all JDK Logging (java.util.logging).
        m_JdkHandler = new JdkHandler( paxLogging );
        Logger rootLogger = LogManager.getLogManager().getLogger( "" );
        rootLogger.addHandler( m_JdkHandler );

        m_frameworkHandler = new FrameworkHandler( paxLogging );
        bundleContext.addBundleListener( m_frameworkHandler );
        bundleContext.addFrameworkListener( m_frameworkHandler );
        bundleContext.addServiceListener( m_frameworkHandler );
    }

    /**
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop( BundleContext bundleContext )
        throws Exception
    {
        // Clean up the listeners.
        bundleContext.removeBundleListener( m_frameworkHandler );
        bundleContext.removeFrameworkListener( m_frameworkHandler );
        bundleContext.removeServiceListener( m_frameworkHandler );

        // Remove the global handler for all JDK Logging (java.util.logging).
        Logger rootLogger = LogManager.getLogManager().getLogger( "" );
        rootLogger.removeHandler( m_JdkHandler );
        m_JdkHandler.flush();
        m_JdkHandler.close();
        m_JdkHandler = null;
        m_RegistrationPaxLogging.unregister();
        m_registrationLogReaderService.unregister();
    }
}

@@ -1192,16 +1192,17 @@
 vator%0A%7B%0A
+%0A
     /**%0A
@@ -1345,16 +1345,389 @@
 ging%22;%0A%0A
+    private static final String%5B%5D LOGFACTORY_SERVICE_NAMES = %7B%0A        LogService.class.getName(),%0A        org.knopflerfish.service.log.LogService.class.getName(),%0A        PaxLoggingService.class.getName()%0A    %7D;%0A%0A    private static final String%5B%5D CONFIG_SERVICE_NAMES = %7B%0A        ManagedService.class.getName(),%0A        LoggingServiceConfiguration.class.getName()%0A    %7D;%0A%0A
     /**%0A
@@ -1984,16 +1984,70 @@
 Handler;
+%0A    private ServiceRegistration m_configRegistration;
 %0A%0A    /*
@@ -3019,233 +3019,8 @@
 );%0A%0A
-        String%5B%5D services =%0A            %7B%0A                LogService.class.getName(),%0A                org.knopflerfish.service.log.LogService.class.getName(),%0A                PaxLoggingService.class.getName()%0A            %7D;%0A%0A
     
@@ -3099,16 +3099,28 @@
 ogging =
+%0A           
  bundleC
@@ -3143,24 +3143,40 @@
 ervice( 
-services
+LOGFACTORY_SERVICE_NAMES
 , loggin
@@ -3200,32 +3200,33 @@
 srProperties );%0A
+%0A
         // Regis
@@ -3496,82 +3496,94 @@
     
-bundleContext.registerService( LoggingServiceConfiguration.class.getName()
+m_configRegistration =%0A            bundleContext.registerService( CONFIG_SERVICE_NAMES
 , lo
@@ -3618,24 +3618,25 @@
 operties );%0A
+%0A
         // A
@@ -4846,16 +4846,17 @@
 = null;%0A
+%0A
         
@@ -4908,49 +4908,219 @@
   m_
-registrationLogReaderService.unregister()
+RegistrationPaxLogging = null;%0A%0A        m_configRegistration.unregister();%0A        m_configRegistration = null;%0A%0A        m_registrationLogReaderService.unregister();%0A        m_registrationLogReaderService = null
 ;%0A  

Fix registration of [LoggingServiceConfiguration] with both service names * ManagedService.class.getName() * LoggingServiceConfiguration.class.getName() Pax logging can now be configured from Configuration Admin