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