KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > filetransferbc > listeners > FileTransferBCJBIListener


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 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 : ${file_name} 154 ${date} rbarraza
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.filetransferbc.listeners;
23
24 import java.io.File JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import javax.jbi.messaging.NormalizedMessage;
30
31 import org.objectweb.petals.binding.filetransferbc.FileTransferBCException;
32 import org.objectweb.petals.binding.filetransferbc.IOUtils;
33 import org.objectweb.petals.component.common.HandlingException;
34 import org.objectweb.petals.component.common.PEtALSComponentSDKException;
35 import org.objectweb.petals.component.common.bc.JBIListener;
36 import org.objectweb.petals.component.common.util.MessageExchangeWrapper;
37 import org.objectweb.petals.component.common.util.SourceHelper;
38 import org.objectweb.petals.tools.jbicommon.descriptor.Extensions;
39 import org.objectweb.petals.tools.jbicommon.util.StringHelper;
40
41 /**
42  * The file transfer BC JBI listener. Writes a file to hard disk on incoming JBI
43  * message.
44  *
45  * @version $Rev: 250 $Date: {date}
46  * @since Petals 1.0
47  * @author alouis, Rocio BARRAZA, Christophe HAMERLING - eBMWebsourcing
48  *
49  */

50 public class FileTransferBCJBIListener implements JBIListener {
51
52     private final Logger JavaDoc logger;
53     
54     private String JavaDoc rootPath;
55
56     /**
57      * Creates a new instance of {@link FileTransferBCJBIListener}
58      *
59      * @param channel
60      * @param logger
61      * @param context
62      * @param workDir
63      */

64     public FileTransferBCJBIListener(String JavaDoc rootPath, Logger JavaDoc logger) {
65         this.logger = logger;
66         this.rootPath = rootPath;
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.objectweb.petals.component.common.bc.JBIListener#onJBIMessage(java.lang.String,
73      * javax.jbi.messaging.MessageExchange,
74      * org.objectweb.petals.tools.jbicommon.descriptor.Extensions)
75      */

76     public boolean onJBIMessage(String JavaDoc address,
77         MessageExchangeWrapper exchange, Extensions extensions)
78         throws HandlingException {
79         boolean result = false;
80
81         String JavaDoc content = null;
82         NormalizedMessage nm = exchange.getInMessage();
83
84         // get message content as string
85
try {
86             content = SourceHelper.createString(nm.getContent());
87         } catch (PEtALSComponentSDKException e) {
88             throw new HandlingException(e.getMessage());
89         }
90
91         // write content a file. The destDir is the endpoint
92
// name. If the address is not an absolute path, the direcotry will be
93
// created on the SU install root path.
94
File JavaDoc tmpDir = new File JavaDoc(address);
95         File JavaDoc parentDir = null;
96         if (!tmpDir.isAbsolute()) {
97             parentDir = new File JavaDoc(rootPath, address);
98         } else {
99             parentDir = new File JavaDoc(address);
100         }
101         if (!parentDir.exists())
102             parentDir.mkdirs();
103
104         String JavaDoc destDir = exchange.getEndpoint().getEndpointName();
105         File JavaDoc outputDir = new File JavaDoc(parentDir, destDir);
106
107         logger.log(Level.INFO, "Writing data to directory : "
108             + outputDir.getAbsolutePath());
109
110         if (StringHelper.isNullOrEmpty(content)) {
111             logger.log(Level.INFO, "content is null");
112
113         } else {
114             try {
115                 String JavaDoc fileName = null;
116                 if (exchange.getOperation() != null) {
117                     fileName = exchange.getOperation().getLocalPart();
118                 } else {
119                     fileName = "content";
120                 }
121
122                 IOUtils.writeStringToFile(content, outputDir, fileName);
123             } catch (FileTransferBCException e) {
124                 throw new HandlingException(e);
125             }
126         }
127
128         // write attachments to files
129
Set JavaDoc attachIds = nm.getAttachmentNames();
130         if (!attachIds.isEmpty()) {
131             try {
132                 IOUtils.writeAttachmentsToFiles(nm, outputDir);
133             } catch (FileTransferBCException e) {
134                 throw new HandlingException(e);
135             }
136         }
137
138         return result;
139     }
140 }
141
Popular Tags