<commit_before>/* Copyright 2004 The JA-SIG Collaborative.  All rights reserved.
*  See license distributed with this file and
*  available online at http://www.uportal.org/license.html
*/

package org.jasig.portal.utils;

import java.io.InterruptedIOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class checks a URL or a webserver hosting a URL. It only allows a specific time allocated for
 * connecting to the URL rather than waiting for a timeout.
 * This class uses the <code>java.util.Timer</code> to schedule a task which is cancelling the attempt of calling
 * httpURLConnection.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Revision$
 * @since uPortal 2.2
 */

public class AddressTester {
    
    private static final Log log = LogFactory.getLog(AddressTester.class);
    
    /**The timer object that takes a timerTask as a parameter when constructed*/
    private static final Timer timer = new Timer();

    /**The connection thread inwhich the connection attempt is made*/
    Thread connectionThread;

    /* urlConnectio to be used */
    private HttpURLConnection urlConnect = null;

    /**The connectioncode returned from connetion attempt*/
    private int connectionCode = 0;

    /**This the url to try. the value is set through the class constructor*/
    private String urlToTry = "";

    /**The amount of time connection attempt can take, the default is 100 ms*/
    int timeToWait = 100;
    static final int defaultTimeToWait = 100;

    /** Get header data only **/
    private boolean headOnly = false;


    /** Debug the code */
    private static boolean DEBUG = false;

    static {
      Runtime.getRuntime().addShutdownHook(new Thread(
      "AddressTester JVM shutdown hook") {
        public void run() {
          timer.cancel();
        }
      });
    }


    /**
     * Constructor
     * @param milliSeconds the number of milliseconds to let the connectioon attempt run
     * @param urlString the String representing a URL
     * @param getHead use setRequestMathod("HEAD")
     */
    public AddressTester(final String urlString, final int milliSeconds, final boolean getHead) throws Exception{
      urlToTry = urlString;
      timeToWait = milliSeconds;
      connectionThread = new Thread() {
        public void run() {
          try {
            URL url = new URL(urlToTry);
            if (DEBUG) {
              System.out.println("URL to try is " + urlToTry);
            }

            RemindTask rt = new RemindTask();
            try {
              timer.schedule(rt, timeToWait);
              urlConnect = (HttpURLConnection) url.openConnection();
              if (headOnly) {
                urlConnect.setRequestMethod("HEAD");
              }
              urlConnect.setInstanceFollowRedirects(false);
              connectionCode = urlConnect.getResponseCode();
            } catch (InterruptedIOException iie) { /* Thread Interrupt */
              if (DEBUG) {
                System.out.println("timed out on " + urlToTry);
              }
              log.info(
                                        "AddressTest::checkURL(): timed out on " +
                                        urlToTry);

            } catch (Exception e) { /* Something went wrong */
              if (DEBUG) {
                System.out.println(urlToTry + " generated exception: " +
                                   e.getMessage());
              }
              log.info(
                                        "AddressTest::checkURL(): "
                                        + urlToTry + " generated exception: " +
                                        e.getMessage());
            } finally {
              rt.cancel();
            }

          } catch (MalformedURLException mue) { /* Garbage In */
            if (DEBUG) {<commit_msg>conditionalized two info-level logging messages.<commit_after>/* Copyright 2004 The JA-SIG Collaborative.  All rights reserved.
*  See license distributed with this file and
*  available online at http://www.uportal.org/license.html
*/

package org.jasig.portal.utils;

import java.io.InterruptedIOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class checks a URL or a webserver hosting a URL. It only allows a specific time allocated for
 * connecting to the URL rather than waiting for a timeout.
 * This class uses the <code>java.util.Timer</code> to schedule a task which is cancelling the attempt of calling
 * httpURLConnection.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Revision$
 * @since uPortal 2.2
 */

public class AddressTester {
    
    private static final Log log = LogFactory.getLog(AddressTester.class);
    
    /**The timer object that takes a timerTask as a parameter when constructed*/
    private static final Timer timer = new Timer();

    /**The connection thread inwhich the connection attempt is made*/
    Thread connectionThread;

    /* urlConnectio to be used */
    private HttpURLConnection urlConnect = null;

    /**The connectioncode returned from connetion attempt*/
    private int connectionCode = 0;

    /**This the url to try. the value is set through the class constructor*/
    private String urlToTry = "";

    /**The amount of time connection attempt can take, the default is 100 ms*/
    int timeToWait = 100;
    static final int defaultTimeToWait = 100;

    /** Get header data only **/
    private boolean headOnly = false;


    /** Debug the code */
    private static boolean DEBUG = false;

    static {
      Runtime.getRuntime().addShutdownHook(new Thread(
      "AddressTester JVM shutdown hook") {
        public void run() {
          timer.cancel();
        }
      });
    }


    /**
     * Constructor
     * @param milliSeconds the number of milliseconds to let the connectioon attempt run
     * @param urlString the String representing a URL
     * @param getHead use setRequestMathod("HEAD")
     */
    public AddressTester(final String urlString, final int milliSeconds, final boolean getHead) throws Exception{
      urlToTry = urlString;
      timeToWait = milliSeconds;
      connectionThread = new Thread() {
        public void run() {
          try {
            URL url = new URL(urlToTry);
            if (DEBUG) {
              System.out.println("URL to try is " + urlToTry);
            }

            RemindTask rt = new RemindTask();
            try {
              timer.schedule(rt, timeToWait);
              urlConnect = (HttpURLConnection) url.openConnection();
              if (headOnly) {
                urlConnect.setRequestMethod("HEAD");
              }
              urlConnect.setInstanceFollowRedirects(false);
              connectionCode = urlConnect.getResponseCode();
            } catch (InterruptedIOException iie) { /* Thread Interrupt */
              if (DEBUG) {
                System.out.println("timed out on " + urlToTry);
              }
              if (log.isInfoEnabled())
                  log.info("AddressTest::checkURL(): timed out on " +
                                        urlToTry);

            } catch (Exception e) { /* Something went wrong */
              if (DEBUG) {
                System.out.println(urlToTry + " generated exception: " +
                                   e.getMessage());
              }
              if (log.isInfoEnabled())
                  log.info( "AddressTest::checkURL(): "
                                        + urlToTry + " generated exception: ", e);
            } finally {
              rt.cancel();
            }

          } catch (MalformedURLException mue) { /* Garbage In */
            if (DEBUG) {
{'fraction_non_alphanumeric': 0.09226778656126482, 'fraction_numerical': 0.003952569169960474, 'mean_word_length': 2.060090702947846, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1555240', 'n_tokens_mistral': 2088, 'n_tokens_neox': 2009, 'n_words': 673}