View Javadoc

1   package com.pow2.user.dao;
2   
3   import org.apache.log4j.Category;
4   
5   import com.pow2.util.Util;
6   import com.pow2.resources.Prefs;
7   
8   
9   /***
10   *  "Creator" class for UserDAO objects.
11   *
12   * @author     Luca Fossato
13   * @created    22 aprile 2002
14   */
15  public class UserDAOFactory
16  {
17    /*** user dao provider key */
18    public final static String KEY_USER_DATA_PROVIDER = "user.data.provider";
19  
20    /*** RDBMS user dao provider */
21    public final static String USER_DATA_PROVIDER_RDBMS = "rdbms";
22  
23    /*** LDAP user dao provider */
24    public final static String USER_DATA_PROVIDER_LDAP = "ldap";
25  
26    /*** an handle to the unique UserDAOFactory instance. */
27    private static UserDAOFactory instance = null;
28  
29    /*** default data access provider */
30    private int defaultProvider;
31  
32    /*** relational data base managements system provider */
33    private final static int PROVIDER_RDBMS = 0;
34  
35    /*** LDAP provider */
36    private final static int PROVIDER_LDAP = 1;
37  
38    /*** Log4J category. */
39    private Category cat = Category.getInstance(this.getClass());
40  
41  
42    /***
43     *  Default private constructor.
44     */
45    private UserDAOFactory()
46    {
47      String provider = Prefs.instance().getProperty(KEY_USER_DATA_PROVIDER);
48  
49      if (Util.isNull(provider))
50        defaultProvider = PROVIDER_RDBMS;
51  
52      if (USER_DATA_PROVIDER_RDBMS.equals(provider))
53        defaultProvider = PROVIDER_RDBMS;
54  
55      else if (USER_DATA_PROVIDER_LDAP.equals(provider))
56        defaultProvider = PROVIDER_LDAP;
57  
58      cat.info("::UserDAOFactory - use [" + provider + "] user data provider");
59    }
60  
61  
62    /***
63     *  Get the unique instance of UserDAOFactory class.
64     *
65     * @return    the instance of UserDAOFactory class
66     */
67    public static synchronized UserDAOFactory instance()
68    {
69      if (instance == null)
70        instance = new UserDAOFactory();
71  
72      return instance;
73    }
74  
75  
76    /***
77     *   Return an UserDAO object whose subclass is specified by
78     *   the <code>defaultProvider</code> member attribute.
79     */
80    public UserDAO getUserDAO()
81    {
82       return getUserDAO(defaultProvider);
83    }
84  
85  
86    /***
87     *  Return an UserDAO object whose subclass is specified by the input
88     *  <code>provider</code> parameter.
89     *
90     * @param  provider specifies the UserDAO class type.
91     * @return the selected userDAO instance, or null if the input
92     *         <code>provider</code> parameter doesn't match an UserDAO
93     *         subclass.<br>
94     *         Supported provider constants are:<br>
95     *         <ul>
96     *           <li>PROVIDER_RDBMS to use rdbms data access</li>
97     *           <li>PROVIDER_LDAP  to use ldap data access</li>
98     *         </ul>
99     */
100   public UserDAO getUserDAO(int provider)
101   {
102     switch (provider)
103     {
104       case PROVIDER_RDBMS: return DBUserDAO.instance();
105       case PROVIDER_LDAP:  return LDAPUserDAO.instance();
106     }
107 
108     return null;
109   }
110 
111 
112   /***
113    *  Get the current User data provider type,
114    *  asspecified by the Prefs object.
115    *
116    * @return the User data provider type
117    */
118   public String getProviderType()
119   {
120     return Prefs.instance().getProperty(KEY_USER_DATA_PROVIDER);
121   }
122 }