KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > core > WorkManager


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.core;
25
26 import java.io.*;
27 import java.net.*;
28
29 import org.myoodb.*;
30 import org.myoodb.util.*;
31 import org.myoodb.exception.*;
32 import org.myoodb.core.command.*;
33
34 public final class WorkManager extends AbstractServer
35 {
36     protected static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(WorkManager.class);
37
38     public WorkManager(int port, boolean secure) throws IOException
39     {
40         super(port, secure);
41     }
42
43     public void startup() throws Exception JavaDoc
44     {
45     }
46
47     public void shutdown() throws Exception JavaDoc
48     {
49         close();
50     }
51
52     public Object JavaDoc processWork(AbstractClient client, Object JavaDoc work)
53     {
54         Object JavaDoc result = null;
55         MyOodbManager manager = MyOodbManager.getTheManager();
56
57         try
58         {
59             if (work instanceof LoginCommand)
60             {
61                 LoginCommand command = (LoginCommand) work;
62
63                 String JavaDoc username = command.getUsername();
64                 String JavaDoc password = command.getPassword();
65
66                 if (command.getCrypto() == true)
67                 {
68                     String JavaDoc passPhrase = Crypto.getPassPhrase();
69                     if (passPhrase == null)
70                     {
71                         result = new PermissionException("Invalid Crypto request");
72                         client.send(result);
73                     }
74
75                     try
76                     {
77                         Crypto crypto = new Crypto(passPhrase);
78                         username = crypto.decrypt(username);
79                         password = crypto.decrypt(password);
80                     }
81                     catch (Exception JavaDoc e)
82                     {
83                         result = new PermissionException("Invalid Crypto request: " + e);
84                         client.send(result);
85                     }
86                 }
87
88                 DatabaseClient dbClient = (DatabaseClient) client;
89
90                 if (manager.m_loginCallback != null)
91                 {
92                     dbClient.setUser(new User(username, password));
93
94                     try
95                     {
96                         manager.m_loginCallback.invoke(null, new Object JavaDoc[] {dbClient});
97                     }
98                     catch (java.lang.reflect.InvocationTargetException JavaDoc e)
99                     {
100                         if (e.getTargetException() instanceof RuntimeException JavaDoc)
101                         {
102                             if (LOGGER.isDebugEnabled() == true)
103                             {
104                                 LOGGER.debug(null, e.getTargetException());
105                             }
106
107                             RuntimeException JavaDoc ee = (RuntimeException JavaDoc) e.getTargetException();
108                             client.send(ee);
109                             throw ee; // XXX: user failed login
110
}
111                         else
112                         {
113                             LOGGER.error(null, e);
114
115                             InternalException ee = new InternalException(e.getMessage(), e);
116                             client.send(ee);
117                             throw ee; // XXX: internal error, fail login
118
}
119                     }
120                     catch (Exception JavaDoc e)
121                     {
122                         LOGGER.error(null, e);
123
124                         InternalException ee = new InternalException(e.getMessage(), e);
125                         client.send(ee);
126                         throw ee; // XXX: internal error, fail login
127
}
128                 }
129
130                 User user = manager.getUserManager().getUser(username);
131
132                 if (UserManager.getAuthenticationPassThroughFlag() == true)
133                 {
134                     if (user == null)
135                     {
136                         user = manager.getUserManager().newUser(username, password);
137                     }
138                     else
139                     {
140                         manager.getUserManager().updateUserPassword(username, password);
141                     }
142                 }
143
144                 if (user == null)
145                 {
146                     result = new PermissionException("Invalid request (no such user): " + username);
147                     client.send(result);
148                 }
149                 else if (user.getPassword().equals(password) == false)
150                 {
151                     result = new PermissionException("Invalid request (wrong password): " + username);
152                     client.send(result);
153                 }
154                 else
155                 {
156                     dbClient.setUser(user);
157                     client.send(null);
158
159                     java.lang.Thread JavaDoc thread = java.lang.Thread.currentThread();
160                     if (thread instanceof org.myoodb.core.command.CommandThread)
161                     {
162                         ((CommandThread)Thread.currentThread()).setOwner(user);
163                     }
164
165                     if (LOGGER.isDebugEnabled() == true)
166                     {
167                         LOGGER.debug("User Login: " + dbClient.getUser() + "-" + dbClient.getSocket());
168                     }
169                 }
170             }
171             else if (work instanceof LogoutCommand)
172             {
173                 LogoutCommand command = (LogoutCommand) work;
174                 DatabaseClient dbClient = (DatabaseClient) client;
175                 manager.getTransactionManager().processCommand(command, dbClient.getUser(), client);
176
177                 if (LOGGER.isDebugEnabled() == true)
178                 {
179                     LOGGER.debug("User Logout: " + dbClient.getUser() + "-" + dbClient.getSocket());
180                 }
181
182                 try
183                 {
184                     if (manager.m_logoutCallback != null)
185                     {
186                         manager.m_logoutCallback.invoke(null, new Object JavaDoc[] {dbClient});
187                     }
188                 }
189                 catch (Exception JavaDoc e)
190                 {
191                     LOGGER.error(null, e);
192                 }
193
194                 super.removeClient(client);
195             }
196             else if (work instanceof AbstractCommand)
197             {
198                 Object JavaDoc tunnelIdentifier = null;
199                 AbstractCommand command = (AbstractCommand) work;
200                 DatabaseClient dbClient = (DatabaseClient) client;
201
202                 if (manager.m_remoteCommandRequestCallback != null)
203                 {
204                     try
205                     {
206                         manager.m_remoteCommandRequestCallback.invoke(null, new Object JavaDoc[] {command, dbClient.getUser()});
207                     }
208                     catch (java.lang.reflect.InvocationTargetException JavaDoc e)
209                     {
210                         if (e.getTargetException() instanceof RuntimeException JavaDoc)
211                         {
212                             if (LOGGER.isDebugEnabled() == true)
213                             {
214                                 LOGGER.debug(null, e.getTargetException());
215                             }
216
217                             RuntimeException JavaDoc ee = (RuntimeException JavaDoc) e.getTargetException();
218                             client.send(ee);
219                             throw ee; // XXX: user failed command
220
}
221                         else
222                         {
223                             LOGGER.error(null, e);
224
225                             InternalException ee = new InternalException(e.getMessage(), e);
226                             client.send(ee);
227                             throw ee; // XXX: internal error, fail command
228
}
229                     }
230                     catch (Exception JavaDoc e)
231                     {
232                         LOGGER.error(null, e);
233
234                         InternalException ee = new InternalException(e.getMessage(), e);
235                         client.send(ee);
236                         throw ee; // XXX: internal error, fail command
237
}
238                 }
239
240                 // XXX: tunnel performance
241
if (command instanceof org.myoodb.core.command.InvokeMethodCommand)
242                 {
243                     tunnelIdentifier = ((org.myoodb.core.command.InvokeMethodCommand) command).getTunnelIdentifier();
244                     if (((org.myoodb.core.command.InvokeMethodCommand) command).getTunnelPassThroughFlag() == true)
245                     {
246                         Object JavaDoc[] args = (Object JavaDoc[]) MyOodbTunnelServlet.INVOKE_COMMANDS.remove(tunnelIdentifier);
247                         ((org.myoodb.core.command.InvokeMethodCommand) command).setArguments(args);
248                     }
249                 }
250
251                 manager.getTransactionManager().processCommand(command, dbClient.getUser(), client);
252                 result = command.getResult();
253
254                 if (tunnelIdentifier != null)
255                 {
256                     MyOodbTunnelServlet.INVOKE_COMMANDS.put(tunnelIdentifier, result);
257                     client.send(null);
258                 }
259                 else
260                 {
261                     client.send(result);
262                 }
263             }
264             else
265             {
266                 throw new InternalException("Invalid work: " + work);
267             }
268         }
269         catch (Exception JavaDoc e)
270         {
271             handleException(client, e);
272         }
273
274         return result;
275     }
276
277     public void handleException(AbstractClient client, Exception JavaDoc e1)
278     {
279         if (LOGGER.isDebugEnabled() == true)
280         {
281             LOGGER.debug(null, e1);
282         }
283
284         try
285         {
286             MyOodbManager manager = MyOodbManager.getTheManager();
287             if (manager != null)
288             {
289                 TransactionManager txManager = manager.getTransactionManager();
290                 if (txManager != null)
291                 {
292                     DatabaseClient dbClient = (DatabaseClient) client;
293                     txManager.processCommand(new LogoutCommand(), dbClient.getUser(), client);
294
295                     if (LOGGER.isDebugEnabled() == true)
296                     {
297                         LOGGER.debug("User Logout (" + e1 + ") : " + dbClient.getUser() + "-" + dbClient.getSocket());
298                     }
299
300                     try
301                     {
302                         if (manager.m_logoutCallback != null)
303                         {
304                             manager.m_logoutCallback.invoke(null, new Object JavaDoc[] {dbClient});
305                         }
306                     }
307                     catch (Exception JavaDoc e2)
308                     {
309                         LOGGER.warn(null, e2);
310                     }
311                 }
312             }
313         }
314         catch (Exception JavaDoc e2)
315         {
316             LOGGER.warn(null, e2);
317         }
318
319         super.removeClient(client);
320     }
321
322     public AbstractClient newClient(Socket socket)
323     {
324         try
325         {
326             return new DatabaseClient(socket, this);
327         }
328         catch (Exception JavaDoc e)
329         {
330             return null;
331         }
332     }
333
334     public Thread JavaDoc newThread(Runnable JavaDoc run)
335     {
336         Thread JavaDoc thread = null;
337         if (run == m_acceptor)
338         {
339             thread = new Thread JavaDoc(getThreadGroup(), run);
340             thread.setPriority(MyOodbManager.ACCEPT_THREAD_PRIORITY);
341         }
342         else
343         {
344             thread = new CommandThread(getThreadGroup(), run);
345             thread.setPriority(MyOodbManager.TRANSACTION_THREAD_PRIORITY);
346         }
347
348         thread.setDaemon(true);
349         return thread;
350     }
351 }
352
Popular Tags