1 /*
2 * Copyright (c) 1997, 2013, 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
102 this.memoryThreshold = memoryThreshold;
103 }
104
105 boolean isOnlyMemory() {
106 return memoryThreshold == -1L;
107 }
108
109 File getTempDir() {
110 return tempDir;
111 }
112
113 String getTempFilePrefix() {
114 return prefix;
115 }
116
117 String getTempFileSuffix() {
118 return suffix;
119 }
120
121 /**
122 * @param dir
123 */
124 public final void setDir(String dir) {
125 if (tempDir == null && dir != null && !dir.equals("")) {
126 tempDir = new File(dir);
127 }
128 }
129
130 /**
131 * Validates if it can create temporary files. Otherwise, it stores
132 * attachment contents in memory.
133 */
134 public void validate() {
135 if (!isOnlyMemory()) {
136 try {
137 File tempFile = (tempDir == null)
138 ? File.createTempFile(prefix, suffix)
139 : File.createTempFile(prefix, suffix, tempDir);
140 boolean deleted = tempFile.delete();
141 if (!deleted) {
142 if (LOGGER.isLoggable(Level.INFO)) {
143 LOGGER.log(Level.INFO, "File {0} was not deleted", tempFile.getAbsolutePath());
144 }
145 }
146 } catch(Exception ioe) {
147 memoryThreshold = -1L; // whole attachment will be in-memory
148 }
149 }
150 }
151
152 }
|
1 /*
2 * Copyright (c) 1997, 2015, 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
102 this.memoryThreshold = memoryThreshold;
103 }
104
105 boolean isOnlyMemory() {
106 return memoryThreshold == -1L;
107 }
108
109 File getTempDir() {
110 return tempDir;
111 }
112
113 String getTempFilePrefix() {
114 return prefix;
115 }
116
117 String getTempFileSuffix() {
118 return suffix;
119 }
120
121 /**
122 * @param directory
123 * temp directory
124 */
125 public final void setDir(String directory) {
126 if (tempDir == null && directory != null && !directory.equals("")) {
127 tempDir = new File(directory);
128 }
129 }
130
131 /**
132 * Validates if it can create temporary files. Otherwise, it stores
133 * attachment contents in memory.
134 */
135 public void validate() {
136 if (!isOnlyMemory()) {
137 try {
138 File tempFile = (tempDir == null)
139 ? File.createTempFile(prefix, suffix)
140 : File.createTempFile(prefix, suffix, tempDir);
141 boolean deleted = tempFile.delete();
142 if (!deleted) {
143 if (LOGGER.isLoggable(Level.INFO)) {
144 LOGGER.log(Level.INFO, "File {0} was not deleted", tempFile.getAbsolutePath());
145 }
146 }
147 } catch(RuntimeException e) {
148 memoryThreshold = -1L; // whole attachment will be in-memory
149 } catch(Exception e) {
150 memoryThreshold = -1L; // whole attachment will be in-memory
151 }
152 }
153 }
154
155 }
|