use of java.net.Authenticator in project cxf by apache.
the class CXFAuthenticatorCleanupTest method runCleanupTestStrongRef.
@Test
public void runCleanupTestStrongRef() throws Exception {
final List<Integer> traceLengths = new ArrayList<>();
// create a chain of CXFAuthenticators, strongly referenced to prevent cleanups
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
traceLengths.add(Thread.currentThread().getStackTrace().length);
return super.getPasswordAuthentication();
}
});
InetAddress add = InetAddress.getLocalHost();
Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
List<CXFAuthenticator> list = new ArrayList<>();
for (int x = 0; x < 20; x++) {
CXFAuthenticator.addAuthenticator();
list.add(CXFAuthenticator.instance);
CXFAuthenticator.instance = null;
}
Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
for (int x = 9; x > 0; x -= 2) {
list.remove(x);
}
for (int x = 0; x < 10; x++) {
System.gc();
Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
}
list.clear();
for (int x = 0; x < 10; x++) {
System.gc();
Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
}
Assert.assertEquals(22, traceLengths.size());
// first trace would be just the raw authenticator above
int raw = traceLengths.get(0);
// second would be the trace with ALL the auths
int all = traceLengths.get(1);
// after remove of 5 and some gc's
int some = traceLengths.get(11);
// after clear and gc's
int none = traceLengths.get(traceLengths.size() - 1);
// System.out.println(traceLengths);
// all should be A LOT above raw
Assert.assertTrue(all > (raw + 20 * 3));
Assert.assertTrue(all > raw);
Assert.assertTrue(all > some);
Assert.assertTrue(some > none);
Assert.assertEquals(raw, none);
}
use of java.net.Authenticator in project cxf by apache.
the class CXFAuthenticatorCleanupTest method runCleanupTestWeakRef.
@Test
public void runCleanupTestWeakRef() throws Exception {
InetAddress add = InetAddress.getLocalHost();
final List<Integer> traceLengths = new ArrayList<>();
// create a chain of CXFAuthenticators, strongly referenced to prevent cleanups
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
traceLengths.add(Thread.currentThread().getStackTrace().length);
return super.getPasswordAuthentication();
}
});
Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
for (int x = 0; x < 20; x++) {
CXFAuthenticator.addAuthenticator();
CXFAuthenticator.instance = null;
System.gc();
}
CXFAuthenticator.addAuthenticator();
System.gc();
Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
CXFAuthenticator.instance = null;
for (int x = 0; x < 10; x++) {
System.gc();
Authenticator.requestPasswordAuthentication("localhost", add, 8080, "http", "hello", "http");
}
Assert.assertEquals(12, traceLengths.size());
// first trace would be just the raw authenticator above
int raw = traceLengths.get(0);
// second trace should still have an Authenticator added
int one = traceLengths.get(1);
// after clear and gc's
int none = traceLengths.get(traceLengths.size() - 1);
// System.out.println(traceLengths);
// one should only be slightly above raw
Assert.assertTrue(one < (raw + (20 * 2)));
Assert.assertTrue(one > raw);
Assert.assertTrue(one > none);
Assert.assertEquals(raw, none);
}
use of java.net.Authenticator in project cxf by apache.
the class CXFAuthenticator method addAuthenticator.
public static synchronized void addAuthenticator() {
if (instance == null) {
instance = new CXFAuthenticator();
Authenticator wrapped = null;
for (final Field f : ReflectionUtil.getDeclaredFields(Authenticator.class)) {
if (f.getType().equals(Authenticator.class)) {
ReflectionUtil.setAccessible(f);
try {
wrapped = (Authenticator) f.get(null);
if (wrapped != null && wrapped.getClass().getName().equals(ReferencingAuthenticator.class.getName())) {
Method m = wrapped.getClass().getMethod("check");
m.setAccessible(true);
m.invoke(wrapped);
}
wrapped = (Authenticator) f.get(null);
} catch (Exception e) {
// ignore
}
}
}
try {
ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return new URLClassLoader(new URL[0], ClassLoader.getSystemClassLoader());
}
}, null);
Method m = ReflectionUtil.getDeclaredMethod(ClassLoader.class, "defineClass", String.class, byte[].class, Integer.TYPE, Integer.TYPE);
InputStream ins = ReferencingAuthenticator.class.getResourceAsStream("ReferencingAuthenticator.class");
byte[] b = IOUtils.readBytesFromStream(ins);
ReflectionUtil.setAccessible(m).invoke(loader, ReferencingAuthenticator.class.getName(), b, 0, b.length);
Class<?> cls = loader.loadClass(ReferencingAuthenticator.class.getName());
final Authenticator auth = (Authenticator) cls.getConstructor(Authenticator.class, Authenticator.class).newInstance(instance, wrapped);
if (System.getSecurityManager() == null) {
Authenticator.setDefault(auth);
} else {
AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
Authenticator.setDefault(auth);
return true;
}
});
}
try {
// clear the acc field that can hold onto the webapp classloader
Field f = ReflectionUtil.getDeclaredField(loader.getClass(), "acc");
ReflectionUtil.setAccessible(f).set(loader, null);
} catch (Throwable t) {
// ignore
}
} catch (Throwable t) {
// ignore
}
}
}
use of java.net.Authenticator in project linuxtools by eclipse.
the class RegistryAccountInfo method restoreAuthenticator.
@Override
protected void restoreAuthenticator() {
IExtension[] extensions = RegistryFactory.getRegistry().getExtensionPoint("org.eclipse.core.net", // $NON-NLS-1$ //$NON-NLS-2$
"authenticator").getExtensions();
if (extensions.length == 0) {
return;
}
IExtension extension = extensions[0];
IConfigurationElement[] configs = extension.getConfigurationElements();
if (configs.length == 0) {
return;
}
try {
IConfigurationElement config = configs[0];
Authenticator original = (Authenticator) config.createExecutableExtension(// $NON-NLS-1$
"class");
Authenticator.setDefault(original);
} catch (CoreException ex) {
}
}
Aggregations