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.security.MessageAuthorizationPolicy;
031import org.apache.activemq.security.SecurityContext;
032import org.apache.activemq.state.ConnectionState;
033import org.apache.activemq.transaction.Transaction;
034
035/**
036 * Used to hold context information needed to process requests sent to a broker.
037 *
038 *
039 */
040public class ConnectionContext {
041
042    private Connection connection;
043    private Connector connector;
044    private Broker broker;
045    private boolean inRecoveryMode;
046    private Transaction transaction;
047    private ConcurrentMap<TransactionId, Transaction> transactions;
048    private SecurityContext securityContext;
049    private ConnectionId connectionId;
050    private String clientId;
051    private String userName;
052    private boolean reconnect;
053    private WireFormatInfo wireFormatInfo;
054    private Object longTermStoreContext;
055    private boolean producerFlowControl = true;
056    private MessageAuthorizationPolicy messageAuthorizationPolicy;
057    private boolean networkConnection;
058    private boolean faultTolerant;
059    private final AtomicBoolean stopping = new AtomicBoolean();
060    private final MessageEvaluationContext messageEvaluationContext;
061    private boolean dontSendReponse;
062    private boolean clientMaster = true;
063    private ConnectionState connectionState;
064    private XATransactionId xid;
065
066    public ConnectionContext() {
067        this.messageEvaluationContext = new MessageEvaluationContext();
068    }
069
070    public ConnectionContext(MessageEvaluationContext messageEvaluationContext) {
071        this.messageEvaluationContext=messageEvaluationContext;
072    }
073
074    public ConnectionContext(ConnectionInfo info) {
075        this();
076        setClientId(info.getClientId());
077        setUserName(info.getUserName());
078        setConnectionId(info.getConnectionId());
079    }
080
081    public ConnectionContext copy() {
082        ConnectionContext rc = new ConnectionContext(this.messageEvaluationContext);
083        rc.connection = this.connection;
084        rc.connector = this.connector;
085        rc.broker = this.broker;
086        rc.inRecoveryMode = this.inRecoveryMode;
087        rc.transaction = this.transaction;
088        rc.transactions = this.transactions;
089        rc.securityContext = this.securityContext;
090        rc.connectionId = this.connectionId;
091        rc.clientId = this.clientId;
092        rc.userName = this.userName;
093        rc.reconnect = this.reconnect;
094        rc.wireFormatInfo = this.wireFormatInfo;
095        rc.longTermStoreContext = this.longTermStoreContext;
096        rc.producerFlowControl = this.producerFlowControl;
097        rc.messageAuthorizationPolicy = this.messageAuthorizationPolicy;
098        rc.networkConnection = this.networkConnection;
099        rc.faultTolerant = this.faultTolerant;
100        rc.stopping.set(this.stopping.get());
101        rc.dontSendReponse = this.dontSendReponse;
102        rc.clientMaster = this.clientMaster;
103        return rc;
104    }
105
106
107    public SecurityContext getSecurityContext() {
108        return securityContext;
109    }
110
111    public void setSecurityContext(SecurityContext subject) {
112        this.securityContext = subject;
113        if (subject != null) {
114            setUserName(subject.getUserName());
115        } else {
116            setUserName(null);
117        }
118    }
119
120    /**
121     * @return the broker being used.
122     */
123    public Broker getBroker() {
124        return broker;
125    }
126
127    /**
128     * @param broker being used
129     */
130    public void setBroker(Broker broker) {
131        this.broker = broker;
132    }
133
134    /**
135     * @return the connection being used
136     */
137    public Connection getConnection() {
138        return connection;
139    }
140
141    /**
142     * @param connection being used
143     */
144    public void setConnection(Connection connection) {
145        this.connection = connection;
146    }
147
148    /**
149     * @return the transaction being used.
150     */
151    public Transaction getTransaction() {
152        return transaction;
153    }
154
155    /**
156     * @param transaction being used.
157     */
158    public void setTransaction(Transaction transaction) {
159        this.transaction = transaction;
160    }
161
162    /**
163     * @return the connector being used.
164     */
165    public Connector getConnector() {
166        return connector;
167    }
168
169    /**
170     * @param connector being used.
171     */
172    public void setConnector(Connector connector) {
173        this.connector = connector;
174    }
175
176    public MessageAuthorizationPolicy getMessageAuthorizationPolicy() {
177        return messageAuthorizationPolicy;
178    }
179
180    /**
181     * Sets the policy used to decide if the current connection is authorized to
182     * consume a given message
183     */
184    public void setMessageAuthorizationPolicy(MessageAuthorizationPolicy messageAuthorizationPolicy) {
185        this.messageAuthorizationPolicy = messageAuthorizationPolicy;
186    }
187
188    /**
189     * @return true if in recovery mode.
190     */
191    public boolean isInRecoveryMode() {
192        return inRecoveryMode;
193    }
194
195    public void setInRecoveryMode(boolean inRecoveryMode) {
196        this.inRecoveryMode = inRecoveryMode;
197    }
198
199    public ConcurrentMap<TransactionId, Transaction> getTransactions() {
200        return transactions;
201    }
202
203    public void setTransactions(ConcurrentMap<TransactionId, Transaction> transactions) {
204        this.transactions = transactions;
205    }
206
207    public boolean isInTransaction() {
208        return transaction != null;
209    }
210
211    public String getClientId() {
212        return clientId;
213    }
214
215    public void setClientId(String clientId) {
216        this.clientId = clientId;
217    }
218
219    public boolean isReconnect() {
220        return reconnect;
221    }
222
223    public void setReconnect(boolean reconnect) {
224        this.reconnect = reconnect;
225    }
226
227    public WireFormatInfo getWireFormatInfo() {
228        return wireFormatInfo;
229    }
230
231    public void setWireFormatInfo(WireFormatInfo wireFormatInfo) {
232        this.wireFormatInfo = wireFormatInfo;
233    }
234
235    public ConnectionId getConnectionId() {
236        return connectionId;
237    }
238
239    public void setConnectionId(ConnectionId connectionId) {
240        this.connectionId = connectionId;
241    }
242
243    public String getUserName() {
244        return userName;
245    }
246
247    protected void setUserName(String userName) {
248        this.userName = userName;
249    }
250
251    public MessageEvaluationContext getMessageEvaluationContext() {
252        return messageEvaluationContext;
253    }
254
255    public Object getLongTermStoreContext() {
256        return longTermStoreContext;
257    }
258
259    public void setLongTermStoreContext(Object longTermStoreContext) {
260        this.longTermStoreContext = longTermStoreContext;
261    }
262
263    public boolean isProducerFlowControl() {
264        return producerFlowControl;
265    }
266
267    public void setProducerFlowControl(boolean disableProducerFlowControl) {
268        this.producerFlowControl = disableProducerFlowControl;
269    }
270
271    public boolean isAllowedToConsume(MessageReference n) throws IOException {
272        if (messageAuthorizationPolicy != null) {
273            return messageAuthorizationPolicy.isAllowedToConsume(this, n.getMessage());
274        }
275        return true;
276    }
277
278    public synchronized boolean isNetworkConnection() {
279        return networkConnection;
280    }
281
282    public synchronized void setNetworkConnection(boolean networkConnection) {
283        this.networkConnection = networkConnection;
284    }
285
286    public AtomicBoolean getStopping() {
287        return stopping;
288    }
289
290    public void setDontSendReponse(boolean b) {
291        this.dontSendReponse = b;
292    }
293
294    public boolean isDontSendReponse() {
295        return dontSendReponse;
296    }
297
298    /**
299     * @return the clientMaster
300     */
301    public boolean isClientMaster() {
302        return this.clientMaster;
303    }
304
305    /**
306     * @param clientMaster the clientMaster to set
307     */
308    public void setClientMaster(boolean clientMaster) {
309        this.clientMaster = clientMaster;
310    }
311
312    public boolean isFaultTolerant() {
313        return faultTolerant;
314    }
315
316    public void setFaultTolerant(boolean faultTolerant) {
317        this.faultTolerant = faultTolerant;
318    }
319
320    public void setConnectionState(ConnectionState connectionState) {
321        this.connectionState = connectionState;
322    }
323
324    public ConnectionState getConnectionState() {
325        return this.connectionState;
326    }
327
328    public void setXid(XATransactionId id) {
329        this.xid = id;
330    }
331
332    public XATransactionId getXid() {
333        return xid;
334    }
335
336    public boolean isAllowLinkStealing(){
337       return connector != null && connector.isAllowLinkStealing();
338    }
339}