1 package ch.qos.logback.core.net;
2
3 import java.util.Hashtable;
4 import java.util.Properties;
5
6 import javax.naming.Context;
7 import javax.naming.InitialContext;
8 import javax.naming.NameNotFoundException;
9 import javax.naming.NamingException;
10
11 import ch.qos.logback.core.AppenderBase;
12
13
14
15
16
17
18
19
20
21
22
23 public abstract class JMSAppenderBase<E> extends AppenderBase<E> {
24
25 protected String securityPrincipalName;
26 protected String securityCredentials;
27 protected String initialContextFactoryName;
28 protected String urlPkgPrefixes;
29 protected String providerURL;
30 protected String userName;
31 protected String password;
32
33
34 protected Object lookup(Context ctx, String name) throws NamingException {
35 try {
36 return ctx.lookup(name);
37 } catch (NameNotFoundException e) {
38 addError("Could not find name [" + name + "].");
39 throw e;
40 }
41 }
42
43 public Context buildJNDIContext() throws NamingException {
44 Context jndi = null;
45
46
47 if (initialContextFactoryName != null) {
48 Properties env = buildEnvProperties();
49 jndi = new InitialContext(env);
50 } else {
51 jndi = new InitialContext();
52 }
53 return jndi;
54 }
55
56 public Properties buildEnvProperties() {
57 Properties env = new Properties();
58 env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
59 if (providerURL != null) {
60 env.put(Context.PROVIDER_URL, providerURL);
61 } else {
62 addWarn("You have set InitialContextFactoryName option but not the "
63 + "ProviderURL. This is likely to cause problems.");
64 }
65 if (urlPkgPrefixes != null) {
66 env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
67 }
68
69 if (securityPrincipalName != null) {
70 env.put(Context.SECURITY_PRINCIPAL, securityPrincipalName);
71 if (securityCredentials != null) {
72 env.put(Context.SECURITY_CREDENTIALS, securityCredentials);
73 } else {
74 addWarn("You have set SecurityPrincipalName option but not the "
75 + "SecurityCredentials. This is likely to cause problems.");
76 }
77 }
78 return env;
79 }
80
81
82
83
84
85
86
87
88 public String getInitialContextFactoryName() {
89 return initialContextFactoryName;
90 }
91
92
93
94
95
96
97
98
99
100
101
102 public void setInitialContextFactoryName(String initialContextFactoryName) {
103 this.initialContextFactoryName = initialContextFactoryName;
104 }
105
106 public String getProviderURL() {
107 return providerURL;
108 }
109
110 public void setProviderURL(String providerURL) {
111 this.providerURL = providerURL;
112 }
113
114 public String getURLPkgPrefixes() {
115 return urlPkgPrefixes;
116 }
117
118 public void setURLPkgPrefixes(String urlPkgPrefixes) {
119 this.urlPkgPrefixes = urlPkgPrefixes;
120 }
121
122 public String getSecurityCredentials() {
123 return securityCredentials;
124 }
125
126 public void setSecurityCredentials(String securityCredentials) {
127 this.securityCredentials = securityCredentials;
128 }
129
130 public String getSecurityPrincipalName() {
131 return securityPrincipalName;
132 }
133
134 public void setSecurityPrincipalName(String securityPrincipalName) {
135 this.securityPrincipalName = securityPrincipalName;
136 }
137
138 public String getUserName() {
139 return userName;
140 }
141
142
143
144
145
146
147
148 public void setUserName(String userName) {
149 this.userName = userName;
150 }
151
152 public String getPassword() {
153 return password;
154 }
155
156
157
158
159 public void setPassword(String password) {
160 this.password = password;
161 }
162
163
164
165
166 }