1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import java.security.Permission;
  24 import java.security.Policy;
  25 import java.security.ProtectionDomain;
  26 import java.util.Map;
  27 import javax.xml.bind.JAXBContext;
  28 import javax.xml.bind.JAXBException;
  29 import javax.xml.bind.Marshaller;
  30 import javax.xml.bind.Unmarshaller;
  31 import javax.xml.bind.Validator;
  32 
  33 /**
  34  * @test
  35  * @bug 8150173
  36  * @summary Verifies that a JAXBContext can be created with a legacy
  37  *          factory class that has static createContext methods.
  38  * @modules java.xml.bind
  39  * @run main/othervm JAXBContextWithLegacyFactory
  40  */
  41 public class JAXBContextWithLegacyFactory {
  42     private static JAXBContext tmp;
  43 
  44         public static class JAXBContextImpl extends JAXBContext {
  45         public final Class<?> creator;
  46         JAXBContextImpl(Class<?> creator) {
  47             this.creator = creator;
  48         }
  49 
  50         @Override
  51         public Unmarshaller createUnmarshaller() throws JAXBException {
  52             return tmp.createUnmarshaller();
  53         }
  54 
  55         @Override
  56         public Marshaller createMarshaller() throws JAXBException {
  57             return tmp.createMarshaller();
  58         }
  59 
  60         @Override
  61         public Validator createValidator() throws JAXBException {
  62             return tmp.createValidator();
  63         }
  64     }
  65 
  66     public static abstract class FactoryBase {
  67         public static JAXBContext createContext(Class<?>[] classesToBeBound,
  68                 Map<String, ?> properties) throws JAXBException {
  69             return new JAXBContextImpl(FactoryBase.class);
  70         }
  71 
  72         public static JAXBContext createContext(String contextPath,
  73                 ClassLoader classLoader, Map<String, ?> properties)
  74                 throws JAXBException {
  75             return new JAXBContextImpl(FactoryBase.class);
  76         }
  77     }
  78 
  79     public static class Factory1 extends FactoryBase {}
  80 
  81     public static class Factory2 extends FactoryBase {
  82         public static JAXBContext createContext(Class<?>[] classesToBeBound,
  83                 Map<String, ?> properties) throws JAXBException {
  84             return new JAXBContextImpl(Factory2.class);
  85         }
  86 
  87         public static JAXBContext createContext(String contextPath,
  88                 ClassLoader classLoader, Map<String, ?> properties)
  89                 throws JAXBException {
  90             return new JAXBContextImpl(Factory2.class);
  91         }
  92     }
  93 
  94     // test both without and then with a security manager as the code path
  95     // can be different when System.getSecurityManager() != null;
  96     public static void main(String[] args) throws JAXBException {
  97         System.out.println("\nWithout security manager\n");
  98         test(FactoryBase.class, FactoryBase.class);
  99         test(Factory1.class, FactoryBase.class);
 100         test(Factory2.class, Factory2.class);
 101 
 102         System.out.println("\nWith security manager\n");
 103         Policy.setPolicy(new Policy() {
 104             @Override
 105             public boolean implies(ProtectionDomain domain, Permission permission) {
 106                 return true; // allow all
 107             }
 108         });
 109         System.setSecurityManager(new SecurityManager());
 110         test(FactoryBase.class, FactoryBase.class);
 111         test(Factory1.class, FactoryBase.class);
 112         test(Factory2.class, Factory2.class);
 113     }
 114 
 115     public static void test(Class<?> factoryClass, Class<?> creatorClass) throws JAXBException {
 116         System.clearProperty(JAXBContext.JAXB_CONTEXT_FACTORY);
 117         System.out.println("** Testing  with Factory Class: " + factoryClass.getName());
 118         System.out.println(JAXBContext.JAXB_CONTEXT_FACTORY + " = "
 119                 + System.getProperty(JAXBContext.JAXB_CONTEXT_FACTORY, ""));
 120         System.out.println("Calling "
 121                 + "JAXBContext.newInstance(JAXBContextWithLegacyFactory.class)");
 122         tmp = JAXBContext.newInstance(JAXBContextWithLegacyFactory.class);
 123         System.setProperty(JAXBContext.JAXB_CONTEXT_FACTORY,
 124                 factoryClass.getName());
 125         System.out.println(JAXBContext.JAXB_CONTEXT_FACTORY + " = "
 126                 + System.getProperty(JAXBContext.JAXB_CONTEXT_FACTORY));
 127         System.out.println("Calling "
 128                 + "JAXBContext.newInstance(JAXBContextWithLegacyFactory.class)");
 129         JAXBContext ctxt = JAXBContext.newInstance(JAXBContextWithLegacyFactory.class);
 130         System.out.println("Successfully loaded JAXBcontext: " +
 131                 System.identityHashCode(ctxt) + "@" + ctxt.getClass().getName());
 132         if (ctxt.getClass() != JAXBContextImpl.class) {
 133             throw new RuntimeException("Wrong JAXBContext class"
 134                 + "\n\texpected: "
 135                 + System.identityHashCode(tmp) + "@" + JAXBContextImpl.class.getName()
 136                 + "\n\tactual:   "
 137                 + System.identityHashCode(ctxt) + "@" + ctxt.getClass().getName());
 138         }
 139         if (((JAXBContextImpl)ctxt).creator != creatorClass) {
 140             throw new RuntimeException("Wrong Factory class"
 141                 + "\n\texpected: "
 142                 + System.identityHashCode(tmp) + "@" + creatorClass.getName()
 143                 + "\n\tactual:   "
 144                 + System.identityHashCode(ctxt) + "@" + ((JAXBContextImpl)ctxt).creator.getName());
 145         }
 146     }
 147 }