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