aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.org.jvnet.mimepull; aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.lang.reflect.Array; aoqi@0: import java.lang.reflect.InvocationTargetException; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.util.logging.Level; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: /** aoqi@0: * Helper utility to support jdk <= jdk1.6. After jdk1.6 EOL reflection can be removed and API can be used directly. aoqi@0: */ aoqi@0: class TempFiles { aoqi@0: aoqi@0: private static final Logger LOGGER = Logger.getLogger(TempFiles.class.getName()); aoqi@0: aoqi@0: private static final Class CLASS_FILES; aoqi@0: private static final Class CLASS_PATH; aoqi@0: private static final Class CLASS_FILE_ATTRIBUTE; aoqi@0: private static final Class CLASS_FILE_ATTRIBUTES; aoqi@0: private static final Method METHOD_FILE_TO_PATH; aoqi@0: private static final Method METHOD_FILES_CREATE_TEMP_FILE; aoqi@0: private static final Method METHOD_FILES_CREATE_TEMP_FILE_WITHPATH; aoqi@0: aoqi@0: private static final Method METHOD_PATH_TO_FILE; aoqi@0: aoqi@0: private static boolean useJdk6API; aoqi@0: aoqi@0: static { aoqi@0: useJdk6API = isJdk6(); aoqi@0: aoqi@0: CLASS_FILES = safeGetClass("java.nio.file.Files"); aoqi@0: CLASS_PATH = safeGetClass("java.nio.file.Path"); aoqi@0: CLASS_FILE_ATTRIBUTE = safeGetClass("java.nio.file.attribute.FileAttribute"); aoqi@0: CLASS_FILE_ATTRIBUTES = safeGetClass("[Ljava.nio.file.attribute.FileAttribute;"); aoqi@0: METHOD_FILE_TO_PATH = safeGetMethod(File.class, "toPath"); aoqi@0: METHOD_FILES_CREATE_TEMP_FILE = safeGetMethod(CLASS_FILES, "createTempFile", String.class, String.class, CLASS_FILE_ATTRIBUTES); aoqi@0: METHOD_FILES_CREATE_TEMP_FILE_WITHPATH = safeGetMethod(CLASS_FILES, "createTempFile", CLASS_PATH, String.class, String.class, CLASS_FILE_ATTRIBUTES); aoqi@0: METHOD_PATH_TO_FILE = safeGetMethod(CLASS_PATH, "toFile"); aoqi@0: } aoqi@0: aoqi@0: private static boolean isJdk6() { aoqi@0: String javaVersion = System.getProperty("java.version"); aoqi@0: LOGGER.log(Level.FINEST, "Detected java version = {0}", javaVersion); aoqi@0: return javaVersion.startsWith("1.6."); aoqi@0: } aoqi@0: aoqi@0: private static Class safeGetClass(String className) { aoqi@0: // it is jdk 6 or something failed already before aoqi@0: if (useJdk6API) return null; aoqi@0: try { aoqi@0: return Class.forName(className); aoqi@0: } catch (ClassNotFoundException e) { aoqi@0: LOGGER.log(Level.SEVERE, "Exception cought", e); aoqi@0: LOGGER.log(Level.WARNING, "Class {0} not found. Temp files will be created using old java.io API.", className); aoqi@0: useJdk6API = true; aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static Method safeGetMethod(Class clazz, String methodName, Class... parameterTypes) { aoqi@0: // it is jdk 6 or something failed already before aoqi@0: if (useJdk6API) return null; aoqi@0: try { aoqi@0: return clazz.getMethod(methodName, parameterTypes); aoqi@0: } catch (NoSuchMethodException e) { aoqi@0: LOGGER.log(Level.SEVERE, "Exception cought", e); aoqi@0: LOGGER.log(Level.WARNING, "Method {0} not found. Temp files will be created using old java.io API.", methodName); aoqi@0: useJdk6API = true; aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: static Object toPath(File f) throws InvocationTargetException, IllegalAccessException { aoqi@0: return METHOD_FILE_TO_PATH.invoke(f); aoqi@0: } aoqi@0: aoqi@0: static File toFile(Object path) throws InvocationTargetException, IllegalAccessException { aoqi@0: return (File) METHOD_PATH_TO_FILE.invoke(path); aoqi@0: } aoqi@0: aoqi@0: static File createTempFile(String prefix, String suffix, File dir) throws IOException { aoqi@0: aoqi@0: if (useJdk6API) { aoqi@0: LOGGER.log(Level.FINEST, "Jdk6 detected, temp file (prefix:{0}, suffix:{1}) being created using old java.io API.", new Object[]{prefix, suffix}); aoqi@0: return File.createTempFile(prefix, suffix, dir); aoqi@0: aoqi@0: } else { aoqi@0: aoqi@0: try { aoqi@0: if (dir != null) { aoqi@0: Object path = toPath(dir); aoqi@0: LOGGER.log(Level.FINEST, "Temp file (path: {0}, prefix:{1}, suffix:{2}) being created using NIO API.", new Object[]{dir.getAbsolutePath(), prefix, suffix}); aoqi@0: return toFile(METHOD_FILES_CREATE_TEMP_FILE_WITHPATH.invoke(null, path, prefix, suffix, Array.newInstance(CLASS_FILE_ATTRIBUTE, 0))); aoqi@0: } else { aoqi@0: LOGGER.log(Level.FINEST, "Temp file (prefix:{0}, suffix:{1}) being created using NIO API.", new Object[]{prefix, suffix}); aoqi@0: return toFile(METHOD_FILES_CREATE_TEMP_FILE.invoke(null, prefix, suffix, Array.newInstance(CLASS_FILE_ATTRIBUTE, 0))); aoqi@0: } aoqi@0: aoqi@0: } catch (IllegalAccessException e) { aoqi@0: LOGGER.log(Level.SEVERE, "Exception caught", e); aoqi@0: LOGGER.log(Level.WARNING, "Error invoking java.nio API, temp file (path: {0}, prefix:{1}, suffix:{2}) being created using old java.io API.", aoqi@0: new Object[]{dir != null ? dir.getAbsolutePath() : null, prefix, suffix}); aoqi@0: return File.createTempFile(prefix, suffix, dir); aoqi@0: aoqi@0: } catch (InvocationTargetException e) { aoqi@0: LOGGER.log(Level.SEVERE, "Exception caught", e); aoqi@0: LOGGER.log(Level.WARNING, "Error invoking java.nio API, temp file (path: {0}, prefix:{1}, suffix:{2}) being created using old java.io API.", aoqi@0: new Object[]{dir != null ? dir.getAbsolutePath() : null, prefix, suffix}); aoqi@0: return File.createTempFile(prefix, suffix, dir); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: aoqi@0: }