<commit_before>/*
 * Copyright (c) 2003-2022, KNOPFLERFISH project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following
 * conditions are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following
 *   disclaimer in the documentation and/or other materials
 *   provided with the distribution.
 *
 * - Neither the name of the KNOPFLERFISH project nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.knopflerfish.bundle.http;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

import org.knopflerfish.service.log.LogRef;

// NOTE: Use raw types to avoid explicit dependency on SSLServerSocketFactory.
public class SocketListener
  implements Runnable, ServiceTrackerCustomizer
{

  // private constants

  private final static String zeroAddress = "0.0.0.0";

  // private fields

  private final HttpConfigWrapper httpConfig;

  private final LogRef log;

  private final TransactionManager transactionManager;

  private final BundleContext bc;

  private final HttpServer httpServer;

  private ServiceTracker<?, ?> securityTracker = null;

  private int port = -1;

  private String host = null;

  private int maxConnections = -1;

  private boolean isSecure = false;

  private boolean isEnabled = false;

  private Boolean requireClientAuth = null;

  private boolean done = false;

  private ServerSocket socket = null;

  private Thread thread = null;

  // constructors

  public SocketListener(final HttpConfigWrapper httpConfig, final LogRef log,
                        final TransactionManager transactionManager,
                        final BundleContext bc, final HttpServer httpServer)
  {
    this.httpConfig = httpConfig;
    this.log = log;
    this.transactionManager = transactionManager;
    this.bc = bc;
    this.httpServer = httpServer;
  }

  public void updated()
      throws ConfigurationException
  {
    // the following if statements prevents unnecessary calls to init
    // (when nothing changed)
    if (portConfigMatch(httpConfig)) {
      return;
    }

    isSecure = httpConfig.isSecure();
    requireClientAuth = httpConfig.requireClientAuth();
    port = httpConfig.getPort();
    host = httpConfig.getHost();
    maxConnections = httpConfig.getMaxConnections();
    isEnabled = httpConfig.isEnabled();

    destroy();

    if (!isEnabled) {
      return;
    }<commit_msg>Added SuppressWarnings needed by java 8<commit_after>/*
 * Copyright (c) 2003-2022, KNOPFLERFISH project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following
 * conditions are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following
 *   disclaimer in the documentation and/or other materials
 *   provided with the distribution.
 *
 * - Neither the name of the KNOPFLERFISH project nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.knopflerfish.bundle.http;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

import org.knopflerfish.service.log.LogRef;

// NOTE: Use raw types to avoid explicit dependency on SSLServerSocketFactory.
@SuppressWarnings("rawtypes")
public class SocketListener
  implements Runnable, ServiceTrackerCustomizer
{

  // private constants

  private final static String zeroAddress = "0.0.0.0";

  // private fields

  private final HttpConfigWrapper httpConfig;

  private final LogRef log;

  private final TransactionManager transactionManager;

  private final BundleContext bc;

  private final HttpServer httpServer;

  private ServiceTracker<?, ?> securityTracker = null;

  private int port = -1;

  private String host = null;

  private int maxConnections = -1;

  private boolean isSecure = false;

  private boolean isEnabled = false;

  private Boolean requireClientAuth = null;

  private boolean done = false;

  private ServerSocket socket = null;

  private Thread thread = null;

  // constructors

  public SocketListener(final HttpConfigWrapper httpConfig, final LogRef log,
                        final TransactionManager transactionManager,
                        final BundleContext bc, final HttpServer httpServer)
  {
    this.httpConfig = httpConfig;
    this.log = log;
    this.transactionManager = transactionManager;
    this.bc = bc;
    this.httpServer = httpServer;
  }

  @SuppressWarnings("unchecked")
  public void updated()
      throws ConfigurationException
  {
    // the following if statements prevents unnecessary calls to init
    // (when nothing changed)
    if (portConfigMatch(httpConfig)) {
      return;
    }

    isSecure = httpConfig.isSecure();
    requireClientAuth = httpConfig.requireClientAuth();
    port = httpConfig.getPort();
    host = httpConfig.getHost();
    maxConnections = httpConfig.getMaxConnections();
    isEnabled = httpConfig.isEnabled();

    destroy();

    if (!isEnabled) {
      return;
    }
{'fraction_non_alphanumeric': 0.06712933753943218, 'fraction_numerical': 0.0036593059936908517, 'mean_word_length': 3.8685503685503684, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4557808', 'n_tokens_mistral': 2140, 'n_tokens_neox': 1984, 'n_words': 801}