001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.core;
025:
026: import java.io.*;
027: import java.net.*;
028:
029: import org.myoodb.*;
030: import org.myoodb.util.*;
031: import org.myoodb.exception.*;
032: import org.myoodb.core.command.*;
033:
034: public final class WorkManager extends AbstractServer {
035: protected static final org.myoodb.util.Logger LOGGER = org.myoodb.util.Logger
036: .getLogger(WorkManager.class);
037:
038: public WorkManager(int port, boolean secure) throws IOException {
039: super (port, secure);
040: }
041:
042: public void startup() throws Exception {
043: }
044:
045: public void shutdown() throws Exception {
046: close();
047: }
048:
049: public Object processWork(AbstractClient client, Object work) {
050: Object result = null;
051: MyOodbManager manager = MyOodbManager.getTheManager();
052:
053: try {
054: if (work instanceof LoginCommand) {
055: LoginCommand command = (LoginCommand) work;
056:
057: String username = command.getUsername();
058: String password = command.getPassword();
059:
060: if (command.getCrypto() == true) {
061: String passPhrase = Crypto.getPassPhrase();
062: if (passPhrase == null) {
063: result = new PermissionException(
064: "Invalid Crypto request");
065: client.send(result);
066: }
067:
068: try {
069: Crypto crypto = new Crypto(passPhrase);
070: username = crypto.decrypt(username);
071: password = crypto.decrypt(password);
072: } catch (Exception e) {
073: result = new PermissionException(
074: "Invalid Crypto request: " + e);
075: client.send(result);
076: }
077: }
078:
079: DatabaseClient dbClient = (DatabaseClient) client;
080: dbClient.setUser(new User(username, password));
081:
082: ((TransactionThread) Thread.currentThread())
083: .setClient(dbClient);
084:
085: if (manager.s_loginCallback != null) {
086: try {
087: manager.s_loginCallback.invoke(null,
088: new Object[] { dbClient });
089: } catch (java.lang.reflect.InvocationTargetException e) {
090: if (e.getTargetException() instanceof RuntimeException) {
091: if (LOGGER.isDebugEnabled() == true) {
092: LOGGER.debug(null, e
093: .getTargetException());
094: }
095:
096: RuntimeException ee = (RuntimeException) e
097: .getTargetException();
098: client.send(ee);
099: throw ee; // XXX: user failed login
100: } else {
101: LOGGER.error(null, e);
102:
103: InternalException ee = new InternalException(
104: e.getMessage(), e);
105: client.send(ee);
106: throw ee; // XXX: internal error, fail login
107: }
108: } catch (Exception e) {
109: LOGGER.error(null, e);
110:
111: InternalException ee = new InternalException(e
112: .getMessage(), e);
113: client.send(ee);
114: throw ee; // XXX: internal error, fail login
115: }
116: }
117:
118: User user = manager.getUserManager().getUser(username);
119:
120: if (UserManager.getAuthenticationPassThroughFlag() == true) {
121: if (user == null) {
122: user = manager.getUserManager().newUser(
123: username, password);
124: } else {
125: manager.getUserManager().updateUserPassword(
126: username, password);
127: }
128: }
129:
130: if (user == null) {
131: result = new PermissionException(
132: "Invalid request (no such user): "
133: + username);
134: client.send(result);
135: } else if (user.getPassword().equals(password) == false) {
136: result = new PermissionException(
137: "Invalid request (wrong password): "
138: + username);
139: client.send(result);
140: } else {
141: client.send(null);
142:
143: if (LOGGER.isDebugEnabled() == true) {
144: LOGGER.debug("User Login: "
145: + dbClient.getUser() + "-"
146: + dbClient.getSocket());
147: }
148: }
149: } else if (work instanceof LogoutCommand) {
150: LogoutCommand command = (LogoutCommand) work;
151: DatabaseClient dbClient = (DatabaseClient) client;
152:
153: manager.getTransactionManager().processCommand(command,
154: dbClient.getUser(), client);
155:
156: if (LOGGER.isDebugEnabled() == true) {
157: LOGGER.debug("User Logout: " + dbClient.getUser()
158: + "-" + dbClient.getSocket());
159: }
160:
161: try {
162: if (manager.s_logoutCallback != null) {
163: manager.s_logoutCallback.invoke(null,
164: new Object[] { dbClient });
165: }
166: } catch (Exception e) {
167: LOGGER.error(null, e);
168: }
169:
170: super .removeClient(client);
171: } else if (work instanceof AbstractCommand) {
172: AbstractCommand command = (AbstractCommand) work;
173: DatabaseClient dbClient = (DatabaseClient) client;
174:
175: if (manager.s_remoteCommandRequestCallback != null) {
176: try {
177: manager.s_remoteCommandRequestCallback.invoke(
178: null,
179: new Object[] { command, dbClient });
180: } catch (java.lang.reflect.InvocationTargetException e) {
181: if (e.getTargetException() instanceof RuntimeException) {
182: if (LOGGER.isDebugEnabled() == true) {
183: LOGGER.debug(null, e
184: .getTargetException());
185: }
186:
187: RuntimeException ee = (RuntimeException) e
188: .getTargetException();
189: client.send(ee);
190:
191: throw ee; // XXX: user failed command
192: } else {
193: LOGGER.error(null, e);
194:
195: InternalException ee = new InternalException(
196: e.getMessage(), e);
197: client.send(ee);
198:
199: throw ee; // XXX: internal error, fail command
200: }
201: } catch (Exception e) {
202: LOGGER.error(null, e);
203:
204: InternalException ee = new InternalException(e
205: .getMessage(), e);
206: client.send(ee);
207:
208: throw ee; // XXX: internal error, fail command
209: }
210: }
211:
212: long tunnelIdentifier = command.getTunnelIdentifier();
213: if (tunnelIdentifier != -1) {
214: synchronized (TunnelManager.INVOKE_COMMANDS) {
215: command = (AbstractCommand) TunnelManager.INVOKE_COMMANDS
216: .get(tunnelIdentifier);
217: }
218: }
219:
220: manager.getTransactionManager().processCommand(command,
221: dbClient.getUser(), client);
222: result = command.getResult();
223:
224: if (tunnelIdentifier != -1) {
225: synchronized (TunnelManager.INVOKE_COMMANDS) {
226: TunnelManager.INVOKE_COMMANDS.put(
227: tunnelIdentifier, result);
228: TunnelManager.INVOKE_COMMANDS.notify();
229: }
230:
231: result = null;
232: }
233:
234: client.send(result);
235: } else {
236: throw new InternalException("Invalid work: " + work);
237: }
238: } catch (Exception e) {
239: handleException(client, e);
240: }
241:
242: return result;
243: }
244:
245: public void handleException(AbstractClient client, Exception e1) {
246: if (LOGGER.isDebugEnabled() == true) {
247: LOGGER.debug(null, e1);
248: }
249:
250: try {
251: MyOodbManager manager = MyOodbManager.getTheManager();
252: if (manager != null) {
253: TransactionManager txManager = manager
254: .getTransactionManager();
255: if (txManager != null) {
256: DatabaseClient dbClient = (DatabaseClient) client;
257: txManager.processCommand(new LogoutCommand(),
258: dbClient.getUser(), client);
259:
260: if (LOGGER.isDebugEnabled() == true) {
261: LOGGER.debug("User Logout (" + e1 + ") : "
262: + dbClient.getUser() + "-"
263: + dbClient.getSocket());
264: }
265:
266: try {
267: if (manager.s_logoutCallback != null) {
268: manager.s_logoutCallback.invoke(null,
269: new Object[] { dbClient });
270: }
271: } catch (Exception e2) {
272: LOGGER.warn(null, e2);
273: }
274: }
275: }
276: } catch (Exception e2) {
277: LOGGER.warn(null, e2);
278: }
279:
280: super .removeClient(client);
281: }
282:
283: public AbstractClient newClient(Socket socket) {
284: try {
285: return new DatabaseClient(MyOodbManager.getTheManager()
286: .getDatabase(), socket, this );
287: } catch (Exception e) {
288: return null;
289: }
290: }
291:
292: public Thread newThread(Runnable run) {
293: Thread thread = null;
294: if (run == m_acceptor) {
295: thread = new Thread(getThreadGroup(), run);
296: thread.setPriority(MyOodbManager.ACCEPT_THREAD_PRIORITY);
297: } else {
298: thread = new TransactionThread(getThreadGroup(), run);
299: thread
300: .setPriority(MyOodbManager.TRANSACTION_THREAD_PRIORITY);
301: }
302:
303: thread.setDaemon(true);
304: thread.setName("MyOodb Work Thread: " + thread.getId());
305: return thread;
306: }
307: }
|