KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > authentication > DODSAuthenticationManager


1 /* DODSAuthenticationManager.java */
2
3 package org.enhydra.shark.authentication;
4
5 import java.sql.SQLException JavaDoc;
6
7 import org.enhydra.dods.DODS;
8 import org.enhydra.shark.api.RootException;
9 import org.enhydra.shark.api.UserTransaction;
10 import org.enhydra.shark.api.internal.authentication.AuthenticationManager;
11 import org.enhydra.shark.api.internal.working.CallbackUtilities;
12 import org.enhydra.shark.usergroup.data.UserDO;
13 import org.enhydra.shark.usergroup.data.UserQuery;
14 import org.enhydra.shark.usertransaction.SharkDODSUserTransaction;
15 import org.enhydra.shark.utilities.MiscUtilities;
16 import org.enhydra.shark.utilities.dods.DODSUtilities;
17
18 import com.lutris.appserver.server.sql.*;
19
20 /**
21  * Authenticates userId or password for some userId.
22  * @author Sasa Bojanic, Vladimir Puskas
23  * @version 0.11
24  */

25 public class DODSAuthenticationManager implements AuthenticationManager {
26
27    private static final String JavaDoc LDB_PARAM_NAME = "DODSAuthenticationManager.DatabaseName";
28    private CallbackUtilities callback;
29    private LogicalDatabase db;
30
31    /**
32     * Configures DODSAuthenticationManager.
33     *
34     * @param cus an instance of CallbackUtilities used to get
35     * properties for configuring AuthenticationManager in Shark.
36     *
37     * @exception RootException thrown if configuring doesn't succeed.
38     */

39    public void configure (CallbackUtilities cus) throws RootException {
40       if (null == cus)
41          throw new RootException("Cannot configure without call back impl.");
42       callback = cus;
43       DODSUtilities.init(callback.getProperties());
44       String JavaDoc dbName = callback
45          .getProperty(LDB_PARAM_NAME, DODS.getDatabaseManager().getDefaultDB());
46       try {
47          db = DODS.getDatabaseManager().findLogicalDatabase(dbName);
48       } catch (DatabaseManagerException e) {
49          throw new RootException("Couldn't find logical database.", e);
50       }
51       callback.debug("DODSUserGroupManager configured");
52    }
53
54    /**
55     * Validates user.
56     *
57     * @param t user transaction.
58     * @param username user name.
59     * @param password user password.
60     *
61     * @return true if user is validated, otherwise false.
62     *
63     * @exception RootException If something unexpected happens.
64     */

65    public boolean validateUser(UserTransaction t,
66                                String JavaDoc username,
67                                String JavaDoc password) throws RootException {
68       boolean ret = false;
69       DBTransaction dbt = beginTransaction(t);
70       try {
71          UserQuery uQry = new UserQuery(dbt);
72          uQry.setQueryUserid(username);
73          uQry.requireUniqueInstance();
74          UserDO user = uQry.getNextDO();
75
76          ret = (null != user)
77             ? MiscUtilities.passwordDigest(password).equals(user.getPasswd())
78             : false;
79
80          endTransaction(t, dbt, false);
81       } catch (Exception JavaDoc e) {
82          if (e instanceof RootException) {
83             throw (RootException)e;
84          }
85          throw new RootException(e);
86       }
87       return ret;
88    }
89
90    private void endTransaction(UserTransaction t,
91                                DBTransaction dbt,
92                                boolean commitToo) throws RootException {
93       if (!dbt.getDatabaseName().equals(db.getName())) {
94          try {
95             if (commitToo) {
96                dbt.commit();
97             }
98          } catch (SQLException JavaDoc e) {
99             throw new RootException(e);
100          } finally {
101             dbt.release();
102          }
103       }
104    }
105
106    private DBTransaction beginTransaction(UserTransaction t) throws RootException {
107       DBTransaction dbt = ((SharkDODSUserTransaction)t).getDODSTransaction();
108       if (!dbt.getDatabaseName().equals(db.getName())) {
109          try {
110             dbt = db.createTransaction();
111          } catch (SQLException JavaDoc e) {
112             throw new RootException(e);
113          }
114       }
115       return dbt;
116    }
117 }
118 /* End of DODSAuthenticationManager.java */
119
120
Popular Tags