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