View Javadoc

1   /*
2    * Copyright (c) 2003 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: FTPConnectionFactory.java,v 1.1.1.1 2004/05/19 12:26:42 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.adaptor.ftp;
21  
22  import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
23  
24  import javax.management.ObjectName;
25  import javax.management.MBeanServer;
26  import javax.management.MalformedObjectNameException;
27  
28  import org.backsource.jmx.ServiceMBeanSupport;
29  import org.backsource.jmx.ObjectNameFactory;
30  
31  import org.backsource.utils.io.CachedStreamFactory;
32  import org.backsource.utils.io.CachedStream;
33  /***
34   * First scetch of a J2ee/jdbc like ftp adaptor.
35   *
36   * <p>This factory is currently hardwired to use the {@link NetFTPConnection}.</p>
37   <p>The typical usage scenarion is much like usage of JDBC. Either you have a preconfigured factory that you lookup through JDBC, or it is configured manually. It is however not serializable.</p>
38   <pre> 
39        FTPConnectionFactory  connectionFactory = new FTPConnectionFactory();
40        connectionFactory.setHostName( "localhost" );
41        connectionFactory.setUser( "pra@tim.se" );
42        connectionFactory.setPassword( anonymous );
43  
44        FTPConnection ftp = null;
45        try {
46          // Transfer a file
47          ftp.put("topfile.xml", new FileInputStream( new File(dir,"topfile.xml") ) );
48          // Create subdir an populate
49          ftp.mkdir("mysub");
50          ftp.put("mysub/subfile1.xml", new FileInputStream( new File(dir,"subfile1.xml") ) );
51  
52          // Remove stuff
53          ftp.deleteFile("topfile.xml");
54          ftp.deleteFile("mysub/subfile1.xml");
55          // delete dir
56          ftp.deleteFile("mysub");
57        } catch (FTPException e) {
58           // throw some own exception
59        } finally {
60          if ( ftp != null) {
61            ftp.close();
62          } // end of if ()      
63  
64        } // end of finally
65  
66        connectionFactory.stop();
67  </pre>         
68        
69   
70   *
71   * @author <a href="mailto:pra@tim.se">Peter Antman</a>
72   * @version $Revision: 1.1.1.1 $
73   * @jmx:mbean name="adaptor:service=ftp,name=serverName" extends="org.backsource.jmx.ServiceMBean"
74   */
75  
76  public class FTPConnectionFactory extends ServiceMBeanSupport
77     implements   org.backsource.adaptor.ftp.FTPConnectionFactoryMBean {
78     public static final javax.management.ObjectName OBJECT_NAME = ObjectNameFactory.create("adaptor:service=ftp,name=serverName");
79     public static final int DEFAULT_TIMEOUT = 60*1000;
80     private String hostName;
81     private String user;
82     private String pwd;
83     private int timeout = DEFAULT_TIMEOUT;
84     protected ObjectName name = OBJECT_NAME;
85     protected MBeanServer server;
86     protected LinkedQueue connections = new LinkedQueue();
87     protected CachedStreamFactory streamFactory;
88     protected CachedStream stream;
89     
90     public FTPConnectionFactory (){
91        
92     }
93     protected ObjectName getObjectName(MBeanServer server, ObjectName name)
94        throws MalformedObjectNameException
95     {
96        this.server = server;
97        if ( name != null) {
98           this.name = name;
99        } // end of if ()
100       
101       return this.name;
102    }
103    /***
104     * Name of the ftp server.
105     *
106     * @jmx:managed-attribute
107     */
108    public void setHostName(String hostName) {
109       this.hostName = hostName;
110    }
111 
112    /***
113     * Name of the ftp server.
114     *
115     * @jmx:managed-attribute
116     */
117    public String getHostName() {
118       return hostName;
119    }
120 
121    /***
122     * Name of the user.
123     *
124     * @jmx:managed-attribute
125     */
126    public void setUser(String user) {
127       this.user = user;
128    }
129 
130    /***
131     * Name of the user.
132     *
133     * @jmx:managed-attribute
134     */
135    public String getUser() {
136       return user;
137    }
138    /***
139     * Password.
140     *
141     * @jmx:managed-attribute
142     */
143    public void setPassword(String pwd) {
144       this.pwd = pwd;
145    }
146 
147    /***
148     * Password.
149     *
150     * @jmx:managed-attribute
151     */
152    public String getPassword() {
153       return pwd;
154    }
155    
156    /***
157     * Timeout.
158     *
159     * @jmx:managed-attribute
160     */
161    public void setTimeout(int timeout) {
162       this.timeout = timeout;
163    }
164 
165    /***
166     * The a {@link org.backsource.utils.io.CachedStreamFactory} to use for stream handling; if not supplied the default one according the default configuration rules are used.
167     */
168    public void setCachedStreamFactory(CachedStreamFactory streamFactory) {
169       this.streamFactory = streamFactory;
170    }
171 
172    /***
173     * Get the cached stream (not the factory).
174     */
175    public CachedStream getCachedStream() throws FTPException {
176       try {
177          synchronized(this) {
178             if ( stream == null) {
179                if ( streamFactory == null) {
180                   streamFactory = CachedStreamFactory.newInstance();
181                   
182                } // end of if ()
183                stream = streamFactory.getCachedStream();
184             }
185          }
186          
187       } catch (ClassNotFoundException e) {
188          throw new FTPException("Could not construct a cached stream: " +e,e);
189       } // end of try-catch
190       return stream;
191    }
192 
193    /***
194     * Timeout.
195     *
196     * @jmx:managed-attribute
197     */
198    public int getTimeout() {
199       return timeout;
200    }
201 
202    public void stop() {
203       log.debug("Stoping factory");
204       NetFTPConnection ftp = null;
205       while (!connections.isEmpty()) {
206          try {      
207             ftp = (NetFTPConnection)connections.take();
208             if ( ftp != null) {
209                ftp.internalClose();
210             } // end of if ()
211          } catch ( InterruptedException e) {
212             log.warn("Pool interrupted while trying to empty");
213          } // end of try-catch
214       } // end of while ()
215       log.debug("Factory stoped");
216    }
217    
218    
219    /***
220     * Get a new FTPConnection, may have been cached/pooled.
221     * <p>The connection MUST be close after usage, use it much like you would use a JDBC connection.
222     */
223    public FTPConnection getConnection() throws FTPException {
224       NetFTPConnection ftp = null;
225       if ( connections.isEmpty() ) {
226          ftp =  new NetFTPConnection(this);
227       } else {
228          try {
229             ftp = (NetFTPConnection)connections.take();
230          } catch (InterruptedException e) {
231             throw new FTPException("Could not get a connection from pool "+e,e);
232          } // end of try-catch
233 
234       } // end of else
235       if ( !ftp.isAlive()) {
236          ftp.connect();
237       } // end of if ()
238       return ftp;
239       
240    }
241 
242    /***
243     * Hook from the connection to return the connection to the pool.
244     */
245    protected void releaseConnection(FTPConnection ftp) {
246       NetFTPConnection myFtp = ( NetFTPConnection)ftp;
247       log.debug("Releasing connection: " +ftp);
248       //if ( myFtp != null) {
249          // Better safe than sorry!
250       //  myFtp.internalClose();
251 
252       //} 
253       // end of if ()
254       try {
255          connections.put(myFtp);
256       } catch (InterruptedException e) {
257          ;//Ignored
258          //What the FUCK!
259       } // end of try-catch
260       log.debug("Released connection: " +ftp);
261    }
262 }// FTPConnectionFactory