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(String name) {
44 super(name);
45 }
46
47
48
49
50
51
52
53
54
55
56
57
58 protected static Context createContext(String factoryName, String providerURL, String urlPkgPrefixes,
59 String securityPrincipalName, String securityCredentials)
60 throws NamingException {
61
62 Properties props = getEnvironment(factoryName, providerURL, urlPkgPrefixes, securityPrincipalName,
63 securityCredentials);
64 return new InitialContext(props);
65 }
66
67
68
69
70
71
72
73
74 protected static Object lookup(Context ctx, String name) throws NamingException {
75 try {
76 return ctx.lookup(name);
77 } catch (NameNotFoundException e) {
78 LOGGER.error("Could not find name [" + name + "].");
79 throw e;
80 }
81 }
82
83
84
85
86
87
88
89
90
91
92
93 protected static Properties getEnvironment(String factoryName, String providerURL, String urlPkgPrefixes,
94 String securityPrincipalName, String securityCredentials) {
95 Properties props = new Properties();
96 if (factoryName != null) {
97 props.put(Context.INITIAL_CONTEXT_FACTORY, factoryName);
98 if (providerURL != null) {
99 props.put(Context.PROVIDER_URL, providerURL);
100 } else {
101 LOGGER.warn("The InitalContext factory name has been provided without a ProviderURL. " +
102 "This is likely to cause problems");
103 }
104 if (urlPkgPrefixes != null) {
105 props.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
106 }
107 if (securityPrincipalName != null) {
108 props.put(Context.SECURITY_PRINCIPAL, securityPrincipalName);
109 if (securityCredentials != null) {
110 props.put(Context.SECURITY_CREDENTIALS, securityCredentials);
111 } else {
112 LOGGER.warn("SecurityPrincipalName has been set without SecurityCredentials. " +
113 "This is likely to cause problems.");
114 }
115 }
116 return props;
117 }
118 return null;
119 }
120
121
122
123
124
125
126 public abstract void send(Serializable object) throws Exception;
127
128
129
130
131
132
133
134
135 public synchronized void send(Serializable object, Session session, MessageProducer producer) throws Exception {
136 try {
137 Message msg;
138 if (object instanceof String) {
139 msg = session.createTextMessage();
140 ((TextMessage) msg).setText((String) object);
141 } else {
142 msg = session.createObjectMessage();
143 ((ObjectMessage) msg).setObject(object);
144 }
145 producer.send(msg);
146 } catch (JMSException ex) {
147 LOGGER.error("Could not publish message via JMS " + getName());
148 throw ex;
149 }
150 }
151 }