01: /*****************************************************************************
02: * Copyright (C) PicoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: * Original code by Paul Hammaant *
09: *****************************************************************************/package org.picocontainer.monitors;
10:
11: import java.text.MessageFormat;
12: import java.lang.reflect.Constructor;
13: import java.lang.reflect.Method;
14: import java.lang.reflect.Field;
15: import java.lang.reflect.Member;
16:
17: import org.picocontainer.ComponentMonitor;
18:
19: /**
20: * An abstract {@link ComponentMonitor} which supports all the message formats.
21: *
22: * @author Mauro Talevi
23: */
24: public final class ComponentMonitorHelper {
25:
26: public final static String INSTANTIATING = "PicoContainer: instantiating {0}";
27: public final static String INSTANTIATED = "PicoContainer: instantiated {0} [{1} ms], component {2}, injected [{3}]";
28: public final static String INSTANTIATION_FAILED = "PicoContainer: instantiation failed: {0}, reason: {1}";
29: public final static String INVOKING = "PicoContainer: invoking {0} on {1}";
30: public final static String INVOKED = "PicoContainer: invoked {0} on {1} [{2} ms]";
31: public final static String INVOCATION_FAILED = "PicoContainer: invocation failed: {0} on {1}, reason: {2}";
32: public final static String LIFECYCLE_INVOCATION_FAILED = "PicoContainer: lifecycle invocation failed: {0} on {1}, reason: {2}";
33: public final static String NO_COMPONENT = "PicoContainer: No component for key: {0}";
34:
35: public static String format(String template, Object... arguments) {
36: return MessageFormat.format(template, arguments);
37: }
38:
39: public static String parmsToString(Object[] injected) {
40: StringBuffer sb = new StringBuffer();
41: for (int i = 0; i < injected.length; i++) {
42: String s = injected[i].getClass().getName();
43: sb.append(s);
44: if (i < injected.length - 1) {
45: sb.append(", ");
46: }
47: }
48: return sb.toString();
49: }
50:
51: public static String ctorToString(Constructor constructor) {
52: Class[] params = constructor.getParameterTypes();
53: StringBuffer sb = new StringBuffer(constructor.getName());
54: sb.append("(");
55: for (int i = 0; i < params.length; i++) {
56: String s = params[i].getName();
57: sb.append(s);
58: if (i < params.length - 1) {
59: sb.append(", ");
60: }
61: }
62: sb.append(")");
63: return sb.toString();
64: }
65:
66: public static String methodToString(Method method) {
67: Class[] params = method.getParameterTypes();
68: StringBuffer sb = new StringBuffer(method.getName());
69: sb.append("(");
70: for (int i = 0; i < params.length; i++) {
71: String s = params[i].getName();
72: sb.append(s);
73: if (i < params.length - 1) {
74: sb.append(", ");
75: }
76: }
77: sb.append(")");
78: return sb.toString();
79: }
80:
81: public static String memberToString(Member m) {
82: if (m instanceof Field) {
83: return toString((Field) m);
84: } else {
85: return methodToString((Method) m);
86: }
87: }
88:
89: public static String toString(Field field) {
90: StringBuffer sb = new StringBuffer(field.getName());
91: sb.append("(").append(field.getName()).append(")");
92: return sb.toString();
93: }
94:
95: }
|