1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mortbay.jetty.plus.webapp;
17
18 import java.util.Random;
19
20 import javax.naming.Context;
21 import javax.naming.InitialContext;
22 import javax.naming.NameNotFoundException;
23
24 import org.mortbay.jetty.plus.naming.EnvEntry;
25 import org.mortbay.jetty.plus.naming.NamingEntry;
26 import org.mortbay.jetty.plus.naming.NamingEntryUtil;
27 import org.mortbay.jetty.plus.naming.Transaction;
28 import org.mortbay.log.Log;
29
30
31
32
33
34
35
36 public class Configuration extends AbstractConfiguration
37 {
38
39 private Integer _key;
40
41
42 public Configuration () throws ClassNotFoundException
43 {
44 super();
45 }
46
47
48
49
50
51
52
53 public void bindEnvEntry(String name, Object value) throws Exception
54 {
55 EnvEntry.bindToENC(name, value);
56 }
57
58
59
60
61
62
63
64
65
66
67
68 public void bindResourceRef(String name, Class typeClass)
69 throws Exception
70 {
71 try
72 {
73 String mappedName = NamingEntryUtil.getMappedName (name);
74 NamingEntryUtil.bindToENC(name, mappedName);
75 }
76 catch (NameNotFoundException e)
77 {
78
79
80
81 NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(typeClass.getName()+"/default");
82 if (defaultNE!=null)
83 defaultNE.bindToENC(name);
84 }
85 }
86
87
88
89
90
91
92 public void bindResourceEnvRef(String name, Class typeClass)
93 throws Exception
94 {
95 try
96 {
97 String mappedName = NamingEntryUtil.getMappedName (name);
98 NamingEntryUtil.bindToENC(name, mappedName);
99 }
100 catch (NameNotFoundException e)
101 {
102
103
104
105 NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(typeClass.getName()+"/default");
106 if (defaultNE!=null)
107 defaultNE.bindToENC(name);
108 }
109 }
110
111
112 public void bindMessageDestinationRef(String name, Class typeClass)
113 throws Exception
114 {
115 try
116 {
117 String mappedName = NamingEntryUtil.getMappedName (name);
118 NamingEntryUtil.bindToENC(name, mappedName);
119 }
120 catch (NameNotFoundException e)
121 {
122
123
124
125 NamingEntry defaultNE = NamingEntryUtil.lookupNamingEntry(typeClass.getName()+"/default");
126 if (defaultNE!=null)
127 defaultNE.bindToENC(name);
128 }
129 }
130
131 public void bindUserTransaction ()
132 throws Exception
133 {
134 try
135 {
136 Transaction.bindToENC();
137 }
138 catch (NameNotFoundException e)
139 {
140 Log.info("No Transaction manager found - if your webapp requires one, please configure one.");
141 }
142 }
143
144 public void configureClassLoader ()
145 throws Exception
146 {
147 super.configureClassLoader();
148 }
149
150
151 public void configureDefaults ()
152 throws Exception
153 {
154 super.configureDefaults();
155 }
156
157
158 public void configureWebApp ()
159 throws Exception
160 {
161 super.configureWebApp();
162
163 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
164 Thread.currentThread().setContextClassLoader(getWebAppContext().getClassLoader());
165 lockCompEnv();
166 Thread.currentThread().setContextClassLoader(oldLoader);
167 }
168
169 public void deconfigureWebApp() throws Exception
170 {
171 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
172 Thread.currentThread().setContextClassLoader(getWebAppContext().getClassLoader());
173 unlockCompEnv();
174 Thread.currentThread().setContextClassLoader(oldLoader);
175 super.deconfigureWebApp();
176 }
177
178 protected void lockCompEnv ()
179 throws Exception
180 {
181 Random random = new Random ();
182 _key = new Integer(random.nextInt());
183 Context context = new InitialContext();
184 Context compCtx = (Context)context.lookup("java:comp");
185 compCtx.addToEnvironment("org.mortbay.jndi.lock", _key);
186 }
187
188 protected void unlockCompEnv ()
189 throws Exception
190 {
191 if (_key!=null)
192 {
193 Context context = new InitialContext();
194 Context compCtx = (Context)context.lookup("java:comp");
195 compCtx.addToEnvironment("org.mortbay.jndi.unlock", _key);
196 }
197 }
198
199
200
201
202 public void parseAnnotations() throws Exception
203 {
204 Log.info(getClass().getName()+" does not support annotations on source. Use org.mortbay.jetty.annotations.Configuration instead");
205 }
206
207 }