Source Code Cross Referenced for Log4JComponentMonitor.java in  » Inversion-of-Control » PicoContainer » org » picocontainer » gems » monitors » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Inversion of Control » PicoContainer » org.picocontainer.gems.monitors 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 Paul Hammant                                             *
009:         *****************************************************************************/package org.picocontainer.gems.monitors;
010:
011:        import static org.picocontainer.monitors.ComponentMonitorHelper.methodToString;
012:        import static org.picocontainer.monitors.ComponentMonitorHelper.memberToString;
013:        import static org.picocontainer.monitors.ComponentMonitorHelper.ctorToString;
014:        import static org.picocontainer.monitors.ComponentMonitorHelper.parmsToString;
015:        import static org.picocontainer.monitors.ComponentMonitorHelper.format;
016:
017:        import java.io.Serializable;
018:        import java.lang.reflect.Constructor;
019:        import java.lang.reflect.Member;
020:        import java.lang.reflect.Method;
021:
022:        import org.apache.log4j.LogManager;
023:        import org.apache.log4j.Logger;
024:        import org.apache.log4j.Priority;
025:        import org.picocontainer.monitors.ComponentMonitorHelper;
026:        import org.picocontainer.monitors.NullComponentMonitor;
027:        import org.picocontainer.ComponentMonitor;
028:        import org.picocontainer.ComponentAdapter;
029:        import org.picocontainer.MutablePicoContainer;
030:        import org.picocontainer.PicoContainer;
031:
032:        /**
033:         * A {@link org.picocontainer.ComponentMonitor} which writes to a Log4J {@link org.apache.log4j.Logger} instance.
034:         * The Logger instance can either be injected or, if not set, the {@link LogManager LogManager}
035:         * will be used to retrieve it at every invocation of the monitor.
036:         *
037:         * @author Paul Hammant
038:         * @author Mauro Talevi
039:         */
040:        public class Log4JComponentMonitor implements  ComponentMonitor,
041:                Serializable {
042:
043:            /**
044:             * Serialization UUID.
045:             */
046:            private static final long serialVersionUID = 6974859796813059085L;
047:
048:            /**
049:             * Log4j Logger.
050:             */
051:            private Logger logger;
052:
053:            private final ComponentMonitor delegate;
054:
055:            /**
056:             * Creates a Log4JComponentMonitor with no Logger instance set.
057:             * The {@link LogManager LogManager} will be used to retrieve the Logger instance
058:             * at every invocation of the monitor.
059:             */
060:            public Log4JComponentMonitor() {
061:                delegate = new NullComponentMonitor();
062:            }
063:
064:            /**
065:             * Creates a Log4JComponentMonitor with a given Logger instance class.
066:             * The class name is used to retrieve the Logger instance.
067:             *
068:             * @param loggerClass the class of the Logger
069:             */
070:            public Log4JComponentMonitor(Class<?> loggerClass) {
071:                this (loggerClass.getName());
072:            }
073:
074:            /**
075:             * Creates a Log4JComponentMonitor with a given Logger instance name. It uses the
076:             * {@link org.apache.log4j.LogManager LogManager} to create the Logger instance.
077:             *
078:             * @param loggerName the name of the Log
079:             */
080:            public Log4JComponentMonitor(String loggerName) {
081:                this (LogManager.getLogger(loggerName));
082:            }
083:
084:            /**
085:             * Creates a Log4JComponentMonitor with a given Logger instance
086:             *
087:             * @param logger the Logger to write to
088:             */
089:            public Log4JComponentMonitor(Logger logger) {
090:                this ();
091:                this .logger = logger;
092:            }
093:
094:            /**
095:             * Creates a Log4JComponentMonitor with a given Logger instance class.
096:             * The class name is used to retrieve the Logger instance.
097:             *
098:             * @param loggerClass the class of the Logger
099:             * @param delegate the delegate
100:             */
101:            public Log4JComponentMonitor(Class<?> loggerClass,
102:                    ComponentMonitor delegate) {
103:                this (loggerClass.getName(), delegate);
104:            }
105:
106:            /**
107:             * Creates a Log4JComponentMonitor with a given Logger instance name. It uses the
108:             * {@link org.apache.log4j.LogManager LogManager} to create the Logger instance.
109:             *
110:             * @param loggerName the name of the Log
111:             * @param delegate the delegate
112:             */
113:            public Log4JComponentMonitor(String loggerName,
114:                    ComponentMonitor delegate) {
115:                this (LogManager.getLogger(loggerName), delegate);
116:            }
117:
118:            /**
119:             * Creates a Log4JComponentMonitor with a given Logger instance
120:             *
121:             * @param logger the Logger to write to
122:             * @param delegate the delegate
123:             */
124:            public Log4JComponentMonitor(Logger logger,
125:                    ComponentMonitor delegate) {
126:                this (delegate);
127:                this .logger = logger;
128:            }
129:
130:            public Log4JComponentMonitor(ComponentMonitor delegate) {
131:                this .delegate = delegate;
132:            }
133:
134:            public <T> Constructor<T> instantiating(PicoContainer container,
135:                    ComponentAdapter<T> componentAdapter,
136:                    Constructor<T> constructor) {
137:                Logger logger = getLogger(constructor);
138:                if (logger.isDebugEnabled()) {
139:                    logger.debug(format(ComponentMonitorHelper.INSTANTIATING,
140:                            ctorToString(constructor)));
141:                }
142:                return delegate.instantiating(container, componentAdapter,
143:                        constructor);
144:            }
145:
146:            public <T> void instantiated(PicoContainer container,
147:                    ComponentAdapter<T> componentAdapter,
148:                    Constructor<T> constructor, Object instantiated,
149:                    Object[] parameters, long duration) {
150:                Logger logger = getLogger(constructor);
151:                if (logger.isDebugEnabled()) {
152:                    logger.debug(format(ComponentMonitorHelper.INSTANTIATED,
153:                            ctorToString(constructor), duration, instantiated
154:                                    .getClass().getName(),
155:                            parmsToString(parameters)));
156:                }
157:                delegate.instantiated(container, componentAdapter, constructor,
158:                        instantiated, parameters, duration);
159:            }
160:
161:            public <T> void instantiationFailed(PicoContainer container,
162:                    ComponentAdapter<T> componentAdapter,
163:                    Constructor<T> constructor, Exception cause) {
164:                Logger logger = getLogger(constructor);
165:                if (logger.isEnabledFor(Priority.WARN)) {
166:                    logger.warn(format(
167:                            ComponentMonitorHelper.INSTANTIATION_FAILED,
168:                            ctorToString(constructor), cause.getMessage()),
169:                            cause);
170:                }
171:                delegate.instantiationFailed(container, componentAdapter,
172:                        constructor, cause);
173:            }
174:
175:            public void invoking(PicoContainer container,
176:                    ComponentAdapter<?> componentAdapter, Member member,
177:                    Object instance) {
178:                Logger logger = getLogger(member);
179:                if (logger.isDebugEnabled()) {
180:                    logger.debug(format(ComponentMonitorHelper.INVOKING,
181:                            memberToString(member), instance));
182:                }
183:                delegate
184:                        .invoking(container, componentAdapter, member, instance);
185:            }
186:
187:            public void invoked(PicoContainer container,
188:                    ComponentAdapter<?> componentAdapter, Method method,
189:                    Object instance, long duration) {
190:                Logger logger = getLogger(method);
191:                if (logger.isDebugEnabled()) {
192:                    logger.debug(format(ComponentMonitorHelper.INVOKED,
193:                            methodToString(method), instance, duration));
194:                }
195:                delegate.invoked(container, componentAdapter, method, instance,
196:                        duration);
197:            }
198:
199:            public void invocationFailed(Member member, Object instance,
200:                    Exception cause) {
201:                Logger logger = getLogger(member);
202:                if (logger.isEnabledFor(Priority.WARN)) {
203:                    logger.warn(format(
204:                            ComponentMonitorHelper.INVOCATION_FAILED,
205:                            memberToString(member), instance, cause
206:                                    .getMessage()), cause);
207:                }
208:                delegate.invocationFailed(member, instance, cause);
209:            }
210:
211:            public void lifecycleInvocationFailed(
212:                    MutablePicoContainer container,
213:                    ComponentAdapter<?> componentAdapter, Method method,
214:                    Object instance, RuntimeException cause) {
215:                Logger logger = getLogger(method);
216:                if (logger.isEnabledFor(Priority.WARN)) {
217:                    logger.warn(format(
218:                            ComponentMonitorHelper.LIFECYCLE_INVOCATION_FAILED,
219:                            methodToString(method), instance, cause
220:                                    .getMessage()), cause);
221:                }
222:                delegate.lifecycleInvocationFailed(container, componentAdapter,
223:                        method, instance, cause);
224:            }
225:
226:            public Object noComponentFound(MutablePicoContainer container,
227:                    Object componentKey) {
228:                Logger logger = this .logger != null ? this .logger : LogManager
229:                        .getLogger(ComponentMonitor.class);
230:                if (logger.isEnabledFor(Priority.WARN)) {
231:                    logger.warn(format(ComponentMonitorHelper.NO_COMPONENT,
232:                            componentKey));
233:                }
234:                return delegate.noComponentFound(container, componentKey);
235:
236:            }
237:
238:            protected Logger getLogger(Member member) {
239:                if (logger != null) {
240:                    return logger;
241:                }
242:                return LogManager.getLogger(member.getDeclaringClass());
243:            }
244:
245:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.