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     */
017    package org.apache.activemq.broker;
018    
019    import java.io.IOException;
020    import java.util.concurrent.ConcurrentHashMap;
021    import java.util.concurrent.atomic.AtomicBoolean;
022    
023    import org.apache.activemq.broker.region.MessageReference;
024    import org.apache.activemq.command.ConnectionId;
025    import org.apache.activemq.command.ConnectionInfo;
026    import org.apache.activemq.command.TransactionId;
027    import org.apache.activemq.command.WireFormatInfo;
028    import org.apache.activemq.filter.MessageEvaluationContext;
029    import org.apache.activemq.security.MessageAuthorizationPolicy;
030    import org.apache.activemq.security.SecurityContext;
031    import org.apache.activemq.transaction.Transaction;
032    
033    /**
034     * Used to hold context information needed to process requests sent to a broker.
035     * 
036     * @version $Revision: 1.5 $
037     */
038    public 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 haAware;
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 SecurityContext getSecurityContext() {
078            return securityContext;
079        }
080    
081        public void setSecurityContext(SecurityContext subject) {
082            this.securityContext = subject;
083            if (subject != null) {
084                setUserName(subject.getUserName());
085            } else {
086                setUserName(null);
087            }
088        }
089    
090        /**
091         * @return the broker being used.
092         */
093        public Broker getBroker() {
094            return broker;
095        }
096    
097        /**
098         * @param broker being used
099         */
100        public void setBroker(Broker broker) {
101            this.broker = broker;
102        }
103    
104        /**
105         * @return the connection being used
106         */
107        public Connection getConnection() {
108            return connection;
109        }
110    
111        /**
112         * @param connection being used
113         */
114        public void setConnection(Connection connection) {
115            this.connection = connection;
116        }
117    
118        /**
119         * @return the transaction being used.
120         */
121        public Transaction getTransaction() {
122            return transaction;
123        }
124    
125        /**
126         * @param transaction being used.
127         */
128        public void setTransaction(Transaction transaction) {
129            this.transaction = transaction;
130        }
131    
132        /**
133         * @return the connector being used.
134         */
135        public Connector getConnector() {
136            return connector;
137        }
138    
139        /**
140         * @param connector being used.
141         */
142        public void setConnector(Connector connector) {
143            this.connector = connector;
144        }
145    
146        public MessageAuthorizationPolicy getMessageAuthorizationPolicy() {
147            return messageAuthorizationPolicy;
148        }
149    
150        /**
151         * Sets the policy used to decide if the current connection is authorized to
152         * consume a given message
153         */
154        public void setMessageAuthorizationPolicy(MessageAuthorizationPolicy messageAuthorizationPolicy) {
155            this.messageAuthorizationPolicy = messageAuthorizationPolicy;
156        }
157    
158        /**
159         * @return
160         */
161        public boolean isInRecoveryMode() {
162            return inRecoveryMode;
163        }
164    
165        public void setInRecoveryMode(boolean inRecoveryMode) {
166            this.inRecoveryMode = inRecoveryMode;
167        }
168    
169        public ConcurrentHashMap<TransactionId, Transaction> getTransactions() {
170            return transactions;
171        }
172    
173        public void setTransactions(ConcurrentHashMap<TransactionId, Transaction> transactions) {
174            this.transactions = transactions;
175        }
176    
177        public boolean isInTransaction() {
178            return transaction != null;
179        }
180    
181        public String getClientId() {
182            return clientId;
183        }
184    
185        public void setClientId(String clientId) {
186            this.clientId = clientId;
187        }
188    
189        public boolean isHaAware() {
190            return haAware;
191        }
192    
193        public void setHaAware(boolean haAware) {
194            this.haAware = haAware;
195        }
196    
197        public WireFormatInfo getWireFormatInfo() {
198            return wireFormatInfo;
199        }
200    
201        public void setWireFormatInfo(WireFormatInfo wireFormatInfo) {
202            this.wireFormatInfo = wireFormatInfo;
203        }
204    
205        public ConnectionId getConnectionId() {
206            return connectionId;
207        }
208    
209        public void setConnectionId(ConnectionId connectionId) {
210            this.connectionId = connectionId;
211        }
212    
213        public String getUserName() {
214            return userName;
215        }
216    
217        protected void setUserName(String userName) {
218            this.userName = userName;
219        }
220    
221        public MessageEvaluationContext getMessageEvaluationContext() {
222            return messageEvaluationContext;
223        }
224    
225        public Object getLongTermStoreContext() {
226            return longTermStoreContext;
227        }
228    
229        public void setLongTermStoreContext(Object longTermStoreContext) {
230            this.longTermStoreContext = longTermStoreContext;
231        }
232    
233        public boolean isProducerFlowControl() {
234            return producerFlowControl;
235        }
236    
237        public void setProducerFlowControl(boolean disableProducerFlowControl) {
238            this.producerFlowControl = disableProducerFlowControl;
239        }
240    
241        public boolean isAllowedToConsume(MessageReference n) throws IOException {
242            if (messageAuthorizationPolicy != null) {
243                return messageAuthorizationPolicy.isAllowedToConsume(this, n.getMessage());
244            }
245            return true;
246        }
247    
248        public synchronized boolean isNetworkConnection() {
249            return networkConnection;
250        }
251    
252        public synchronized void setNetworkConnection(boolean networkConnection) {
253            this.networkConnection = networkConnection;
254        }
255    
256        public AtomicBoolean getStopping() {
257            return stopping;
258        }
259    
260        public void setDontSendReponse(boolean b) {
261            this.dontSendReponse = b;
262        }
263    
264        public boolean isDontSendReponse() {
265            return dontSendReponse;
266        }
267    
268        /**
269         * @return the slave
270         */
271        public boolean isSlave() {
272            return (this.broker != null && this.broker.getBrokerService().isSlave()) || !this.clientMaster;
273        }
274    
275        /**
276         * @return the clientMaster
277         */
278        public boolean isClientMaster() {
279            return this.clientMaster;
280        }
281    
282        /**
283         * @param clientMaster the clientMaster to set
284         */
285        public void setClientMaster(boolean clientMaster) {
286            this.clientMaster = clientMaster;
287        }
288    
289        public boolean isFaultTolerant() {
290            return faultTolerant;
291        }
292    
293        public void setFaultTolerant(boolean faultTolerant) {
294            this.faultTolerant = faultTolerant;
295        }
296    }