| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.component.irc; |
| 18 |
|
|
| 19 |
|
import java.net.URI; |
| 20 |
|
import java.util.HashMap; |
| 21 |
|
import java.util.Map; |
| 22 |
|
|
| 23 |
|
import org.apache.camel.CamelContext; |
| 24 |
|
import org.apache.camel.RuntimeCamelException; |
| 25 |
|
import org.apache.camel.impl.DefaultComponent; |
| 26 |
|
import org.apache.camel.util.IntrospectionSupport; |
| 27 |
|
import org.apache.commons.logging.Log; |
| 28 |
|
import org.apache.commons.logging.LogFactory; |
| 29 |
|
|
| 30 |
|
import org.schwering.irc.lib.IRCConnection; |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
0 |
public class IrcComponent extends DefaultComponent<IrcExchange> { |
| 38 |
0 |
private static final transient Log LOG = LogFactory.getLog(IrcComponent.class); |
| 39 |
|
private IrcConfiguration configuration; |
| 40 |
0 |
private final Map<String, IRCConnection> connectionCache = new HashMap<String, IRCConnection>(); |
| 41 |
|
|
| 42 |
0 |
public IrcComponent() { |
| 43 |
0 |
configuration = new IrcConfiguration(); |
| 44 |
0 |
} |
| 45 |
|
|
| 46 |
0 |
public IrcComponent(IrcConfiguration configuration) { |
| 47 |
0 |
this.configuration = configuration; |
| 48 |
0 |
} |
| 49 |
|
|
| 50 |
|
public IrcComponent(CamelContext context) { |
| 51 |
0 |
super(context); |
| 52 |
0 |
configuration = new IrcConfiguration(); |
| 53 |
0 |
} |
| 54 |
|
|
| 55 |
|
public static IrcComponent ircComponent() { |
| 56 |
0 |
return new IrcComponent(); |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
protected IrcEndpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception { |
| 60 |
0 |
IrcConfiguration config = getConfiguration().copy(); |
| 61 |
0 |
config.configure(new URI(uri)); |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
0 |
final IrcEndpoint endpoint = new IrcEndpoint(uri, this, config); |
| 66 |
|
|
| 67 |
0 |
IntrospectionSupport.setProperties(endpoint.getConfiguration(), parameters); |
| 68 |
0 |
return endpoint; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
public IrcConfiguration getConfiguration() { |
| 72 |
0 |
return configuration; |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
public void setConfiguration(IrcConfiguration configuration) { |
| 76 |
0 |
this.configuration = configuration; |
| 77 |
0 |
} |
| 78 |
|
|
| 79 |
|
public synchronized IRCConnection getIRCConnection(IrcConfiguration configuration) { |
| 80 |
|
final IRCConnection connection; |
| 81 |
0 |
if (connectionCache.containsKey(configuration.getCacheKey())) { |
| 82 |
0 |
if (LOG.isDebugEnabled()) { |
| 83 |
0 |
LOG.debug("Returning Cached Connection to " + configuration.getHostname() + " " + configuration.getTarget()); |
| 84 |
|
} |
| 85 |
0 |
connection = connectionCache.get(configuration.getCacheKey()); |
| 86 |
0 |
} else { |
| 87 |
0 |
connection = createConnection(configuration); |
| 88 |
0 |
connectionCache.put(configuration.getCacheKey(), connection); |
| 89 |
|
} |
| 90 |
0 |
return connection; |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
protected IRCConnection createConnection(IrcConfiguration configuration) { |
| 94 |
0 |
LOG.debug("Creating Connection to " + configuration.getHostname() + " destination: " + configuration.getTarget() + " nick: " + configuration.getNickname() + " user: " |
| 95 |
|
+ configuration.getUsername()); |
| 96 |
|
|
| 97 |
0 |
final IRCConnection conn = new IRCConnection(configuration.getHostname(), configuration.getPorts(), configuration.getPassword(), configuration.getNickname(), configuration.getUsername(), |
| 98 |
|
configuration.getRealname()); |
| 99 |
0 |
conn.setEncoding("UTF-8"); |
| 100 |
|
|
| 101 |
0 |
conn.setColors(configuration.isColors()); |
| 102 |
0 |
conn.setPong(true); |
| 103 |
|
|
| 104 |
|
try { |
| 105 |
0 |
conn.connect(); |
| 106 |
0 |
} catch (Exception e) { |
| 107 |
0 |
LOG.error("Failed to connect: " + e, e); |
| 108 |
|
|
| 109 |
|
|
| 110 |
0 |
throw new RuntimeCamelException(e); |
| 111 |
0 |
} |
| 112 |
0 |
return conn; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
public void closeConnection(String key, IRCConnection connection) { |
| 116 |
|
try { |
| 117 |
0 |
connection.doQuit(); |
| 118 |
0 |
connection.close(); |
| 119 |
0 |
} catch (Exception e) { |
| 120 |
0 |
e.printStackTrace(); |
| 121 |
0 |
} |
| 122 |
0 |
} |
| 123 |
|
|
| 124 |
|
@Override |
| 125 |
|
protected synchronized void doStop() throws Exception { |
| 126 |
|
|
| 127 |
|
|
| 128 |
0 |
Map<String, IRCConnection> map = new HashMap<String, IRCConnection>(connectionCache); |
| 129 |
0 |
connectionCache.clear(); |
| 130 |
0 |
for (Map.Entry<String, IRCConnection> entry : map.entrySet()) { |
| 131 |
0 |
closeConnection(entry.getKey(), entry.getValue()); |
| 132 |
0 |
} |
| 133 |
0 |
super.doStop(); |
| 134 |
0 |
} |
| 135 |
|
} |