KickJava   Java API By Example, From Geeks To Geeks.

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


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/CreateAliasCmd.java,v 1.3 2005/12/25 03:45:34 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:34 $
28  */

29  
30 package com.sun.cli.jmx.cmd;
31
32 import javax.management.ObjectName JavaDoc;
33
34 import com.sun.cli.jmx.support.ResultsForGetSet;
35 import com.sun.cli.jmx.support.CLISupportMBeanProxy;
36 import com.sun.cli.util.stringifier.*;
37 import com.sun.cli.util.ExceptionUtil;
38
39 public class CreateAliasCmd extends JMXCmd
40 {
41         public
42     CreateAliasCmd( final CmdEnv env )
43     {
44         super( env );
45     }
46     
47     
48     
49         public String JavaDoc
50     getUsage()
51     {
52         return( CmdStrings.CREATE_ALIAS_HELP.toString() );
53     }
54     
55         public static String JavaDoc []
56     getNames( )
57     {
58         return( new String JavaDoc [] { "create-alias", "ca" } );
59     }
60     
61         private void
62     aliasCreationFailed( String JavaDoc name, Exception JavaDoc e)
63         throws Exception JavaDoc
64     {
65         final String JavaDoc value = getAliasMgr().resolveAlias( name );
66         
67         if ( value != null )
68         {
69             printError( "Failed to create alias: " + name + "(already exists with value " + value + ")" );
70             printError( "If you want to change it, delete it first." );
71         }
72         else if ( ExceptionUtil.getRootCause( e ) instanceof IllegalArgumentException JavaDoc )
73         {
74             printError( "Illegal alias name: " + name );
75         }
76         else
77         {
78             printError( "Failed to create alias: " + name );
79         }
80     }
81     
82     static private final String JavaDoc OPTIONS_INFO ="replace";
83         
84     
85         ArgHelper.OptionsInfo
86     getOptionInfo()
87         throws ArgHelper.IllegalOptionException
88     {
89         return( new ArgHelperOptionsInfo( OPTIONS_INFO ) );
90     }
91     
92         void
93     executeInternal()
94         throws Exception JavaDoc
95     {
96         final String JavaDoc [] pairs = getOperands();
97         
98         final boolean replace = getBoolean( "replace", Boolean.FALSE).booleanValue();
99         
100         for( int i = 0; i < pairs.length; ++i )
101         {
102             final String JavaDoc pair = pairs[ i ];
103             final int separatorIndex = pair.indexOf( '=' );
104             
105             if ( separatorIndex < 0 )
106             {
107                 printError( "Alias request must be of form name=value: " + pair );
108                 continue;
109             }
110             
111             final String JavaDoc name = pair.substring( 0,separatorIndex);
112             final String JavaDoc value = pair.substring( separatorIndex + 1, pair.length() );
113             
114             try
115             {
116                 if ( replace )
117                 {
118                     getAliasMgr().deleteAlias( name );
119                 }
120                 
121                 getAliasMgr().createAlias( name, value );
122                 printError( "Created alias: " + name );
123             }
124             catch( Exception JavaDoc e )
125             {
126                 aliasCreationFailed( name, e);
127             }
128         }
129     }
130 }
131
Popular Tags