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: * @(#)TestComponentConfiguration.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.Set;
033: import javax.management.remote.*;
034: import javax.management.*;
035: import javax.management.openmbean.*;
036:
037: /**
038: */
039: public class TestComponentConfiguration {
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: private static final String COMPONENT_NAME = "component.name";
049: private static final String LOGGER_NAME = "logger.name";
050:
051: /** Application Variable Composite Type */
052: private static CompositeType sAppVarComposite;
053:
054: /** Application Variable Tabular Type */
055: private static TabularType sAppVarTable;
056:
057: /** Application Variable Composite Item Names */
058: private static String[] sAppVarItemNames;
059:
060: /** The index of the Application Variables **/
061: private static String[] sappVarRowIndex;
062:
063: /** The application configuration TabularData */
064: private TabularDataSupport mAppConfigs;
065:
066: /** Application Configuration Composite Type */
067: private static CompositeType sAppConfigComposite;
068:
069: /** Application Configuration Tabular Type */
070: private static TabularType sAppConfigTable;
071:
072: /** Application Configuration Composite Item Names */
073: private static String[] sAppConfigItemNames;
074:
075: /** The index of the Application Configurations **/
076: private static String[] sAppConfigRowIndex;
077:
078: public TestComponentConfiguration() {
079: try {
080: initOpenTypes();
081: initMBeanServerConnection();
082: } catch (Exception ex) {
083: System.out.println(ex.getMessage());
084: }
085: }
086:
087: private void initOpenTypes()
088: throws javax.management.openmbean.OpenDataException {
089: // Define the Application Variables CompositeType
090: sAppVarItemNames = new String[] { "name", "value", "type" };
091:
092: String[] appVarItemDesc = { "Application variable name",
093: "Application variable value",
094: "Application variable type" };
095:
096: OpenType[] appVarRowAttrTypes = { SimpleType.STRING,
097: SimpleType.STRING, SimpleType.STRING };
098:
099: sappVarRowIndex = new String[] { "name" };
100:
101: sAppVarComposite = new CompositeType(
102: "ApplicationVariableComposite",
103: "Application variable name and value pair",
104: sAppVarItemNames, appVarItemDesc, appVarRowAttrTypes);
105:
106: sAppVarTable = new TabularType("ApplicationVariableList",
107: "List of application variables", sAppVarComposite,
108: sappVarRowIndex);
109:
110: // Define the Application Configuration OpenType
111: sAppConfigItemNames = new String[] { "configurationName",
112: "connectionURL", "securityPrincipal",
113: "securityCredential", "jndienv" };
114:
115: String[] appConfigItemDesc = {
116: "Application configuration name", "Connection URL",
117: "Security Principal", "Security Credential",
118: "JNDI Properties" };
119:
120: ArrayType jndiEnvArrayType = new ArrayType(1, SimpleType.STRING);
121: OpenType[] appConfigRowAttrTypes = { SimpleType.STRING,
122: SimpleType.STRING, SimpleType.STRING,
123: SimpleType.STRING, jndiEnvArrayType };
124:
125: sAppConfigRowIndex = new String[] { "configurationName" };
126:
127: sAppConfigComposite = new CompositeType(
128: "ApplicationConfigurationComposite",
129: "Application Configuration Composite",
130: sAppConfigItemNames, appConfigItemDesc,
131: appConfigRowAttrTypes);
132:
133: sAppConfigTable = new TabularType(
134: "ApplicationConfigurationList",
135: "List of application configurations",
136: sAppConfigComposite, sAppConfigRowIndex);
137: }
138:
139: public void initMBeanServerConnection() throws Exception {
140: java.util.Map<String, String[]> env = new java.util.HashMap();
141: String user = System.getProperty(USER);
142: String pass = System.getProperty(PASSWORD);
143: String[] credentials = new String[] { user, pass };
144: env.put("jmx.remote.credentials", credentials);
145:
146: String jmxProvider = System.getProperty(PROVIDER);
147:
148: JMXConnector connector = JMXConnectorFactory.connect(
149: new JMXServiceURL(jmxProvider), env);
150:
151: mbns = connector.getMBeanServerConnection();
152: }
153:
154: public ObjectName getComponentConfigurationName() throws Exception {
155: String info = "com.sun.jbi:Target="
156: + System.getProperty(TARGET, "domain")
157: + ",ComponentName="
158: + System.getProperty(COMPONENT_NAME)
159: + ",ServiceType=Configuration";
160: return new ObjectName(info);
161: }
162:
163: public void queryMBeans() throws Exception {
164: java.util.Set names = mbns.queryMBeans(new ObjectName(
165: "com.sun.jbi:Target=server,*"), null);
166:
167: java.util.Iterator itr = names.iterator();
168:
169: while (itr.hasNext()) {
170: System.out.println(((ObjectInstance) itr.next())
171: .getObjectName());
172: }
173:
174: }
175:
176: public void setAttribute(String attrName, Object value)
177: throws Exception {
178: mbns.setAttribute(getComponentConfigurationName(),
179: new Attribute(attrName, value));
180:
181: System.out.println(RESULT_PREFIX + " setAttribute = "
182: + "SUCCESS");
183: }
184:
185: public void setAttributes(AttributeList attribs) throws Exception {
186: mbns.setAttributes(getComponentConfigurationName(), attribs);
187:
188: System.out.println(RESULT_PREFIX + " setAttributes = "
189: + "SUCCESS");
190: }
191:
192: public void getAttribute(String attrName) throws Exception {
193: Object attribValue = (Object) mbns.getAttribute(
194: getComponentConfigurationName(), attrName);
195:
196: System.out.println(RESULT_PREFIX + " getAttribute = "
197: + attribValue.toString());
198: }
199:
200: public void getAttributes(String[] attrNames) throws Exception {
201: AttributeList attribs = (AttributeList) mbns.getAttributes(
202: getComponentConfigurationName(), attrNames);
203:
204: System.out.print(RESULT_PREFIX + " getAttributes = ");
205:
206: for (int i = 0; i < attribs.size(); i++) {
207: Attribute attrib = (Attribute) attribs.get(i);
208: System.out.print(attrib.getName() + "=" + attrib.getValue()
209: + ", ");
210: }
211: System.out.println("");
212: }
213:
214: public void setConfigurationAttribute(String attrName, Object value)
215: throws Exception {
216: String result = (String) mbns.invoke(
217: getComponentConfigurationName(),
218: "setConfigurationAttribute",
219: new Object[] { new Attribute(attrName, value) },
220: new String[] { "javax.management.Attribute" });
221:
222: // replace "en_US" with "en"
223: result = result.replace("en_US", "en");
224: System.out.println(RESULT_PREFIX
225: + " setConfigurationAttribute = " + result);
226: }
227:
228: public void setConfigurationAttributes(AttributeList attribs)
229: throws Exception {
230: String result = (String) mbns.invoke(
231: getComponentConfigurationName(),
232: "setConfigurationAttributes", new Object[] { attribs },
233: new String[] { "javax.management.AttributeList" });
234:
235: // replace "en_US" with "en"
236: result = result.replace("en_US", "en");
237: System.out.println(RESULT_PREFIX
238: + " setConfigurationAttributes = " + result);
239: }
240:
241: /**
242: * List the Attributes in the MBean
243: */
244: public void getMBeanAttributeInfo(ObjectName mbeanName)
245: throws Exception {
246: MBeanInfo mbeanInfo = mbns.getMBeanInfo(mbeanName);
247:
248: if (mbeanInfo != null) {
249: System.out.println("Attribute Information for MBean "
250: + mbeanName);
251:
252: MBeanAttributeInfo[] attribs = mbeanInfo.getAttributes();
253:
254: for (MBeanAttributeInfo attrib : attribs) {
255: System.out.println("\nName : " + attrib.getName()
256: + " Type : " + attrib.getType());
257: }
258: }
259:
260: }
261:
262: /**
263: *
264: */
265: public void addApplicationVariable(String name, String value,
266: String type) throws Exception {
267: CompositeData appVar = new CompositeDataSupport(
268: sAppVarComposite, sAppVarItemNames, new String[] {
269: name, value, type });
270:
271: String result = (String) mbns.invoke(
272: getComponentConfigurationName(),
273: "addApplicationVariable",
274: new Object[] { name, appVar }, new String[] {
275: "java.lang.String",
276: "javax.management.openmbean.CompositeData" });
277:
278: // replace "en_US" with "en"
279: result = result.replace("en_US", "en");
280: System.out.println(RESULT_PREFIX + " addApplicationVariable = "
281: + result);
282: }
283:
284: /**
285: *
286: */
287: public void setApplicationVariable(String name, String value,
288: String type) throws Exception {
289: CompositeData appVar = new CompositeDataSupport(
290: sAppVarComposite, sAppVarItemNames, new String[] {
291: name, value, type });
292:
293: String result = (String) mbns.invoke(
294: getComponentConfigurationName(),
295: "setApplicationVariable",
296: new Object[] { name, appVar }, new String[] {
297: "java.lang.String",
298: "javax.management.openmbean.CompositeData" });
299:
300: // replace "en_US" with "en"
301: result = result.replace("en_US", "en");
302: System.out.println(RESULT_PREFIX + " setApplicationVariable = "
303: + result);
304: }
305:
306: /**
307: *
308: */
309: public void deleteApplicationVariable(String name) throws Exception {
310: String result = (String) mbns.invoke(
311: getComponentConfigurationName(),
312: "deleteApplicationVariable", new Object[] { name },
313: new String[] { "java.lang.String" });
314:
315: // replace "en_US" with "en"
316: result = result.replace("en_US", "en");
317: System.out.println(RESULT_PREFIX
318: + " deleteApplicationVariable = " + result);
319: }
320:
321: /**
322: *
323: */
324: public void getApplicationVariables() throws Exception {
325: TabularData appVars = (TabularData) mbns.invoke(
326: getComponentConfigurationName(),
327: "getApplicationVariables", new Object[] {},
328: new String[] {});
329:
330: System.out.println(RESULT_PREFIX
331: + " getApplicationVariables = ");
332:
333: Set names = appVars.keySet();
334:
335: for (Object name : names) {
336: Object[] key = ((java.util.List) name).toArray();
337:
338: CompositeData cd = appVars.get(key);
339:
340: if (cd != null) {
341: String appVarName = (String) cd.get("name");
342: String appVarValue = (String) cd.get("value");
343: String appVarType = (String) cd.get("type");
344:
345: if (appVarValue == null || appVarType == null) {
346: throw new Exception("Illegal CompositeData Values");
347: }
348:
349: System.out.println(" Name : " + appVarName
350: + " Value : " + appVarValue + " Type : "
351: + appVarType);
352: }
353: }
354: }
355:
356: /**
357: *
358: */
359: public void getApplicationConfigurations() throws Exception {
360: TabularData appCfgs = (TabularData) mbns.invoke(
361: getComponentConfigurationName(),
362: "getApplicationConfigurations", new Object[] {},
363: new String[] {});
364:
365: System.out.println(RESULT_PREFIX
366: + " getApplicationConfigurations = ");
367:
368: Set names = appCfgs.keySet();
369:
370: for (Object name : names) {
371: Object[] key = ((java.util.List) name).toArray();
372:
373: CompositeData cd = appCfgs.get(key);
374:
375: if (cd != null) {
376: System.out.println("Application Configuration : "
377: + cd.get("configurationName"));
378: System.out.println("\tconnectionURL = "
379: + (String) cd.get("connectionURL"));
380: System.out.println("\tsecurityPrincipal = "
381: + (String) cd.get("securityPrincipal"));
382: System.out.println("\tsecurityCredential = "
383: + (String) cd.get("securityCredential"));
384: System.out.println("\tjndienv = ");
385: String[] jndienv = (String[]) cd.get("jndienv");
386: System.out.print("\t\t[ ");
387: for (String env : jndienv) {
388: System.out.print(env + " ");
389: }
390: System.out.println(" ]");
391: }
392: }
393: }
394:
395: /**
396: *
397: */
398: public void addApplicationConfiguration() throws Exception {
399: CompositeData appConfig = new CompositeDataSupport(
400: sAppConfigComposite, sAppConfigItemNames, new Object[] {
401: "newAppConfig", "http://www.google.com",
402: "admin", "adminadmin",
403: new String[] { "var1", "var2", "var3" } });
404:
405: String result = (String) mbns.invoke(
406: getComponentConfigurationName(),
407: "addApplicationConfiguration", new Object[] {
408: "newAppConfig", appConfig }, new String[] {
409: "java.lang.String",
410: "javax.management.openmbean.CompositeData" });
411:
412: // replace "en_US" with "en"
413: result = result.replace("en_US", "en");
414: System.out.println(RESULT_PREFIX
415: + " addApplicationConfiguration = " + result);
416: }
417:
418: /**
419: *
420: */
421: public void setApplicationConfiguration() throws Exception {
422: CompositeData appConfig = new CompositeDataSupport(
423: sAppConfigComposite, sAppConfigItemNames, new Object[] {
424: "newAppConfig",
425: "http://www.youtube.com",
426: "su",
427: "suPass",
428: new String[] { "var1=value1", "var2=value2",
429: "var3=value3" } });
430:
431: String result = (String) mbns.invoke(
432: getComponentConfigurationName(),
433: "setApplicationConfiguration", new Object[] {
434: "newAppConfig", appConfig }, new String[] {
435: "java.lang.String",
436: "javax.management.openmbean.CompositeData" });
437:
438: // replace "en_US" with "en"
439: result = result.replace("en_US", "en");
440: System.out.println(RESULT_PREFIX
441: + " setApplicationConfiguration = " + result);
442: }
443:
444: /**
445: *
446: */
447: public void deleteApplicationConfiguration() throws Exception {
448:
449: String result = (String) mbns.invoke(
450: getComponentConfigurationName(),
451: "deleteApplicationConfiguration",
452: new Object[] { "newAppConfig" },
453: new String[] { "java.lang.String" });
454:
455: // replace "en_US" with "en"
456: result = result.replace("en_US", "en");
457: System.out.println(RESULT_PREFIX
458: + " deleteApplicationConfiguration = " + result);
459: }
460:
461: /**
462: *
463: */
464: public void queryApplicationConfigurationType() throws Exception {
465:
466: CompositeType ct = (CompositeType) mbns.invoke(
467: getComponentConfigurationName(),
468: "queryApplicationConfigurationType", new Object[] {},
469: new String[] {});
470:
471: System.out.println(RESULT_PREFIX
472: + " queryApplicationConfigurationType = "
473: + ct.toString());
474: }
475:
476: public static void main(String[] params) throws Exception {
477: TestComponentConfiguration test = new TestComponentConfiguration();
478:
479: //test.queryMBeans();
480: //test.getMBeanAttributeInfo(test.getComponentConfigurationName());
481:
482: // Simple get/set
483:
484: test.setAttribute("HostName", "Mystique01");
485: test.getAttribute("HostName");
486:
487: test.setAttribute("ProxyPassword", "secret");
488: test.getAttribute("ProxyPassword");
489:
490: // get/setAttributes
491: AttributeList attrList = new AttributeList();
492: attrList.add(new Attribute("HostName", "Magnetto"));
493: attrList.add(new Attribute("Port", 6789));
494: test.setAttributes(attrList);
495: test.getAttributes(new String[] { "HostName", "Port" });
496:
497: // setConfigurationAttribute
498: test.setConfigurationAttribute("Port", 10789);
499: test.getAttribute("Port");
500:
501: // setConfigurationAttributes
502: attrList = new AttributeList();
503: attrList.add(new Attribute("HostName", "Wolverine"));
504: attrList.add(new Attribute("Port", 1234));
505: test.setConfigurationAttributes(attrList);
506: test.getAttributes(new String[] { "HostName", "Port",
507: "ProxyPassword" });
508:
509: // Application Variable operations
510: test.getApplicationVariables();
511: test.addApplicationVariable("Location", "http://www.sun.com",
512: "STRING");
513: test.addApplicationVariable("SecurityCredential",
514: "http://www.sun.com", "PASSWORD");
515: test.getApplicationVariables();
516: test.setApplicationVariable("Location",
517: "http://www.google.com", "STRING");
518: test.getApplicationVariables();
519: test.deleteApplicationVariable("Location");
520: test.getApplicationVariables();
521:
522: // Application configuration operations
523: test.queryApplicationConfigurationType();
524: test.getApplicationConfigurations();
525: test.addApplicationConfiguration();
526: test.getApplicationConfigurations();
527: test.setApplicationConfiguration();
528: test.getApplicationConfigurations();
529: test.deleteApplicationConfiguration();
530: test.getApplicationConfigurations();
531: }
532: }
|