package org.rioproject.monitor;

import com.sun.jini.config.Config;
import net.jini.config.Configuration;
import org.rioproject.event.DispatchEventHandler;
import org.rioproject.event.EventHandler;
import org.rioproject.monitor.tasks.ProvisionMonitorEventTask;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Sends {@link ProvisionMonitorEvent}s
 */
public class ProvisionMonitorEventProcessor {
    /**
     * ThreadPool for sending ProvisionMonitorEvent notifications
     */
    private Executor monitorEventPool;
    /** Component name we use to find items in the configuration */
    static final String CONFIG_COMPONENT = "org.rioproject.monitor";
    static Logger logger = Logger.getLogger(ProvisionMonitorEventProcessor.class.getName());
    private EventHandler monitorEventHandler;

    public ProvisionMonitorEventProcessor(Configuration config) throws Exception {
        /*
         * Set up the pool for ProvisionMonitorEvent notifications
         */
        int provisionMonitorEventTaskPoolMaximum = 10;
        try {
            provisionMonitorEventTaskPoolMaximum =
                Config.getIntEntry(config,
                                   CONFIG_COMPONENT,
                                   "provisionMonitorEventTaskPoolMaximum",
                                   provisionMonitorEventTaskPoolMaximum,
                                   1,
                                   100);
        } catch (Throwable t) {
            logger.log(Level.WARNING,
                       "Exception getting provisionMonitorEventTaskPoolMaximum, use default of 10",
                       t);
        }
        monitorEventPool = Executors.newFixedThreadPool(provisionMonitorEventTaskPoolMaximum);
        monitorEventHandler = new DispatchEventHandler(ProvisionMonitorEvent.getEventDescriptor(), config);
    }

    public EventHandler getMonitorEventHandler() {
        return monitorEventHandler;
    }

    /**
     * Sends a ProvisionMonitorEvent using a thread obtained from a thread pool
     *
     * @param event The ProvisionMonitorEvent to send
     */
    public void processEvent(ProvisionMonitorEvent event) {
        monitorEventPool.execute(new ProvisionMonitorEventTask(monitorEventHandler, event));
    }
}

@@ -1,71 +1,650 @@
-package org.rioproject.monitor;%0A%0Aimport com.sun.jini.config.Config;
+/*%0A * Copyright to the original author or authors.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage org.rioproject.monitor;%0A
 %0Aimp
@@ -914,73 +914,8 @@
 ors;
-%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;
 %0A%0A/*
@@ -955,16 +955,44 @@
 rEvent%7Ds
+.%0A *%0A * @author Dennis Reedy
 %0A */%0Apub
@@ -1163,1196 +1163,191 @@
     
-/** Component name we use to find items in the configuration */%0A    static final String CONFIG_COMPONENT = %22org.rioproject.monitor%22;%0A    static Logger logger = Logger.getLogger(ProvisionMonitorEventProcessor.class.getName());%0A    private EventHandler monitorEventHandler;%0A%0A    public ProvisionMonitorEventProcessor(Configuration config) throws Exception %7B%0A        /*%0A         * Set up the pool for ProvisionMonitorEvent notifications%0A         */%0A        int provisionMonitorEventTaskPoolMaximum = 10;%0A        try %7B%0A            provisionMonitorEventTaskPoolMaximum =%0A                Config.getIntEntry(config,%0A                                   CONFIG_COMPONENT,%0A                                   %22provisionMonitorEventTaskPoolMaximum%22,%0A                                   provisionMonitorEventTaskPoolMaximum,%0A                                   1,%0A                                   100);%0A        %7D catch (Throwable t) %7B%0A            logger.log(Level.WARNING,%0A                       %22Exception getting provisionMonitorEventTaskPoolMaximum, use default of 10%22,%0A                       t);%0A        %7D%0A        monitorEventPool = Executors.newFixedThreadPool(provisionMonitorEventTaskPoolMaximum
+private EventHandler monitorEventHandler;%0A%0A    public ProvisionMonitorEventProcessor(Configuration config) throws Exception %7B%0A        monitorEventPool = Executors.newCachedThreadPool(
 );%0A 

Use a cached thread pool instead of a fixed thread pool