KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > filetransferbc > IOUtils


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id$
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.filetransferbc;
23
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.FileWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import javax.activation.DataHandler JavaDoc;
34 import javax.jbi.messaging.NormalizedMessage;
35
36 /**
37  * Some IO utilities for the file transfer BC
38  *
39  * @author chamerling - eBM WebSourcing
40  *
41  */

42 public class IOUtils {
43
44     private static final String JavaDoc DATE_FORMAT = "ddMMyyHHmmssSSS";
45
46     /**
47      * Write all attachments to files. One file per attachement. The file will
48      * have the name of the attachment.
49      *
50      * @param message
51      * the message to get attachment from
52      * @param destDir
53      * the direcptry where the files will be written
54      * @throws FileTransferBCException
55      */

56     public static void writeAttachmentsToFiles(NormalizedMessage message,
57         File JavaDoc destDir) throws FileTransferBCException {
58         
59         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(DATE_FORMAT);
60         String JavaDoc date = sdf.format(new Date JavaDoc(System.currentTimeMillis()));
61         Set JavaDoc attachIds = message.getAttachmentNames();
62
63         for (Object JavaDoc name : attachIds) {
64             DataHandler JavaDoc handler = message.getAttachment((String JavaDoc) name);
65             FileOutputStream JavaDoc fos = null;
66
67             try {
68                 if (!destDir.exists())
69                     destDir.mkdirs();
70
71                 File JavaDoc outFile = new File JavaDoc(destDir.getAbsolutePath(), handler
72                     .getName());
73
74                 fos = new FileOutputStream JavaDoc(new File JavaDoc(outFile.getAbsoluteFile()
75                     + "_" + date));
76
77                 handler.writeTo(fos);
78                 fos.flush();
79
80             } catch (Exception JavaDoc e) {
81                 throw new FileTransferBCException("Error: writing attachment "
82                     + handler.getName() + " " + e.getMessage());
83             } finally {
84                 try {
85                     fos.close();
86                 } catch (IOException JavaDoc e) {
87                 }
88             }
89         }
90     }
91
92     /**
93      * Write a string to a file
94      *
95      * @param content
96      * @param destDir
97      * @param fileName
98      * @throws FileTransferBCException
99      */

100     public static File JavaDoc writeStringToFile(String JavaDoc content, File JavaDoc destDir,
101         String JavaDoc fileName) throws FileTransferBCException {
102         BufferedWriter JavaDoc out = null;
103         File JavaDoc outFile = null;
104         
105         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(DATE_FORMAT);
106         fileName = fileName + "_" + sdf.format(new Date JavaDoc(System.currentTimeMillis())) + ".xml";
107
108         try {
109             // create destination directory if it do not exists
110
if (!destDir.exists()) {
111                 destDir.mkdirs();
112             }
113
114             // create and write file
115
outFile = new File JavaDoc(destDir.getAbsolutePath(), fileName);
116             out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(outFile.getAbsolutePath()));
117             out.write(content);
118
119         } catch (IOException JavaDoc e) {
120             throw new FileTransferBCException("Error creating new file "
121                 + fileName, e.getMessage());
122         } finally {
123             try {
124                 out.close();
125             } catch (IOException JavaDoc e) {
126             }
127         }
128         
129         return outFile;
130     }
131 }
132
Popular Tags