All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.osgi.internal.container.InternalUtils Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2012, 2013 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.internal.container;

import java.security.Permission;
import java.util.*;
import org.osgi.framework.*;
import org.osgi.framework.namespace.*;
import org.osgi.framework.wiring.*;
import org.osgi.resource.*;

public class InternalUtils {

	/**
	 * Coerce the generic type of a list from List
	 * to List
	 * @param l List to be coerced.
	 * @return l coerced to List
	 */
	@SuppressWarnings("unchecked")
	public static List asListCapability(List l) {
		return (List) l;
	}

	/**
	 * Coerce the generic type of a list from List
	 * to List
	 * @param l List to be coerced.
	 * @return l coerced to List
	 */
	@SuppressWarnings("unchecked")
	public static List asListRequirement(List l) {
		return (List) l;
	}

	/**
	 * Coerce the generic type of a list from List
	 * to List
	 * @param l List to be coerced.
	 * @return l coerced to List
	 */
	@SuppressWarnings("unchecked")
	public static List asListBundleCapability(List l) {
		return (List) l;
	}

	/**
	 * Coerce the generic type of a list from List
	 * to List
	 * @param l List to be coerced.
	 * @return l coerced to List
	 */
	@SuppressWarnings("unchecked")
	public static List asListBundleRequirement(List l) {
		return (List) l;
	}

	/**
	 * Coerce the generic type of a list from List
	 * to List
	 * @param l List to be coerced.
	 * @return l coerced to List
	 */
	@SuppressWarnings("unchecked")
	public static List asListBundleWire(List l) {
		return (List) l;
	}

	/**
	 * Coerce the generic type of a list from List
	 * to List
	 * @param l List to be coerced.
	 * @return l coerced to List
	 */
	@SuppressWarnings("unchecked")
	public static List asListWire(List l) {
		return (List) l;
	}

	/**
	 * Coerce the generic type of a list from List
	 * to List
	 * @param l List to be coerced.
	 * @return l coerced to List
	 */
	@SuppressWarnings("unchecked")
	public static List asListBundleRevision(List l) {
		return (List) l;
	}

	/**
	 * Coerce the generic type of a collection from Collection
	 * to Collection
	 * @param c List to be coerced.
	 * @return c coerced to Collection
	 */
	@SuppressWarnings("unchecked")
	public static Collection asCollectionResource(Collection c) {
		return (Collection) c;
	}

	public static void filterCapabilityPermissions(Collection capabilities) {
		if (System.getSecurityManager() == null) {
			return;
		}
		for (Iterator iCapabilities = capabilities.iterator(); iCapabilities.hasNext();) {
			BundleCapability capability = iCapabilities.next();
			Permission permission = getProvidePermission(capability);
			Bundle provider = capability.getRevision().getBundle();
			if (provider != null && !provider.hasPermission(permission)) {
				iCapabilities.remove();
			}
		}
	}

	public static Permission getRequirePermission(BundleCapability candidate) {
		String name = candidate.getNamespace();
		if (PackageNamespace.PACKAGE_NAMESPACE.equals(name)) {
			return new PackagePermission(getPermisionName(candidate), candidate.getRevision().getBundle(), PackagePermission.IMPORT);
		}
		if (HostNamespace.HOST_NAMESPACE.equals(name)) {
			return new BundlePermission(getPermisionName(candidate), BundlePermission.FRAGMENT);
		}
		if (BundleNamespace.BUNDLE_NAMESPACE.equals(name)) {
			return new BundlePermission(getPermisionName(candidate), BundlePermission.REQUIRE);
		}
		return new CapabilityPermission(name, candidate.getAttributes(), candidate.getRevision().getBundle(), CapabilityPermission.REQUIRE);
	}

	public static Permission getProvidePermission(BundleCapability candidate) {
		String name = candidate.getNamespace();
		if (PackageNamespace.PACKAGE_NAMESPACE.equals(name)) {
			return new PackagePermission(getPermisionName(candidate), PackagePermission.EXPORTONLY);
		}
		if (HostNamespace.HOST_NAMESPACE.equals(name)) {
			return new BundlePermission(getPermisionName(candidate), BundlePermission.HOST);
		}
		if (BundleNamespace.BUNDLE_NAMESPACE.equals(name)) {
			return new BundlePermission(getPermisionName(candidate), BundlePermission.PROVIDE);
		}
		return new CapabilityPermission(name, CapabilityPermission.PROVIDE);
	}

	private static String getPermisionName(BundleCapability candidate) {
		Object name = candidate.getAttributes().get(candidate.getNamespace());
		if (name instanceof String) {
			return (String) name;
		}
		if (name instanceof Collection) {
			Collection names = (Collection) name;
			return names.isEmpty() ? "unknown" : names.iterator().next().toString(); //$NON-NLS-1$
		}
		return "unknown"; //$NON-NLS-1$
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy