KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > oamp > plugin > CommunicatorClientList


1 /*
2  * CommunicatorClientList.java
3  *
4  * Created on June 1, 2003, 10:13 AM
5  */

6
7 package com.quikj.application.web.oamp.plugin;
8
9 import java.sql.*;
10 import java.util.*;
11
12 import com.quikj.server.web.*;
13 import com.quikj.client.raccess.*;
14 import com.quikj.server.framework.*;
15
16 /**
17  *
18  * @author amit
19  */

20 public class CommunicatorClientList implements RemoteServiceInterface
21 {
22     // database account table name constant
23
public static final String JavaDoc ACCOUNT_TABLE_NAME = "account_tbl";
24
25     // database account table column name constants
26
public static final String JavaDoc NAME = "userid";
27
28     public static final String JavaDoc PASSWORD = "password";
29
30     public static final String JavaDoc DOMAIN = "domain";
31
32     public static final String JavaDoc FLAGS = "flags";
33
34     public static final String JavaDoc LEVEL = "level";
35
36     public static final String JavaDoc ADDITIONAL_INFO = "addnl_info";
37
38     public static final String JavaDoc FEATURES = "features";
39
40     private HashMap list = new HashMap();
41
42     private static CommunicatorClientList instance = null;
43
44     private Connection connection;
45
46     private Random random = new Random((new java.util.Date JavaDoc()).getTime());
47
48     /** Creates a new instance of CommunicatorClientList */
49     public CommunicatorClientList()
50     {
51         connection = JDBCConnection.getInstance().getNewConnection(
52                 FeatureOAMP.Instance().getDatabaseName());
53         AceRMIImpl.getInstance().registerService(
54                 "com.quikj.application.web.oamp.plugin.CommunicatorClientList",
55                 this);
56
57         instance = this;
58     }
59
60     public static CommunicatorClientList getInstance()
61     {
62         return instance;
63     }
64
65     public AccountElement get(String JavaDoc auth)
66     {
67         return (AccountElement) list.get(auth);
68     }
69
70     public void dispose()
71     {
72         if (connection != null)
73         {
74             try
75             {
76                 connection.close();
77                 connection = null;
78             }
79             catch (SQLException ex)
80             {
81             }
82         }
83     }
84
85     public synchronized void unregisterCommunicatorUser(String JavaDoc authcode)
86     {
87         list.remove(authcode);
88     }
89
90     private void exerciseConnection()
91     {
92         Statement st;
93         try
94         {
95             st = connection.createStatement();
96             st.executeQuery("SELECT 1");
97             st.close();
98         }
99         catch (SQLException e)
100         {
101             // do nothing, this should set the state of the connection
102
// where connection.isClosed() is going to return true.
103
return;
104         }
105     }
106
107     private PreparedStatement getLoginQuery(String JavaDoc username, String JavaDoc password)
108             throws SQLException
109     {
110         String JavaDoc sql = "select " + DOMAIN + ", " + LEVEL + ", " + ADDITIONAL_INFO
111                 + ", " + FEATURES + " from " + ACCOUNT_TABLE_NAME + " where "
112                 + NAME + " = ? and " + PASSWORD + " = password(?)";
113
114         exerciseConnection();
115         if (connection.isClosed() == true)
116         {
117             connection = JDBCConnection.getInstance().getNewConnection(
118                     FeatureOAMP.Instance().getDatabaseName());
119         }
120
121         PreparedStatement pstmt = connection.prepareStatement(sql);
122         pstmt.setString(1, username);
123         pstmt.setString(2, password);
124         return pstmt;
125     }
126
127     private void processLoginResult(AccountElement userinfo, ResultSet result)
128             throws SQLException
129     {
130         processQueryResult(userinfo, result);
131     }
132
133     private void processQueryResult(AccountElement userinfo, ResultSet result)
134             throws SQLException
135     {
136         userinfo.setDomain(result.getString(1));
137         userinfo.setLevel(result.getInt(2));
138         userinfo.setAdditionalInfo(result.getString(3));
139
140         Blob features = result.getBlob(4);
141         int len = (int) features.length();
142         userinfo.stringToFeatureList(new String JavaDoc(features.getBytes(1, len))
143                 .trim());
144     }
145
146     public synchronized String JavaDoc registerCommunicatorUser(String JavaDoc user,
147             String JavaDoc password)
148     {
149         try
150         {
151             PreparedStatement st = getLoginQuery(user, password);
152             ResultSet rs = st.executeQuery();
153             if (rs.next() == false)
154             {
155                 return null;
156             }
157
158             AccountElement e = new AccountElement();
159             processLoginResult(e, rs);
160             e.setName(user);
161
162             String JavaDoc authcode = generateAuthCode();
163             list.put(authcode, e);
164             return authcode;
165         }
166         catch (SQLException ex)
167         {
168             AceLogger.Instance().log(
169                     AceLogger.ERROR,
170                     AceLogger.SYSTEM_LOG,
171                     "CommunicatorClientList.registerCommunicatorUser(): SQLException "
172                             + ex.getMessage());
173
174             return null;
175         }
176     }
177
178     private String JavaDoc generateAuthCode()
179     {
180         while (true)
181         {
182             int rand = random.nextInt();
183             rand = (rand >= 0) ? rand : 0 - rand;
184             if (list.containsKey(new Integer JavaDoc(rand)) == false)
185             {
186                 return String.valueOf(rand);
187             }
188         }
189     }
190
191     public String JavaDoc getRMIParam(String JavaDoc param)
192     {
193         StringTokenizer tokens = new StringTokenizer(param, ":");
194         int num_tokens = tokens.countTokens();
195         String JavaDoc command = tokens.nextToken();
196
197         if (command.equals("register") == true)
198         {
199             if (num_tokens < 3)
200             {
201                 return null;
202             }
203
204             return registerCommunicatorUser(tokens.nextToken(), tokens
205                     .nextToken());
206         }
207
208         return null;
209     }
210
211     public boolean setRMIParam(String JavaDoc param, String JavaDoc value)
212     {
213         StringTokenizer tokens = new StringTokenizer(param, ":");
214         int num_tokens = tokens.countTokens();
215         String JavaDoc command = tokens.nextToken();
216
217         if (command.equals("unregister") == true)
218         {
219             if (num_tokens < 2)
220             {
221                 return false;
222             }
223
224             unregisterCommunicatorUser(tokens.nextToken());
225             return true;
226         }
227
228         return false;
229     }
230
231 }
Popular Tags