org.eclipse.osgi.internal.url.MultiplexingFactory Maven / Gradle / Ivy
Show all versions of aspectjtools Show documentation
/*******************************************************************************
* Copyright (c) 2006, 2021 Cognos Incorporated, 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
*
*******************************************************************************/
package org.eclipse.osgi.internal.url;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
import org.eclipse.osgi.internal.framework.EquinoxBundle;
import org.eclipse.osgi.internal.framework.EquinoxContainer;
import org.eclipse.osgi.storage.StorageUtil;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
/*
* An abstract class for handler factory impls (Stream and Content) that can
* handle environments running multiple osgi frameworks with the same VM.
*/
public abstract class MultiplexingFactory {
/**
* As a short-term (hopefully) solution we use a special class which is defined
* using the Unsafe class from the VM. This class is an implementation of
* Collection simply to provide a method add(AccessibleObject)
* which turns around and calls AccessibleObject.setAccessible(true).
*
* The reason this is needed is to hack into the VM to get deep reflective access to
* the java.net package for the various hacks we have to do to multiplex the
* URL and Content handlers. Note that on Java 9 deep reflection is not possible
* by default on the java.net package.
*
* The setAccessible class will be defined in the java.base module which grants
* it the ability to call setAccessible(true) on other types from the java.base module
*/
public static final Collection setAccessible;
static final Collection systemLoaders;
static {
Collection result = null;
try {
// Use reflection on Unsafe to avoid having to compile against it
Class> unsafeClass = Class.forName("sun.misc.Unsafe"); //$NON-NLS-1$
Field theUnsafe = unsafeClass.getDeclaredField("theUnsafe"); //$NON-NLS-1$
// NOTE: deep reflection is allowed on sun.misc package for java 9.
theUnsafe.setAccessible(true);
Object unsafe = theUnsafe.get(null);
// The SetAccessible bytes stored in a resource to avoid real loading of it (see SetAccessible.java.src for source).
byte[] bytes = StorageUtil.getBytes(MultiplexingFactory.class.getResource("SetAccessible.bytes").openStream(), -1, 4000); //$NON-NLS-1$
Class> collectionClass = null;
// using defineAnonymousClass here because it seems more simple to get what we need
try {
Method defineAnonymousClass = unsafeClass.getMethod("defineAnonymousClass", Class.class, byte[].class, //$NON-NLS-1$
Object[].class);
@SuppressWarnings("unchecked")
Class> unchecked = (Class>) defineAnonymousClass
.invoke(unsafe, URL.class, bytes, (Object[]) null);
collectionClass = unchecked;
} catch (NoSuchMethodException e) {
long offset = (long) unsafeClass.getMethod("staticFieldOffset", Field.class).invoke(unsafe, //$NON-NLS-1$
MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP")); //$NON-NLS-1$
MethodHandles.Lookup lookup = (MethodHandles.Lookup) unsafeClass
.getMethod("getObject", Object.class, long.class) //$NON-NLS-1$
.invoke(unsafe, MethodHandles.Lookup.class, offset);
lookup = lookup.in(URL.class);
Class> classOption = Class.forName("java.lang.invoke.MethodHandles$Lookup$ClassOption"); //$NON-NLS-1$
Object classOptions = Array.newInstance(classOption, 0);
Method defineHiddenClass = Lookup.class.getMethod("defineHiddenClass", byte[].class, boolean.class, //$NON-NLS-1$
classOptions.getClass());
lookup = (Lookup) defineHiddenClass.invoke(lookup, bytes, Boolean.FALSE, classOptions);
@SuppressWarnings("unchecked")
Class> unchecked = (Class>) lookup
.lookupClass();
collectionClass = unchecked;
}
result = collectionClass.getConstructor().newInstance();
} catch (Throwable t) {
t.printStackTrace();
// ingore as if there is no Unsafe
}
setAccessible = result;
Collection loaders = new ArrayList<>();
try {
ClassLoader cl = ClassLoader.getSystemClassLoader();
while (cl != null) {
loaders.add(cl);
cl = cl.getParent();
}
} catch (Throwable t) {
// ignore as if no loaders
}
systemLoaders = Collections.unmodifiableCollection(loaders);
}
protected EquinoxContainer container;
protected BundleContext context;
private List