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: * @(#)PropertiesValueConverter.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * PropertiesValueConverter.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.Properties;
042:
043: /**
044: *
045: * @author Sun Microsystems, Inc.
046: */
047: public class PropertiesValueConverter implements ValueConverter {
048:
049: public String[] getSupportedTypes() {
050: return new String[] { "java.util.Properties" };
051: }
052:
053: public Object valueOf(String value, String type) throws Exception {
054: if (isTypeSupported(type)) {
055: // -- The only supported type is Properties
056: return convertStringToProperties(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.Properties convertStringToProperties(String value) {
066: Properties props = new Properties();
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(",");
073: for (int i = 0; i < values.length; i++) {
074: String nv[] = values[i].trim().split("=", 2);
075:
076: if (nv.length == 2) {
077: props.put(nv[0], nv[1]);
078: }
079: }
080: }
081: return props;
082: }
083:
084: /**
085: * Check if this value Converter supports the type.
086: */
087: private boolean isTypeSupported(String type) {
088: String[] types = getSupportedTypes();
089: for (int i = 0; i < types.length; i++) {
090: if (types[i].equals(type)) {
091: return true;
092: }
093: }
094: return false;
095: }
096:
097: /**
098: * Set the ValueConverter
099: */
100: public static void main(String[] args) {
101: ValueConverter vc = new PropertiesValueConverter();
102: ValueFactory.getInstance().registerValueConverter(vc);
103:
104: /**
105: try
106: {
107: java.util.Properties props = (java.util.Properties) vc.valueOf("{var1=[STRING]val1}", "java.util.Properties");
108:
109: PropertiesValueConverter vc = new PropertiesValueConverter();
110: java.util.Properties props = (java.util.Properties) vc.valueOf("{configurationName=adminConfig, connectionURL=http://localhost:8080/sun, securityPrincpal=admin, securityCredential=adminadmin, jndienv.1=n1=v1, jndienv.2=n2=v2 }", "java.util.Properties");
111: System.out.println(props.toString());
112: }
113: catch (Exception ex)
114: {
115: ex.printStackTrace();
116: }
117: */
118:
119: }
120: }
|