Package org.quickserver.net.client.monitoring.impl

Source Code of org.quickserver.net.client.monitoring.impl.SocketMonitor

/*
* This file is part of the QuickServer library
* Copyright (C) QuickServer.org
*
* Use, modification, copying and distribution of this software is subject to
* the terms and conditions of the GNU Lesser General Public License.
* You should have received a copy of the GNU LGP License along with this
* library; if not, you can download a copy from <http://www.quickserver.org/>.
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* visit http://www.quickserver.org
*
*/
package org.quickserver.net.client.monitoring.impl;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLSocketFactory;
import org.quickserver.net.client.BlockingClient;
import org.quickserver.net.client.Host;
import org.quickserver.net.client.monitoring.HostMonitor;
import org.quickserver.net.client.SocketBasedHost;

/**
* @since 1.4.8
* @author Akshathkumar Shetty
*/
public class SocketMonitor implements HostMonitor {
  private static final Logger logger = Logger.getLogger(SocketMonitor.class.getName());
 
  private boolean useDummyTrustManager;
  private SSLSocketFactory sslSocketFactory;
 

  public char monitor(Host host) {   
    SocketBasedHost socketBasedHost = (SocketBasedHost) host;
    BlockingClient client = new BlockingClient();
    try {   
      client.setSecure(socketBasedHost.isSecure());
      client.setUseDummyTrustManager(isUseDummyTrustManager());
     
      if(getSslSocketFactory()!=null) {
        client.setSslSocketFactory(getSslSocketFactory());
      }
     
      client.connect(socketBasedHost.getInetAddress().getHostAddress(),
          socketBasedHost.getInetSocketAddress().getPort());
     
      if(socketBasedHost.getTextToExpect()!=null) {
        String textGot = client.readBytes();
        if(textGot.indexOf(socketBasedHost.getTextToExpect())!=-1) {
          return Host.ACTIVE;
        } else {
          logger.fine(socketBasedHost+" Error: Text ["+socketBasedHost.getTextToExpect()
              +"]Not found! Got: "+textGot);
          return Host.DOWN;
        }
      } else {     
        return Host.ACTIVE;
      }
    } catch (IOException e) {
      logger.fine(socketBasedHost+" IOError: "+e);
      return Host.DOWN;
    } catch (Exception e) {
      logger.warning(socketBasedHost+" Error: "+e);
      return Host.ERROR;
    } finally {
      if(client!=null) {
        try {
          client.close();
        } catch (IOException ex) {
          Logger.getLogger(SocketMonitor.class.getName()).log(Level.FINE, "Error", ex);
        }
      }
      host.setLastCheckedOn(new Date());
    }   
  }
 
  public boolean isUseDummyTrustManager() {
    return useDummyTrustManager;
  }

  public void setUseDummyTrustManager(boolean useDummyTrustManager) {
    this.useDummyTrustManager = useDummyTrustManager;
  }
 
  public SSLSocketFactory getSslSocketFactory() {
    return sslSocketFactory;
  }

  public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
    this.sslSocketFactory = sslSocketFactory;
  }
}
TOP

Related Classes of org.quickserver.net.client.monitoring.impl.SocketMonitor

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.