View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.geronimo.ews.ws4j2ee.context.impl;
18  
19  import org.apache.geronimo.ews.ws4j2ee.context.InputOutputFile;
20  import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileNotFoundException;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  
30  /***
31   * @author hemapani@opensource.lk
32   */
33  public class InputOutputFileImpl implements InputOutputFile {
34      private InputStream instream;
35      private String fileName;
36      private OutputStream outstream;
37  
38      public InputOutputFileImpl(InputStream instream) {
39          this.instream = instream;
40      }
41  
42      public InputOutputFileImpl(String fileName) throws GenerationFault {
43          this.fileName = fileName;
44      }
45  
46      public InputOutputFileImpl(String fileName, InputStream instream) {
47          this.instream = instream;
48          this.fileName = fileName;
49      }
50  
51      public String fileName() {
52          if (fileName == null)
53              throw new UnsupportedOperationException("asking for file name when input/output is a stream");
54          return fileName;
55      }
56  
57      public InputStream getInputStream() throws GenerationFault {
58          try {
59              if (instream == null) {
60                  File file = new File(fileName);
61                  this.instream = new FileInputStream(file);
62              }
63              return instream;
64          } catch (FileNotFoundException e) {
65              e.printStackTrace();
66              throw GenerationFault.createGenerationFault(e);
67          }
68      }
69  
70      public OutputStream getOutStream() throws GenerationFault {
71          try {
72              if (outstream == null) {
73                  File file = new File(fileName);
74                  File parent = file.getParentFile();
75                  if (!parent.exists())
76                      parent.mkdirs();
77                  this.outstream = new FileOutputStream(file);
78              }
79              return outstream;
80          } catch (FileNotFoundException e) {
81              e.printStackTrace();
82              throw GenerationFault.createGenerationFault(e);
83          }
84      }
85  
86      /* (non-Javadoc)
87       * @see java.lang.Object#finalize()
88       */
89      protected void finalize() throws Throwable {
90          try {
91              if (outstream != null)
92                  outstream.close();
93              if (instream != null) {
94                  instream.close();
95              }
96          } catch (IOException e) {
97          }
98          super.finalize();
99      }
100 
101 }