1 /*
   2  * Copyright (c) 2017, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.bind;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.io.InputStreamReader;
  32 import java.lang.Module;
  33 import java.util.ArrayList;
  34 import java.util.List;
  35 import java.util.logging.Level;
  36 import java.util.logging.Logger;
  37 
  38 /**
  39  * Propagates openness of JAXB annottated classess packages to JAXB impl module.
  40  *
  41  * @author Roman Grigoriadi
  42  */
  43 class ModuleUtil {
  44 
  45     private static Logger logger = Logger.getLogger("javax.xml.bind");
  46 
  47     /**
  48      * Resolves classes from context path.
  49      * Only one class per package is needed to access its {@link java.lang.Module}
  50      */
  51     static Class[] getClassesFromContextPath(String contextPath, ClassLoader classLoader) throws JAXBException {
  52         List<Class> classes = new ArrayList<>();
  53         if (contextPath == null || contextPath.isEmpty()){
  54           return classes.toArray(new Class[]{});
  55         }
  56 
  57         String [] tokens = contextPath.split(":");
  58         for (String pkg : tokens){
  59 
  60            // look for ObjectFactory and load it
  61            final Class<?> o;
  62            try {
  63                o = classLoader.loadClass(pkg+".ObjectFactory");
  64                classes.add(o);
  65                continue;
  66            } catch (ClassNotFoundException e) {
  67                // not necessarily an error
  68            }
  69 
  70            // look for jaxb.index and load the list of classes
  71            try {
  72                final Class firstByJaxbIndex = findFirstByJaxbIndex(pkg, classLoader);
  73                if (firstByJaxbIndex != null) {
  74                    classes.add(firstByJaxbIndex);
  75                }
  76            } catch (IOException e) {
  77                throw new JAXBException(e);
  78            }
  79         }
  80 
  81         if (logger.isLoggable(Level.FINE)) {
  82             logger.log(Level.FINE, "Resolved classes from context path: {0}", classes);
  83         }
  84         return classes.toArray(new Class[]{});
  85     }
  86 
  87     /**
  88      * Find first class in package by {@code jaxb.index} file.
  89      */
  90     static Class findFirstByJaxbIndex(String pkg, ClassLoader classLoader) throws IOException, JAXBException {
  91         final String resource = pkg.replace('.', '/') + "/jaxb.index";
  92         final InputStream resourceAsStream = classLoader.getResourceAsStream(resource);
  93 
  94         if (resourceAsStream == null) {
  95             return null;
  96         }
  97 
  98         BufferedReader in =
  99                 new BufferedReader(new InputStreamReader(resourceAsStream, "UTF-8"));
 100         try {
 101             String className = in.readLine();
 102             while (className != null) {
 103                 className = className.trim();
 104                 if (className.startsWith("#") || (className.length() == 0)) {
 105                     className = in.readLine();
 106                     continue;
 107                 }
 108 
 109                 try {
 110                     return classLoader.loadClass(pkg + '.' + className);
 111                 } catch (ClassNotFoundException e) {
 112                     throw new JAXBException(Messages.format(Messages.ERROR_LOAD_CLASS, className, pkg), e);
 113                 }
 114 
 115             }
 116         } finally {
 117             in.close();
 118         }
 119         return null;
 120     }
 121 
 122     /**
 123      * Implementation may be defined in other module than {@code java.xml.bind}. In that case openness
 124      * {@linkplain Module#isOpen open} of classes should be delegated to implementation module.
 125      *
 126      * @param classes used to resolve module for {@linkplain Module#addOpens(String, Module)}
 127      * @param factorySPI used to resolve {@link Module} of the implementation.
 128      *
 129      * @throws JAXBException if ony of a classes package is not open to {@code java.xml.bind} module.
 130      */
 131     static void delegateAddOpensToImplModule(Class[] classes, Class<?> factorySPI) throws JAXBException {
 132         final Module implModule = factorySPI.getModule();
 133         if (!implModule.isNamed()) {
 134             return;
 135         }
 136 
 137         Module jaxbModule = JAXBContext.class.getModule();
 138 
 139         for (Class cls : classes) {
 140             final Module classModule = cls.getModule();
 141             final String packageName = cls.getPackageName();
 142             //no need for unnamed
 143             if (!classModule.isNamed()) {
 144                 continue;
 145             }
 146             //report error if they are not open to java.xml.bind
 147             if (!classModule.isOpen(packageName, jaxbModule)) {
 148                 throw new JAXBException(Messages.format(Messages.JAXB_CLASSES_NOT_OPEN,
 149                                                         packageName, cls.getName(), classModule.getName()));
 150             }
 151             //propagate openness to impl module
 152             classModule.addOpens(packageName, implModule);
 153             if (logger.isLoggable(Level.FINE)) {
 154                 logger.log(Level.FINE, "Propagating openness of package {0} in {1} to {2}.",
 155                            new String[]{ packageName, classModule.getName(), implModule.getName() });
 156             }
 157         }
 158     }
 159 
 160 }
--- EOF ---