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