View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  
25  package com.mindtree.techworks.insight.download;
26  
27  import java.util.HashMap;
28  
29  
30  /**
31   * Returns instances of <code>RemoteClient</code> based on the Fileset passed.
32   * The class looks at the Fileset type to determine the RemoteClient to pass.
33   * 
34   * @see com.mindtree.techworks.insight.download.Fileset#getTypeString() Fileset type.
35   * @author Bindul Bhowmik
36   * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
37   */
38  public class RemoteClientFactory {
39  
40  	//
41  	// Classloader variables
42  	//
43  	
44  	/**
45  	 * Static map containing the protocols and the client classnames.
46  	 */
47  	private static HashMap clients = new HashMap();
48  
49  	//
50  	// Static methods
51  	//
52  	
53  	/**
54  	 * Method to be called on load by all Handlers. The protocol identifier,
55  	 * should be one from {@link RemoteClient RemoteClient}identifiers.
56  	 * 
57  	 * @param protocolIdentifier The protocol identifier
58  	 * @param handlerClass The class handling the protocol
59  	 */
60  	public static void registerClient (String protocolIdentifier,
61  			Class handlerClass) {
62  
63  		clients.put(protocolIdentifier, handlerClass);
64  	}
65  
66  	/**
67  	 * Identifies and returns a client for the fileset provided
68  	 * 
69  	 * @param fileset The fileset to get a client for
70  	 * @return An instance of RemoteClient to download the files
71  	 * @throws RemoteClientException If no handler is configured for the
72  	 *             protocol in the fileset, or if a client cannot be
73  	 *             instantiated.
74  	 */
75  	public static RemoteClient getClientForFileset (Fileset fileset)
76  			throws RemoteClientException {
77  
78  		String protocolIdentifier = fileset.getTypeString();
79  
80  		// Got the identifier, now return the client
81  		Class handlerClass = (Class) clients.get(protocolIdentifier);
82  		if (null != handlerClass) {
83  			try {
84  				return (RemoteClient) handlerClass.newInstance();
85  			} catch (InstantiationException e) {
86  				throw new RemoteClientException(
87  						"Cannot create instance of class", e);
88  			} catch (IllegalAccessException e) {
89  				throw new RemoteClientException(
90  						"Cannot create instance of class", e);
91  			}
92  		} else {
93  			throw new RemoteClientException(
94  					"No client configured for the type.");
95  		}
96  	}
97  }