1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.net.jms;
18
19 import java.io.Serializable;
20 import java.util.Properties;
21
22 import javax.jms.JMSException;
23 import javax.jms.Message;
24 import javax.jms.MessageProducer;
25 import javax.jms.ObjectMessage;
26 import javax.jms.Session;
27 import javax.jms.TextMessage;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NameNotFoundException;
31 import javax.naming.NamingException;
32
33 import org.apache.logging.log4j.core.appender.AbstractManager;
34
35
36
37
38 public abstract class AbstractJmsManager extends AbstractManager {
39
40
41
42
43
44 public AbstractJmsManager(final String name) {
45 super(name);
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59 protected static Context createContext(final String factoryName, final String providerURL,
60 final String urlPkgPrefixes, final String securityPrincipalName,
61 final String securityCredentials)
62 throws NamingException {
63
64 final Properties props = getEnvironment(factoryName, providerURL, urlPkgPrefixes, securityPrincipalName,
65 securityCredentials);
66 return new InitialContext(props);
67 }
68
69
70
71
72
73
74
75
76 protected static Object lookup(final Context ctx, final String name) throws NamingException {
77 try {
78 return ctx.lookup(name);
79 } catch (final NameNotFoundException e) {
80 LOGGER.warn("Could not find name [" + name + "].");
81 throw e;
82 }
83 }
84
85
86
87
88
89
90
91
92
93
94
95 protected static Properties getEnvironment(final String factoryName, final String providerURL,
96 final String urlPkgPrefixes, final String securityPrincipalName,
97 final String securityCredentials) {
98 final Properties props = new Properties();
99 if (factoryName != null) {
100 props.put(Context.INITIAL_CONTEXT_FACTORY, factoryName);
101 if (providerURL != null) {
102 props.put(Context.PROVIDER_URL, providerURL);
103 } else {
104 LOGGER.warn("The InitialContext factory name has been provided without a ProviderURL. " +
105 "This is likely to cause problems");
106 }
107 if (urlPkgPrefixes != null) {
108 props.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
109 }
110 if (securityPrincipalName != null) {
111 props.put(Context.SECURITY_PRINCIPAL, securityPrincipalName);
112 if (securityCredentials != null) {
113 props.put(Context.SECURITY_CREDENTIALS, securityCredentials);
114 } else {
115 LOGGER.warn("SecurityPrincipalName has been set without SecurityCredentials. " +
116 "This is likely to cause problems.");
117 }
118 }
119 return props;
120 }
121 return null;
122 }
123
124
125
126
127
128
129 public abstract void send(Serializable object) throws Exception;
130
131
132
133
134
135
136
137
138 public synchronized void send(final Serializable object, final Session session, final MessageProducer producer)
139 throws Exception {
140 try {
141 Message msg;
142 if (object instanceof String) {
143 msg = session.createTextMessage();
144 ((TextMessage) msg).setText((String) object);
145 } else {
146 msg = session.createObjectMessage();
147 ((ObjectMessage) msg).setObject(object);
148 }
149 producer.send(msg);
150 } catch (final JMSException ex) {
151 LOGGER.error("Could not publish message via JMS " + getName());
152 throw ex;
153 }
154 }
155 }