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.irc;
018
019 import java.net.URI;
020 import java.util.HashMap;
021 import java.util.Map;
022
023 import org.apache.camel.CamelContext;
024 import org.apache.camel.RuntimeCamelException;
025 import org.apache.camel.impl.DefaultComponent;
026 import org.apache.commons.logging.Log;
027 import org.apache.commons.logging.LogFactory;
028 import org.schwering.irc.lib.IRCConnection;
029
030 import org.schwering.irc.lib.ssl.SSLIRCConnection;
031
032 /**
033 * Defines the <a href="http://camel.apache.org/irc.html">IRC Component</a>
034 *
035 * @version $Revision: 791762 $
036 */
037 public class IrcComponent extends DefaultComponent {
038 private static final transient Log LOG = LogFactory.getLog(IrcComponent.class);
039 private IrcConfiguration configuration;
040 private final Map<String, IRCConnection> connectionCache = new HashMap<String, IRCConnection>();
041
042 public IrcComponent() {
043 configuration = new IrcConfiguration();
044 }
045
046 public IrcComponent(IrcConfiguration configuration) {
047 this.configuration = configuration;
048 }
049
050 public IrcComponent(CamelContext context) {
051 super(context);
052 configuration = new IrcConfiguration();
053 }
054
055 protected IrcEndpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
056 // lets make sure we copy the configuration as each endpoint can customize its own version
057 IrcConfiguration config = getConfiguration().copy();
058 config.configure(uri);
059
060 IrcEndpoint endpoint = new IrcEndpoint(uri, this, config);
061 setProperties(endpoint.getConfiguration(), parameters);
062 return endpoint;
063 }
064
065 public synchronized IRCConnection getIRCConnection(IrcConfiguration configuration) {
066 final IRCConnection connection;
067 if (connectionCache.containsKey(configuration.getCacheKey())) {
068 if (LOG.isDebugEnabled()) {
069 LOG.debug("Returning Cached Connection to " + configuration.getHostname() + " " + configuration.getTarget());
070 }
071 connection = connectionCache.get(configuration.getCacheKey());
072 } else {
073 connection = createConnection(configuration);
074 connectionCache.put(configuration.getCacheKey(), connection);
075 }
076 return connection;
077 }
078
079 protected IRCConnection createConnection(IrcConfiguration configuration) {
080 IRCConnection conn = null;
081
082 if (configuration.getUsingSSL()) {
083 if (LOG.isDebugEnabled()) {
084 LOG.debug("Creating SSL Connection to " + configuration.getHostname() + " destination: " + configuration.getTarget()
085 + " nick: " + configuration.getNickname() + " user: " + configuration.getUsername());
086 }
087 SSLIRCConnection sconn = new SSLIRCConnection(configuration.getHostname(), configuration.getPorts(), configuration.getPassword(),
088 configuration.getNickname(), configuration.getUsername(), configuration.getRealname());
089
090 sconn.addTrustManager(configuration.getTrustManager());
091 conn = sconn;
092
093 } else {
094 if (LOG.isDebugEnabled()) {
095 LOG.debug("Creating Connection to " + configuration.getHostname() + " destination: " + configuration.getTarget()
096 + " nick: " + configuration.getNickname() + " user: " + configuration.getUsername());
097 }
098
099 conn = new IRCConnection(configuration.getHostname(), configuration.getPorts(), configuration.getPassword(),
100 configuration.getNickname(), configuration.getUsername(), configuration.getRealname());
101 }
102 conn.setEncoding("UTF-8");
103 conn.setColors(configuration.isColors());
104 conn.setPong(true);
105
106 try {
107 conn.connect();
108 } catch (Exception e) {
109 throw new RuntimeCamelException(e);
110 }
111 return conn;
112 }
113
114 public void closeConnection(String key, IRCConnection connection) {
115 try {
116 connection.doQuit();
117 connection.close();
118 } catch (Exception e) {
119 LOG.warn("Error during closing connection.", e);
120 }
121 }
122
123 @Override
124 protected synchronized void doStop() throws Exception {
125 // lets use a copy so we can clear the connections eagerly in case of exceptions
126 Map<String, IRCConnection> map = new HashMap<String, IRCConnection>(connectionCache);
127 connectionCache.clear();
128 for (Map.Entry<String, IRCConnection> entry : map.entrySet()) {
129 closeConnection(entry.getKey(), entry.getValue());
130 }
131 super.doStop();
132 }
133
134 public IrcConfiguration getConfiguration() {
135 return configuration;
136 }
137
138 public void setConfiguration(IrcConfiguration configuration) {
139 this.configuration = configuration;
140 }
141
142 }