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.camel.component.kestrel;
018    
019    import java.net.InetSocketAddress;
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import org.apache.camel.RuntimeCamelException;
024    
025    /**
026     * Represents the configuration of the Kestrel component and/or endpoint.
027     */
028    public class KestrelConfiguration implements Cloneable {
029        /**
030         * The default port on which kestrel runs
031         */
032        public static final int DEFAULT_KESTREL_PORT = 22133;
033    
034        /**
035         * The address(es) on which kestrel is running
036         */
037        private String[] addresses = new String[]{"localhost:" + DEFAULT_KESTREL_PORT};
038    
039        /**
040         * How long a given wait should block (server side), in milliseconds
041         */
042        private int waitTimeMs = 100;
043    
044        /**
045         * How many concurrent listeners to schedule for the thread pool
046         */
047        private int concurrentConsumers = 1;
048    
049        public String[] getAddresses() {
050            return addresses;
051        }
052    
053        public void setAddresses(String[] addresses) {
054            this.addresses = addresses;
055        }
056    
057        public int getWaitTimeMs() {
058            return waitTimeMs;
059        }
060    
061        public void setWaitTimeMs(int waitTimeMs) {
062            this.waitTimeMs = waitTimeMs;
063        }
064    
065        public int getConcurrentConsumers() {
066            return concurrentConsumers;
067        }
068    
069        public void setConcurrentConsumers(int concurrentConsumers) {
070            if (concurrentConsumers <= 0) {
071                throw new IllegalArgumentException("Invalid value for concurrentConsumers: " + concurrentConsumers);
072            }
073            this.concurrentConsumers = concurrentConsumers;
074        }
075    
076        public String getAddressesAsString() {
077            StringBuilder bld = new StringBuilder();
078            for (String address : addresses) {
079                if (bld.length() > 0) {
080                    bld.append(',');
081                }
082                bld.append(address);
083            }
084            return bld.toString();
085        }
086    
087        public List<InetSocketAddress> getInetSocketAddresses() {
088            List<InetSocketAddress> list = new ArrayList<InetSocketAddress>();
089            for (String address : addresses) {
090                String[] tok = address.split(":");
091                String host;
092                int port;
093                if (tok.length == 2) {
094                    host = tok[0];
095                    port = Integer.parseInt(tok[1]);
096                } else if (tok.length == 1) {
097                    host = tok[0];
098                    port = DEFAULT_KESTREL_PORT;
099                } else {
100                    throw new IllegalArgumentException("Invalid address: " + address);
101                }
102                list.add(new InetSocketAddress(host, port));
103            }
104            return list;
105        }
106    
107        public KestrelConfiguration copy() {
108            try {
109                return (KestrelConfiguration) clone();
110            } catch (CloneNotSupportedException e) {
111                throw new RuntimeCamelException(e);
112            }
113        }
114    }