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.command;
018
019import java.io.IOException;
020import java.util.Properties;
021import org.apache.activemq.state.CommandVisitor;
022import org.apache.activemq.util.MarshallingSupport;
023
024/**
025 * When a client connects to a broker, the broker send the client a BrokerInfo
026 * so that the client knows which broker node he's talking to and also any peers
027 * that the node has in his cluster. This is the broker helping the client out
028 * in discovering other nodes in the cluster.
029 * 
030 * @openwire:marshaller code="2"
031 * 
032 */
033public class BrokerInfo extends BaseCommand {
034    private static final String PASSIVE_SLAVE_KEY = "passiveSlave";
035    public static final byte DATA_STRUCTURE_TYPE = CommandTypes.BROKER_INFO;
036    BrokerId brokerId;
037    String brokerURL;
038    boolean slaveBroker;
039    boolean masterBroker;
040    boolean faultTolerantConfiguration;
041    boolean networkConnection;
042    boolean duplexConnection;
043    BrokerInfo peerBrokerInfos[];
044    String brokerName;
045    long connectionId;
046    String brokerUploadUrl;
047    String networkProperties;
048    transient int refCount = 0;
049    
050    public BrokerInfo copy() {
051        BrokerInfo copy = new BrokerInfo();
052        copy(copy);
053        return copy;
054    }
055    
056    private void copy(BrokerInfo copy) {
057        super.copy(copy);
058        copy.brokerId = this.brokerId;
059        copy.brokerURL = this.brokerURL;
060        copy.slaveBroker = this.slaveBroker;
061        copy.masterBroker = this.masterBroker;
062        copy.faultTolerantConfiguration = this.faultTolerantConfiguration;
063        copy.networkConnection = this.networkConnection;
064        copy.duplexConnection = this.duplexConnection;
065        copy.peerBrokerInfos = this.peerBrokerInfos;
066        copy.brokerName = this.brokerName;
067        copy.connectionId = this.connectionId;
068        copy.brokerUploadUrl = this.brokerUploadUrl;
069        copy.networkProperties = this.networkProperties;
070    } 
071
072    @Override
073    public boolean isBrokerInfo() {
074        return true;
075    }
076
077    public byte getDataStructureType() {
078        return DATA_STRUCTURE_TYPE;
079    }
080
081    /**
082     * @openwire:property version=1 cache=true
083     */
084    public BrokerId getBrokerId() {
085        return brokerId;
086    }
087
088    public void setBrokerId(BrokerId brokerId) {
089        this.brokerId = brokerId;
090    }
091
092    /**
093     * @openwire:property version=1
094     */
095    public String getBrokerURL() {
096        return brokerURL;
097    }
098
099    public void setBrokerURL(String brokerURL) {
100        this.brokerURL = brokerURL;
101    }
102
103    /**
104     * @openwire:property version=1 testSize=0
105     */
106    public BrokerInfo[] getPeerBrokerInfos() {
107        return peerBrokerInfos;
108    }
109
110    public void setPeerBrokerInfos(BrokerInfo[] peerBrokerInfos) {
111        this.peerBrokerInfos = peerBrokerInfos;
112    }
113
114    /**
115     * @openwire:property version=1
116     */
117    public String getBrokerName() {
118        return brokerName;
119    }
120
121    public void setBrokerName(String brokerName) {
122        this.brokerName = brokerName;
123    }
124
125    public Response visit(CommandVisitor visitor) throws Exception {
126        return visitor.processBrokerInfo(this);
127    }
128
129    /**
130     * @openwire:property version=1
131     */
132    public boolean isSlaveBroker() {
133        return slaveBroker;
134    }
135
136    public void setSlaveBroker(boolean slaveBroker) {
137        this.slaveBroker = slaveBroker;
138    }
139
140    /**
141     * @openwire:property version=1
142     */
143    public boolean isMasterBroker() {
144        return masterBroker;
145    }
146
147    /**
148     * @param masterBroker The masterBroker to set.
149     */
150    public void setMasterBroker(boolean masterBroker) {
151        this.masterBroker = masterBroker;
152    }
153
154    /**
155     * @openwire:property version=1
156     * @return Returns the faultTolerantConfiguration.
157     */
158    public boolean isFaultTolerantConfiguration() {
159        return faultTolerantConfiguration;
160    }
161
162    /**
163     * @param faultTolerantConfiguration The faultTolerantConfiguration to set.
164     */
165    public void setFaultTolerantConfiguration(boolean faultTolerantConfiguration) {
166        this.faultTolerantConfiguration = faultTolerantConfiguration;
167    }
168
169    /**
170     * @openwire:property version=2
171     * @return the duplexConnection
172     */
173    public boolean isDuplexConnection() {
174        return this.duplexConnection;
175    }
176
177    /**
178     * @param duplexConnection the duplexConnection to set
179     */
180    public void setDuplexConnection(boolean duplexConnection) {
181        this.duplexConnection = duplexConnection;
182    }
183
184    /**
185     * @openwire:property version=2
186     * @return the networkConnection
187     */
188    public boolean isNetworkConnection() {
189        return this.networkConnection;
190    }
191
192    /**
193     * @param networkConnection the networkConnection to set
194     */
195    public void setNetworkConnection(boolean networkConnection) {
196        this.networkConnection = networkConnection;
197    }
198
199    /**
200     * The broker assigns a each connection it accepts a connection id.
201     * 
202     * @openwire:property version=2
203     */
204    public long getConnectionId() {
205        return connectionId;
206    }
207
208    public void setConnectionId(long connectionId) {
209        this.connectionId = connectionId;
210    }
211
212    /**
213     * The URL to use when uploading BLOBs to the broker or some other external
214     * file/http server
215     * 
216     * @openwire:property version=3
217     */
218    public String getBrokerUploadUrl() {
219        return brokerUploadUrl;
220    }
221
222    public void setBrokerUploadUrl(String brokerUploadUrl) {
223        this.brokerUploadUrl = brokerUploadUrl;
224    }
225
226    /**
227     * @openwire:property version=3 cache=false
228     * @return the networkProperties
229     */
230    public String getNetworkProperties() {
231        return this.networkProperties;
232    }
233
234    /**
235     * @param networkProperties the networkProperties to set
236     */
237    public void setNetworkProperties(String networkProperties) {
238        this.networkProperties = networkProperties;
239    }
240    
241    public boolean isPassiveSlave() {
242        boolean result = false;
243        Properties props = getProperties();
244        if (props != null) {
245            result = Boolean.parseBoolean(props.getProperty(PASSIVE_SLAVE_KEY, "false"));
246        }
247        return result;
248    }
249    
250    public void setPassiveSlave(boolean value) {
251        Properties props = new Properties();
252        props.put(PASSIVE_SLAVE_KEY, Boolean.toString(value));
253        try {
254            this.networkProperties=MarshallingSupport.propertiesToString(props);
255        } catch (IOException e) {
256            e.printStackTrace();
257        }
258    }
259    
260    public Properties getProperties() {
261        Properties result = null;
262        try {
263            result = MarshallingSupport.stringToProperties(getNetworkProperties());
264        } catch (IOException e) {
265           e.printStackTrace();
266        }
267        return result;
268    }
269
270    public int getRefCount() {
271        return refCount;
272    }
273
274    public void incrementRefCount() {
275        refCount++;
276    }
277    public int decrementRefCount() {
278        return --refCount;
279    }
280}