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.nagios;
018    
019    import java.net.URI;
020    
021    import com.googlecode.jsendnsca.core.NagiosSettings;
022    import org.apache.camel.RuntimeCamelException;
023    import org.apache.camel.util.ObjectHelper;
024    
025    /**
026     * @version $Revision: 17492 $
027     */
028    public class NagiosConfiguration implements Cloneable {
029    
030        private NagiosSettings nagiosSettings;
031        private String host;
032        private int port;
033        private int connectionTimeout = 5000;
034        private int timeout = 5000;
035        private String password;
036    
037    
038        /**
039         * Returns a copy of this configuration
040         */
041        public NagiosConfiguration copy() {
042            try {
043                NagiosConfiguration copy = (NagiosConfiguration) clone();
044                return copy;
045            } catch (CloneNotSupportedException e) {
046                throw new RuntimeCamelException(e);
047            }
048        }
049    
050        public void configure(URI uri) {
051            String value = uri.getHost();
052            if (value != null) {
053                setHost(value);
054            }
055            int port = uri.getPort();
056            if (port > 0) {
057                setPort(port);
058            }
059        }
060    
061        public synchronized NagiosSettings getNagiosSettings() {
062            if (nagiosSettings == null) {
063    
064                // validate parameters
065                ObjectHelper.notEmpty(host, "host", this);
066                if (port <= 0) {
067                    throw new IllegalArgumentException("Port must be a positive number on " + this);
068                }
069    
070                // create settings
071                nagiosSettings = new NagiosSettings();
072                nagiosSettings.setConnectTimeout(getConnectionTimeout());
073                nagiosSettings.setTimeout(getTimeout());
074                nagiosSettings.setNagiosHost(getHost());
075                nagiosSettings.setPort(getPort());
076                nagiosSettings.setPassword(getPassword());
077            }
078    
079            return nagiosSettings;
080        }
081    
082        public void setNagiosSettings(NagiosSettings nagiosSettings) {
083            this.nagiosSettings = nagiosSettings;
084        }
085    
086        public String getHost() {
087            return host;
088        }
089    
090        public void setHost(String host) {
091            this.host = host;
092        }
093    
094        public int getPort() {
095            return port;
096        }
097    
098        public void setPort(int port) {
099            this.port = port;
100        }
101    
102        public int getConnectionTimeout() {
103            return connectionTimeout;
104        }
105    
106        public void setConnectionTimeout(int connectionTimeout) {
107            this.connectionTimeout = connectionTimeout;
108        }
109    
110        public int getTimeout() {
111            return timeout;
112        }
113    
114        public void setTimeout(int timeout) {
115            this.timeout = timeout;
116        }
117    
118        public String getPassword() {
119            return password;
120        }
121    
122        public void setPassword(String password) {
123            this.password = password;
124        }
125    
126        @Override
127        public String toString() {
128            return "NagiosConfiguration[host=" + host + ":" + port + ", connectionTimeout=" + connectionTimeout + ", timeout=" + timeout;
129        }
130    
131    }