<commit_before>/*******************************************************************************
 *  Copyright (c) 2012 VMware, Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *      VMware, Inc. - initial API and implementation
 *******************************************************************************/
package org.springsource.ide.eclipse.commons.internal.core.net;

import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.springsource.ide.eclipse.commons.internal.core.CorePlugin;


/**
 * @author <NAME>
 */
public class P2TransportService implements ITransportService {

	private Object transport;

	private Method downloadMethod;

	private Method streamMethod;

	private Method getLastModifiedMethod;

	public P2TransportService() throws ClassNotFoundException {
		// TODO e3.5 remove reflection
		try {
			Class<?> clazz;
			try {
				clazz = Class.forName("org.eclipse.equinox.internal.p2.repository.RepositoryTransport"); //$NON-NLS-1$
				Method getInstanceMethod = clazz.getDeclaredMethod("getInstance"); //$NON-NLS-1$
				transport = getInstanceMethod.invoke(null);
			}
			catch (ClassNotFoundException e) {
				// the class moved to a different bundle in 3.7
				transport = getTransport_e3_7();
				clazz = transport.getClass();
			}
			downloadMethod = clazz.getDeclaredMethod("download", URI.class, OutputStream.class, IProgressMonitor.class); //$NON-NLS-1$
			streamMethod = clazz.getDeclaredMethod("stream", URI.class, IProgressMonitor.class); //$NON-NLS-1$
			getLastModifiedMethod = clazz.getDeclaredMethod("getLastModified", URI.class, IProgressMonitor.class); //$NON-NLS-1$
		}
		catch (LinkageError e) {
			throw new ClassNotFoundException("Failed to load P2 transport", e); //$NON-NLS-1$
		}
		catch (Exception e) {
			throw new ClassNotFoundException("Failed to load P2 transport", e); //$NON-NLS-1$
		}
	}

	private static Object getTransport_e3_7() throws Exception {
		BundleContext bundleContext = CorePlugin.getDefault().getBundle().getBundleContext();
		ServiceReference serviceReference = bundleContext
				.getServiceReference("org.eclipse.equinox.p2.core.IProvisioningAgent");
		if (serviceReference != null) {
			try {
				Object agent = bundleContext.getService(serviceReference);
				if (agent != null) {<commit_msg>STS-2666: fix a race condition which led to using a fallback transport mechanism which didn't allow using file: URLs<commit_after>/*******************************************************************************
 *  Copyright (c) 2012 VMware, Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *      VMware, Inc. - initial API and implementation
 *******************************************************************************/
package org.springsource.ide.eclipse.commons.internal.core.net;

import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.springsource.ide.eclipse.commons.internal.core.CorePlugin;

/**
 * @author <NAME>
 */
public class P2TransportService implements ITransportService {

	private Object transport;

	private Method downloadMethod;

	private Method streamMethod;

	private Method getLastModifiedMethod;

	public P2TransportService() throws ClassNotFoundException {
		// TODO e3.5 remove reflection
		try {
			Class<?> clazz;
			try {
				clazz = Class.forName("org.eclipse.equinox.internal.p2.repository.RepositoryTransport"); //$NON-NLS-1$
				Method getInstanceMethod = clazz.getDeclaredMethod("getInstance"); //$NON-NLS-1$
				transport = getInstanceMethod.invoke(null);
			}
			catch (ClassNotFoundException e) {
				// the class moved to a different bundle in 3.7
				transport = getTransport_e3_7();
				clazz = transport.getClass();
			}
			downloadMethod = clazz.getDeclaredMethod("download", URI.class, OutputStream.class, IProgressMonitor.class); //$NON-NLS-1$
			streamMethod = clazz.getDeclaredMethod("stream", URI.class, IProgressMonitor.class); //$NON-NLS-1$
			getLastModifiedMethod = clazz.getDeclaredMethod("getLastModified", URI.class, IProgressMonitor.class); //$NON-NLS-1$
		}
		catch (LinkageError e) {
			throw new ClassNotFoundException("Failed to load P2 transport", e); //$NON-NLS-1$
		}
		catch (Exception e) {
			throw new ClassNotFoundException("Failed to load P2 transport", e); //$NON-NLS-1$
		}
	}

	private static Object getTransport_e3_7() throws Exception {
		// This line is here merely to make sure that the bundle gets activated
		// before trying to use the service (to get rid of a race condition).
		Class<?> clazz = Class.forName("org.eclipse.equinox.p2.core.IProvisioningAgent"); //$NON-NLS-1$

		BundleContext bundleContext = CorePlugin.getDefault().getBundle().getBundleContext();
		ServiceReference serviceReference = bundleContext
				.getServiceReference("org.eclipse.equinox.p2.core.IProvisioningAgent");
		if (serviceReference != null) {
			try {
				Object agent = bundleContext.getService(serviceReference);
				if (agent != null) {
{'fraction_non_alphanumeric': 0.15525849573200193, 'fraction_numerical': 0.010307617973908842, 'mean_word_length': 4.836466165413534, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7397909', 'n_tokens_mistral': 1886, 'n_tokens_neox': 1728, 'n_words': 428}