View Javadoc

1   /*
2    * Maven SourceForge plugin 
3    * Author: Ludovic Claude (ludovicc@users.sourceforge.net)
4    * Distributable under BSD license.
5    */
6   
7   package net.sourceforge.mavenplugins.sourceforge;
8   
9   import org.apache.commons.jelly.JellyTagException;
10  import org.apache.commons.jelly.MissingAttributeException;
11  import org.apache.commons.jelly.TagSupport;
12  import org.apache.commons.jelly.XMLOutput;
13  import org.apache.commons.net.ftp.FTP;
14  import org.apache.commons.net.ftp.FTPClient;
15  import org.apache.commons.net.ftp.FTPReply;
16  
17  import java.io.File;
18  import java.io.FileInputStream;
19  import java.io.IOException;
20  
21  /***
22   * Bean used for accessing Sourceforge FTP site and sending there the files to deploy
23   * 
24   * @author <a href="mailto:ludovicc@users.sourceforge.net">Ludovic Claude</a>
25   * @author Stephen Colebourne
26   * @version $Revision: 1.6 $ $Date: 2006/01/07 22:50:45 $
27   * @created on 4 dec. 2003
28   */
29  public class FTPBean extends TagSupport{
30      private String _site;
31      private String _srcDir;
32      private String _destDir;
33      private String _userEmail;
34  
35      /***
36       * Constructor for FTPBean
37       */
38      public FTPBean() {
39          super();
40      }
41  
42      /***
43       * Gets the site.
44       * @return the site.
45       */
46      public String getSite() {
47          return _site;
48      }
49  
50      /***
51       * Sets the site.
52       * @param site The site to set.
53       */
54      public void setSite(String site) {
55          _site = site;
56      }
57  
58      /***
59       * Gets the destDir.
60       * @return the destDir.
61       */
62      public String getDestDir() {
63          return _destDir;
64      }
65  
66      /***
67       * Sets the destDir.
68       * @param destDir The destDir to set.
69       */
70      public void setDestDir(String destDir) {
71          _destDir = destDir;
72      }
73  
74      /***
75       * Gets the srcDir.
76       * @return the srcDir.
77       */
78      public String getSrcDir() {
79          return _srcDir;
80      }
81  
82      /***
83       * Sets the srcDir.
84       * @param srcDir The srcDir to set.
85       */
86      public void setSrcDir(String srcDir) {
87          _srcDir = srcDir;
88      }
89      
90      /***
91       * Gets the userEmail.
92       * @return the userEmail.
93       */
94      public String getUserEmail() {
95          return _userEmail;
96      }
97  
98      /***
99       * Sets the userEmail.
100      * @param userEmail The userEmail to set.
101      */
102     public void setUserEmail(String userEmail) {
103         _userEmail = userEmail;
104     }
105 
106     public void execute() throws Exception {
107         FTPClient ftp = new FTPClient();
108         
109         // Sanity check
110         if (_site == null) {
111             throw new Exception("Site is not defined. Check that property maven.sourceforge.ftp.site is set");
112         }
113         if (_srcDir == null) {
114             throw new Exception("Src dir is not defined. Check that property maven.sourceforge.publishDir is set");
115         }
116         if (_destDir == null) {
117             throw new Exception("Dest dir is not defined. Check that property maven.sourceforge.ftp.incomingDir is set");
118         }
119         if (_userEmail == null) {
120             throw new Exception("User email is not defined. Check that property maven.sourceforge.userEmail is set");
121         }
122        
123         try {
124             ftp.connect(_site);
125             ftp.enterLocalPassiveMode();
126             System.out.println("Connected to " + _site + ".");
127 
128             // After connection attempt, you should check the reply code to verify
129             // success.
130             checkReply(ftp);
131             
132             ftp.login("anonymous", _userEmail);
133             checkReply(ftp);
134             
135             ftp.setFileType(FTP.IMAGE_FILE_TYPE);
136             ftp.changeWorkingDirectory(_destDir);
137             checkReply(ftp);
138             
139             File sourceDir = new File(_srcDir);
140             File[] files = sourceDir.listFiles();
141             if (files == null || files.length == 0) {
142                 throw new Exception("No files to upload");
143             }
144             for (int i = 0; i < files.length; i++) {
145                 File file = files[i];
146                 System.out.println("copying file " + file.getName());
147                 ftp.storeFile(
148                         file.getName(),
149                         new FileInputStream(file));
150                 
151                 // Do a little pause before sending a new file or do something else.
152                 try {
153                     Thread.sleep(500);
154                 } catch (InterruptedException e) {
155                     // ignore
156                 }
157             }
158             
159             
160         } catch (IOException ex) {
161             ex.printStackTrace();
162             throw ex;
163         } finally {
164             if (ftp.isConnected()) {
165                 try {
166                     ftp.disconnect();
167                 } catch(IOException f) {
168                     // do nothing
169                 }
170             }
171         }
172     }
173     
174     /*** 
175      * {@inheritDoc}
176      */
177     public void doTag(XMLOutput out) throws MissingAttributeException, JellyTagException {
178         try {
179             execute();
180         } catch (Exception e) {
181             throw new JellyTagException(e);
182         }
183     }
184        
185     private void checkReply(FTPClient ftp) throws IOException {
186         System.out.print(ftp.getReplyString());
187         int reply = ftp.getReplyCode();
188         if(!FTPReply.isPositiveCompletion(reply)) {
189             ftp.disconnect();
190             throw new IOException("FTP server refused connection.");
191         }
192     }
193 
194 }