<commit_before>package org.springframework.roo.support.osgi;

import java.net.URL;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.springframework.roo.support.util.Assert;

/**
 * Helper to locate bundle symbolic names used by Spring Roo.
 * 
 * @author <NAME>
 * @since 1.1.1
 *
 */
public abstract class BundleFindingUtils {
	
	/**
	 * Locates the first bundle that contains the presented type name.
	 * 
	 * @param context that can be used to obtain bundles to search (required)
	 * @param typeNameInExternalForm a type name (eg com.foo.Bar)
	 * @return the bundle symbolic name, if found (or null if not found)
	 */
	public static String findFirstBundleForTypeName(BundleContext context, String typeNameInExternalForm) {
		Assert.notNull(context, "Bundle context required to perform the search");
		Assert.hasText(typeNameInExternalForm, "Resource name to locate is required");
		String resourceName = "/" + typeNameInExternalForm.replace('.', '/') + ".class";

		Bundle[] bundles = context.getBundles();
		if (bundles == null) {
			return null;
		}
		
		for (Bundle bundle : bundles) {
			try {
				URL url = bundle.getEntry(resourceName);
				if (url != null) {
					return bundle.getSymbolicName();
				}
			} catch (RuntimeException e) {
				return null;
			}
		}
		
		return null;
	}
}<commit_msg>ROO-1816: BundleFindingUtils to offer ability to retrieve a Class instance from an unknown bundle<commit_after>package org.springframework.roo.support.osgi;

import java.net.URL;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.springframework.roo.support.util.Assert;

/**
 * Helper to locate bundle symbolic names used by Spring Roo.
 * 
 * @author <NAME>
 * @since 1.1.1
 *
 */
public abstract class BundleFindingUtils {
	
	/**
	 * Locates the first bundle that contains the presented type name and return its
	 * bundle symbolic name. 
	 * 
	 * @param context that can be used to obtain bundles to search (required)
	 * @param typeNameInExternalForm a type name (eg com.foo.Bar)
	 * @return the bundle symbolic name, if found (or null if not found)
	 */
	public static String findFirstBundleForTypeName(BundleContext context, String typeNameInExternalForm) {
		Assert.notNull(context, "Bundle context required to perform the search");
		Assert.hasText(typeNameInExternalForm, "Resource name to locate is required");
		String resourceName = "/" + typeNameInExternalForm.replace('.', '/') + ".class";

		Bundle[] bundles = context.getBundles();
		if (bundles == null) {
			return null;
		}
		
		for (Bundle bundle : bundles) {
			try {
				URL url = bundle.getEntry(resourceName);
				if (url != null) {
					return bundle.getSymbolicName();
				}
			} catch (RuntimeException e) {
				return null;
			}
		}
		
		return null;
	}

	/**
	 * Locates the first bundle that contains the presented type name and return
	 * that class.
	 * 
	 * @param context that can be used to obtain bundles to search (required)
	 * @param typeNameInExternalForm a type name (eg com.foo.Bar)
	 * @return the class, if found (or null if not found)
	 */
	public static Class<?> findFirstBundleWithType(BundleContext context, String typeNameInExternalForm) {
		Assert.notNull(context, "Bundle context required to perform the search");
		Assert.hasText(typeNameInExternalForm, "Resource name to locate is required");
		String resourceName = "/" + typeNameInExternalForm.replace('.', '/') + ".class";
	
		Bundle[] bundles = context.getBundles();
		if (bundles == null) {
			return null;
		}
		
		for (Bundle bundle : bundles) {
			try {
				URL url = bundle.getEntry(resourceName);
				if (url != null) {
					return bundle.loadClass(typeNameInExternalForm);
				}
			} catch (Throwable e) {
				return null;
			}
		}
		
		return null;
	}
}
{'fraction_non_alphanumeric': 0.10710526315789473, 'fraction_numerical': 0.002631578947368421, 'mean_word_length': 3.686806411837238, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '4559768', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1093, 'n_words': 362}