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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2010, 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
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.org.jvnet.mimepull;
  27 
  28 import java.io.File;
  29 import java.io.IOException;

  30 
  31 /**
  32  * Configuration for MIME message parsing and storing.
  33  *
  34  * @author Jitendra Kotamraju
  35  */
  36 public class MIMEConfig {
  37 
  38     private static final int DEFAULT_CHUNK_SIZE = 8192;
  39     private static final long DEFAULT_MEMORY_THRESHOLD = 1048576L;
  40     private static final String DEFAULT_FILE_PREFIX = "MIME";
  41 


  42     // Parses the entire message eagerly
  43     boolean parseEagerly;
  44 
  45     // Approximate Chunk size
  46     int chunkSize;
  47 
  48     // Maximum in-memory data per attachment
  49     long memoryThreshold;
  50 
  51     // Do not store to disk
  52     boolean onlyMemory;
  53 
  54     // temp Dir to store large files
  55     File tempDir;
  56     String prefix;
  57     String suffix;
  58 
  59 
  60     private MIMEConfig(boolean parseEagerly, int chunkSize,
  61                        long inMemoryThreshold, String dir, String prefix, String suffix) {
  62         this.parseEagerly = parseEagerly;
  63         this.chunkSize = chunkSize;
  64         this.memoryThreshold = inMemoryThreshold;
  65         this.prefix = prefix;
  66         this.suffix = suffix;
  67         setDir(dir);
  68     }
  69 
  70     public MIMEConfig() {
  71         this(false, DEFAULT_CHUNK_SIZE, DEFAULT_MEMORY_THRESHOLD, null,
  72                 DEFAULT_FILE_PREFIX, null);
  73     }
  74 
  75     boolean isParseEagerly() {
  76         return parseEagerly;
  77     }
  78 
  79     public void setParseEagerly(boolean parseEagerly) {


 105 
 106     boolean isOnlyMemory() {
 107         return memoryThreshold == -1L;
 108     }
 109 
 110     File getTempDir() {
 111         return tempDir;
 112     }
 113 
 114     String getTempFilePrefix() {
 115         return prefix;
 116     }
 117 
 118     String getTempFileSuffix() {
 119         return suffix;
 120     }
 121 
 122     /**
 123      * @param dir
 124      */
 125     public void setDir(String dir) {
 126         if (tempDir == null && dir != null && !dir.equals("")) {
 127             tempDir = new File(dir);
 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                 tempFile.delete();





 142             } catch(Exception ioe) {
 143                 memoryThreshold = -1L;      // whole attachment will be in-memory
 144             }
 145         }
 146     }
 147 
 148 }
   1 /*
   2  * Copyright (c) 1997, 2012, 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
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.org.jvnet.mimepull;
  27 
  28 import java.io.File;
  29 import java.util.logging.Level;
  30 import java.util.logging.Logger;
  31 
  32 /**
  33  * Configuration for MIME message parsing and storing.
  34  *
  35  * @author Jitendra Kotamraju
  36  */
  37 public class MIMEConfig {
  38 
  39     private static final int DEFAULT_CHUNK_SIZE = 8192;
  40     private static final long DEFAULT_MEMORY_THRESHOLD = 1048576L;
  41     private static final String DEFAULT_FILE_PREFIX = "MIME";
  42 
  43     private static final Logger LOGGER = Logger.getLogger(MIMEConfig.class.getName());
  44 
  45     // Parses the entire message eagerly
  46     boolean parseEagerly;
  47 
  48     // Approximate Chunk size
  49     int chunkSize;
  50 
  51     // Maximum in-memory data per attachment
  52     long memoryThreshold;
  53 



  54     // temp Dir to store large files
  55     File tempDir;
  56     String prefix;
  57     String suffix;
  58 

  59     private MIMEConfig(boolean parseEagerly, int chunkSize,
  60                        long inMemoryThreshold, String dir, String prefix, String suffix) {
  61         this.parseEagerly = parseEagerly;
  62         this.chunkSize = chunkSize;
  63         this.memoryThreshold = inMemoryThreshold;
  64         this.prefix = prefix;
  65         this.suffix = suffix;
  66         setDir(dir);
  67     }
  68 
  69     public MIMEConfig() {
  70         this(false, DEFAULT_CHUNK_SIZE, DEFAULT_MEMORY_THRESHOLD, null,
  71                 DEFAULT_FILE_PREFIX, null);
  72     }
  73 
  74     boolean isParseEagerly() {
  75         return parseEagerly;
  76     }
  77 
  78     public void setParseEagerly(boolean parseEagerly) {


 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 }