/*
 * Copyright 2006-2007 the original author or authors.
 *
 * 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.openspaces.pu.container.servicegrid;

import com.sun.jini.config.Config;
import net.jini.config.ConfigurationException;
import net.jini.config.ConfigurationProvider;
import org.jini.rio.resources.client.AbstractFaultDetectionHandler;

import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A processing unit fault detection handler. Invokes {@link PUServiceBean#isAlive()}.
 *
 * @author kimchy
 */
public class PUFaultDetectionHandler extends AbstractFaultDetectionHandler {

    public static final String INVOCATION_DELAY_KEY = "invocationDelay";
    /**
     * Component name, used for config and logger
     */
    private static final String COMPONENT =
            "org.openspaces.pu.container.servicegrid.PUFaultDetectionHandler";
    /**
     * A Logger
     */
    static Logger logger = Logger.getLogger(COMPONENT);

    /**
     * @see org.jini.rio.core.FaultDetectionHandler#setConfiguration
     */
    public void setConfiguration(String[] configArgs) {
        if (configArgs == null)
            throw new NullPointerException("configArgs is null");
        try {
            this.configArgs = new String[configArgs.length];
            System.arraycopy(configArgs, 0, this.configArgs, 0, configArgs.length);

            this.config = ConfigurationProvider.getInstance(configArgs);

            invocationDelay = Config.getLongEntry(config,
                    COMPONENT,
                    INVOCATION_DELAY_KEY,
                    DEFAULT_INVOCATION_DELAY,
                    0,
                    Long.MAX_VALUE);
            retryCount = Config.getIntEntry(config,
                    COMPONENT,
                    RETRY_COUNT_KEY,
                    DEFAULT_RETRY_COUNT,
                    0,
                    Integer.MAX_VALUE);
            retryTimeout = Config.getLongEntry(config,
                    COMPONENT,
                    RETRY_TIMEOUT_KEY,
                    DEFAULT_RETRY_TIMEOUT,
                    0,
                    Long.MAX_VALUE);

            if (logger.isLoggable(Level.FINEST)) {
                StringBuffer buffer = new StringBuffer();
                buffer.append("PUFaultDetectionHandler Properties : ");
                buffer.append("invocation delay=" + invocationDelay + ", ");
                buffer.append("retry count=" + retryCount + ", ");
                buffer.append("retry timeout=" + retryTimeout);
                logger.finest(buffer.toString());
            }
        } catch (ConfigurationException e) {
            logger.log(Level.SEVERE, "Setting Configuration", e);
        }
    }

    /**
     * Get the class which implements the ServiceMonitor
     */
    protected ServiceMonitor getServiceMonitor() throws Exception {
        return new ServiceAdminManager();
    }

    class ServiceAdminManager implements ServiceMonitor {

        /**
         * Its all over
         */
        public void drop() {
        }

        /**
         * Verify service can be reached. If the service cannot be reached
         * return false
         */
        public boolean verify() {
            try {
                return ((PUServiceBean) proxy).isAlive();
            } catch (RemoteException e) {
                return false;
            }
        }
    }

}

@@ -891,24 +891,49 @@
 eException;%0A
+import java.util.Arrays;%0A
 import java.
@@ -952,16 +952,16 @@
 .Level;%0A
-
 import j
@@ -2738,14 +2738,14 @@
 vel.
-FINEST
+CONFIG
 )) %7B
@@ -2906,16 +2906,19 @@
 append(%22
+%5Cn 
 invocati
@@ -2945,23 +2945,16 @@
 ionDelay
- + %22, %22
 );%0A     
@@ -2975,24 +2975,27 @@
 fer.append(%22
+%5Cn 
 retry count=
@@ -3049,16 +3049,19 @@
 append(%22
+%5Cn 
 retry ti
@@ -3085,16 +3085,97 @@
 meout);%0A
+                buffer.append(%22%5Cn configArgs: %22 + Arrays.toString( configArgs));%0A
         
@@ -3193,14 +3193,14 @@
 ger.
-finest
+config
 (buf
@@ -3680,24 +3680,165 @@
 id drop() %7B%0A
+            if (logger.isLoggable(Level.FINE)) %7B%0A                logger.fine(%22Dropping monitor for service: %5B%22 + proxy +%22%5D%22 );%0A            %7D%0A
         %7D%0A%0A 
@@ -4031,91 +4031,746 @@
     
-return ((PUServiceBean) proxy).isAlive();%0A            %7D catch (RemoteException e) %7B
+if (logger.isLoggable(Level.FINEST)) %7B%0A                    logger.finest(%22Requesting isAlive() on service: %5B%22 + proxy +%22%5D%22 );%0A                %7D%0A                %0A                final boolean isAlive = ((PUServiceBean) proxy).isAlive();%0A%0A                if (logger.isLoggable(Level.FINEST)) %7B%0A                    logger.finest(%22isAlive() succesfully returned: %5B%22+isAlive+%22%5D for service: %5B%22 + proxy +%22%5D%22 );%0A                %7D%0A                %0A                return isAlive;%0A            %7D catch (RemoteException e) %7B%0A                if(logger.isLoggable(Level.FINE)) %7B%0A                    logger.log( Level.FINE, %22RemoteException reaching service: %5B%22%0A                            + proxy + %22%5D service cannot be reached%22, e);%0A                %7D
 %0A   

Improve the logging of the PUFaultDetecationHandler.