View Javadoc

1   /*
2    * Copyright (c) 2003, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of OSJava nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  package org.osjava.oscube.service.store;
33  
34  import java.util.Iterator;
35  
36  import java.io.File;
37  import java.io.FileOutputStream;
38  import java.io.InputStream;
39  import java.io.IOException;
40  import java.io.OutputStream;
41  
42  import java.net.URL;
43  
44  import com.generationjava.io.StreamW;
45  
46  import org.apache.log4j.Logger;
47  
48  import com.generationjava.config.Config;
49  import org.osjava.oscube.container.Session;
50  import org.osjava.oscube.container.Header;
51  import org.osjava.oscube.container.Result;
52  
53  public class FileStore implements Store {
54  
55      private static Logger logger = Logger.getLogger(FileStore.class);
56  
57  // FileStore assumes that the result contains a URL, InputStream, other?
58  // Assumes only row(?)
59  // TODO: If no saveAs, then it can handle multiple rows
60  // TODO: Optional argument 'maintainExtension=true'. In which case the 
61  //       saveAs does not need to mention the extension.
62      public void store(Result result, Config cfg, Session session) throws StoringException {
63          OutputStream out = null;
64          InputStream in = null;
65  try {
66          String path = cfg.getString("file.path");
67          String saveAs = cfg.getString("file.saveAs");
68  
69          File file = new File(path, saveAs);
70          FileOutputStream fos = new FileOutputStream(file);
71  
72          Iterator iterator = result.iterateRows();
73          if(iterator.hasNext()) {
74              Object[] row = (Object[])iterator.next();
75              if(row.length == 0) {
76                  logger.error("Empty row found. Skipping. ");
77                  return;
78              }
79              in = open(row[0]);
80              if(in != null) {
81                  StreamW.pushStream(in, fos);
82              }
83          }
84  
85  } catch(IOException ioe) {
86      throw new StoringException("File I/O Storing Error: "+ioe.getMessage(), ioe);
87  } finally {
88          try { if(in != null) in.close(); } catch(IOException ioe) { } 
89          try { if(out != null) out.close(); } catch(IOException ioe) { } 
90  }
91      }
92  
93      public boolean exists(Header header, Config cfg, Session session) throws StoringException {
94          // need to write some kind of checking in here
95          return false;
96      }
97  
98      private InputStream open(Object obj) throws IOException {
99          if(obj instanceof URL) {
100             URL url = (URL) obj;
101             return url.openStream();
102         } else 
103         if(obj instanceof String) {
104             return new java.io.ByteArrayInputStream( ( (String) obj ).getBytes() );
105         } else 
106         if(obj instanceof InputStream) {
107             return (InputStream) obj;
108         } else 
109         if(obj instanceof File) {
110             return new java.io.FileInputStream( (File) obj);
111         } else
112         if(obj instanceof byte[]) {
113             return new java.io.ByteArrayInputStream( (byte[]) obj);
114         } else {
115             return null;
116         }
117     }
118 }