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: * @(#)AdminValueConverter.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * AdminValueConverter.java
031: *
032: * SUN PROPRIETARY/CONFIDENTIAL.
033: * This software is the proprietary information of Sun Microsystems, Inc.
034: * Use is subject to license terms.
035: *
036: * Created on November 29, 2005, 2:35 PM
037: */package jmx4ant;
038:
039: import org.apache.tools.ant.taskdefs.optional.jmx.converter.ValueConverter;
040: import org.apache.tools.ant.taskdefs.optional.jmx.converter.ValueFactory;
041: import java.util.ArrayList;
042:
043: /**
044: *
045: * @author Sun Microsystems, Inc.
046: */
047: public class AdminValueConverter implements ValueConverter {
048:
049: public String[] getSupportedTypes() {
050: return new String[] { "java.util.List" };
051: }
052:
053: public Object valueOf(String value, String type) throws Exception {
054: if (isTypeSupported(type)) {
055: // -- The only supported type is List
056: return convertStringToList(value);
057: } else {
058: throw new Exception("Unsupported Type : " + type);
059: }
060: }
061:
062: /**
063: * @return a List created from the String value.
064: */
065: private java.util.List convertStringToList(String value) {
066: ArrayList list = new ArrayList();
067: value = value.trim();
068:
069: if (value.charAt(0) == '['
070: && value.charAt(value.length() - 1) == ']') {
071: String[] values = (value.substring(1, value.length() - 1))
072: .split("\\s");
073: for (int i = 0; i < values.length; i++) {
074: list.add(values[i]);
075: }
076: }
077: return list;
078: }
079:
080: /**
081: * Check if this value Converter supports the type.
082: */
083: private boolean isTypeSupported(String type) {
084: String[] types = getSupportedTypes();
085: for (int i = 0; i < types.length; i++) {
086: if (types[i].equals(type)) {
087: return true;
088: }
089: }
090: return false;
091: }
092:
093: /**
094: * Set the ValueConverter
095: */
096: public static void main(String[] args) {
097: ValueConverter vc = new AdminValueConverter();
098: ValueFactory.getInstance().registerValueConverter(vc);
099:
100: /**
101: try
102: {
103: java.util.List list = (java.util.List) vc.valueOf("[CAS ESBMember]", "java.util.List");
104: System.out.println("Size : " + list.size());
105:
106: for ( java.util.Iterator itr = list.iterator(); itr.hasNext(); )
107: {
108: System.out.println("List item : " + (String)itr.next());
109: }
110: }
111: catch (Exception ex)
112: {
113:
114: }
115: **/
116: }
117: }
|