1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.component.irc; |
19 |
|
|
20 |
|
import org.apache.camel.CamelContext; |
21 |
|
import org.apache.camel.RuntimeCamelException; |
22 |
|
import org.apache.camel.impl.DefaultComponent; |
23 |
|
import org.apache.camel.util.IntrospectionSupport; |
24 |
|
import org.apache.commons.logging.Log; |
25 |
|
import org.apache.commons.logging.LogFactory; |
26 |
|
import org.schwering.irc.lib.IRCConnection; |
27 |
|
|
28 |
|
import java.net.URI; |
29 |
|
import java.util.HashMap; |
30 |
|
import java.util.Map; |
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 |
|
public static IrcComponent ircComponent() { |
43 |
0 |
return new IrcComponent(); |
44 |
|
} |
45 |
|
|
46 |
0 |
public IrcComponent() { |
47 |
0 |
configuration = new IrcConfiguration(); |
48 |
0 |
} |
49 |
|
|
50 |
0 |
public IrcComponent(IrcConfiguration configuration) { |
51 |
0 |
this.configuration = configuration; |
52 |
0 |
} |
53 |
|
|
54 |
|
public IrcComponent(CamelContext context) { |
55 |
0 |
super(context); |
56 |
0 |
configuration = new IrcConfiguration(); |
57 |
0 |
} |
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 |
0 |
final IrcEndpoint endpoint = new IrcEndpoint(uri, this, config); |
65 |
|
|
66 |
0 |
IntrospectionSupport.setProperties(endpoint.getConfiguration(), parameters); |
67 |
0 |
return endpoint; |
68 |
|
} |
69 |
|
|
70 |
|
public IrcConfiguration getConfiguration() { |
71 |
0 |
return configuration; |
72 |
|
} |
73 |
|
|
74 |
|
public void setConfiguration(IrcConfiguration configuration) { |
75 |
0 |
this.configuration = configuration; |
76 |
0 |
} |
77 |
|
|
78 |
|
public synchronized IRCConnection getIRCConnection(IrcConfiguration configuration) { |
79 |
|
final IRCConnection connection; |
80 |
0 |
if (connectionCache.containsKey(configuration.getCacheKey())) { |
81 |
0 |
if (log.isDebugEnabled()) { |
82 |
0 |
log.debug("Returning Cached Connection to " + configuration.getHostname() + " " + configuration.getTarget()); |
83 |
|
} |
84 |
0 |
connection = connectionCache.get(configuration.getCacheKey()); |
85 |
0 |
} |
86 |
|
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() |
95 |
|
+ " nick: " + configuration.getNickname() + " user: " + configuration.getUsername()); |
96 |
|
|
97 |
0 |
final IRCConnection conn = new IRCConnection(configuration.getHostname(), configuration.getPorts(), configuration.getPassword(), configuration.getNickname(), configuration.getUsername(), configuration.getRealname()); |
98 |
0 |
conn.setEncoding("UTF-8"); |
99 |
|
|
100 |
0 |
conn.setColors(configuration.isColors()); |
101 |
0 |
conn.setPong(true); |
102 |
|
|
103 |
|
try { |
104 |
0 |
conn.connect(); |
105 |
|
} |
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 |
|
} |
120 |
0 |
catch (Exception e) { |
121 |
0 |
e.printStackTrace(); |
122 |
0 |
} |
123 |
0 |
} |
124 |
|
|
125 |
|
@Override |
126 |
|
protected synchronized void doStop() throws Exception { |
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 |
|
} |