src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/TempFiles.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 0
373ffda63c9a
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.org.jvnet.mimepull;
aoqi@0 27
aoqi@0 28 import java.io.File;
aoqi@0 29 import java.io.IOException;
aoqi@0 30 import java.lang.reflect.Array;
aoqi@0 31 import java.lang.reflect.InvocationTargetException;
aoqi@0 32 import java.lang.reflect.Method;
aoqi@0 33 import java.util.logging.Level;
aoqi@0 34 import java.util.logging.Logger;
aoqi@0 35
aoqi@0 36 /**
aoqi@0 37 * Helper utility to support jdk <= jdk1.6. After jdk1.6 EOL reflection can be removed and API can be used directly.
aoqi@0 38 */
aoqi@0 39 class TempFiles {
aoqi@0 40
aoqi@0 41 private static final Logger LOGGER = Logger.getLogger(TempFiles.class.getName());
aoqi@0 42
aoqi@0 43 private static final Class<?> CLASS_FILES;
aoqi@0 44 private static final Class<?> CLASS_PATH;
aoqi@0 45 private static final Class<?> CLASS_FILE_ATTRIBUTE;
aoqi@0 46 private static final Class<?> CLASS_FILE_ATTRIBUTES;
aoqi@0 47 private static final Method METHOD_FILE_TO_PATH;
aoqi@0 48 private static final Method METHOD_FILES_CREATE_TEMP_FILE;
aoqi@0 49 private static final Method METHOD_FILES_CREATE_TEMP_FILE_WITHPATH;
aoqi@0 50
aoqi@0 51 private static final Method METHOD_PATH_TO_FILE;
aoqi@0 52
aoqi@0 53 private static boolean useJdk6API;
aoqi@0 54
aoqi@0 55 static {
aoqi@0 56 useJdk6API = isJdk6();
aoqi@0 57
aoqi@0 58 CLASS_FILES = safeGetClass("java.nio.file.Files");
aoqi@0 59 CLASS_PATH = safeGetClass("java.nio.file.Path");
aoqi@0 60 CLASS_FILE_ATTRIBUTE = safeGetClass("java.nio.file.attribute.FileAttribute");
aoqi@0 61 CLASS_FILE_ATTRIBUTES = safeGetClass("[Ljava.nio.file.attribute.FileAttribute;");
aoqi@0 62 METHOD_FILE_TO_PATH = safeGetMethod(File.class, "toPath");
aoqi@0 63 METHOD_FILES_CREATE_TEMP_FILE = safeGetMethod(CLASS_FILES, "createTempFile", String.class, String.class, CLASS_FILE_ATTRIBUTES);
aoqi@0 64 METHOD_FILES_CREATE_TEMP_FILE_WITHPATH = safeGetMethod(CLASS_FILES, "createTempFile", CLASS_PATH, String.class, String.class, CLASS_FILE_ATTRIBUTES);
aoqi@0 65 METHOD_PATH_TO_FILE = safeGetMethod(CLASS_PATH, "toFile");
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 private static boolean isJdk6() {
aoqi@0 69 String javaVersion = System.getProperty("java.version");
aoqi@0 70 LOGGER.log(Level.FINEST, "Detected java version = {0}", javaVersion);
aoqi@0 71 return javaVersion.startsWith("1.6.");
aoqi@0 72 }
aoqi@0 73
aoqi@0 74 private static Class<?> safeGetClass(String className) {
aoqi@0 75 // it is jdk 6 or something failed already before
aoqi@0 76 if (useJdk6API) return null;
aoqi@0 77 try {
aoqi@0 78 return Class.forName(className);
aoqi@0 79 } catch (ClassNotFoundException e) {
aoqi@0 80 LOGGER.log(Level.SEVERE, "Exception cought", e);
aoqi@0 81 LOGGER.log(Level.WARNING, "Class {0} not found. Temp files will be created using old java.io API.", className);
aoqi@0 82 useJdk6API = true;
aoqi@0 83 return null;
aoqi@0 84 }
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 private static Method safeGetMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
aoqi@0 88 // it is jdk 6 or something failed already before
aoqi@0 89 if (useJdk6API) return null;
aoqi@0 90 try {
aoqi@0 91 return clazz.getMethod(methodName, parameterTypes);
aoqi@0 92 } catch (NoSuchMethodException e) {
aoqi@0 93 LOGGER.log(Level.SEVERE, "Exception cought", e);
aoqi@0 94 LOGGER.log(Level.WARNING, "Method {0} not found. Temp files will be created using old java.io API.", methodName);
aoqi@0 95 useJdk6API = true;
aoqi@0 96 return null;
aoqi@0 97 }
aoqi@0 98 }
aoqi@0 99
aoqi@0 100
aoqi@0 101 static Object toPath(File f) throws InvocationTargetException, IllegalAccessException {
aoqi@0 102 return METHOD_FILE_TO_PATH.invoke(f);
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 static File toFile(Object path) throws InvocationTargetException, IllegalAccessException {
aoqi@0 106 return (File) METHOD_PATH_TO_FILE.invoke(path);
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 static File createTempFile(String prefix, String suffix, File dir) throws IOException {
aoqi@0 110
aoqi@0 111 if (useJdk6API) {
aoqi@0 112 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 113 return File.createTempFile(prefix, suffix, dir);
aoqi@0 114
aoqi@0 115 } else {
aoqi@0 116
aoqi@0 117 try {
aoqi@0 118 if (dir != null) {
aoqi@0 119 Object path = toPath(dir);
aoqi@0 120 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 121 return toFile(METHOD_FILES_CREATE_TEMP_FILE_WITHPATH.invoke(null, path, prefix, suffix, Array.newInstance(CLASS_FILE_ATTRIBUTE, 0)));
aoqi@0 122 } else {
aoqi@0 123 LOGGER.log(Level.FINEST, "Temp file (prefix:{0}, suffix:{1}) being created using NIO API.", new Object[]{prefix, suffix});
aoqi@0 124 return toFile(METHOD_FILES_CREATE_TEMP_FILE.invoke(null, prefix, suffix, Array.newInstance(CLASS_FILE_ATTRIBUTE, 0)));
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 } catch (IllegalAccessException e) {
aoqi@0 128 LOGGER.log(Level.SEVERE, "Exception caught", e);
aoqi@0 129 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 130 new Object[]{dir != null ? dir.getAbsolutePath() : null, prefix, suffix});
aoqi@0 131 return File.createTempFile(prefix, suffix, dir);
aoqi@0 132
aoqi@0 133 } catch (InvocationTargetException e) {
aoqi@0 134 LOGGER.log(Level.SEVERE, "Exception caught", e);
aoqi@0 135 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 136 new Object[]{dir != null ? dir.getAbsolutePath() : null, prefix, suffix});
aoqi@0 137 return File.createTempFile(prefix, suffix, dir);
aoqi@0 138 }
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 }
aoqi@0 142
aoqi@0 143
aoqi@0 144 }

mercurial