src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/Utils.java

Tue, 15 Apr 2014 16:26:48 -0400

author
mkos
date
Tue, 15 Apr 2014 16:26:48 -0400
changeset 567
7d8cd27f1543
parent 450
b0c2840e2513
child 637
9c07ef4934dd
child 721
06807f9a6835
permissions
-rw-r--r--

8035613: With active Securitymanager JAXBContext.newInstance fails
Summary: Adding required doPrivileged section into JAXB classes; fix also reviewed by Iaroslav Savytskyi, Alexander Fomin
Reviewed-by: mullan, mgrebac

mkos@450 1 /*
mkos@567 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
mkos@450 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mkos@450 4 *
mkos@450 5 * This code is free software; you can redistribute it and/or modify it
mkos@450 6 * under the terms of the GNU General Public License version 2 only, as
mkos@450 7 * published by the Free Software Foundation. Oracle designates this
mkos@450 8 * particular file as subject to the "Classpath" exception as provided
mkos@450 9 * by Oracle in the LICENSE file that accompanied this code.
mkos@450 10 *
mkos@450 11 * This code is distributed in the hope that it will be useful, but WITHOUT
mkos@450 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mkos@450 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mkos@450 14 * version 2 for more details (a copy is included in the LICENSE file that
mkos@450 15 * accompanied this code).
mkos@450 16 *
mkos@450 17 * You should have received a copy of the GNU General Public License version
mkos@450 18 * 2 along with this work; if not, write to the Free Software Foundation,
mkos@450 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mkos@450 20 *
mkos@450 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mkos@450 22 * or visit www.oracle.com if you need additional information or have any
mkos@450 23 * questions.
mkos@450 24 */
mkos@450 25
mkos@450 26 package com.sun.xml.internal.bind.v2.runtime;
mkos@450 27
mkos@450 28 import com.sun.xml.internal.bind.v2.model.nav.Navigator;
mkos@450 29
mkos@450 30 import java.lang.reflect.Field;
mkos@450 31 import java.lang.reflect.InvocationTargetException;
mkos@450 32 import java.lang.reflect.Method;
mkos@450 33 import java.lang.reflect.Type;
mkos@567 34 import java.security.AccessController;
mkos@567 35 import java.security.PrivilegedAction;
mkos@450 36 import java.util.logging.Level;
mkos@450 37 import java.util.logging.Logger;
mkos@450 38
mkos@450 39 /**
mkos@450 40 * Utils class.
mkos@450 41 * Has *package private* access to avoid inappropriate usage.
mkos@450 42 */
mkos@567 43 final class Utils {
mkos@450 44
mkos@450 45 private static final Logger LOGGER = Logger.getLogger(Utils.class.getName());
mkos@450 46
mkos@450 47 /**
mkos@450 48 * static ReflectionNavigator field to avoid usage of reflection every time we use it.
mkos@450 49 */
mkos@567 50 static final Navigator<Type, Class, Field, Method> REFLECTION_NAVIGATOR;
mkos@450 51
mkos@450 52 static { // we statically initializing REFLECTION_NAVIGATOR property
mkos@450 53 try {
mkos@567 54 Class refNav = Class.forName("com.sun.xml.internal.bind.v2.model.nav.ReflectionNavigator");
mkos@450 55 //noinspection unchecked
mkos@567 56 final Method getInstance = refNav.getDeclaredMethod("getInstance");
mkos@567 57
mkos@567 58 // requires accessClassInPackage privilege
mkos@567 59 AccessController.doPrivileged(
mkos@567 60 new PrivilegedAction<Object>() {
mkos@567 61 @Override
mkos@567 62 public Object run() {
mkos@567 63 getInstance.setAccessible(true);
mkos@567 64 return null;
mkos@567 65 }
mkos@567 66 }
mkos@567 67 );
mkos@567 68
mkos@450 69 //noinspection unchecked
mkos@450 70 REFLECTION_NAVIGATOR = (Navigator<Type, Class, Field, Method>) getInstance.invoke(null);
mkos@450 71 } catch (ClassNotFoundException e) {
mkos@450 72 e.printStackTrace();
mkos@450 73 throw new IllegalStateException("Can't find ReflectionNavigator class");
mkos@450 74 } catch (InvocationTargetException e) {
mkos@450 75 e.printStackTrace();
mkos@450 76 throw new IllegalStateException("ReflectionNavigator.getInstance throws the exception");
mkos@450 77 } catch (NoSuchMethodException e) {
mkos@450 78 e.printStackTrace();
mkos@450 79 throw new IllegalStateException("ReflectionNavigator.getInstance can't be found");
mkos@450 80 } catch (IllegalAccessException e) {
mkos@450 81 e.printStackTrace();
mkos@450 82 throw new IllegalStateException("ReflectionNavigator.getInstance method is inaccessible");
mkos@450 83 } catch (SecurityException e) {
mkos@450 84 LOGGER.log(Level.FINE, "Unable to access ReflectionNavigator.getInstance", e);
mkos@450 85 throw e;
mkos@450 86 }
mkos@450 87 }
mkos@450 88
mkos@450 89 /**
mkos@450 90 * private constructor to avoid util class instantiating
mkos@450 91 */
mkos@450 92 private Utils() {
mkos@450 93 }
mkos@450 94 }

mercurial