1 /* 2 * Copyright (c) 2005, 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 36 /** 37 * Propagates openness of JAXB annottated classess packages to JAXB impl module. 38 * 39 * @author Roman Grigoriadi 40 */ 41 class ModuleUtil { 42 43 /** 44 * Resolves classes from context path. 45 * Only one class per package is needed to access its {@link java.lang.Module} 46 */ 47 static Class[] getClassesFromContextPath(String contextPath, ClassLoader classLoader) throws JAXBException { 48 List<Class> classes = new ArrayList<>(); 49 if (contextPath == null || contextPath.isEmpty()){ 50 return classes.toArray(new Class[]{}); 51 } 52 53 String [] tokens = contextPath.split(":"); 54 for (String pkg : tokens){ 55 56 // look for ObjectFactory and load it 57 final Class<?> o; 58 try { 59 o = classLoader.loadClass(pkg+".ObjectFactory"); 60 classes.add(o); 61 continue; 62 } catch (ClassNotFoundException e) { 63 // not necessarily an error 64 } 65 66 // look for jaxb.index and load the list of classes 67 try { 68 final Class firstByJaxbIndex = findFirstByJaxbIndex(pkg, classLoader); 69 if (firstByJaxbIndex != null) { 70 classes.add(firstByJaxbIndex); 71 } 72 } catch (IOException e) { 73 throw new JAXBException(e); 74 } 75 } 76 77 return classes.toArray(new Class[]{}); 78 } 79 80 /** 81 * Find first class in package by {@code jaxb.index} file. 82 */ 83 static Class findFirstByJaxbIndex(String pkg, ClassLoader classLoader) throws IOException, JAXBException { 84 final String resource = pkg.replace('.', '/') + "/jaxb.index"; 85 final InputStream resourceAsStream = classLoader.getResourceAsStream(resource); 86 87 if (resourceAsStream == null) { 88 return null; 89 } 90 91 BufferedReader in = 92 new BufferedReader(new InputStreamReader(resourceAsStream, "UTF-8")); 93 try { 94 String className = in.readLine(); 95 while (className != null) { 96 className = className.trim(); 97 if (className.startsWith("#") || (className.length() == 0)) { 98 className = in.readLine(); 99 continue; 100 } 101 102 try { 103 return classLoader.loadClass(pkg + '.' + className); 104 } catch (ClassNotFoundException e) { 105 throw new JAXBException(Messages.format(Messages.ERROR_LOAD_CLASS, className, pkg), e); 106 } 107 108 } 109 } finally { 110 in.close(); 111 } 112 return null; 113 } 114 115 /** 116 * Implementation may be defined in other module than {@code java.xml.bind}. In that case openness 117 * {@linkplain Module#isOpen open} of classes should be delegated to implementation module. 118 * 119 * @param classes used to resolve module for {@linkplain Module#addOpens(String, Module)} 120 * @param factorySPI used to resolve {@link Module} of the implementation. 121 * 122 * @throws JAXBException if ony of a classes package is not open to {@code java.xml.bind} module. 123 */ 124 static void delegateAddOpensToImplModule(Class[] classes, Class<?> factorySPI) throws JAXBException { 125 final Module implModule = factorySPI.getModule(); 126 if (!implModule.isNamed()) { 127 return; 128 } 129 130 Module jaxbModule = JAXBContext.class.getModule(); 131 132 for (Class cls : classes) { 133 final Module classModule = cls.getModule(); 134 final String packageName = cls.getPackageName(); 135 //no need for unnamed 136 if (!classModule.isNamed()) { 137 continue; 138 } 139 //report error if they are not open to java.xml.bind 140 if (!classModule.isOpen(packageName, jaxbModule)) { 141 throw new JAXBException(Messages.format(Messages.JAXB_CLASSES_NOT_OPEN, packageName)); 142 } 143 //propagate openness to impl module 144 classModule.addOpens(packageName, implModule); 145 } 146 } 147 }