01: /**
02: * Copyright 2004 Sun Microsystems, Inc. All
03: * rights reserved. Use of this product is subject
04: * to license terms. Federal Acquisitions:
05: * Commercial Software -- Government Users
06: * Subject to Standard License Terms and
07: * Conditions.
08: *
09: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
10: * are trademarks or registered trademarks of Sun Microsystems,
11: * Inc. in the United States and other countries.
12: */package com.sun.ssoadapter;
13:
14: import java.util.logging.Logger;
15: import java.lang.reflect.Method;
16:
17: /**
18: * It uses Portal Logger if available else Java Logger
19: */
20: public class SSOAdapterLogger {
21:
22: public static String LOGGER_CLASS = "com.sun.portal.log.common.PortalLogger";
23: public static String METHOD_NAME = "getLogger";
24:
25: /**
26: * Returns the Logger for the specified name.
27: * This checks whether PortalLogger is in the same JVM.
28: * If its present, invokes getLogger method of the PortalLogger to get the Logger.
29: * If its not present, invokes the getLogger method of the JDK Logger to get the Logger
30: * by passing SSOAdapterLogMessages resource bundle.
31: * SSOAdapterLogMessages.properties is the concatenation of all logmsg.properties
32: * in the ssoadapter module.
33: *
34: * @param name the name of the logger
35: * @return the Logger
36: */
37: public static Logger getLogger(String name) {
38: try {
39: Class cls = SSOAdapterLogger.class.getClassLoader()
40: .loadClass(LOGGER_CLASS);
41: Method[] methodlist = cls.getDeclaredMethods();
42: for (int i = 0; i < methodlist.length; i++) {
43: Method method = methodlist[i];
44: if (method.getName().equals(METHOD_NAME)) {
45: if (method.getParameterTypes().length == 1
46: && ((Class[]) method.getParameterTypes())[0]
47: .getName().equals(
48: "java.lang.String")) {
49: return (Logger) (method.invoke(cls
50: .newInstance(), new Object[] { name }));
51: }
52: }
53: }
54: } catch (Exception e) {
55: System.err.println(e);
56: }
57: // The SSOAdapterLogMessages.properties is the ResourceBundle built by the ant target "mergelogmsg"
58: // and it is the concatenation of all logmsg.properties in the ssoadapter module
59: return Logger.getLogger(name, "SSOAdapterLogMessages");
60: }
61:
62: /**
63: * Returns the Logger for the class object.
64: * Derives the package name from the class object and uses it
65: * to create the Logger.
66: *
67: * @param cls a class object
68: * @return the Logger
69: */
70: public static Logger getLogger(Class cls) {
71: Package pkg = cls.getPackage();
72: String packageName = (pkg == null) ? "debug" : pkg.getName();
73: return getLogger(packageName);
74: }
75: }
|