You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by gp...@apache.org on 2013/09/07 01:40:57 UTC
svn commit: r1520714 - in /wink/2.x/trunk:
wink-cdi-server/src/main/java/org/apache/wink/cdi/server/internal/extension/
wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/
wink-common/src/main/java/org/apache/wink/common/internal/provi...
Author: gpetracek
Date: Fri Sep 6 23:40:57 2013
New Revision: 1520714
URL: http://svn.apache.org/r1520714
Log:
WINK-408 System#getSecurityManager check and cleanup
Added:
wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/ExceptionUtils.java
Modified:
wink/2.x/trunk/wink-cdi-server/src/main/java/org/apache/wink/cdi/server/internal/extension/WinkInjectionTarget.java
wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/CreationUtils.java
wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/JSR250LifecycleManagerUtils.java
wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/LifecycleManagerUtils.java
wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/FileProvider.java
wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/AbstractJAXBProvider.java
wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/JAXBXmlProvider.java
wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/ClassUtils.java
wink/2.x/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
wink/2.x/trunk/wink-guice-server/src/main/java/org/apache/wink/guice/server/internal/lifecycle/WinkGuiceModule.java
Modified: wink/2.x/trunk/wink-cdi-server/src/main/java/org/apache/wink/cdi/server/internal/extension/WinkInjectionTarget.java
URL: http://svn.apache.org/viewvc/wink/2.x/trunk/wink-cdi-server/src/main/java/org/apache/wink/cdi/server/internal/extension/WinkInjectionTarget.java?rev=1520714&r1=1520713&r2=1520714&view=diff
==============================================================================
--- wink/2.x/trunk/wink-cdi-server/src/main/java/org/apache/wink/cdi/server/internal/extension/WinkInjectionTarget.java (original)
+++ wink/2.x/trunk/wink-cdi-server/src/main/java/org/apache/wink/cdi/server/internal/extension/WinkInjectionTarget.java Fri Sep 6 23:40:57 2013
@@ -18,20 +18,19 @@
*/
package org.apache.wink.cdi.server.internal.extension;
-import org.apache.wink.common.internal.i18n.Messages;
import org.apache.wink.common.internal.lifecycle.CreationUtils;
import org.apache.wink.common.internal.registry.metadata.ApplicationMetadataCollector;
import org.apache.wink.common.internal.registry.metadata.ClassMetadata;
import org.apache.wink.common.internal.registry.metadata.ProviderMetadataCollector;
import org.apache.wink.common.internal.registry.metadata.ResourceMetadataCollector;
import org.apache.wink.common.internal.runtime.RuntimeContextTLS;
+import org.apache.wink.common.internal.utils.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
-import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
@@ -53,24 +52,18 @@ class WinkInjectionTarget<T> implements
public void inject(final T instance, final CreationalContext<T> creationalContext) {
logger.trace("inject({}, {}) entry", instance, creationalContext);
try {
- AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
-
- public Object run() throws PrivilegedActionException {
- if (classMetadata == null) {
- logger.trace("Collecting classMetadata for {}", this);
- classMetadata = collectClassMetadata(instance.getClass());
- }
- logger.trace("Calling CreationUtils.injectFields for instance");
- try {
- CreationUtils.injectFields(instance, classMetadata, RuntimeContextTLS.getRuntimeContext());
- } catch (IOException e) {
- throw new PrivilegedActionException(e);
+ if (System.getSecurityManager() != null) {
+ AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
+ public Object run() throws PrivilegedActionException {
+ tryToInject(instance);
+ return null;
}
- return null;
- }
- });
+ });
+ } else {
+ tryToInject(instance);
+ }
} catch (Exception e) {
- logger.warn(Messages.getMessage("exceptionDuringInjection"), e);
+ throw ExceptionUtils.throwAsRuntimeException(e);
}
logger.trace("calling delegate.inject(instance, creationalContext)");
@@ -78,6 +71,19 @@ class WinkInjectionTarget<T> implements
logger.trace("inject() exit");
}
+ private void tryToInject(T instance) {
+ if (classMetadata == null) {
+ logger.trace("Collecting classMetadata for {}", this);
+ classMetadata = collectClassMetadata(instance.getClass());
+ }
+ logger.trace("Calling CreationUtils.injectFields for instance");
+ try {
+ CreationUtils.injectFields(instance, classMetadata, RuntimeContextTLS.getRuntimeContext());
+ } catch (Exception e) {
+ throw ExceptionUtils.throwAsRuntimeException(e);
+ }
+ }
+
public void postConstruct(T instance) {
delegate.postConstruct(instance);
}
Modified: wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/CreationUtils.java
URL: http://svn.apache.org/viewvc/wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/CreationUtils.java?rev=1520714&r1=1520713&r2=1520714&view=diff
==============================================================================
--- wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/CreationUtils.java (original)
+++ wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/CreationUtils.java Fri Sep 6 23:40:57 2013
@@ -27,7 +27,6 @@ import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.List;
@@ -38,6 +37,7 @@ import org.apache.wink.common.internal.r
import org.apache.wink.common.internal.registry.InjectableFactory;
import org.apache.wink.common.internal.registry.metadata.ClassMetadata;
import org.apache.wink.common.internal.registry.metadata.ConstructorMetadata;
+import org.apache.wink.common.internal.utils.ExceptionUtils;
import org.apache.wink.common.spi.lifecycle.ObjectCreationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -82,8 +82,7 @@ public class CreationUtils {
public static void injectFields(final Object object,
ClassMetadata metadata,
- RuntimeContext runtimeContext) throws IOException,
- PrivilegedActionException {
+ RuntimeContext runtimeContext) throws IOException {
logger.trace("entry {} {} {}", new Object[]{object, metadata, runtimeContext});
List<Injectable> injectableFields = metadata.getInjectableFields();
@@ -105,35 +104,57 @@ public class CreationUtils {
logger.trace("exit");
}
- static void invokeMethod(final Object object, final Object value, final Method method)
- throws PrivilegedActionException {
+ static void invokeMethod(final Object object, final Object value, final Method method) {
logger.trace("entry {} {} {}", new Object[]{object, value, method});
- AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
- public Object run() throws Exception {
- if (!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method
- .getDeclaringClass().getModifiers())) {
- method.setAccessible(true);
- }
- method.invoke(object, value);
- return null;
+ try {
+ if (System.getSecurityManager() != null) {
+ AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
+ public Object run() throws Exception {
+ invokeMethod(method, object, value);
+ return null;
+ }
+ });
+ } else {
+ invokeMethod(method, object, value);
}
- });
+ } catch (Exception e) {
+ throw ExceptionUtils.throwAsRuntimeException(e);
+ }
logger.trace("exit");
}
- static void injectField(final Object object, final Object value, final Field field)
- throws PrivilegedActionException {
- AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
-
- public Object run() throws Exception {
- if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field
- .getDeclaringClass().getModifiers())) {
- field.setAccessible(true);
- }
- field.set(object, value);
- return null;
+ static void injectField(final Object object, final Object value, final Field field) {
+ try {
+ if (System.getSecurityManager() != null) {
+ AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
+ public Object run() throws Exception {
+ setField(field, object, value);
+ return null;
+ }
+ });
+ } else {
+ setField(field, object, value);
}
- });
+ } catch (Exception e) {
+ throw ExceptionUtils.throwAsRuntimeException(e);
+ }
+ }
+
+ private static void invokeMethod(Method method, Object object, Object value) throws
+ IllegalAccessException, InvocationTargetException {
+ if (!Modifier.isPublic(method.getModifiers()) ||
+ !Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
+ method.setAccessible(true);
+ }
+ method.invoke(object, value);
+ }
+
+ private static void setField(Field field, Object object, Object value) throws IllegalAccessException {
+ if (!Modifier.isPublic(field.getModifiers()) ||
+ !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
+ field.setAccessible(true);
+ }
+ field.set(object, value);
}
}
Modified: wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/JSR250LifecycleManagerUtils.java
URL: http://svn.apache.org/viewvc/wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/JSR250LifecycleManagerUtils.java?rev=1520714&r1=1520713&r2=1520714&view=diff
==============================================================================
--- wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/JSR250LifecycleManagerUtils.java (original)
+++ wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/JSR250LifecycleManagerUtils.java Fri Sep 6 23:40:57 2013
@@ -27,6 +27,7 @@ import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -35,6 +36,7 @@ import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.apache.wink.common.internal.registry.metadata.ClassMetadata;
+import org.apache.wink.common.internal.utils.ExceptionUtils;
import org.apache.wink.common.spi.lifecycle.ObjectCreationException;
import org.apache.wink.common.spi.lifecycle.ObjectFactory;
import org.slf4j.Logger;
@@ -194,19 +196,31 @@ public class JSR250LifecycleManagerUtils
@SuppressWarnings("unchecked")
private static void invokeMethod(final Object object, final Method m, final Object[] params) throws ObjectCreationException {
try {
- AccessController.doPrivileged(
+ if (System.getSecurityManager() != null) {
+ AccessController.doPrivileged(
new PrivilegedExceptionAction() {
public Object run() throws InvocationTargetException, IllegalAccessException {
- return m.invoke(object, params);
+ invoke(object, m, params);
+ return null;
}
}
- );
+ );
+ } else {
+ invoke(object, m, params);
+ }
} catch (PrivilegedActionException e) {
- throw new ObjectCreationException(e.getException());
+ throw ExceptionUtils.throwAsRuntimeException(e.getException());
+ }
+ }
+
+ private static void invoke(Object object, Method method, Object[] params) {
+ try {
+ method.invoke(object, params);
+ } catch (Exception e) {
+ throw ExceptionUtils.throwAsRuntimeException(e);
}
}
-
/**
* Gets all of the methods in this class and the super classes
*
@@ -216,55 +230,63 @@ public class JSR250LifecycleManagerUtils
@SuppressWarnings("unchecked")
private static List<Method> getMethods(final Class beanClass) {
// This class must remain private due to Java 2 Security concerns
- List<Method> methods;
- methods = (List<Method>)AccessController.doPrivileged(
+ if (System.getSecurityManager() != null) {
+ return (List<Method>)AccessController.doPrivileged(
new PrivilegedAction() {
public Object run() {
- List<Method> methods = new ArrayList<Method>();
- Class cls = beanClass;
- while (cls != null) {
- Method[] methodArray = cls.getDeclaredMethods();
- for (Method method : methodArray) {
- methods.add(method);
- }
- cls = cls.getSuperclass();
- }
- return methods;
+ return getDeclaredMethods(beanClass);
}
}
- );
+ );
+ } else {
+ return getDeclaredMethods(beanClass);
+ }
+ }
+ private static List<Method> getDeclaredMethods(Class beanClass) {
+ List<Method> methods = new ArrayList<Method>();
+ Class currentClass = beanClass;
+ while (currentClass != null && !Object.class.equals(currentClass)) {
+ Method[] methodArray = currentClass.getDeclaredMethods();
+ Collections.addAll(methods, methodArray);
+ currentClass = currentClass.getSuperclass();
+ }
return methods;
}
@SuppressWarnings("unchecked")
private static boolean isPostConstruct(final Method method) {
- Annotation[] annotations = (Annotation[]) AccessController.doPrivileged(
- new PrivilegedAction() {
- public Object run() {
- return method.getDeclaredAnnotations();
- }
- }
- );
+ Annotation[] annotations = getDeclaredAnnotations(method);
for (Annotation annotation : annotations) {
- return PostConstruct.class.isAssignableFrom(annotation.annotationType());
+ if (PostConstruct.class.isAssignableFrom(annotation.annotationType())) {
+ return true;
+ }
}
return false;
}
@SuppressWarnings("unchecked")
private static boolean isPreDestroy(final Method method) {
- Annotation[] annotations = (Annotation[]) AccessController.doPrivileged(
+ Annotation[] annotations = getDeclaredAnnotations(method);
+ for (Annotation annotation : annotations) {
+ if (PreDestroy.class.isAssignableFrom(annotation.annotationType())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static Annotation[] getDeclaredAnnotations(final Method method) {
+ if (System.getSecurityManager() != null) {
+ return (Annotation[]) AccessController.doPrivileged(
new PrivilegedAction() {
public Object run() {
return method.getDeclaredAnnotations();
}
}
- );
- for (Annotation annotation : annotations) {
- return PreDestroy.class.isAssignableFrom(annotation.annotationType());
+ );
+ } else {
+ return method.getDeclaredAnnotations();
}
- return false;
}
-
}
Modified: wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/LifecycleManagerUtils.java
URL: http://svn.apache.org/viewvc/wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/LifecycleManagerUtils.java?rev=1520714&r1=1520713&r2=1520714&view=diff
==============================================================================
--- wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/LifecycleManagerUtils.java (original)
+++ wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/lifecycle/LifecycleManagerUtils.java Fri Sep 6 23:40:57 2013
@@ -20,9 +20,6 @@
package org.apache.wink.common.internal.lifecycle;
-import java.io.IOException;
-import java.security.PrivilegedActionException;
-
import org.apache.wink.common.internal.i18n.Messages;
import org.apache.wink.common.internal.registry.metadata.ApplicationMetadataCollector;
import org.apache.wink.common.internal.registry.metadata.ClassMetadata;
@@ -33,6 +30,8 @@ import org.apache.wink.common.spi.lifecy
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.IOException;
+
public class LifecycleManagerUtils {
private static Logger logger = LoggerFactory.getLogger(LifecycleManagerUtils.class);
@@ -49,11 +48,6 @@ public class LifecycleManagerUtils {
logger.error(Messages.getMessage("injectionFailureSingleton", cls.getName())); //$NON-NLS-1$
}
throw new ObjectCreationException(e);
- } catch (PrivilegedActionException e) {
- if (logger.isErrorEnabled()) {
- logger.error(Messages.getMessage("injectionFailureSingleton", cls.getName())); //$NON-NLS-1$
- }
- throw new ObjectCreationException(e);
}
}
Modified: wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/FileProvider.java
URL: http://svn.apache.org/viewvc/wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/FileProvider.java?rev=1520714&r1=1520713&r2=1520714&view=diff
==============================================================================
--- wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/FileProvider.java (original)
+++ wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/FileProvider.java Fri Sep 6 23:40:57 2013
@@ -79,27 +79,16 @@ public class FileProvider implements Mes
final OutputStream entityStream) throws IOException,
WebApplicationException {
try {
- AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
-
- public Object run() throws IOException {
- if (!t.canRead() || t.isDirectory()) {
- if(logger.isWarnEnabled()) {
- logger.error(Messages.getMessage("cannotUseFileAsResponse", //$NON-NLS-1$
- t.getAbsoluteFile()));
- }
- throw new WebApplicationException();
- } else {
- FileInputStream fis = new FileInputStream(t);
- try {
- pipe(fis, entityStream);
- } finally {
- fis.close();
- }
+ if (System.getSecurityManager() != null) {
+ AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
+ public Object run() throws IOException {
+ write(t, entityStream);
+ return null;
}
- return null;
- }
-
- });
+ });
+ } else {
+ write(t, entityStream);
+ }
} catch (PrivilegedActionException e) {
if (e.getException() instanceof IOException)
throw (IOException)e.getException();
@@ -109,6 +98,20 @@ public class FileProvider implements Mes
}
}
+ private void write(File file, OutputStream entityStream) throws IOException {
+ if (!file.canRead() || file.isDirectory()) {
+ logger.warn(Messages.getMessage("cannotUseFileAsResponse", file.getAbsoluteFile()));
+ throw new WebApplicationException();
+ } else {
+ FileInputStream fis = new FileInputStream(file);
+ try {
+ pipe(fis, entityStream);
+ } finally {
+ fis.close();
+ }
+ }
+ }
+
/********************** Reader **************************************/
public boolean isReadable(Class<?> type,
@@ -124,43 +127,50 @@ public class FileProvider implements Mes
MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
final InputStream entityStream) throws IOException, WebApplicationException {
- File dir = null;
+ final File dir;
if (uploadDir != null) {
dir = new File(uploadDir);
if (!dir.exists() || !dir.isDirectory()) {
- dir = null;
if (logger.isWarnEnabled()) {
logger.error(Messages.getMessage("uploadDirDoesNotExist", uploadDir)); //$NON-NLS-1$
}
throw new WebApplicationException();
-
}
+ } else {
+ dir = null;
}
- final File _dir = dir;
try {
- return AccessController.doPrivileged(new PrivilegedExceptionAction<File>() {
-
- public File run() throws Exception {
- File f = File.createTempFile(prefix, suffix, _dir);
- FileOutputStream fos = new FileOutputStream(f);
- try {
- pipe(entityStream, fos);
- } finally {
- fos.close();
+ if (System.getSecurityManager() != null) {
+ return AccessController.doPrivileged(new PrivilegedExceptionAction<File>() {
+ public File run() throws Exception {
+ return createTempFile(dir, entityStream);
}
- return f;
- }
-
- });
+ });
+ } else {
+ return createTempFile(dir, entityStream);
+ }
} catch(PrivilegedActionException e) {
- if (e.getException() instanceof IOException)
+ if (e.getException() instanceof IOException) {
throw (IOException)e.getException();
- if (e.getException() instanceof WebApplicationException)
+ }
+ if (e.getException() instanceof WebApplicationException) {
throw (WebApplicationException)e.getException();
+ }
throw new WebApplicationException(e.getException());
}
}
+ private File createTempFile(File dir, InputStream entityStream) throws IOException {
+ File file = File.createTempFile(prefix, suffix, dir);
+ FileOutputStream fos = new FileOutputStream(file);
+ try {
+ pipe(entityStream, fos);
+ } finally {
+ fos.close();
+ }
+ return file;
+ }
+
/********************** Help methods ************************************/
private void pipe(InputStream is, OutputStream os) throws IOException {
byte[] ba = new byte[1024];
Modified: wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/AbstractJAXBProvider.java?rev=1520714&r1=1520713&r2=1520714&view=diff
==============================================================================
--- wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/AbstractJAXBProvider.java (original)
+++ wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/AbstractJAXBProvider.java Fri Sep 6 23:40:57 2013
@@ -59,6 +59,7 @@ import org.apache.wink.common.RuntimeCon
import org.apache.wink.common.internal.WinkConfiguration;
import org.apache.wink.common.internal.i18n.Messages;
import org.apache.wink.common.internal.runtime.RuntimeContextTLS;
+import org.apache.wink.common.internal.utils.ClassUtils;
import org.apache.wink.common.internal.utils.JAXBUtils;
import org.apache.wink.common.internal.utils.MediaTypeUtils;
import org.apache.wink.common.internal.utils.SoftConcurrentMap;
@@ -335,8 +336,6 @@ public abstract class AbstractJAXBProvid
/**
* A consistent place to get a properly configured XMLStreamReader.
*
- * @param entityStream
- * @return
* @throws XMLStreamException
*/
protected XMLStreamReader getXMLStreamReader(InputStreamReader entityStreamReader)
@@ -365,17 +364,20 @@ public abstract class AbstractJAXBProvid
private static Unmarshaller internalCreateUnmarshaller(final JAXBContext context)
throws JAXBException {
- Unmarshaller unm;
try {
- unm = AccessController.doPrivileged(new PrivilegedExceptionAction<Unmarshaller>() {
- public Unmarshaller run() throws JAXBException {
- return context.createUnmarshaller();
- }
- });
+ if (System.getSecurityManager() != null) {
+ return AccessController.doPrivileged(new PrivilegedExceptionAction<Unmarshaller>() {
+ public Unmarshaller run() throws JAXBException {
+ return context.createUnmarshaller();
+ }
+ });
+ } else {
+ return context.createUnmarshaller();
+ }
+
} catch (PrivilegedActionException e) {
throw (JAXBException)e.getCause();
}
- return unm;
}
/**
@@ -395,17 +397,19 @@ public abstract class AbstractJAXBProvid
private static Marshaller internalCreateMarshaller(final JAXBContext context)
throws JAXBException {
- Marshaller marshaller;
try {
- marshaller = AccessController.doPrivileged(new PrivilegedExceptionAction<Marshaller>() {
- public Marshaller run() throws JAXBException {
- return context.createMarshaller();
- }
- });
+ if (System.getSecurityManager() != null) {
+ return AccessController.doPrivileged(new PrivilegedExceptionAction<Marshaller>() {
+ public Marshaller run() throws JAXBException {
+ return context.createMarshaller();
+ }
+ });
+ } else {
+ return context.createMarshaller();
+ }
} catch (PrivilegedActionException e) {
throw (JAXBException)e.getCause();
}
- return marshaller;
}
/**
@@ -726,53 +730,52 @@ public abstract class AbstractJAXBProvid
throws JAXBException {
logger.trace("getDefaultContext({}, {}) entry", type, genericType); //$NON-NLS-1$
try {
- return AccessController.doPrivileged(new PrivilegedExceptionAction<JAXBContext>() {
-
- public JAXBContext run() throws Exception {
- JAXBContext context = jaxbDefaultContexts.get(type);
- if (context == null) {
-
- // CAUTION: be careful with this. Adding a second or
- // more classes to
- // the JAXBContext has the side
- // effect of putting a namespace prefix and the
- // namespace decl on
- // the subelements of the
- // desired type, thus degrading performance.
-
- if (!isXMLRootElement(type) && !isXMLType(type)) { // use
- // genericType.
- // If that fails,
- // we'll know
- // soon enough
- logger.trace("Using genericType to create context"); //$NON-NLS-1$
- context = JAXBContext.newInstance((Class<?>)genericType);
- } else {
- logger.trace("Using type to create context"); //$NON-NLS-1$
- context = JAXBContext.newInstance(type);
- }
-
- jaxbDefaultContexts.put(type, context);
- }
- if (logger.isTraceEnabled()) {
- logger.trace("getDefaultContext() exit returning", context); //$NON-NLS-1$
- logger
- .trace("returning context {}@{}", context.getClass().getName(), System.identityHashCode(context)); //$NON-NLS-1$
+ if (System.getSecurityManager() != null) {
+ return AccessController.doPrivileged(new PrivilegedExceptionAction<JAXBContext>() {
+ public JAXBContext run() throws Exception {
+ return createJAXBContext(type, (Class<?>) genericType);
}
- return context;
- }
-
- });
+ });
+ } else {
+ return createJAXBContext(type, (Class<?>) genericType);
+ }
} catch (PrivilegedActionException e) {
throw (JAXBException)e.getException();
}
}
+ private JAXBContext createJAXBContext(Class<?> type, Class<?> genericType) throws JAXBException {
+ JAXBContext context = jaxbDefaultContexts.get(type);
+ if (context == null) {
+ // CAUTION: be careful with this. Adding a second or
+ // more classes to
+ // the JAXBContext has the side
+ // effect of putting a namespace prefix and the
+ // namespace decl on
+ // the subelements of the
+ // desired type, thus degrading performance.
+
+ if (!isXMLRootElement(type) && !isXMLType(type)) { // use genericType. If that fails, we'll know soon enough
+ logger.trace("Using genericType to create context");
+ context = JAXBContext.newInstance((Class<?>)genericType);
+ } else {
+ logger.trace("Using type to create context");
+ context = JAXBContext.newInstance(type);
+ }
+
+ jaxbDefaultContexts.put(type, context);
+ }
+ if (logger.isTraceEnabled()) {
+ logger.trace("getDefaultContext() exit returning", context);
+ logger.trace("returning context {}@{}", context.getClass().getName(), System.identityHashCode(context));
+ }
+ return context;
+ }
+
/**
* If the object is not a JAXBElement and is annotated with XmlType but not
* with XmlRootElement, then it is automatically wrapped in a JAXBElement
*
- * @param t
* @param type
* @return
*/
@@ -839,14 +842,10 @@ public abstract class AbstractJAXBProvid
// Search for Factory
final StringBuilder b = new StringBuilder(type.getPackage().getName());
b.append(".ObjectFactory"); //$NON-NLS-1$
- Class<?> factoryClass = null;
+ Class<?> factoryClass;
try {
- factoryClass = AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() {
- public Class<?> run() throws ClassNotFoundException {
- return Thread.currentThread().getContextClassLoader().loadClass(b.toString());
- }
- });
- } catch(PrivilegedActionException e) {
+ factoryClass = ClassUtils.loadClass(b.toString());
+ } catch(Exception e) {
if (logger.isDebugEnabled()) {
logger.debug(Messages.getMessage("jaxbObjectFactoryNotFound", type.getName()), e); //$NON-NLS-1$
}
Modified: wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/JAXBXmlProvider.java
URL: http://svn.apache.org/viewvc/wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/JAXBXmlProvider.java?rev=1520714&r1=1520713&r2=1520714&view=diff
==============================================================================
--- wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/JAXBXmlProvider.java (original)
+++ wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/xml/JAXBXmlProvider.java Fri Sep 6 23:40:57 2013
@@ -47,6 +47,7 @@ import javax.xml.stream.XMLStreamExcepti
import javax.xml.stream.XMLStreamReader;
import org.apache.wink.common.internal.i18n.Messages;
+import org.apache.wink.common.internal.utils.ExceptionUtils;
import org.apache.wink.common.internal.utils.MediaTypeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -74,18 +75,17 @@ public class JAXBXmlProvider extends Abs
final InputStream entityStream) throws IOException,
WebApplicationException {
- Class<?> concreteType = getConcreteTypeFromTypeMap(type, annotations);
-
- Unmarshaller unmarshaller = null;
- Object unmarshaledResource = null;
- XMLStreamReader xmlStreamReader = null;
+ final Class<?> concreteType = getConcreteTypeFromTypeMap(type, annotations);
+ Object unmarshaledResource;
+ XMLStreamReader openXmlStreamReader = null;
try {
JAXBContext context = getContext(concreteType, mediaType);
- unmarshaller = getJAXBUnmarshaller(concreteType, context, mediaType);
- xmlStreamReader = getXMLStreamReader(entityStream);
+ final Unmarshaller unmarshaller = getJAXBUnmarshaller(concreteType, context, mediaType);
+ final XMLStreamReader xmlStreamReader = getXMLStreamReader(entityStream);
+ openXmlStreamReader = xmlStreamReader;
if (concreteType.isAnnotationPresent(XmlRootElement.class)) {
+ //TODO discuss it (not consistent)
unmarshaledResource = unmarshaller.unmarshal(xmlStreamReader);
- closeXMLStreamReader(xmlStreamReader);
if (unmarshaledResource instanceof JAXBElement) {
// this can happen if the JAXBContext object used to create
// the unmarshaller
@@ -101,55 +101,51 @@ public class JAXBXmlProvider extends Abs
unmarshaledResource = ((JAXBElement)unmarshaledResource).getValue();
}
} else {
- try {
- final Unmarshaller _unmarshaller = unmarshaller;
- final XMLStreamReader _xmlStreamReader = xmlStreamReader;
- final Class<?> _concreteType = concreteType;
+ if (System.getSecurityManager() != null) {
unmarshaledResource =
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
public Object run() throws PrivilegedActionException {
- try {
- Object obj = _unmarshaller.unmarshal(_xmlStreamReader, _concreteType).getValue();
- closeXMLStreamReader(_xmlStreamReader);
- return obj;
- } catch (JAXBException e) {
- throw new PrivilegedActionException(e);
- }
+ return unmarshal(unmarshaller, xmlStreamReader, concreteType);
}
});
- } catch (PrivilegedActionException e) {
- closeXMLStreamReader(xmlStreamReader);
- if (logger.isErrorEnabled()) {
- logger
- .error(Messages.getMessage("jaxbFailToUnmarshal", concreteType.getName()), e.getException()); //$NON-NLS-1$
- }
- throw new WebApplicationException(e.getException(), Response.Status.BAD_REQUEST);
+ } else {
+ unmarshaledResource = unmarshal(unmarshaller, xmlStreamReader, concreteType);
}
}
releaseJAXBUnmarshaller(context, unmarshaller);
} catch (JAXBException e) {
- closeXMLStreamReader(xmlStreamReader);
if (logger.isErrorEnabled()) {
logger.error(Messages.getMessage("jaxbFailToUnmarshal", concreteType.getName()), e); //$NON-NLS-1$
}
throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
} catch (EntityReferenceXMLStreamException e) {
- closeXMLStreamReader(xmlStreamReader);
if (logger.isErrorEnabled()) {
logger.error(Messages.getMessage("entityRefsNotSupported")); //$NON-NLS-1$
}
throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
} catch (XMLStreamException e) {
- closeXMLStreamReader(xmlStreamReader);
throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
} catch (RuntimeException e) {
- closeXMLStreamReader(xmlStreamReader);
throw e;
+ } catch (Exception e) {
+ throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
+ } finally {
+ if (openXmlStreamReader != null) {
+ closeXMLStreamReader(openXmlStreamReader);
+ }
}
return unmarshalWithXmlAdapter(unmarshaledResource, type, type, annotations);
}
+ private Object unmarshal(Unmarshaller unmarshaller, XMLStreamReader xmlStreamReader, Class<?> concreteType) {
+ try {
+ return unmarshaller.unmarshal(xmlStreamReader, concreteType).getValue();
+ } catch (JAXBException e) {
+ throw ExceptionUtils.throwAsRuntimeException(e);
+ }
+ }
+
public long getSize(Object t,
Class<?> type,
Type genericType,
Modified: wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/ClassUtils.java
URL: http://svn.apache.org/viewvc/wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/ClassUtils.java?rev=1520714&r1=1520713&r2=1520714&view=diff
==============================================================================
--- wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/ClassUtils.java (original)
+++ wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/ClassUtils.java Fri Sep 6 23:40:57 2013
@@ -52,50 +52,27 @@ public class ClassUtils {
* not have classloader visibility into the J2EE application. Prioritizing the thread context
* classloader restores visibility.
*
- * @param _className Class name
+ * @param className Class name
* @return java class
* @throws ClassNotFoundException if the class is not found
*/
- public static Class loadClass(final String _className)
- throws ClassNotFoundException {
+ public static Class loadClass(final String className) throws ClassNotFoundException {
- final String className = _className;
- Object ret = null;
- try {
- // use doPrivileged to handle java2security
- ret = AccessController.doPrivileged(
+ Object ret;
+ try {
+ if (System.getSecurityManager() != null) {
+ ret = AccessController.doPrivileged(
new PrivilegedAction<Object>() {
public Object run() {
-
- try {
- // try context class loader first
- logger.trace("Loading class {} using thread context classloader.", className); //$NON-NLS-1$
- return Class.forName(className,
- true,
- Thread.currentThread().getContextClassLoader());
- } catch (ClassNotFoundException cnfe) {
- try {
- // fallback to current classloader
- logger.trace("Loading class {} using current classloader.", className); //$NON-NLS-1$
- return Class.forName(className,
- true,
- ClassUtils.class.getClassLoader());
- } catch (ClassNotFoundException cnfe2) {
- // fallback to system classloader
- logger.trace("Loading class {} using system classloader.", className); //$NON-NLS-1$
- try {
- return Class.forName(className);
- } catch (ClassNotFoundException cnfe3) {
- return cnfe3;
- }
- }
- }
+ return load(className);
}
});
+ } else {
+ ret = load(className);
+ }
}
- catch (SecurityException se) {
- // SecurityException means java2security did not allow visibility to className
- throw new ClassNotFoundException(className);
+ catch (Exception e) {
+ throw ExceptionUtils.throwAsRuntimeException(e);
}
// class was located, return it
@@ -111,7 +88,32 @@ public class ClassUtils {
throw (ClassNotFoundException) ret;
}
throw new ClassNotFoundException(className);
+ }
+ private static Object load(String className)
+ {
+ try {
+ // try context class loader first
+ logger.trace("Loading class {} using thread context classloader.", className); //$NON-NLS-1$
+ return Class.forName(className,
+ true,
+ Thread.currentThread().getContextClassLoader());
+ } catch (ClassNotFoundException cnfe) {
+ try {
+ // fallback to current classloader
+ logger.trace("Loading class {} using current classloader.", className); //$NON-NLS-1$
+ return Class.forName(className,
+ true,
+ ClassUtils.class.getClassLoader());
+ } catch (ClassNotFoundException cnfe2) {
+ // fallback to system classloader
+ logger.trace("Loading class {} using system classloader.", className); //$NON-NLS-1$
+ try {
+ return Class.forName(className);
+ } catch (ClassNotFoundException cnfe3) {
+ return cnfe3;
+ }
+ }
+ }
}
-
}
Added: wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/ExceptionUtils.java
URL: http://svn.apache.org/viewvc/wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/ExceptionUtils.java?rev=1520714&view=auto
==============================================================================
--- wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/ExceptionUtils.java (added)
+++ wink/2.x/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/ExceptionUtils.java Fri Sep 6 23:40:57 2013
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ *******************************************************************************/
+package org.apache.wink.common.internal.utils;
+
+import java.lang.reflect.Constructor;
+
+public abstract class ExceptionUtils {
+ private ExceptionUtils() {
+ // prevent instantiation
+ }
+
+ public static RuntimeException throwAsRuntimeException(Throwable throwable) {
+ //Attention: helper which allows to use a trick to throw
+ // a catched checked exception without a wrapping exception
+ new ExceptionHelper<RuntimeException>().throwException(throwable);
+ return null; //not needed due to the helper trick, but it's easier for using it
+ }
+
+ private static Throwable createNewException(Throwable throwable, String message) {
+ Class<? extends Throwable> throwableClass = throwable.getClass();
+
+ try {
+ Constructor<? extends Throwable> constructor = throwableClass.getDeclaredConstructor(String.class);
+ constructor.setAccessible(true);
+ Throwable result = constructor.newInstance(message);
+ result.initCause(throwable.getCause());
+ return result;
+ }
+ catch (Exception e) {
+ //use the original exception if there is any issue
+ return throwable;
+ }
+ }
+
+ @SuppressWarnings({ "unchecked" })
+ private static class ExceptionHelper<T extends Throwable> {
+ private void throwException(Throwable exception) throws T {
+ try {
+ //exception-type is only checked at compile-time
+ throw (T) exception;
+ }
+ catch (ClassCastException e) {
+ //doesn't happen with existing JVMs! - if that changes the local ClassCastException needs to be ignored
+ //-> throw original exception
+ if (e.getStackTrace()[0].toString().contains(getClass().getName())) {
+ if (exception instanceof RuntimeException) {
+ throw (RuntimeException) exception;
+ }
+ throw new RuntimeException(exception);
+ }
+ //if the exception to throw is a ClassCastException, throw it
+ throw e;
+ }
+ }
+ }
+}
Modified: wink/2.x/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
URL: http://svn.apache.org/viewvc/wink/2.x/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties?rev=1520714&r1=1520713&r2=1520714&view=diff
==============================================================================
--- wink/2.x/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties (original)
+++ wink/2.x/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties Fri Sep 6 23:40:57 2013
@@ -219,7 +219,6 @@ entityRefsNotSupportedSunJDK5=Entity ref
saxParseException=The system cannot parse the XML content into a {0} instance. Verify that the XML content is valid.
saxParserConfigurationException=The system cannot configure the SAX parser with the given configuration parameter.
badXMLReaderInitialStart=The XMLStreamReader instance has already been partially processed.
-exceptionDuringInjection=Exception encountered during CDI injection
#couldNotFindBeanManager=Could not find BeanManager.
processingRequestTo=Processing {0} request to {1}, source content type is {2}, acceptable media types include {3}
registeredResources=Registered JAX-RS resources: {0}
Modified: wink/2.x/trunk/wink-guice-server/src/main/java/org/apache/wink/guice/server/internal/lifecycle/WinkGuiceModule.java
URL: http://svn.apache.org/viewvc/wink/2.x/trunk/wink-guice-server/src/main/java/org/apache/wink/guice/server/internal/lifecycle/WinkGuiceModule.java?rev=1520714&r1=1520713&r2=1520714&view=diff
==============================================================================
--- wink/2.x/trunk/wink-guice-server/src/main/java/org/apache/wink/guice/server/internal/lifecycle/WinkGuiceModule.java (original)
+++ wink/2.x/trunk/wink-guice-server/src/main/java/org/apache/wink/guice/server/internal/lifecycle/WinkGuiceModule.java Fri Sep 6 23:40:57 2013
@@ -19,9 +19,6 @@
package org.apache.wink.guice.server.internal.lifecycle;
-import java.io.IOException;
-import java.security.PrivilegedActionException;
-
import org.apache.wink.common.internal.i18n.Messages;
import org.apache.wink.common.internal.lifecycle.CreationUtils;
import org.apache.wink.common.spi.lifecycle.ObjectCreationException;
@@ -70,13 +67,7 @@ public class WinkGuiceModule extends Abs
try {
CreationUtils.injectFields(instance, classMetaData, RuntimeContextTLS
.getRuntimeContext());
- } catch (IOException e) {
- if (logger.isErrorEnabled()) {
- logger.error(Messages.getMessage("injectionFailureSingleton", instance
- .getClass().getName()), e);
- }
- throw new ObjectCreationException(e);
- } catch (PrivilegedActionException e) {
+ } catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error(Messages.getMessage("injectionFailureSingleton", instance
.getClass().getName()), e);