Author: alessio.soldano(a)jboss.com
Date: 2009-11-27 13:05:01 -0500 (Fri, 27 Nov 2009)
New Revision: 11164
Added:
spi/branches/jbossws-spi-1.1.2/src/main/java/org/jboss/wsf/spi/util/SecurityActions.java
Modified:
spi/branches/jbossws-spi-1.1.2/src/main/java/org/jboss/wsf/spi/util/ServiceLoader.java
Log:
[JBPAPP-3177] Cache resource name lookup through Services API
Added:
spi/branches/jbossws-spi-1.1.2/src/main/java/org/jboss/wsf/spi/util/SecurityActions.java
===================================================================
---
spi/branches/jbossws-spi-1.1.2/src/main/java/org/jboss/wsf/spi/util/SecurityActions.java
(rev 0)
+++
spi/branches/jbossws-spi-1.1.2/src/main/java/org/jboss/wsf/spi/util/SecurityActions.java 2009-11-27
18:05:01 UTC (rev 11164)
@@ -0,0 +1,142 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.wsf.spi.util;
+
+import java.io.InputStream;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+
+/**
+ * Security actions for this package
+ *
+ * @author alessio.soldano(a)jboss.com
+ * @since 19-Jun-2009
+ *
+ */
+class SecurityActions
+{
+ /**
+ * Get context classloader.
+ *
+ * @return the current context classloader
+ */
+ static ClassLoader getContextClassLoader()
+ {
+ SecurityManager sm = System.getSecurityManager();
+ if (sm == null)
+ {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ else
+ {
+ return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
{
+ public ClassLoader run()
+ {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ });
+ }
+ }
+
+ /**
+ * Set context classloader.
+ *
+ */
+ static void setContextClassLoader(final ClassLoader cl)
+ {
+ SecurityManager sm = System.getSecurityManager();
+ if (sm == null)
+ {
+ Thread.currentThread().setContextClassLoader(cl);
+ }
+ else
+ {
+ AccessController.doPrivileged(new PrivilegedAction<Object>() {
+ public Object run()
+ {
+ Thread.currentThread().setContextClassLoader(cl);
+ return null;
+ }
+ });
+ }
+ }
+
+ /**
+ * Get resource as stream
+ *
+ * @param cl
+ * @param filename
+ * @return input stream
+ * @throws PrivilegedActionException
+ */
+ static InputStream getResourceAsStream(final ClassLoader cl, final String filename)
+ {
+ SecurityManager sm = System.getSecurityManager();
+ if (sm == null)
+ {
+ return cl.getResourceAsStream(filename);
+ }
+ else
+ {
+ return AccessController.doPrivileged(new PrivilegedAction<InputStream>()
{
+ public InputStream run()
+ {
+ return cl.getResourceAsStream(filename);
+ }
+ });
+ }
+ }
+
+ /**
+ * Load a class using the provided classloader
+ *
+ * @param name
+ * @return
+ * @throws PrivilegedActionException
+ */
+ static Class<?> loadClass(final ClassLoader cl, final String name) throws
PrivilegedActionException, ClassNotFoundException
+ {
+ SecurityManager sm = System.getSecurityManager();
+ if (sm == null)
+ {
+ return cl.loadClass(name);
+ }
+ else
+ {
+ return AccessController.doPrivileged(new
PrivilegedExceptionAction<Class<?>>() {
+ public Class<?> run() throws PrivilegedActionException
+ {
+ try
+ {
+ return cl.loadClass(name);
+ }
+ catch (Exception e)
+ {
+ throw new PrivilegedActionException(e);
+ }
+ }
+ });
+ }
+ }
+}
Property changes on:
spi/branches/jbossws-spi-1.1.2/src/main/java/org/jboss/wsf/spi/util/SecurityActions.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified:
spi/branches/jbossws-spi-1.1.2/src/main/java/org/jboss/wsf/spi/util/ServiceLoader.java
===================================================================
---
spi/branches/jbossws-spi-1.1.2/src/main/java/org/jboss/wsf/spi/util/ServiceLoader.java 2009-11-27
18:04:04 UTC (rev 11163)
+++
spi/branches/jbossws-spi-1.1.2/src/main/java/org/jboss/wsf/spi/util/ServiceLoader.java 2009-11-27
18:05:01 UTC (rev 11164)
@@ -29,7 +29,11 @@
import java.io.InputStreamReader;
import java.security.AccessController;
import java.security.PrivilegedAction;
+import java.util.Collections;
+import java.util.Map;
import java.util.Properties;
+import java.util.WeakHashMap;
+import java.util.concurrent.ConcurrentHashMap;
/**
* Load a service class using this ordered lookup procedure
@@ -40,6 +44,12 @@
public abstract class ServiceLoader
{
/**
+ * A synchronized weak hash map that keeps factory names retrieved using Service API
(META-INF/services/*) for each classloader.
+ * Weak keys are used to remove entries when classloaders are garbage collected;
values are service-property-name -> factory name maps.
+ */
+ private static Map<ClassLoader, Map<String, String>> serviceMap =
Collections.synchronizedMap(new WeakHashMap<ClassLoader, Map<String,
String>>());
+
+ /**
* This method uses the algorithm below using the JAXWS Provider as an example.
*
* 1. If a resource with the name of META-INF/services/javax.xml.ws.spi.Provider
exists, then
@@ -78,25 +88,19 @@
// Use the Services API (as detailed in the JAR specification), if available, to
determine the classname.
String filename = "META-INF/services/" + propertyName;
- InputStream inStream = loader.getResourceAsStream(filename);
- if (inStream != null)
+ try
{
- try
+ factoryName = getServiceNameUsingCache(loader, filename);
+ if (factoryName != null)
{
- BufferedReader br = new BufferedReader(new InputStreamReader(inStream,
"UTF-8"));
- factoryName = br.readLine();
- br.close();
- if (factoryName != null)
- {
- Class factoryClass = loader.loadClass(factoryName);
- factory = factoryClass.newInstance();
- }
+ Class<?> factoryClass = SecurityActions.loadClass(loader,
factoryName);
+ factory = factoryClass.newInstance();
}
- catch (Throwable t)
- {
- throw new IllegalStateException("Failed to load " + propertyName +
": " + factoryName, t);
- }
}
+ catch (Throwable t)
+ {
+ throw new IllegalStateException("Failed to load " + propertyName +
": " + factoryName, t);
+ }
// Use the default factory implementation class.
if (factory == null && defaultFactory != null)
@@ -106,6 +110,33 @@
return factory;
}
+
+ private static String getServiceNameUsingCache(ClassLoader loader, String filename)
throws IOException
+ {
+ Map<String, String> map = serviceMap.get(loader);
+ if (map != null && map.containsKey(filename))
+ {
+ return map.get(filename);
+ }
+ else
+ {
+ if (map == null)
+ {
+ map = new ConcurrentHashMap<String, String>();
+ serviceMap.put(loader, map);
+ }
+ InputStream inStream = SecurityActions.getResourceAsStream(loader, filename);
+ String factoryName = null;
+ if (inStream != null)
+ {
+ BufferedReader br = new BufferedReader(new InputStreamReader(inStream,
"UTF-8"));
+ factoryName = br.readLine();
+ br.close();
+ map.put(filename, factoryName);
+ }
+ return factoryName;
+ }
+ }
/** Use the system property
*/