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.cometd;
018    
019    import java.net.URI;
020    import java.net.URISyntaxException;
021    import java.util.Map;
022    
023    import org.apache.camel.Consumer;
024    import org.apache.camel.Processor;
025    import org.apache.camel.Producer;
026    import org.apache.camel.impl.DefaultEndpoint;
027    import org.apache.camel.util.ObjectHelper;
028    
029    /**
030     * Endpoint for Camel Cometd.
031     *
032     * @version $Revision:520964 $
033     */
034    public class CometdEndpoint extends DefaultEndpoint {
035       
036        private String resourceBase;
037        private int timeout = 240000;
038        private int interval;
039        private int maxInterval = 30000;
040        private int multiFrameInterval = 1500;
041        private boolean jsonCommented = true;
042        private int logLevel = 1;
043        private URI uri;
044        private CometdComponent component;
045        
046        public CometdEndpoint(CometdComponent component, String uri, String remaining, Map<String, Object> parameters) {
047            super(uri, component);
048            this.component = component;
049            try {
050                this.uri = new URI(uri);
051            } catch (URISyntaxException e) {
052                throw new IllegalArgumentException(e);
053            }
054        }
055    
056        public Producer createProducer() throws Exception {
057            ObjectHelper.notNull(component, "component");
058            CometdProducer producer = new CometdProducer(this);
059            return producer;
060        }
061    
062        public Consumer createConsumer(Processor processor) throws Exception {
063            ObjectHelper.notNull(component, "component");
064            CometdConsumer consumer =  new CometdConsumer(this, processor);
065            return consumer;
066        }
067    
068        public void connect(CometdProducerConsumer prodcons) throws Exception {
069            component.connect(prodcons);
070        }
071        
072        public void disconnect(CometdProducerConsumer prodcons) throws Exception {
073            component.disconnect(prodcons);
074        }
075        
076        public CometdComponent getComponent() {
077            return component;
078        }
079    
080        public boolean isSingleton() {
081            return false;
082        }
083        
084        public String getPath() {
085            return uri.getPath();
086        }
087    
088        public int getPort() {
089            if (uri.getPort() == -1) {
090                if ("cometds".equals(getProtocol())) {
091                    return 443;
092                } else {
093                    return 80;
094                }
095            }
096            return uri.getPort();
097        }
098    
099        public String getProtocol() {
100            return uri.getScheme();
101        }
102    
103        public URI getUri() {
104            return uri;
105        }
106    
107        public String getResourceBase() {
108            return resourceBase;
109        }
110    
111        public void setResourceBase(String resourceBase) {
112            this.resourceBase = resourceBase;
113        }
114       
115        public int getTimeout() {
116            return timeout;
117        }
118    
119        public void setTimeout(int timeout) {
120            this.timeout = timeout;
121        }
122    
123        public int getInterval() {
124            return interval;
125        }
126    
127        public void setInterval(int interval) {
128            this.interval = interval;
129        }
130    
131        public int getMaxInterval() {
132            return maxInterval;
133        }
134    
135        public void setMaxInterval(int maxInterval) {
136            this.maxInterval = maxInterval;
137        }
138    
139        public int getMultiFrameInterval() {
140            return multiFrameInterval;
141        }
142    
143        public void setMultiFrameInterval(int multiFrameInterval) {
144            this.multiFrameInterval = multiFrameInterval;
145        }
146    
147        public boolean isJsonCommented() {
148            return jsonCommented;
149        }
150    
151        public void setJsonCommented(boolean commented) {
152            jsonCommented = commented;
153        }
154    
155        public int getLogLevel() {
156            return logLevel;
157        }
158    
159        public void setLogLevel(int logLevel) {
160            this.logLevel = logLevel;
161        }
162    }