001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.tomcat.catalina;
018:
019: import org.apache.catalina.Lifecycle;
020: import org.apache.catalina.LifecycleEvent;
021: import org.apache.catalina.LifecycleListener;
022: import org.apache.catalina.core.StandardServer;
023: import org.apache.catalina.deploy.ContextEjb;
024: import org.apache.catalina.deploy.ContextEnvironment;
025: import org.apache.catalina.deploy.ContextLocalEjb;
026: import org.apache.catalina.deploy.ContextResource;
027: import org.apache.catalina.deploy.ContextResourceEnvRef;
028: import org.apache.catalina.deploy.ContextResourceLink;
029: import org.apache.catalina.deploy.NamingResources;
030: import org.apache.openejb.OpenEJBException;
031: import org.apache.openejb.assembler.classic.Assembler;
032: import org.apache.openejb.assembler.classic.ResourceInfo;
033: import org.apache.openejb.assembler.dynamic.PassthroughFactory;
034: import org.apache.openejb.loader.SystemInstance;
035: import org.apache.openejb.util.LogCategory;
036: import org.apache.openejb.util.Logger;
037:
038: import javax.naming.Context;
039: import javax.naming.NamingException;
040: import java.beans.PropertyChangeEvent;
041: import java.beans.PropertyChangeListener;
042:
043: @SuppressWarnings({"UnusedDeclaration"})
044: public class OpenEJBNamingContextListener implements LifecycleListener,
045: PropertyChangeListener {
046: private static final Logger logger = Logger.getInstance(
047: LogCategory.OPENEJB.createChild("tomcat"),
048: "org.apache.openejb.util.resources");
049:
050: /**
051: * Associated standardServer.
052: */
053: private final StandardServer standardServer;
054:
055: /**
056: * Has the listener been started?
057: */
058: private boolean running = false;
059:
060: /**
061: * Associated naming resources.
062: */
063: private final NamingResources namingResources;
064:
065: public OpenEJBNamingContextListener(StandardServer standardServer) {
066: this .standardServer = standardServer;
067: namingResources = standardServer.getGlobalNamingResources();
068: }
069:
070: public void lifecycleEvent(LifecycleEvent event) {
071: if (event.getLifecycle() != standardServer) {
072: return;
073: }
074:
075: if (Lifecycle.START_EVENT.equals(event.getType())) {
076: start();
077:
078: } else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
079:
080: stop();
081: }
082: }
083:
084: public boolean isRunning() {
085: return running;
086: }
087:
088: public void start() {
089: if (running)
090: return;
091:
092: namingResources.addPropertyChangeListener(this );
093: processInitialNamingResources();
094:
095: running = true;
096: }
097:
098: public void stop() {
099: if (!running)
100: return;
101:
102: namingResources.removePropertyChangeListener(this );
103:
104: running = false;
105: }
106:
107: public void propertyChange(PropertyChangeEvent event) {
108: if (!running)
109: return;
110:
111: Object source = event.getSource();
112: if (source == namingResources) {
113: processGlobalResourcesChange(event.getPropertyName(), event
114: .getOldValue(), event.getNewValue());
115: }
116: }
117:
118: /**
119: * Process a property change on the global naming resources, by making the
120: * corresponding addition or removal to OpenEJB.
121: *
122: * @param name Property name of the change to be processed
123: * @param oldValue The old value (or <code>null</code> if adding)
124: * @param newValue The new value (or <code>null</code> if removing)
125: */
126: private void processGlobalResourcesChange(String name,
127: Object oldValue, Object newValue) {
128:
129: // NOTE - It seems that the Context for global JNDI resources
130: // is left in read-write mode, so we do not have to change it here
131:
132: if (name.equals("ejb")) {
133: if (oldValue != null) {
134: ContextEjb ejb = (ContextEjb) oldValue;
135: if (ejb.getName() != null) {
136: removeEjb(ejb.getName());
137: }
138: }
139: if (newValue != null) {
140: ContextEjb ejb = (ContextEjb) newValue;
141: if (ejb.getName() != null) {
142: addEjb(ejb);
143: }
144: }
145: } else if (name.equals("environment")) {
146: if (oldValue != null) {
147: ContextEnvironment env = (ContextEnvironment) oldValue;
148: if (env.getName() != null) {
149: removeEnvironment(env.getName());
150: }
151: }
152: if (newValue != null) {
153: ContextEnvironment env = (ContextEnvironment) newValue;
154: if (env.getName() != null) {
155: addEnvironment(env);
156: }
157: }
158: } else if (name.equals("localEjb")) {
159: if (oldValue != null) {
160: ContextLocalEjb ejb = (ContextLocalEjb) oldValue;
161: if (ejb.getName() != null) {
162: removeLocalEjb(ejb.getName());
163: }
164: }
165: if (newValue != null) {
166: ContextLocalEjb ejb = (ContextLocalEjb) newValue;
167: if (ejb.getName() != null) {
168: addLocalEjb(ejb);
169: }
170: }
171: } else if (name.equals("resource")) {
172: if (oldValue != null) {
173: ContextResource resource = (ContextResource) oldValue;
174: if (resource.getName() != null) {
175: removeResource(resource.getName());
176: }
177: }
178: if (newValue != null) {
179: ContextResource resource = (ContextResource) newValue;
180: if (resource.getName() != null) {
181: addResource(resource);
182: }
183: }
184: } else if (name.equals("resourceEnvRef")) {
185: if (oldValue != null) {
186: ContextResourceEnvRef resourceEnvRef = (ContextResourceEnvRef) oldValue;
187: if (resourceEnvRef.getName() != null) {
188: removeResourceEnvRef(resourceEnvRef.getName());
189: }
190: }
191: if (newValue != null) {
192: ContextResourceEnvRef resourceEnvRef = (ContextResourceEnvRef) newValue;
193: if (resourceEnvRef.getName() != null) {
194: addResourceEnvRef(resourceEnvRef);
195: }
196: }
197: } else if (name.equals("resourceLink")) {
198: if (oldValue != null) {
199: ContextResourceLink rl = (ContextResourceLink) oldValue;
200: if (rl.getName() != null) {
201: removeResourceLink(rl.getName());
202: }
203: }
204: if (newValue != null) {
205: ContextResourceLink rl = (ContextResourceLink) newValue;
206: if (rl.getName() != null) {
207: addResourceLink(rl);
208: }
209: }
210: }
211: }
212:
213: private void processInitialNamingResources() {
214: // Resource links
215: ContextResourceLink[] resourceLinks = namingResources
216: .findResourceLinks();
217: for (ContextResourceLink resourceLink : resourceLinks) {
218: addResourceLink(resourceLink);
219: }
220:
221: // Resources
222: ContextResource[] resources = namingResources.findResources();
223: for (ContextResource resource : resources) {
224: addResource(resource);
225: }
226:
227: // Resources Env
228: ContextResourceEnvRef[] resourceEnvRefs = namingResources
229: .findResourceEnvRefs();
230: for (ContextResourceEnvRef resourceEnvRef : resourceEnvRefs) {
231: addResourceEnvRef(resourceEnvRef);
232: }
233:
234: // Environment entries
235: ContextEnvironment[] contextEnvironments = namingResources
236: .findEnvironments();
237: for (ContextEnvironment contextEnvironment : contextEnvironments) {
238: addEnvironment(contextEnvironment);
239: }
240:
241: // EJB references
242: ContextEjb[] ejbs = namingResources.findEjbs();
243: for (ContextEjb ejb : ejbs) {
244: addEjb(ejb);
245: }
246: }
247:
248: public void addEjb(ContextEjb ejb) {
249: }
250:
251: public void addEnvironment(ContextEnvironment env) {
252: }
253:
254: public void addLocalEjb(ContextLocalEjb localEjb) {
255: }
256:
257: public void addResource(ContextResource resource) {
258: try {
259: Context globalNamingContext = standardServer
260: .getGlobalNamingContext();
261: Object value = globalNamingContext.lookup(resource
262: .getName());
263: String type = resource.getType();
264: bindResource(resource.getName(), value, type);
265: } catch (NamingException e) {
266: logger.error("Unable to lookup Global Tomcat resource "
267: + resource.getName(), e);
268: }
269: }
270:
271: public void addResourceEnvRef(ContextResourceEnvRef resourceEnvRef) {
272: try {
273: Context globalNamingContext = standardServer
274: .getGlobalNamingContext();
275: Object value = globalNamingContext.lookup(resourceEnvRef
276: .getName());
277: String type = resourceEnvRef.getType();
278: bindResource(resourceEnvRef.getName(), value, type);
279: } catch (NamingException e) {
280: logger.error("Unable to lookup Global Tomcat resource "
281: + resourceEnvRef.getName(), e);
282: }
283: }
284:
285: private void bindResource(String name, Object value, String type) {
286: ResourceInfo resourceInfo = new ResourceInfo();
287: resourceInfo.id = name;
288: resourceInfo.service = "Resource";
289: resourceInfo.types.add(type);
290: PassthroughFactory.add(resourceInfo, value);
291: Assembler assembler = (Assembler) SystemInstance.get()
292: .getComponent(org.apache.openejb.spi.Assembler.class);
293:
294: try {
295: assembler.createResource(resourceInfo);
296: } catch (OpenEJBException e) {
297: logger.error("Unable to bind Global Tomcat resource "
298: + name + " into OpenEJB", e);
299: }
300: }
301:
302: public void addResourceLink(ContextResourceLink resourceLink) {
303: }
304:
305: public void removeEjb(String name) {
306: }
307:
308: public void removeEnvironment(String name) {
309: }
310:
311: public void removeLocalEjb(String name) {
312: }
313:
314: public void removeResource(String name) {
315: // there isn't any way to remove a resource yet
316: }
317:
318: public void removeResourceEnvRef(String name) {
319: // there isn't any way to remove a resource yet
320: }
321:
322: public void removeResourceLink(String name) {
323: }
324: }
|