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.camel.util.IntrospectionSupport;
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029
030 import org.schwering.irc.lib.IRCConnection;
031
032 /**
033 * Defines the <a href="http://activemq.apache.org/camel/irc.html">IRC Component</a>
034 *
035 * @version $Revision:$
036 */
037 public class IrcComponent extends DefaultComponent<IrcExchange> {
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 public static IrcComponent ircComponent() {
056 return new IrcComponent();
057 }
058
059 protected IrcEndpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
060 IrcConfiguration config = getConfiguration().copy();
061 config.configure(new URI(uri));
062
063 // lets make sure we copy the configuration as each endpoint can
064 // customize its own version
065 final IrcEndpoint endpoint = new IrcEndpoint(uri, this, config);
066
067 IntrospectionSupport.setProperties(endpoint.getConfiguration(), parameters);
068 return endpoint;
069 }
070
071 public IrcConfiguration getConfiguration() {
072 return configuration;
073 }
074
075 public void setConfiguration(IrcConfiguration configuration) {
076 this.configuration = configuration;
077 }
078
079 public synchronized IRCConnection getIRCConnection(IrcConfiguration configuration) {
080 final IRCConnection connection;
081 if (connectionCache.containsKey(configuration.getCacheKey())) {
082 if (LOG.isDebugEnabled()) {
083 LOG.debug("Returning Cached Connection to " + configuration.getHostname() + " " + configuration.getTarget());
084 }
085 connection = connectionCache.get(configuration.getCacheKey());
086 } else {
087 connection = createConnection(configuration);
088 connectionCache.put(configuration.getCacheKey(), connection);
089 }
090 return connection;
091 }
092
093 protected IRCConnection createConnection(IrcConfiguration configuration) {
094 LOG.debug("Creating Connection to " + configuration.getHostname() + " destination: " + configuration.getTarget() + " nick: " + configuration.getNickname() + " user: "
095 + configuration.getUsername());
096
097 final IRCConnection conn = new IRCConnection(configuration.getHostname(), configuration.getPorts(), configuration.getPassword(), configuration.getNickname(), configuration.getUsername(),
098 configuration.getRealname());
099 conn.setEncoding("UTF-8");
100 // conn.setDaemon(true);
101 conn.setColors(configuration.isColors());
102 conn.setPong(true);
103
104 try {
105 conn.connect();
106 } catch (Exception e) {
107 LOG.error("Failed to connect: " + e, e);
108
109 // TODO use checked exceptions?
110 throw new RuntimeCamelException(e);
111 }
112 return conn;
113 }
114
115 public void closeConnection(String key, IRCConnection connection) {
116 try {
117 connection.doQuit();
118 connection.close();
119 } catch (Exception e) {
120 e.printStackTrace();
121 }
122 }
123
124 @Override
125 protected synchronized void doStop() throws Exception {
126 // lets use a copy so we can clear the connections eagerly in case of
127 // exceptions
128 Map<String, IRCConnection> map = new HashMap<String, IRCConnection>(connectionCache);
129 connectionCache.clear();
130 for (Map.Entry<String, IRCConnection> entry : map.entrySet()) {
131 closeConnection(entry.getKey(), entry.getValue());
132 }
133 super.doStop();
134 }
135 }