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