001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestRuntimeLoggerMethods.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package java4ant;
030:
031: import java.util.Map;
032: import java.util.logging.Level;
033:
034: import javax.management.remote.*;
035: import javax.management.*;
036:
037: /**
038: */
039: public class TestRuntimeLoggerMethods {
040:
041: private MBeanServerConnection mbns;
042: private static final String RESULT_PREFIX = "##### Result of ";
043:
044: private static final String USER = "jmx.user";
045: private static final String PASSWORD = "jmx.password";
046: private static final String PROVIDER = "jmx.provider";
047: private static final String TARGET = "target";
048:
049: private String target;
050:
051: public void initMBeanServerConnection() throws Exception {
052: java.util.Map<String, String[]> env = new java.util.HashMap();
053: String user = System.getProperty(USER);
054: String pass = System.getProperty(PASSWORD);
055: String[] credentials = new String[] { user, pass };
056: env.put("jmx.remote.credentials", credentials);
057:
058: String jmxProvider = System.getProperty(PROVIDER);
059:
060: JMXConnector connector = JMXConnectorFactory.connect(
061: new JMXServiceURL(jmxProvider), env);
062:
063: mbns = connector.getMBeanServerConnection();
064: }
065:
066: public ObjectName getJbiAdminCommandsUI() throws Exception {
067: String admin = "com.sun.jbi:"
068: + "ServiceName=JbiReferenceAdminUiService,ComponentType=System";
069: return new ObjectName(admin);
070: }
071:
072: public void getRuntimeLoggerLevels() throws Exception {
073: target = System.getProperty(TARGET, "server");
074:
075: Object[] params = new Object[2];
076: params[0] = target;
077: params[1] = target;
078:
079: String[] signature = new String[2];
080: signature[0] = "java.lang.String";
081: signature[1] = "java.lang.String";
082:
083: Map<String, Level> loggers = (Map<String, Level>) mbns.invoke(
084: getJbiAdminCommandsUI(), "getRuntimeLoggerLevels",
085: params, signature);
086:
087: System.out.println(RESULT_PREFIX + " getRuntimeLoggerLevels ="
088: + loggers);
089: }
090:
091: public void getRuntimeLoggerLevel(String logger,
092: boolean expectingException) {
093: target = System.getProperty(TARGET, "server");
094:
095: Object[] params = new Object[3];
096: params[0] = logger;
097: params[1] = target;
098: params[2] = target;
099:
100: String[] signature = new String[3];
101: signature[0] = "java.lang.String";
102: signature[1] = "java.lang.String";
103: signature[2] = "java.lang.String";
104:
105: try {
106: Level level = (Level) mbns.invoke(getJbiAdminCommandsUI(),
107: "getRuntimeLoggerLevel", params, signature);
108:
109: System.out.println(RESULT_PREFIX
110: + " getRuntimeLoggerLevel =" + level);
111: if (expectingException) {
112: System.out
113: .println("*** Was expecting exception, but didn't get one! ***");
114: }
115: } catch (Exception e) {
116: if (expectingException) {
117: System.out.println(RESULT_PREFIX
118: + " getRuntimeLoggerLevel: "
119: + "caught expected exception.");
120: } else {
121: System.out.println(RESULT_PREFIX
122: + " getRuntimeLoggerLevel: Exception: "
123: + e.getMessage());
124: }
125: }
126: }
127:
128: public void setRuntimeLoggerLevel(String logger, Level level,
129: boolean expectingException) {
130: target = System.getProperty(TARGET, "server");
131:
132: Object[] params = new Object[3];
133: params[0] = logger;
134: params[1] = level;
135: params[2] = target;
136:
137: String[] signature = new String[3];
138: signature[0] = "java.lang.String";
139: signature[1] = "java.util.logging.Level";
140: signature[2] = "java.lang.String";
141:
142: try {
143: mbns.invoke(getJbiAdminCommandsUI(),
144: "setRuntimeLoggerLevel", params, signature);
145:
146: System.out.println(RESULT_PREFIX
147: + " setRuntimeLoggerLevel() called.");
148: if (expectingException) {
149: System.out
150: .println("*** Was expecting exception, but didn't get one! ***");
151: }
152: } catch (Exception e) {
153: if (expectingException) {
154: System.out.println(RESULT_PREFIX
155: + " setRuntimeLoggerLevel: "
156: + "caught expected exception.");
157: } else {
158: System.out.println(RESULT_PREFIX
159: + " setRuntimeLoggerLevel: Exception: "
160: + e.getMessage());
161: e.printStackTrace();
162: }
163: }
164: }
165:
166: public void getRuntimeLoggerNames() throws Exception {
167: target = System.getProperty(TARGET, "server");
168:
169: Object[] params = new Object[2];
170: params[0] = target;
171: params[1] = target;
172:
173: String[] signature = new String[2];
174: signature[0] = "java.lang.String";
175: signature[1] = "java.lang.String";
176:
177: Map<String, String> loggers = (Map<String, String>) mbns
178: .invoke(getJbiAdminCommandsUI(),
179: "getRuntimeLoggerNames", params, signature);
180:
181: System.out.println(RESULT_PREFIX + " getRuntimeLoggerNames ="
182: + loggers);
183: }
184:
185: public String getRuntimeLoggerDisplayName(String logger,
186: boolean expectingException) {
187: target = System.getProperty(TARGET, "server");
188:
189: Object[] params = new Object[3];
190: params[0] = logger;
191: params[1] = target;
192: params[2] = target;
193:
194: String[] signature = new String[3];
195: signature[0] = "java.lang.String";
196: signature[1] = "java.lang.String";
197: signature[2] = "java.lang.String";
198:
199: String name = null;
200: try {
201: name = (String) mbns.invoke(getJbiAdminCommandsUI(),
202: "getRuntimeLoggerDisplayName", params, signature);
203:
204: System.out.println(RESULT_PREFIX
205: + " getRuntimeLoggerDisplayName =" + name);
206: if (expectingException) {
207: System.out
208: .println("*** Was expecting exception, but didn't get one! ***");
209: }
210:
211: } catch (Exception e) {
212: if (expectingException) {
213: System.out.println(RESULT_PREFIX
214: + " getRuntimeLoggerDisplayName: "
215: + "caught expected exception.");
216: } else {
217: System.out.println(RESULT_PREFIX
218: + " getRuntimeLoggerDisplayName: Exception: "
219: + e.getMessage());
220: }
221: }
222: return name;
223: }
224:
225: public static void main(String[] params) throws Exception {
226: TestRuntimeLoggerMethods test = new TestRuntimeLoggerMethods();
227: String loggerName = "com.sun.jbi.framework";
228:
229: test.initMBeanServerConnection();
230: test.getRuntimeLoggerLevels();
231: test.getRuntimeLoggerLevel(loggerName, false);
232: test.setRuntimeLoggerLevel(loggerName, Level.FINER, false);
233: test.getRuntimeLoggerLevel(loggerName, false);
234: test.setRuntimeLoggerLevel(loggerName, Level.INFO, false);
235: test.getRuntimeLoggerLevel(loggerName, false);
236: test.getRuntimeLoggerLevel("bogus logger", true);
237: test.setRuntimeLoggerLevel("bogus logger", Level.ALL, true);
238: test.getRuntimeLoggerNames();
239: test.getRuntimeLoggerDisplayName(loggerName, false);
240: test.getRuntimeLoggerDisplayName("bogus logger", true);
241: }
242:
243: }
|