1 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 44 { 45 } 46 47 public void shutdown() throws Exception 48 { 49 close(); 50 } 51 52 public Object processWork(AbstractClient client, Object work) 53 { 54 Object 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 username = command.getUsername(); 64 String password = command.getPassword(); 65 66 if (command.getCrypto() == true) 67 { 68 String 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 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 [] {dbClient}); 97 } 98 catch (java.lang.reflect.InvocationTargetException e) 99 { 100 if (e.getTargetException() instanceof RuntimeException ) 101 { 102 if (LOGGER.isDebugEnabled() == true) 103 { 104 LOGGER.debug(null, e.getTargetException()); 105 } 106 107 RuntimeException ee = (RuntimeException ) e.getTargetException(); 108 client.send(ee); 109 throw ee; } 111 else 112 { 113 LOGGER.error(null, e); 114 115 InternalException ee = new InternalException(e.getMessage(), e); 116 client.send(ee); 117 throw ee; } 119 } 120 catch (Exception e) 121 { 122 LOGGER.error(null, e); 123 124 InternalException ee = new InternalException(e.getMessage(), e); 125 client.send(ee); 126 throw ee; } 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 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 [] {dbClient}); 187 } 188 } 189 catch (Exception e) 190 { 191 LOGGER.error(null, e); 192 } 193 194 super.removeClient(client); 195 } 196 else if (work instanceof AbstractCommand) 197 { 198 Object 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 [] {command, dbClient.getUser()}); 207 } 208 catch (java.lang.reflect.InvocationTargetException e) 209 { 210 if (e.getTargetException() instanceof RuntimeException ) 211 { 212 if (LOGGER.isDebugEnabled() == true) 213 { 214 LOGGER.debug(null, e.getTargetException()); 215 } 216 217 RuntimeException ee = (RuntimeException ) e.getTargetException(); 218 client.send(ee); 219 throw ee; } 221 else 222 { 223 LOGGER.error(null, e); 224 225 InternalException ee = new InternalException(e.getMessage(), e); 226 client.send(ee); 227 throw ee; } 229 } 230 catch (Exception e) 231 { 232 LOGGER.error(null, e); 233 234 InternalException ee = new InternalException(e.getMessage(), e); 235 client.send(ee); 236 throw ee; } 238 } 239 240 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 [] args = (Object []) 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 e) 270 { 271 handleException(client, e); 272 } 273 274 return result; 275 } 276 277 public void handleException(AbstractClient client, Exception 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 [] {dbClient}); 305 } 306 } 307 catch (Exception e2) 308 { 309 LOGGER.warn(null, e2); 310 } 311 } 312 } 313 } 314 catch (Exception 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 e) 329 { 330 return null; 331 } 332 } 333 334 public Thread newThread(Runnable run) 335 { 336 Thread thread = null; 337 if (run == m_acceptor) 338 { 339 thread = new Thread (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 |