KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > joram > mom > dest > ftp > FtpThread


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2007 ScalAgent Distributed Technologies
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 any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Nicolas Tachker (ScalAgent)
21  * Contributor(s):
22  */

23 package com.scalagent.joram.mom.dest.ftp;
24
25 import java.net.InetAddress JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Enumeration JavaDoc;
29
30 import org.objectweb.joram.mom.notifications.ClientMessages;
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.util.monolog.api.Logger;
33
34 import fr.dyade.aaa.agent.AgentId;
35 import fr.dyade.aaa.agent.Channel;
36 import fr.dyade.aaa.agent.Debug;
37
38 public class FtpThread extends Thread JavaDoc {
39
40   public static Logger logger =
41     Debug.getLogger("com.scalagent.joram.mom.dest.ftp.FtpThread");
42   
43   private TransferItf transfer;
44   private AgentId destId;
45   private FtpMessage ftpMsg;
46   private AgentId dmqId;
47   private int clientContext;
48   private int requestId;
49   private String JavaDoc user;
50   private String JavaDoc pass;
51   private String JavaDoc path;
52
53   public String JavaDoc ftpImplName;
54
55   public FtpThread(TransferItf transfer,
56                    FtpMessage ftpMsg,
57                    AgentId destId,
58                    AgentId dmqId,
59                    int clientContext,
60                    int requestId,
61                    String JavaDoc user,
62                    String JavaDoc pass,
63                    String JavaDoc path) {
64     this.transfer = transfer;
65     this.ftpMsg = ftpMsg;
66     this.destId = destId;
67     this.dmqId = dmqId;
68     this.clientContext = clientContext;
69     this.requestId = requestId;
70     this.user = user;
71     this.pass = pass;
72     this.path = path;
73     if (logger.isLoggable(BasicLevel.DEBUG))
74       logger.log(BasicLevel.DEBUG, "--- " + this);
75   }
76
77   public void run() {
78     if (logger.isLoggable(BasicLevel.DEBUG))
79       logger.log(BasicLevel.DEBUG, "--- run()");
80     doFtp(ftpMsg);
81   }
82
83
84   protected void doFtp(FtpMessage msg) {
85     String JavaDoc urlName = null;
86     long crc = -1;
87     boolean ack = false;
88     
89     try {
90       urlName = msg.getStringProperty(SharedObj.url);
91       crc = msg.getLongProperty(SharedObj.crc);
92       ack = msg.getBooleanProperty(SharedObj.ack);
93
94       URL JavaDoc url = new URL JavaDoc(urlName);
95       String JavaDoc urlFileName = url.getFile();
96       String JavaDoc fileName = null;
97       String JavaDoc type = null;
98       String JavaDoc remotePath = null;
99       
100       if (logger.isLoggable(BasicLevel.DEBUG))
101         logger.log(BasicLevel.DEBUG, "--- doFtp : url host = " + url.getHost() +
102                                      ", urlFileName = " + urlFileName);
103
104       if (urlFileName.indexOf(";") > 1) {
105         fileName = urlFileName.substring(
106           urlFileName.lastIndexOf('/')+1,urlFileName.indexOf(";"));
107         type = urlFileName.substring(
108           urlFileName.lastIndexOf("=")+1,urlFileName.length());
109       } else {
110         fileName = urlFileName.substring(
111           urlFileName.lastIndexOf('/')+1,urlFileName.length());
112       }
113       
114       if (urlFileName.indexOf('/') < urlFileName.lastIndexOf('/'))
115         remotePath = urlFileName.substring(
116           urlFileName.indexOf('/')+1,urlFileName.lastIndexOf('/'));
117       
118       
119       String JavaDoc userInfo = url.getUserInfo();
120       String JavaDoc remoteUser = "anonymous";
121       String JavaDoc remotePass = "no@no.no";
122       if (userInfo != null) {
123         remoteUser = userInfo.substring(0,userInfo.indexOf(':'));
124         remotePass = userInfo.substring(userInfo.indexOf(':')+1,userInfo.length());
125       }
126       String JavaDoc protocol = url.getProtocol();
127       int port = url.getPort();
128
129       if (logger.isLoggable(BasicLevel.DEBUG))
130         logger.log(BasicLevel.DEBUG,
131                                       "doFtp : remoteUser = " + remoteUser +
132                                       ", protocol = " + protocol +
133                                       ", port = " + port);
134
135       String JavaDoc file = transfer.getFile(protocol,
136                                      InetAddress.getByName(url.getHost()).getHostName(),
137                                      port,
138                                      remoteUser,
139                                      remotePass,
140                                      remotePath,
141                                      path,
142                                      fileName,
143                                      fileName,
144                                      type,
145                                      crc);
146       
147       file = file.replace('\\','/');
148       if (!file.startsWith("/"))
149         file = "/" + file;
150       
151       URI JavaDoc uri = new URI JavaDoc("file",null,file,null,null);
152       uri = uri.normalize();
153
154       if (logger.isLoggable(BasicLevel.DEBUG))
155         logger.log(BasicLevel.DEBUG, "--- doFtp : uri = " + uri);
156       
157 // StringBuffer sb = new StringBuffer();
158
// sb.append(protocol + "://");
159
// sb.append(user);
160
// sb.append(":");
161
// sb.append("****");//pass);
162
// sb.append("@");
163
// sb.append(InetAddress.getLocalHost().getHostName());
164
// if (port > -1) {
165
// sb.append(":");
166
// sb.append(port);
167
// }
168
// sb.append("/");
169
// if (path != null)
170
// sb.append(path + "/");
171
// sb.append(fileName);
172
// sb.append(";type=");
173
// sb.append(type);
174

175
176       FtpMessage clone = (FtpMessage) msg.clone();
177       clone.clearProperties();
178       for (Enumeration JavaDoc e = msg.getPropertyNames(); e.hasMoreElements(); ) {
179         String JavaDoc key = (String JavaDoc) e.nextElement();
180         clone.setObjectProperty(key, msg.getObjectProperty(key));
181       }
182       clone.setStringProperty("url", uri.toString());
183       
184       Channel.sendTo(destId, new FtpNot(clientContext,
185                                        requestId,
186                                        clone.getSharedMessage()));
187       
188     } catch (Exception JavaDoc exc) {
189       ClientMessages deadM =
190         new ClientMessages(clientContext,
191                            requestId);
192       msg.setNotWriteable(true);
193       deadM.addMessage(msg.getSharedMessage());
194       if (dmqId != null)
195         Channel.sendTo(dmqId, deadM);
196     }
197   }
198
199   public String JavaDoc toString() {
200     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
201     buf.append("FtpThread (");
202     buf.append("transfer=");
203     buf.append(transfer);
204     buf.append(", ftpMsg=");
205     buf.append(ftpMsg);
206     buf.append(", destId=");
207     buf.append(destId);
208     buf.append(", dmqId=");
209     buf.append(dmqId);
210     buf.append(", clientContext=");
211     buf.append(clientContext);
212     buf.append(", requestId=");
213     buf.append(requestId);
214     buf.append(", user=");
215     buf.append(user);
216     buf.append(", pass=");
217     buf.append(pass);
218     buf.append(", path=");
219     buf.append(path);
220     buf.append(")");
221     return buf.toString();
222   }
223 }
224
Popular Tags