Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/alicebot/server/core/DBMultiplexor.java


1   // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
2   // Jad home page: http://www.geocities.com/kpdus/jad.html
3   // Decompiler options: packimports(3) 
4   
5   package org.alicebot.server.core;
6   
7   import java.io.UnsupportedEncodingException;
8   import java.net.URLDecoder;
9   import java.net.URLEncoder;
10  import java.sql.ResultSet;
11  import java.sql.SQLException;
12  import java.util.HashMap;
13  import org.alicebot.server.core.logging.Log;
14  import org.alicebot.server.core.util.DeveloperError;
15  import org.alicebot.server.core.util.UserError;
16  import org.alicebot.server.sql.pool.DbAccess;
17  import org.alicebot.server.sql.pool.DbAccessRefsPoolMgr;
18  
19  // Referenced classes of package org.alicebot.server.core:
20  //            Multiplexor, NoSuchPredicateException, Globals
21  
22  public class DBMultiplexor extends Multiplexor
23  {
24  
25      public DBMultiplexor()
26      {
27      }
28  
29      public void initialize()
30      {
31          super.initialize();
32          Log.devinfo("Opening database pool.", new String[] {
33              Log.DATABASE, Log.STARTUP
34          });
35          dbManager = new DbAccessRefsPoolMgr(Globals.getProperty("programd.database.driver", ""), Globals.getProperty("programd.database.url", ""), Globals.getProperty("programd.database.user", ""), Globals.getProperty("programd.database.password", ""));
36          Log.devinfo("Populating database pool.", new String[] {
37              Log.DATABASE, Log.STARTUP
38          });
39          dbManager.populate(Integer.parseInt(Globals.getProperty("programd.database.connections", "")));
40      }
41  
42      public void savePredicate(String s, String s1, String s2, String s3)
43      {
44          String s4;
45          try
46          {
47  //            s4 = URLEncoder.encode(s1.trim(), "UTF-8");
48              s4 = URLEncoder.encode(s1.trim());
49          }
50  //        catch(UnsupportedEncodingException unsupportedencodingexception)
51          catch(Exception unsupportedencodingexception)
52          {
53              throw new DeveloperError("This platform does not support UTF-8!");
54          }
55          DbAccess dbaccess = null;
56          try
57          {
58              dbaccess = dbManager.takeDbaRef();
59          }
60          catch(Exception exception)
61          {
62              throw new UserError("Could not get database reference when setting predicate name \"" + s + "\" to value \"" + s1 + "\" for \"" + s2 + "\" as known to \"" + s3 + "\".", exception);
63          }
64          try
65          {
66              ResultSet resultset = dbaccess.executeQuery("select value from predicates where botid = '" + s3 + "' and userid = '" + s2 + "' and name = '" + s + "'");
67              int i;
68              for(i = 0; resultset.next(); i++);
69              if(i > 0)
70                  dbaccess.executeUpdate("update predicates set value = '" + s4 + "' where botid = '" + s3 + "' and userid= '" + s2 + "' and name = '" + s + "'");
71              else
72                  dbaccess.executeUpdate("insert into predicates (userid, botid, name, value) values ('" + s2 + "', '" + s3 + "' , '" + s + "','" + s4 + "')");
73              resultset.close();
74              dbManager.returnDbaRef(dbaccess);
75          }
76          catch(SQLException sqlexception)
77          {
78              Log.userinfo("Database error: " + sqlexception, new String[] {
79                  Log.DATABASE, Log.ERROR
80              });
81          }
82      }
83  
84      public String loadPredicate(String s, String s1, String s2)
85          throws NoSuchPredicateException
86      {
87          String s3 = null;
88          DbAccess dbaccess = null;
89          try
90          {
91              dbaccess = dbManager.takeDbaRef();
92          }
93          catch(Exception exception)
94          {
95              throw new UserError("Could not get database reference when getting value for predicate name \"" + s + "\" for \"" + s1 + "\" as known to \"" + s2 + "\".", exception);
96          }
97          try
98          {
99              ResultSet resultset = dbaccess.executeQuery("select value from predicates where botid = '" + s2 + "' and userid = '" + s1 + "' and name = '" + s + "'");
100             int i = 0;
101             while(resultset.next()) 
102             {
103                 i++;
104                 s3 = resultset.getString("value");
105             }
106             resultset.close();
107             dbManager.returnDbaRef(dbaccess);
108         }
109         catch(SQLException sqlexception)
110         {
111             Log.log("Database error: " + sqlexception, Log.ERROR);
112             throw new NoSuchPredicateException(s);
113         }
114         if(s3 == null)
115             throw new NoSuchPredicateException(s);
116         try
117         {
118 //            return URLDecoder.decode(s3, "UTF-8");
119             return URLDecoder.decode(s3);
120         }
121 //        catch(UnsupportedEncodingException unsupportedencodingexception)
122         catch(Exception unsupportedencodingexception)
123         {
124             throw new DeveloperError("This platform does not support UTF-8!");
125         }
126     }
127 
128     public boolean createUser(String s, String s1, String s2, String s3)
129     {
130         if(!s2.equals(Multiplexor.SECRET_KEY))
131         {
132             Log.userinfo("ACCESS VIOLATION: Tried to create a user with invalid secret key.", Log.ERROR);
133             return false;
134         }
135         s = s.trim().toLowerCase();
136         s1 = s1.trim().toLowerCase();
137         DbAccess dbaccess = null;
138         try
139         {
140             dbaccess = dbManager.takeDbaRef();
141         }
142         catch(Exception exception)
143         {
144             throw new UserError("Could not get database reference when creating user \"" + s + "\" with password \"" + s1 + "\" and secret key \"" + s2 + "\".", exception);
145         }
146         try
147         {
148             ResultSet resultset = dbaccess.executeQuery("select * from users where userid = '" + s + "' and botid = '" + s3 + "'");
149             int i = 0;
150             while(resultset.next()) 
151                 if(++i == 1)
152                 {
153                     resultset.close();
154                     dbManager.returnDbaRef(dbaccess);
155                     return false;
156                 }
157             dbaccess.executeUpdate("insert into users (userid, password, botid) values ('" + s + "' , '" + s1 + "' , '" + s3 + "')");
158             resultset.close();
159         }
160         catch(SQLException sqlexception)
161         {
162             throw new UserError("Error working with database.", sqlexception);
163         }
164         dbManager.returnDbaRef(dbaccess);
165         return true;
166     }
167 
168     public boolean checkUser(String s, String s1, String s2, String s3)
169     {
170         if(!s2.equals(Multiplexor.SECRET_KEY))
171         {
172             Log.userinfo("ACCESS VIOLATION: Tried to create a user with invalid secret key.", Log.ERROR);
173             return false;
174         }
175         if(!userCacheForBots.containsKey(s3))
176             userCacheForBots.put(s3, new HashMap());
177         HashMap hashmap = (HashMap)userCacheForBots.get(s3);
178         if(hashmap.containsKey(s))
179             return ((String)hashmap.get(s)).equals(s1);
180         if(checkUserInDB(s, s1, s3))
181         {
182             hashmap.put(s, s1);
183             return true;
184         } else
185         {
186             return false;
187         }
188     }
189 
190     private boolean checkUserInDB(String s, String s1, String s2)
191     {
192         String s3 = null;
193         s = s.trim().toLowerCase();
194         s1 = s1.trim().toLowerCase();
195         DbAccess dbaccess = null;
196         try
197         {
198             dbaccess = dbManager.takeDbaRef();
199         }
200         catch(Exception exception)
201         {
202             throw new UserError("Could not get database reference when checking user \"" + s + "\" with password \"" + s1 + "\".", exception);
203         }
204         try
205         {
206             ResultSet resultset = dbaccess.executeQuery("select * from users where userid = '" + s + "' and botid = '" + s2 + "'");
207             int i = 0;
208             while(resultset.next()) 
209             {
210                 if(++i == 1)
211                     s3 = resultset.getString("password");
212                 if(i == 0)
213                 {
214                     resultset.close();
215                     dbManager.returnDbaRef(dbaccess);
216                     return false;
217                 }
218                 if(i > 1)
219                     throw new UserError("Duplicate user name: \"" + s + "\"");
220             }
221             resultset.close();
222             dbManager.returnDbaRef(dbaccess);
223         }
224         catch(SQLException sqlexception)
225         {
226             throw new UserError("Database error.", sqlexception);
227         }
228         if(s3 == null)
229             return false;
230         return s1.equals(s3);
231     }
232 
233     public boolean changePassword(String s, String s1, String s2, String s3)
234     {
235         if(!s2.equals(Multiplexor.SECRET_KEY))
236         {
237             Log.userinfo("ACCESS VIOLATION: Tried to create a user with invalid secret key.", Log.ERROR);
238             return false;
239         }
240         s = s.trim().toLowerCase();
241         s1 = s1.trim().toLowerCase();
242         DbAccess dbaccess = null;
243         try
244         {
245             dbaccess = dbManager.takeDbaRef();
246         }
247         catch(Exception exception)
248         {
249             throw new UserError("Could not get database reference when changing password to \"" + s1 + "\" for \"" + s + "\" as known to \"" + s3 + "\".", exception);
250         }
251         try
252         {
253             ResultSet resultset = dbaccess.executeQuery("select * from users where userid = '" + s + "' and botid = '" + s3 + "'");
254             int i;
255             for(i = 0; resultset.next(); i++);
256             if(i == 0)
257             {
258                 resultset.close();
259                 dbManager.returnDbaRef(dbaccess);
260                 return false;
261             }
262             dbaccess.executeUpdate("update users set password = '" + s1 + "' where userid = '" + s + "' and botid = '" + s3 + "'");
263             resultset.close();
264             dbManager.returnDbaRef(dbaccess);
265         }
266         catch(SQLException sqlexception)
267         {
268             throw new UserError("Database error.", sqlexception);
269         }
270         userCacheForBots.remove(s);
271         userCacheForBots.put(s, s1);
272         return true;
273     }
274 
275     public int useridCount(String s)
276     {
277         return ((HashMap)userCacheForBots.get(s)).size();
278     }
279 
280     private static DbAccessRefsPoolMgr dbManager;
281     private static HashMap userCacheForBots = new HashMap();
282     private static final String ENC_UTF8 = "UTF-8";
283 
284 }