KickJava   Java API By Example, From Geeks To Geeks.

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


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

29  
30 package com.sun.cli.jmx.cmd;
31
32 import java.util.HashMap JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 import com.sun.cli.jmx.support.CLISupportMBeanProxy;
37 import com.sun.cli.util.ClassUtil;
38 import com.sun.cli.util.stringifier.SmartStringifier;
39
40 public class CmdFactory
41 {
42     private final HashMap JavaDoc mCmds;
43     
44     public static final String JavaDoc DEFAULT_CMD_NAME = "DEFAULT_CMD";
45     
46         private static void
47     p( Object JavaDoc o )
48     {
49         System.out.println( SmartStringifier.toString( o ) );
50     }
51     
52         public
53     CmdFactory()
54     {
55         mCmds = new HashMap JavaDoc();
56     }
57     
58         public void
59     addCmdMapping( String JavaDoc name, Class JavaDoc theClass )
60     {
61         if ( ! Cmd.class.isAssignableFrom( theClass ) )
62         {
63             throw new IllegalArgumentException JavaDoc( "Command " + theClass.getName() +
64                 " must implement " + Cmd.class.getName() );
65         }
66         
67         mCmds.put( name, theClass );
68     }
69     
70         public void
71     removeCmdMapping( String JavaDoc name )
72     {
73         mCmds.remove( name );
74     }
75     
76         public Cmd
77     createCmd( String JavaDoc name, CmdEnv env )
78         throws Exception JavaDoc
79     {
80         final String JavaDoc cmdString = name;
81         
82         final Cmd cmd = instantiateCmd( cmdString, env );
83         
84         return( cmd );
85     }
86     
87         public Class JavaDoc
88     getClass( String JavaDoc cmdName )
89         throws Exception JavaDoc
90     {
91         final Class JavaDoc cmdClass = (Class JavaDoc)mCmds.get( cmdName );
92         
93         return( cmdClass );
94     }
95     
96         public String JavaDoc []
97     getNames( Class JavaDoc theClass )
98     {
99         final ArrayList JavaDoc list = new ArrayList JavaDoc();
100         
101         final Iterator JavaDoc keys = mCmds.keySet().iterator();
102         
103         while( keys.hasNext() )
104         {
105             final String JavaDoc key = (String JavaDoc)keys.next();
106             
107             final Class JavaDoc thisClass = (Class JavaDoc)mCmds.get( key );
108             if ( thisClass == theClass )
109             {
110                 list.add( key );
111             }
112         }
113         
114         final String JavaDoc [] names = new String JavaDoc [ list.size() ];
115         list.toArray( names );
116         
117         return( names );
118     }
119     
120         private Cmd
121     instantiateCmd( String JavaDoc cmdName, CmdEnv env )
122         throws Exception JavaDoc
123     {
124         Class JavaDoc cmdClass = (Class JavaDoc)mCmds.get( cmdName );
125         
126         if ( cmdClass == null )
127         {
128             // not found; see if there is a default registered
129
cmdClass = (Class JavaDoc)mCmds.get( DEFAULT_CMD_NAME );
130         }
131         
132         if ( cmdClass == null )
133         {
134             return( null );
135         }
136
137         final Object JavaDoc [] args = new Object JavaDoc [] { env };
138
139         Cmd cmd = null;
140         try
141         {
142             cmd = (CmdBase)ClassUtil.InstantiateObject( cmdClass, args );
143         }
144         catch( Exception JavaDoc e )
145         {
146             p( e.getMessage() );
147             e.printStackTrace();
148             throw e;
149         }
150         
151         return( cmd );
152     }
153 }
154
155
Popular Tags