001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.broker;
018
019import java.io.IOException;
020import java.util.concurrent.ConcurrentMap;
021import java.util.concurrent.atomic.AtomicBoolean;
022
023import org.apache.activemq.broker.region.MessageReference;
024import org.apache.activemq.command.ConnectionId;
025import org.apache.activemq.command.ConnectionInfo;
026import org.apache.activemq.command.TransactionId;
027import org.apache.activemq.command.WireFormatInfo;
028import org.apache.activemq.command.XATransactionId;
029import org.apache.activemq.filter.MessageEvaluationContext;
030import org.apache.activemq.filter.NonCachedMessageEvaluationContext;
031import org.apache.activemq.security.MessageAuthorizationPolicy;
032import org.apache.activemq.security.SecurityContext;
033import org.apache.activemq.state.ConnectionState;
034import org.apache.activemq.transaction.Transaction;
035
036/**
037 * Used to hold context information needed to process requests sent to a broker.
038 *
039 *
040 */
041public class ConnectionContext {
042
043    private Connection connection;
044    private Connector connector;
045    private Broker broker;
046    private boolean inRecoveryMode;
047    private Transaction transaction;
048    private ConcurrentMap<TransactionId, Transaction> transactions;
049    private SecurityContext securityContext;
050    private ConnectionId connectionId;
051    private String clientId;
052    private String userName;
053    private boolean reconnect;
054    private WireFormatInfo wireFormatInfo;
055    private Object longTermStoreContext;
056    private boolean producerFlowControl = true;
057    private MessageAuthorizationPolicy messageAuthorizationPolicy;
058    private boolean networkConnection;
059    private boolean faultTolerant;
060    private final AtomicBoolean stopping = new AtomicBoolean();
061    private final MessageEvaluationContext messageEvaluationContext;
062    private boolean dontSendReponse;
063    private boolean clientMaster = true;
064    private ConnectionState connectionState;
065    private XATransactionId xid;
066
067    public ConnectionContext() {
068        this.messageEvaluationContext = new NonCachedMessageEvaluationContext();
069    }
070
071    public ConnectionContext(MessageEvaluationContext messageEvaluationContext) {
072        this.messageEvaluationContext=messageEvaluationContext;
073    }
074
075    public ConnectionContext(ConnectionInfo info) {
076        this();
077        setClientId(info.getClientId());
078        setUserName(info.getUserName());
079        setConnectionId(info.getConnectionId());
080    }
081
082    public ConnectionContext copy() {
083        ConnectionContext rc = new ConnectionContext(this.messageEvaluationContext);
084        rc.connection = this.connection;
085        rc.connector = this.connector;
086        rc.broker = this.broker;
087        rc.inRecoveryMode = this.inRecoveryMode;
088        rc.transaction = this.transaction;
089        rc.transactions = this.transactions;
090        rc.securityContext = this.securityContext;
091        rc.connectionId = this.connectionId;
092        rc.clientId = this.clientId;
093        rc.userName = this.userName;
094        rc.reconnect = this.reconnect;
095        rc.wireFormatInfo = this.wireFormatInfo;
096        rc.longTermStoreContext = this.longTermStoreContext;
097        rc.producerFlowControl = this.producerFlowControl;
098        rc.messageAuthorizationPolicy = this.messageAuthorizationPolicy;
099        rc.networkConnection = this.networkConnection;
100        rc.faultTolerant = this.faultTolerant;
101        rc.stopping.set(this.stopping.get());
102        rc.dontSendReponse = this.dontSendReponse;
103        rc.clientMaster = this.clientMaster;
104        return rc;
105    }
106
107
108    public SecurityContext getSecurityContext() {
109        return securityContext;
110    }
111
112    public void setSecurityContext(SecurityContext subject) {
113        this.securityContext = subject;
114        if (subject != null) {
115            setUserName(subject.getUserName());
116        } else {
117            setUserName(null);
118        }
119    }
120
121    /**
122     * @return the broker being used.
123     */
124    public Broker getBroker() {
125        return broker;
126    }
127
128    /**
129     * @param broker being used
130     */
131    public void setBroker(Broker broker) {
132        this.broker = broker;
133    }
134
135    /**
136     * @return the connection being used
137     */
138    public Connection getConnection() {
139        return connection;
140    }
141
142    /**
143     * @param connection being used
144     */
145    public void setConnection(Connection connection) {
146        this.connection = connection;
147    }
148
149    /**
150     * @return the transaction being used.
151     */
152    public Transaction getTransaction() {
153        return transaction;
154    }
155
156    /**
157     * @param transaction being used.
158     */
159    public void setTransaction(Transaction transaction) {
160        this.transaction = transaction;
161    }
162
163    /**
164     * @return the connector being used.
165     */
166    public Connector getConnector() {
167        return connector;
168    }
169
170    /**
171     * @param connector being used.
172     */
173    public void setConnector(Connector connector) {
174        this.connector = connector;
175    }
176
177    public MessageAuthorizationPolicy getMessageAuthorizationPolicy() {
178        return messageAuthorizationPolicy;
179    }
180
181    /**
182     * Sets the policy used to decide if the current connection is authorized to
183     * consume a given message
184     */
185    public void setMessageAuthorizationPolicy(MessageAuthorizationPolicy messageAuthorizationPolicy) {
186        this.messageAuthorizationPolicy = messageAuthorizationPolicy;
187    }
188
189    /**
190     * @return
191     */
192    public boolean isInRecoveryMode() {
193        return inRecoveryMode;
194    }
195
196    public void setInRecoveryMode(boolean inRecoveryMode) {
197        this.inRecoveryMode = inRecoveryMode;
198    }
199
200    public ConcurrentMap<TransactionId, Transaction> getTransactions() {
201        return transactions;
202    }
203
204    public void setTransactions(ConcurrentMap<TransactionId, Transaction> transactions) {
205        this.transactions = transactions;
206    }
207
208    public boolean isInTransaction() {
209        return transaction != null;
210    }
211
212    public String getClientId() {
213        return clientId;
214    }
215
216    public void setClientId(String clientId) {
217        this.clientId = clientId;
218    }
219
220    public boolean isReconnect() {
221        return reconnect;
222    }
223
224    public void setReconnect(boolean reconnect) {
225        this.reconnect = reconnect;
226    }
227
228    public WireFormatInfo getWireFormatInfo() {
229        return wireFormatInfo;
230    }
231
232    public void setWireFormatInfo(WireFormatInfo wireFormatInfo) {
233        this.wireFormatInfo = wireFormatInfo;
234    }
235
236    public ConnectionId getConnectionId() {
237        return connectionId;
238    }
239
240    public void setConnectionId(ConnectionId connectionId) {
241        this.connectionId = connectionId;
242    }
243
244    public String getUserName() {
245        return userName;
246    }
247
248    protected void setUserName(String userName) {
249        this.userName = userName;
250    }
251
252    public MessageEvaluationContext getMessageEvaluationContext() {
253        return messageEvaluationContext;
254    }
255
256    public Object getLongTermStoreContext() {
257        return longTermStoreContext;
258    }
259
260    public void setLongTermStoreContext(Object longTermStoreContext) {
261        this.longTermStoreContext = longTermStoreContext;
262    }
263
264    public boolean isProducerFlowControl() {
265        return producerFlowControl;
266    }
267
268    public void setProducerFlowControl(boolean disableProducerFlowControl) {
269        this.producerFlowControl = disableProducerFlowControl;
270    }
271
272    public boolean isAllowedToConsume(MessageReference n) throws IOException {
273        if (messageAuthorizationPolicy != null) {
274            return messageAuthorizationPolicy.isAllowedToConsume(this, n.getMessage());
275        }
276        return true;
277    }
278
279    public synchronized boolean isNetworkConnection() {
280        return networkConnection;
281    }
282
283    public synchronized void setNetworkConnection(boolean networkConnection) {
284        this.networkConnection = networkConnection;
285    }
286
287    public AtomicBoolean getStopping() {
288        return stopping;
289    }
290
291    public void setDontSendReponse(boolean b) {
292        this.dontSendReponse = b;
293    }
294
295    public boolean isDontSendReponse() {
296        return dontSendReponse;
297    }
298
299    /**
300     * @return the clientMaster
301     */
302    public boolean isClientMaster() {
303        return this.clientMaster;
304    }
305
306    /**
307     * @param clientMaster the clientMaster to set
308     */
309    public void setClientMaster(boolean clientMaster) {
310        this.clientMaster = clientMaster;
311    }
312
313    public boolean isFaultTolerant() {
314        return faultTolerant;
315    }
316
317    public void setFaultTolerant(boolean faultTolerant) {
318        this.faultTolerant = faultTolerant;
319    }
320
321    public void setConnectionState(ConnectionState connectionState) {
322        this.connectionState = connectionState;
323    }
324
325    public ConnectionState getConnectionState() {
326        return this.connectionState;
327    }
328
329    public void setXid(XATransactionId id) {
330        this.xid = id;
331    }
332
333    public XATransactionId getXid() {
334        return xid;
335    }
336
337    public boolean isAllowLinkStealing(){
338       return connector != null && connector.isAllowLinkStealing();
339    }
340}