KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > cmd > ConnectInfo


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/cmd/ConnectInfo.java,v 1.3 2005/12/25 03:45:33 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:33 $
28  */

29  
30
31 package com.sun.cli.jmx.cmd;
32
33 import javax.management.remote.JMXServiceURL JavaDoc;
34 import javax.management.remote.JMXConnector JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36
37 import java.util.Map JavaDoc;
38 import java.util.HashMap JavaDoc;
39
40 import com.sun.cli.jmx.support.ArgParserImpl;
41 import com.sun.cli.jmx.support.ArgParserException;
42
43
44 public final class ConnectInfo
45 {
46     public final Map JavaDoc mParams;
47     
48     public final static char ESCAPE_CHAR = '\\';
49     public final static char VALUE_DELIM = '=';
50     public final static char PAIR_DELIM = ',';
51     
52     
53     /*
54         A connect string consists of a series of name/value pairs eg:
55         host=myhost,port=9993,user=foo
56     */

57         static Map JavaDoc
58     connectStringToParams( final String JavaDoc connectString )
59         throws ArgParserException
60     {
61         final Map JavaDoc m = new java.util.HashMap JavaDoc();
62         
63         final ArgParserImpl.ParseChars parseChars = new ArgParserImpl.ParseChars();
64         parseChars.mArgDelim = PAIR_DELIM;
65         final ArgParserImpl parser = new ArgParserImpl();
66         
67         final String JavaDoc [] pairs = parser.ParseNames( connectString );
68         
69         for( int i = 0; i < pairs.length; ++i )
70         {
71             final String JavaDoc pair = pairs[ i ];
72             final int delimIndex = pair.indexOf( VALUE_DELIM );
73             
74             if ( delimIndex <= 0 )
75             {
76                 throw new IllegalArgumentException JavaDoc( "Illegal connect string: " + connectString );
77             }
78                 
79                 
80             final String JavaDoc name = pair.substring( 0, delimIndex );
81             final String JavaDoc value = pair.substring( delimIndex + 1, pair.length() );
82             
83             m.put( name, value );
84         }
85
86         return( m );
87     }
88
89         public
90     ConnectInfo( final String JavaDoc connectString )
91         throws ArgParserException
92     {
93         this( connectStringToParams( connectString ) );
94     }
95     
96         public
97     ConnectInfo( final Map JavaDoc params )
98         throws ArgParserException
99     {
100         mParams = params;
101     }
102
103     
104         public
105     ConnectInfo( final ConnectInfo rhs )
106     {
107         mParams = new HashMap JavaDoc();
108         mParams.putAll( rhs.mParams );
109     }
110     
111
112         static String JavaDoc
113     escapeString( char charToEscape, String JavaDoc stringToEscape )
114     {
115         String JavaDoc result = stringToEscape;
116         
117         if ( result.indexOf( charToEscape ) >= 0 )
118         {
119             result = result.replaceAll( "" + charToEscape,
120                                 ESCAPE_CHAR + "" + charToEscape );
121         }
122         return( result );
123     }
124     
125         static String JavaDoc
126     escapeString( String JavaDoc value )
127     {
128         String JavaDoc escapedValue = escapeString( PAIR_DELIM, value );
129         escapedValue = escapeString( VALUE_DELIM, escapedValue );
130         
131         return( escapedValue );
132     }
133     
134         String JavaDoc
135     paramsToString( )
136     {
137         final java.util.Iterator JavaDoc iter = mParams.keySet().iterator();
138         
139         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
140         while ( iter.hasNext() )
141         {
142             final String JavaDoc key = (String JavaDoc)iter.next();
143             final String JavaDoc value =(String JavaDoc)mParams.get( key );
144             
145             final String JavaDoc pair = key + VALUE_DELIM + escapeString( value );
146             
147             buf.append( PAIR_DELIM + pair );
148         }
149         
150         String JavaDoc result = buf.toString();
151         if ( result.length() != 0 )
152         {
153             // strip leading ":"
154
result = result.substring( 1, result.length() );
155         }
156         
157         return( result );
158     }
159     
160         String JavaDoc
161     getParam( String JavaDoc name )
162     {
163         return( (String JavaDoc)mParams.get( name ) );
164     }
165         
166         public String JavaDoc
167     toString()
168     {
169         return( paramsToString() );
170     }
171     
172     
173         public boolean
174     equals( Object JavaDoc o )
175     {
176         if ( o == this )
177         {
178             return( true );
179         }
180             
181         if ( ! (o instanceof ConnectInfo ) )
182         {
183             return( false );
184         }
185         
186         final ConnectInfo rhs = (ConnectInfo)o;
187         
188         return( mParams.equals( rhs.mParams ) );
189     }
190 }
191
192
193
194
195
196
197
198
Popular Tags