/hazelcast/src/main/java/com/hazelcast/impl/ClientRequestHandler.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 83 lines · 61 code · 7 blank · 15 comment · 7 complexity · c096dd25de930ba7ea491d84100eaeb2 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.impl;
  17. import com.hazelcast.impl.ClientHandlerService.ClientOperationHandler;
  18. import com.hazelcast.logging.ILogger;
  19. import com.hazelcast.nio.Connection;
  20. import com.hazelcast.nio.Packet;
  21. import javax.security.auth.Subject;
  22. import java.security.PrivilegedExceptionAction;
  23. import java.util.logging.Level;
  24. public class ClientRequestHandler extends FallThroughRunnable {
  25. private final Packet packet;
  26. private final CallContext callContext;
  27. private final Node node;
  28. private final ClientHandlerService.ClientOperationHandler clientOperationHandler;
  29. private volatile Thread runningThread = null;
  30. private volatile boolean valid = true;
  31. private final ILogger logger;
  32. private final Subject subject;
  33. public ClientRequestHandler(Node node, Packet packet, CallContext callContext,
  34. ClientOperationHandler clientOperationHandler, Subject subject) {
  35. this.packet = packet;
  36. this.callContext = callContext;
  37. this.node = node;
  38. this.clientOperationHandler = clientOperationHandler;
  39. this.logger = node.getLogger(this.getClass().getName());
  40. this.subject = subject;
  41. }
  42. @Override
  43. public void doRun() {
  44. runningThread = Thread.currentThread();
  45. ThreadContext.get().setCallContext(callContext);
  46. try {
  47. if (!valid) return;
  48. final PrivilegedExceptionAction<Void> action = new PrivilegedExceptionAction<Void>() {
  49. public Void run() {
  50. Connection connection = packet.conn;
  51. clientOperationHandler.handle(node, packet);
  52. node.clientHandlerService.getClientEndpoint(connection).removeRequest(ClientRequestHandler.this);
  53. return null;
  54. }
  55. };
  56. if (node.securityContext == null) {
  57. action.run();
  58. } else {
  59. node.securityContext.doAsPrivileged(subject, action);
  60. }
  61. } catch (Throwable e) {
  62. logger.log(Level.WARNING, e.getMessage(), e);
  63. if (node.isActive()) {
  64. throw (RuntimeException) e;
  65. }
  66. } finally {
  67. runningThread = null;
  68. }
  69. }
  70. public void cancel() {
  71. valid = false;
  72. if (runningThread != null) {
  73. runningThread.interrupt();
  74. }
  75. }
  76. }