<commit_before>/*
 * Copyright (c) 2013-2016, 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.framework;

import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.SortedMap;

import org.osgi.framework.ServiceReference;
import org.osgi.framework.hooks.weaving.WeavingException;
import org.osgi.framework.hooks.weaving.WeavingHook;
import org.osgi.framework.hooks.weaving.WovenClass;
import org.osgi.framework.hooks.weaving.WovenClassListener;
import org.osgi.framework.wiring.BundleWiring;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

class WeavingHooks {
  final private FrameworkContext fwCtx;
  ServiceTracker<WeavingHook, TrackedWeavingHook> weavingHookTracker;
  ServiceTracker<WovenClassListener,WovenClassListener> listenerTracker;

  WeavingHooks(FrameworkContext fwCtx) {
    this.fwCtx = fwCtx;
  }

  synchronized void open() {
    if (fwCtx.debug.hooks) {
      fwCtx.debug.println("Begin Tracking Weaving Hooks");
    }

    weavingHookTracker = new ServiceTracker<WeavingHook, TrackedWeavingHook>(
        fwCtx.systemBundle.bundleContext, WeavingHook.class,
        new ServiceTrackerCustomizer<WeavingHook, TrackedWeavingHook>() {
          int cnt = 0;
          public TrackedWeavingHook addingService(
              ServiceReference<WeavingHook> reference) {
            if (cnt++ == 0) {
              openListener();
            }
            return new TrackedWeavingHook(
                fwCtx.systemBundle.bundleContext
                    .getService(reference), reference);
          }

          public void modifiedService(ServiceReference<WeavingHook> reference,
              TrackedWeavingHook service) {
          }

          public void removedService(ServiceReference<WeavingHook> reference,
              TrackedWeavingHook service) {
            if (--cnt == 0) {
              closeListener();
            }
          }
        });

    weavingHookTracker.open();
  }

  private void openListener() {
    listenerTracker = new ServiceTracker<WovenClassListener,WovenClassListener>
      (fwCtx.systemBundle.bundleContext, WovenClassListener.class, null);
    listenerTracker.open();
  }

  synchronized void close() {
    weavingHookTracker.close();
    weavingHookTracker = null;
  }

  private void closeListener() {
    listenerTracker.close();
    listenerTracker = null;
  }

  synchronized public boolean isOpen() {
    return weavingHookTracker != null;
  }

  synchronized void callHooks(WovenClassImpl wc) throws Exception {
    boolean ok = false;
    if (!isOpen()) {
      return;
    }

    if (wc.isWeavingComplete()) {
      throw new RuntimeException("ERROR!!");
    }

    try {
      final SortedMap<ServiceReference<WeavingHook>, TrackedWeavingHook> hooks = weavingHookTracker
          .getTracked();

      for (final TrackedWeavingHook twh : hooks.values()) {
        if (twh.isBlackListed())
          continue;
        try {
          twh.weave(wc);
        } catch (final WeavingException we) {
          fwCtx.frameworkError(twh.reference.getBundle(), we);
          final ClassFormatError cfe = new ClassFormatError(
              "WeavingException thrown: " + we.getMessage() + " by hook "
                  + twh.getClass().getName());
          cfe.initCause(we);
          throw cfe;
        } catch (final Throwable t) {
          fwCtx.frameworkError(twh.reference.getBundle(), t);
          twh.blacklist();
          final ClassFormatError cfe = new ClassFormatError("Exception throw: " + t
              + " while calling hook " + twh.getClass().getName());
          cfe.initCause(t);
          throw cfe;
        }
      }
      ok = true;
    } finally {
      wc.markAsComplete(ok);
    }
  }

  synchronized void callListeners(WovenClassImpl wc) {
    WovenClassListener wcl = fwCtx.perm.getWovenClassListener();
    if (wcl != null) {
      wcl.modified(wc);
    }
    if (listenerTracker == null) {
      return;
    }
    ServiceReference<WovenClassListener> [] srs = listenerTracker.getServiceReferences();
    if (srs != null) {
      for (ServiceReference<WovenClassListener> wlsr : srs) {
        try {
          fwCtx.systemBundle.bundleContext.getService(wlsr).modified(wc);
        } catch (final Throwable t) {
          fwCtx.frameworkWarning(wlsr.getBundle(), t);
        }
      }
    }
  }
<commit_msg>Remove synchronize for hook calls since it is protected by classloader<commit_after>/*
 * Copyright (c) 2013-2016, 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.framework;

import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.SortedMap;

import org.osgi.framework.ServiceReference;
import org.osgi.framework.hooks.weaving.WeavingException;
import org.osgi.framework.hooks.weaving.WeavingHook;
import org.osgi.framework.hooks.weaving.WovenClass;
import org.osgi.framework.hooks.weaving.WovenClassListener;
import org.osgi.framework.wiring.BundleWiring;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

class WeavingHooks {
  final private FrameworkContext fwCtx;
  ServiceTracker<WeavingHook, TrackedWeavingHook> weavingHookTracker;
  ServiceTracker<WovenClassListener,WovenClassListener> listenerTracker;

  WeavingHooks(FrameworkContext fwCtx) {
    this.fwCtx = fwCtx;
  }

  synchronized void open() {
    if (fwCtx.debug.hooks) {
      fwCtx.debug.println("Begin Tracking Weaving Hooks");
    }

    weavingHookTracker = new ServiceTracker<WeavingHook, TrackedWeavingHook>(
        fwCtx.systemBundle.bundleContext, WeavingHook.class,
        new ServiceTrackerCustomizer<WeavingHook, TrackedWeavingHook>() {
          int cnt = 0;
          public TrackedWeavingHook addingService(
              ServiceReference<WeavingHook> reference) {
            if (cnt++ == 0) {
              openListener();
            }
            return new TrackedWeavingHook(
                fwCtx.systemBundle.bundleContext
                    .getService(reference), reference);
          }

          public void modifiedService(ServiceReference<WeavingHook> reference,
              TrackedWeavingHook service) {
          }

          public void removedService(ServiceReference<WeavingHook> reference,
              TrackedWeavingHook service) {
            if (--cnt == 0) {
              closeListener();
            }
          }
        });

    weavingHookTracker.open();
  }

  private void openListener() {
    listenerTracker = new ServiceTracker<WovenClassListener,WovenClassListener>
      (fwCtx.systemBundle.bundleContext, WovenClassListener.class, null);
    listenerTracker.open();
  }

  synchronized void close() {
    weavingHookTracker.close();
    weavingHookTracker = null;
  }

  private void closeListener() {
    listenerTracker.close();
    listenerTracker = null;
  }

  /**
   * Call hooks
   * Called by from a syncronized BundleClassLoader
   *
   * @param wc
   */
  void callHooks(WovenClassImpl wc) throws Exception {
    boolean ok = false;
    final SortedMap<ServiceReference<WeavingHook>, TrackedWeavingHook> hooks;

    if (wc.isWeavingComplete()) {
      throw new RuntimeException("ERROR!!");
    }

    synchronized(this) {
      if (weavingHookTracker == null) {
        return;
      }
      hooks = weavingHookTracker.getTracked();
    }

    try {
      for (final TrackedWeavingHook twh : hooks.values()) {
        if (twh.isBlackListed())
          continue;
        try {
          twh.weave(wc);
        } catch (final WeavingException we) {
          fwCtx.frameworkError(twh.reference.getBundle(), we);
          final ClassFormatError cfe = new ClassFormatError(
              "WeavingException thrown: " + we.getMessage() + " by hook "
                  + twh.getClass().getName());
          cfe.initCause(we);
          throw cfe;
        } catch (final Throwable t) {
          fwCtx.frameworkError(twh.reference.getBundle(), t);
          twh.blacklist();
          final ClassFormatError cfe = new ClassFormatError("Exception throw: " + t
              + " while calling hook " + twh.getClass().getName());
          cfe.initCause(t);
          throw cfe;
        }
      }
      ok = true;
    } finally {
      wc.markAsComplete(ok);
    }
  }

  /**
   * Call listeners
   * Called indirectly from a syncronized BundleClassLoader
   *
   * @param wc
   */
  void callListeners(WovenClassImpl wc) {
    WovenClassListener wcl = fwCtx.perm.getWovenClassListener();
    ServiceReference<WovenClassListener> [] srs;
    if (wcl != null) {
      wcl.modified(wc);
    }
    synchronized (this) {
      if (listenerTracker == null) {
        return;
      }
      srs = listenerTracker.getServiceReferences();
    }
    if (srs != null) {
      for (ServiceReference<WovenClassListener> wlsr : srs) {
        try {
          fwCtx.systemBundle.bundleContext.getService(wlsr).modified(wc);
        } catch (final Throwable t) {
          fwCtx.frameworkWarning(wlsr.getBundle(), t);
        }
      }
    }
  }

{'fraction_non_alphanumeric': 0.0867951923852545, 'fraction_numerical': 0.001774622892635315, 'mean_word_length': 2.976266837716485, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4669199', 'n_tokens_mistral': 3520, 'n_tokens_neox': 3407, 'n_words': 956}