View Javadoc

1   /**********************************************************************
2    * AdapterFactory.java
3    * created on 15.03.2005 by netseeker
4    * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/adapter/AdapterFactory.java,v $
5    * $Date: 2006/02/04 14:16:49 $
6    * $Revision: 1.5 $
7    *********************************************************************/
8   package de.netseeker.ejoe.adapter;
9   
10  import java.util.Hashtable;
11  import java.util.Map;
12  import java.util.logging.Level;
13  import java.util.logging.Logger;
14  
15  /***
16   * @author netseeker
17   */
18  public final class AdapterFactory
19  {
20  	/***
21  	 * Logger for this class
22  	 */
23  	private static final Logger	logger		= Logger.getLogger(AdapterFactory.class.getName());
24  
25  	/***
26  	 * cache for already created adapter instances, use a Hashtable because it's
27  	 * synchronized
28  	 */
29  	private static final Map	_adapters	= new Hashtable();
30  
31  	/***
32  	 * @param name
33  	 * @return
34  	 */
35  	public static SerializeAdapter createAdapter(String name) throws IllegalAdapterException
36  	{
37  		SerializeAdapter adapter = (SerializeAdapter) _adapters.get(name);
38  
39  		if (adapter == null)
40  		{
41  			try
42  			{
43  				adapter = (SerializeAdapter) Class.forName(name).newInstance();
44  				_adapters.put(name, adapter);
45  			}
46  			catch (InstantiationException e)
47  			{
48  				logger.log(Level.SEVERE, "Error while trying to instaniate SerializeAdapter: " + name, e);
49  				throw new IllegalAdapterException(e.getMessage());
50  			}
51  			catch (IllegalAccessException e)
52  			{
53  				logger.log(Level.SEVERE, "Error while trying to access SerializeAdapter: " + name, e);
54  				throw new IllegalAdapterException(e.getMessage());
55  			}
56  			catch (ClassNotFoundException e)
57  			{
58  				logger.log(Level.SEVERE, "Class for SerializeAdapter: " + name + " not found.", e);
59  				throw new IllegalAdapterException(e.getMessage());
60  			}
61  		}
62  
63  		return adapter;
64  	}
65  }