1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/TempFiles.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,144 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.org.jvnet.mimepull; 1.30 + 1.31 +import java.io.File; 1.32 +import java.io.IOException; 1.33 +import java.lang.reflect.Array; 1.34 +import java.lang.reflect.InvocationTargetException; 1.35 +import java.lang.reflect.Method; 1.36 +import java.util.logging.Level; 1.37 +import java.util.logging.Logger; 1.38 + 1.39 +/** 1.40 + * Helper utility to support jdk <= jdk1.6. After jdk1.6 EOL reflection can be removed and API can be used directly. 1.41 + */ 1.42 +class TempFiles { 1.43 + 1.44 + private static final Logger LOGGER = Logger.getLogger(TempFiles.class.getName()); 1.45 + 1.46 + private static final Class<?> CLASS_FILES; 1.47 + private static final Class<?> CLASS_PATH; 1.48 + private static final Class<?> CLASS_FILE_ATTRIBUTE; 1.49 + private static final Class<?> CLASS_FILE_ATTRIBUTES; 1.50 + private static final Method METHOD_FILE_TO_PATH; 1.51 + private static final Method METHOD_FILES_CREATE_TEMP_FILE; 1.52 + private static final Method METHOD_FILES_CREATE_TEMP_FILE_WITHPATH; 1.53 + 1.54 + private static final Method METHOD_PATH_TO_FILE; 1.55 + 1.56 + private static boolean useJdk6API; 1.57 + 1.58 + static { 1.59 + useJdk6API = isJdk6(); 1.60 + 1.61 + CLASS_FILES = safeGetClass("java.nio.file.Files"); 1.62 + CLASS_PATH = safeGetClass("java.nio.file.Path"); 1.63 + CLASS_FILE_ATTRIBUTE = safeGetClass("java.nio.file.attribute.FileAttribute"); 1.64 + CLASS_FILE_ATTRIBUTES = safeGetClass("[Ljava.nio.file.attribute.FileAttribute;"); 1.65 + METHOD_FILE_TO_PATH = safeGetMethod(File.class, "toPath"); 1.66 + METHOD_FILES_CREATE_TEMP_FILE = safeGetMethod(CLASS_FILES, "createTempFile", String.class, String.class, CLASS_FILE_ATTRIBUTES); 1.67 + METHOD_FILES_CREATE_TEMP_FILE_WITHPATH = safeGetMethod(CLASS_FILES, "createTempFile", CLASS_PATH, String.class, String.class, CLASS_FILE_ATTRIBUTES); 1.68 + METHOD_PATH_TO_FILE = safeGetMethod(CLASS_PATH, "toFile"); 1.69 + } 1.70 + 1.71 + private static boolean isJdk6() { 1.72 + String javaVersion = System.getProperty("java.version"); 1.73 + LOGGER.log(Level.FINEST, "Detected java version = {0}", javaVersion); 1.74 + return javaVersion.startsWith("1.6."); 1.75 + } 1.76 + 1.77 + private static Class<?> safeGetClass(String className) { 1.78 + // it is jdk 6 or something failed already before 1.79 + if (useJdk6API) return null; 1.80 + try { 1.81 + return Class.forName(className); 1.82 + } catch (ClassNotFoundException e) { 1.83 + LOGGER.log(Level.SEVERE, "Exception cought", e); 1.84 + LOGGER.log(Level.WARNING, "Class {0} not found. Temp files will be created using old java.io API.", className); 1.85 + useJdk6API = true; 1.86 + return null; 1.87 + } 1.88 + } 1.89 + 1.90 + private static Method safeGetMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) { 1.91 + // it is jdk 6 or something failed already before 1.92 + if (useJdk6API) return null; 1.93 + try { 1.94 + return clazz.getMethod(methodName, parameterTypes); 1.95 + } catch (NoSuchMethodException e) { 1.96 + LOGGER.log(Level.SEVERE, "Exception cought", e); 1.97 + LOGGER.log(Level.WARNING, "Method {0} not found. Temp files will be created using old java.io API.", methodName); 1.98 + useJdk6API = true; 1.99 + return null; 1.100 + } 1.101 + } 1.102 + 1.103 + 1.104 + static Object toPath(File f) throws InvocationTargetException, IllegalAccessException { 1.105 + return METHOD_FILE_TO_PATH.invoke(f); 1.106 + } 1.107 + 1.108 + static File toFile(Object path) throws InvocationTargetException, IllegalAccessException { 1.109 + return (File) METHOD_PATH_TO_FILE.invoke(path); 1.110 + } 1.111 + 1.112 + static File createTempFile(String prefix, String suffix, File dir) throws IOException { 1.113 + 1.114 + if (useJdk6API) { 1.115 + LOGGER.log(Level.FINEST, "Jdk6 detected, temp file (prefix:{0}, suffix:{1}) being created using old java.io API.", new Object[]{prefix, suffix}); 1.116 + return File.createTempFile(prefix, suffix, dir); 1.117 + 1.118 + } else { 1.119 + 1.120 + try { 1.121 + if (dir != null) { 1.122 + Object path = toPath(dir); 1.123 + LOGGER.log(Level.FINEST, "Temp file (path: {0}, prefix:{1}, suffix:{2}) being created using NIO API.", new Object[]{dir.getAbsolutePath(), prefix, suffix}); 1.124 + return toFile(METHOD_FILES_CREATE_TEMP_FILE_WITHPATH.invoke(null, path, prefix, suffix, Array.newInstance(CLASS_FILE_ATTRIBUTE, 0))); 1.125 + } else { 1.126 + LOGGER.log(Level.FINEST, "Temp file (prefix:{0}, suffix:{1}) being created using NIO API.", new Object[]{prefix, suffix}); 1.127 + return toFile(METHOD_FILES_CREATE_TEMP_FILE.invoke(null, prefix, suffix, Array.newInstance(CLASS_FILE_ATTRIBUTE, 0))); 1.128 + } 1.129 + 1.130 + } catch (IllegalAccessException e) { 1.131 + LOGGER.log(Level.SEVERE, "Exception caught", e); 1.132 + LOGGER.log(Level.WARNING, "Error invoking java.nio API, temp file (path: {0}, prefix:{1}, suffix:{2}) being created using old java.io API.", 1.133 + new Object[]{dir != null ? dir.getAbsolutePath() : null, prefix, suffix}); 1.134 + return File.createTempFile(prefix, suffix, dir); 1.135 + 1.136 + } catch (InvocationTargetException e) { 1.137 + LOGGER.log(Level.SEVERE, "Exception caught", e); 1.138 + LOGGER.log(Level.WARNING, "Error invoking java.nio API, temp file (path: {0}, prefix:{1}, suffix:{2}) being created using old java.io API.", 1.139 + new Object[]{dir != null ? dir.getAbsolutePath() : null, prefix, suffix}); 1.140 + return File.createTempFile(prefix, suffix, dir); 1.141 + } 1.142 + } 1.143 + 1.144 + } 1.145 + 1.146 + 1.147 +}