org.eclipse.osgi.internal.permadmin.PermissionInfoCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aspectjtools Show documentation
Show all versions of aspectjtools Show documentation
Tools from the AspectJ project
/*******************************************************************************
* Copyright (c) 2008, 2021 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.osgi.internal.permadmin;
import java.io.File;
import java.io.FilePermission;
import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.AllPermission;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import org.osgi.service.permissionadmin.PermissionInfo;
public final class PermissionInfoCollection extends PermissionCollection {
private static final long serialVersionUID = 3140511562980923957L;
/* Used to find permission constructors in addPermissions */
static private final Class> twoStringClassArray[] = new Class[] {String.class, String.class};
static private final Class> oneStringClassArray[] = new Class[] {String.class};
static private final Class> noArgClassArray[] = new Class[] {};
static private final Class>[][] permClassArrayArgs = new Class[][] {noArgClassArray, oneStringClassArray, twoStringClassArray};
static private final String ALL_PERMISSION_NAME = AllPermission.class.getName();
static final String FILE_PERMISSION_NAME = FilePermission.class.getName();
static final String ALL_FILES = "<>"; //$NON-NLS-1$
/* @GuardedBy(cachedPermissionCollections) */
private final Map, PermissionCollection> cachedPermissionCollections = new HashMap<>();
private final Map cachedRelativeFilePermissionCollections;
private final boolean hasAllPermission;
private final PermissionInfo[] permInfos;
public PermissionInfoCollection(PermissionInfo[] permInfos) {
this.permInfos = permInfos;
boolean tempAllPermissions = false;
boolean allAbsolutePaths = true;
for (PermissionInfo info : permInfos) {
if (ALL_PERMISSION_NAME.equals(info.getType())) {
tempAllPermissions = true;
} else if (FILE_PERMISSION_NAME.equals(info.getType())) {
if (!(new File(info.getActions()).isAbsolute())) {
allAbsolutePaths = false;
}
}
}
this.hasAllPermission = tempAllPermissions;
this.cachedRelativeFilePermissionCollections = allAbsolutePaths ? null : new HashMap<>();
setReadOnly(); // collections are managed with ConditionalPermissionAdmin
}
@Override
public void add(Permission arg0) {
throw new SecurityException();
}
@Override
public Enumeration elements() {
// TODO return an empty enumeration for now;
return Collections.emptyEnumeration();
}
@Override
public boolean implies(Permission perm) {
return implies(null, perm);
}
boolean implies(final BundlePermissions bundlePermissions, Permission perm) {
if (hasAllPermission)
return true;
final Class extends Permission> permClass = perm.getClass();
PermissionCollection collection = getCachedCollection(bundlePermissions, permClass);
// must populate the collection outside of the lock to prevent class loader deadlock
if (collection == null) {
collection = perm.newPermissionCollection();
if (collection == null) {
collection = new PermissionsHash();
}
try {
final PermissionCollection targetCollection = collection;
AccessController.doPrivileged((PrivilegedExceptionAction