001: /*****************************************************************************
002: * Copyright (C) PicoContainer Organization. All rights reserved. *
003: * ------------------------------------------------------------------------- *
004: * The software in this package is published under the terms of the BSD *
005: * style license a copy of which has been included with this distribution in *
006: * the LICENSE.txt file. *
007: * *
008: * Original code by *
009: *****************************************************************************/package org.picocontainer.gems.containers;
010:
011: import org.picocontainer.ComponentAdapter;
012: import org.picocontainer.MutablePicoContainer;
013: import org.picocontainer.Parameter;
014: import org.picocontainer.PicoContainer;
015: import org.picocontainer.PicoVisitor;
016: import org.picocontainer.NameBinding;
017:
018: import java.util.Collection;
019: import java.util.List;
020: import java.util.Properties;
021: import java.io.Serializable;
022: import java.lang.annotation.Annotation;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: /** @author Michael Rimov */
028: public class CommonsLoggingTracingContainerDecorator implements
029: MutablePicoContainer, Serializable {
030:
031: /** Wrapped container. */
032: private final MutablePicoContainer delegate;
033:
034: /** Logger instance used for writing events. */
035: private transient Log log;
036:
037: /** Serialized log category. */
038: private final String logCategory;
039:
040: /**
041: * Default typical wrapper that wraps another MutablePicoContainer.
042: *
043: * @param delegate Container to be decorated.
044: *
045: * @throws NullPointerException if delegate is null.
046: */
047: public CommonsLoggingTracingContainerDecorator(
048: final MutablePicoContainer delegate) {
049: this (delegate, PicoContainer.class.getName());
050: }
051:
052: /**
053: * Alternate constructor that allows specification of the Logger to
054: * use.
055: *
056: * @param delegate Container to be decorated.
057: * @param loggingCategory specific Log4j Logger to use.
058: *
059: * @throws NullPointerException if delegate or log is null.
060: */
061: public CommonsLoggingTracingContainerDecorator(
062: final MutablePicoContainer delegate,
063: final String loggingCategory) {
064: if (delegate == null) {
065: throw new NullPointerException("delegate");
066: }
067:
068: if (loggingCategory == null) {
069: throw new NullPointerException("loggingCategory");
070: }
071:
072: log = LogFactory.getLog(loggingCategory);
073:
074: this .delegate = delegate;
075: logCategory = loggingCategory;
076: }
077:
078: /**
079: * Standard message handling for cases when a null object is returned
080: * for a given key.
081: *
082: * @param componentKey Component key that does not exist
083: * @param target Logger to log into
084: */
085: protected void onKeyOrTypeDoesNotExistInContainer(
086: final Object componentKey, final Log target) {
087: log
088: .info("Could not find component "
089: + (componentKey instanceof Class ? ((Class) componentKey)
090: .getName()
091: : componentKey)
092: + " in container or parent container.");
093: }
094:
095: /**
096: * {@inheritDoc}
097: *
098: * @param visitor
099: *
100: * @see org.picocontainer.PicoContainer#accept(org.picocontainer.PicoVisitor)
101: */
102: public void accept(final PicoVisitor visitor) {
103: if (log.isDebugEnabled()) {
104: log.debug("Visiting Container " + delegate
105: + " with visitor " + visitor);
106: }
107: delegate.accept(visitor);
108: }
109:
110: /**
111: * {@inheritDoc}
112: *
113: * @param child
114: *
115: * @return
116: *
117: * @see org.picocontainer.MutablePicoContainer#addChildContainer(org.picocontainer.PicoContainer)
118: */
119: public MutablePicoContainer addChildContainer(
120: final PicoContainer child) {
121: if (log.isDebugEnabled()) {
122: log.debug("Adding child container: " + child
123: + " to container " + delegate);
124: }
125: return delegate.addChildContainer(child);
126: }
127:
128: /**
129: * {@inheritDoc}
130: *
131: * @see org.picocontainer.Disposable#dispose()
132: */
133: public void dispose() {
134: if (log.isDebugEnabled()) {
135: log.debug("Disposing container " + delegate);
136: }
137: delegate.dispose();
138: }
139:
140: /**
141: * {@inheritDoc}
142: *
143: * @param componentKey
144: *
145: * @return
146: *
147: * @see org.picocontainer.PicoContainer#getComponentAdapter(java.lang.Object)
148: */
149: public ComponentAdapter<?> getComponentAdapter(
150: final Object componentKey) {
151: if (log.isDebugEnabled()) {
152: log.debug("Locating component adapter with key "
153: + componentKey);
154: }
155:
156: ComponentAdapter adapter = delegate
157: .getComponentAdapter(componentKey);
158: if (adapter == null) {
159: onKeyOrTypeDoesNotExistInContainer(componentKey, log);
160: }
161: return adapter;
162: }
163:
164: /**
165: * {@inheritDoc}
166: *
167: * @param componentType
168: *
169: * @return ComponentAdapter or null.
170: *
171: * @see org.picocontainer.PicoContainer#getComponentAdapter(java.lang.Class)
172: */
173:
174: public <T> ComponentAdapter<T> getComponentAdapter(
175: Class<T> componentType, NameBinding componentNameBinding) {
176: if (log.isDebugEnabled()) {
177: log.debug("Locating component adapter with type "
178: + componentType);
179: }
180:
181: ComponentAdapter<T> ca = delegate.getComponentAdapter(
182: componentType, componentNameBinding);
183:
184: if (ca == null) {
185: onKeyOrTypeDoesNotExistInContainer(ca, log);
186: }
187: return ca;
188: }
189:
190: /**
191: * {@inheritDoc}
192: *
193: * @return Collection or null.
194: *
195: * @see org.picocontainer.PicoContainer#getComponentAdapters()
196: */
197: public Collection<ComponentAdapter<?>> getComponentAdapters() {
198: if (log.isDebugEnabled()) {
199: log.debug("Grabbing all component adapters for container: "
200: + delegate);
201: }
202: return delegate.getComponentAdapters();
203: }
204:
205: /**
206: * {@inheritDoc}
207: *
208: * @param componentType
209: *
210: * @return List of ComponentAdapters
211: *
212: * @see org.picocontainer.PicoContainer#getComponentAdapters(java.lang.Class)
213: */
214: public <T> List<ComponentAdapter<T>> getComponentAdapters(
215: final Class<T> componentType) {
216: if (log.isDebugEnabled()) {
217: log
218: .debug("Grabbing all component adapters for container: "
219: + delegate
220: + " of type: "
221: + componentType.getName());
222: }
223: return delegate.getComponentAdapters(componentType);
224: }
225:
226: public <T> List<ComponentAdapter<T>> getComponentAdapters(
227: Class<T> componentType, Class<? extends Annotation> binding) {
228: if (log.isDebugEnabled()) {
229: log.debug("Grabbing all component adapters for container: "
230: + delegate + " of type: " + componentType.getName()
231: + ", binding:" + binding.getName());
232: }
233: return delegate.getComponentAdapters(componentType, binding);
234: }
235:
236: public <T> ComponentAdapter<T> getComponentAdapter(
237: Class<T> componentType, Class<? extends Annotation> binding) {
238: if (log.isDebugEnabled()) {
239: log.debug("Grabbing component adapter for container: "
240: + delegate + " of type: " + componentType.getName()
241: + ", binding:" + binding.getName());
242: }
243: return delegate.getComponentAdapter(componentType, binding);
244: }
245:
246: /**
247: * {@inheritDoc}
248: *
249: * @param componentKeyOrType
250: *
251: * @return
252: *
253: * @see org.picocontainer.PicoContainer#getComponent(java.lang.Object)
254: */
255: public Object getComponent(final Object componentKeyOrType) {
256:
257: if (log.isDebugEnabled()) {
258: log.debug("Attempting to load component instance with "
259: + (componentKeyOrType instanceof Class ? "type"
260: : "key") + ": " + componentKeyOrType
261: + " for container " + delegate);
262:
263: }
264:
265: Object result = delegate.getComponent(componentKeyOrType);
266: if (result == null) {
267: onKeyOrTypeDoesNotExistInContainer(componentKeyOrType, log);
268: }
269:
270: return result;
271: }
272:
273: public <T> T getComponent(Class<T> componentType) {
274: return componentType.cast(getComponent((Object) componentType));
275: }
276:
277: public <T> T getComponent(Class<T> componentType,
278: Class<? extends Annotation> binding) {
279: if (log.isDebugEnabled()) {
280: log.debug("Grabbing component for container: " + delegate
281: + " of type: " + componentType.getName()
282: + ", binding:" + binding.getName());
283: }
284: return delegate.getComponent(componentType, binding);
285: }
286:
287: /**
288: * {@inheritDoc}
289: *
290: * @return
291: *
292: * @see org.picocontainer.PicoContainer#getComponents()
293: */
294: public List getComponents() {
295: if (log.isDebugEnabled()) {
296: log
297: .debug("Retrieving all component instances for container "
298: + delegate);
299: }
300: return delegate.getComponents();
301: }
302:
303: /**
304: * {@inheritDoc}
305: *
306: * @param componentType
307: *
308: * @return
309: *
310: * @see org.picocontainer.PicoContainer#getComponents(java.lang.Class)
311: */
312: public <T> List<T> getComponents(final Class<T> componentType) {
313: if (log.isDebugEnabled()) {
314: log.debug("Loading all component instances of type "
315: + componentType + " for container " + delegate);
316: }
317: List<T> result = delegate.getComponents(componentType);
318: if (result == null || result.isEmpty()) {
319: if (log.isInfoEnabled()) {
320: log.info("Could not find any components "
321: + " in container or parent container.");
322: }
323: }
324:
325: return result;
326: }
327:
328: /**
329: * {@inheritDoc}
330: *
331: * @return
332: *
333: * @see org.picocontainer.PicoContainer#getParent()
334: */
335: public PicoContainer getParent() {
336: if (log.isDebugEnabled()) {
337: log
338: .debug("Retrieving the parent for container "
339: + delegate);
340: }
341:
342: return delegate.getParent();
343: }
344:
345: /**
346: * {@inheritDoc}
347: *
348: * @return
349: *
350: * @see org.picocontainer.MutablePicoContainer#makeChildContainer()
351: */
352: public MutablePicoContainer makeChildContainer() {
353: if (log.isDebugEnabled()) {
354: log.debug("Making child container for container "
355: + delegate);
356: }
357:
358: //Wrap the new delegate
359: return new Log4jTracingContainerDecorator(delegate
360: .makeChildContainer());
361: }
362:
363: /**
364: * {@inheritDoc}
365: *
366: * @param componentAdapter
367: *
368: * @return
369: *
370: * @see org.picocontainer.MutablePicoContainer#addAdapter(org.picocontainer.ComponentAdapter)
371: */
372: public MutablePicoContainer addAdapter(
373: final ComponentAdapter componentAdapter) {
374: if (log.isDebugEnabled()) {
375: log.debug("Registering component adapter "
376: + componentAdapter);
377: }
378:
379: return delegate.addAdapter(componentAdapter);
380: }
381:
382: /**
383: * {@inheritDoc}
384: *
385: * @param componentKey
386: * @param componentImplementationOrInstance
387: *
388: * @param parameters
389: *
390: * @return
391: */
392: public MutablePicoContainer addComponent(final Object componentKey,
393: final Object componentImplementationOrInstance,
394: final Parameter... parameters) {
395: if (log.isDebugEnabled()) {
396: log
397: .debug("Registering component "
398: + (componentImplementationOrInstance instanceof Class ? "implementation"
399: : "instance")
400: + " with key "
401: + componentKey
402: + " and implementation "
403: + (componentImplementationOrInstance instanceof Class ? ((Class) componentImplementationOrInstance)
404: .getCanonicalName()
405: : componentImplementationOrInstance
406: .getClass())
407: + " using parameters " + parameters);
408: }
409:
410: return delegate.addComponent(componentKey,
411: componentImplementationOrInstance, parameters);
412: }
413:
414: /**
415: * {@inheritDoc}
416: *
417: * @param implOrInstance
418: *
419: * @return
420: *
421: * @see org.picocontainer.MutablePicoContainer#addComponent(java.lang.Object)
422: */
423: public MutablePicoContainer addComponent(final Object implOrInstance) {
424: if (log.isDebugEnabled()) {
425: log.debug("Registering component impl or instance "
426: + implOrInstance
427: + "(class: "
428: + ((implOrInstance != null) ? implOrInstance
429: .getClass().getName() : " null "));
430: }
431:
432: return delegate.addComponent(implOrInstance);
433: }
434:
435: public MutablePicoContainer addConfig(String name, Object val) {
436: if (log.isDebugEnabled()) {
437: log.debug("Registering config: " + name);
438: }
439:
440: return delegate.addConfig(name, val);
441: }
442:
443: /**
444: * {@inheritDoc}
445: *
446: * @param child
447: *
448: * @return
449: *
450: * @see org.picocontainer.MutablePicoContainer#removeChildContainer(org.picocontainer.PicoContainer)
451: */
452: public boolean removeChildContainer(final PicoContainer child) {
453: if (log.isDebugEnabled()) {
454: log.debug("Removing child container: " + child
455: + " from parent: " + delegate);
456: }
457: return delegate.removeChildContainer(child);
458: }
459:
460: /**
461: * {@inheritDoc}
462: *
463: * @see org.picocontainer.Startable#start()
464: */
465: public void start() {
466: if (log.isInfoEnabled()) {
467: log.info("Starting Container " + delegate);
468: }
469:
470: delegate.start();
471: }
472:
473: /**
474: * {@inheritDoc}
475: *
476: * @see org.picocontainer.Startable#stop()
477: */
478: public void stop() {
479: if (log.isInfoEnabled()) {
480: log.info("Stopping Container " + delegate);
481: }
482: delegate.stop();
483: }
484:
485: /**
486: * {@inheritDoc}
487: *
488: * @param componentKey
489: *
490: * @return
491: *
492: * @see org.picocontainer.MutablePicoContainer#removeComponent(java.lang.Object)
493: */
494: public ComponentAdapter removeComponent(final Object componentKey) {
495: if (log.isDebugEnabled()) {
496: log.debug("Unregistering component " + componentKey
497: + " from container " + delegate);
498: }
499:
500: return delegate.removeComponent(componentKey);
501: }
502:
503: /**
504: * {@inheritDoc}
505: *
506: * @param componentInstance
507: *
508: * @return
509: *
510: * @see org.picocontainer.MutablePicoContainer#removeComponentByInstance(java.lang.Object)
511: */
512: public ComponentAdapter removeComponentByInstance(
513: final Object componentInstance) {
514: if (log.isDebugEnabled()) {
515: log.debug("Unregistering component by instance ("
516: + componentInstance + ") from container "
517: + delegate);
518: }
519:
520: return delegate.removeComponentByInstance(componentInstance);
521: }
522:
523: /**
524: * Retrieves the log instance used by this decorator.
525: *
526: * @return Logger instance.
527: */
528: public Log getLoggerUsed() {
529: return this .log;
530: }
531:
532: private void readObject(final java.io.ObjectInputStream s)
533: throws java.io.IOException,
534: java.lang.ClassNotFoundException {
535: s.defaultReadObject();
536: log = LogFactory.getLog(this .logCategory);
537: }
538:
539: public MutablePicoContainer change(Properties... properties) {
540: return delegate.change(properties);
541: }
542:
543: public MutablePicoContainer as(Properties... properties) {
544: return delegate.as(properties);
545: }
546: }
|