PageRenderTime 23ms CodeModel.GetById 2ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/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
17package com.hazelcast.impl;
18
19import com.hazelcast.impl.ClientHandlerService.ClientOperationHandler;
20import com.hazelcast.logging.ILogger;
21import com.hazelcast.nio.Connection;
22import com.hazelcast.nio.Packet;
23
24import javax.security.auth.Subject;
25import java.security.PrivilegedExceptionAction;
26import java.util.logging.Level;
27
28public class ClientRequestHandler extends FallThroughRunnable {
29    private final Packet packet;
30    private final CallContext callContext;
31    private final Node node;
32    private final ClientHandlerService.ClientOperationHandler clientOperationHandler;
33    private volatile Thread runningThread = null;
34    private volatile boolean valid = true;
35    private final ILogger logger;
36    private final Subject subject;
37
38    public ClientRequestHandler(Node node, Packet packet, CallContext callContext,
39                                ClientOperationHandler clientOperationHandler, Subject subject) {
40        this.packet = packet;
41        this.callContext = callContext;
42        this.node = node;
43        this.clientOperationHandler = clientOperationHandler;
44        this.logger = node.getLogger(this.getClass().getName());
45        this.subject = subject;
46    }
47
48    @Override
49    public void doRun() {
50        runningThread = Thread.currentThread();
51        ThreadContext.get().setCallContext(callContext);
52        try {
53            if (!valid) return;
54            final PrivilegedExceptionAction<Void> action = new PrivilegedExceptionAction<Void>() {
55                public Void run() {
56                    Connection connection = packet.conn;
57                    clientOperationHandler.handle(node, packet);
58                    node.clientHandlerService.getClientEndpoint(connection).removeRequest(ClientRequestHandler.this);
59                    return null;
60                }
61            };
62            if (node.securityContext == null) {
63                action.run();
64            } else {
65                node.securityContext.doAsPrivileged(subject, action);
66            }
67        } catch (Throwable e) {
68            logger.log(Level.WARNING, e.getMessage(), e);
69            if (node.isActive()) {
70                throw (RuntimeException) e;
71            }
72        } finally {
73            runningThread = null;
74        }
75    }
76
77    public void cancel() {
78        valid = false;
79        if (runningThread != null) {
80            runningThread.interrupt();
81        }
82    }
83}