KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > GlobalsManager


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     Some preexisting portions Copyright 2001 Netscape
26     Communications Corp. All rights reserved.
27  */

28
29 /*
30  * Portions Copyright 2004 Sun Microsystems, Inc. All rights reserved.
31  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
32  */

33
34 /**
35     $Id: GlobalsManager.java,v 1.3 2005/12/25 03:46:59 tcfujii Exp $
36  */

37
38 package com.sun.enterprise.cli.framework;
39
40 import java.util.ResourceBundle JavaDoc;
41 import java.net.URL JavaDoc;
42 import java.text.MessageFormat JavaDoc;
43 import java.util.HashMap JavaDoc;
44
45 /**
46     A utility class which sets and gets the command environment,
47     user input information.
48  */

49 public class GlobalsManager
50 {
51     private static GlobalsManager sGlobalsMgr = null;
52     private ICommandEnvironment mGlobalCommandEnvironment = null;
53     
54     private static HashMap JavaDoc resourceBundles = new HashMap JavaDoc();
55     
56     private static String JavaDoc COMMANDS_BASE_PACKAGE = null;
57     private final static String JavaDoc FRAMEWORK_BASE_PACKAGE = GlobalsManager.class.getPackage().getName();
58     private static String JavaDoc FRAMEWORK_PROPERTY_FILE_NAME = "LocalStrings";
59     private static String JavaDoc COMMANDS_PROPERTY_FILE_NAME = "LocalStrings";
60
61     /**
62         A constructor which is accessed by it's sub classes only.
63      */

64
65     protected GlobalsManager() {
66         this(new CommandEnvironment());
67     }
68   
69     protected GlobalsManager(ICommandEnvironment env){
70       mGlobalCommandEnvironment = env;
71       resourceBundles = new HashMap JavaDoc();
72     }
73
74     /**
75         Sets the GlobalsManager object.
76         @param globalsMgr is the GlobalsManager object to be set.
77     */

78     public static void setInstance( GlobalsManager globalsMgr )
79     {
80         if ( sGlobalsMgr != globalsMgr )
81         {
82             sGlobalsMgr = globalsMgr;
83         }
84     }
85
86     /**
87         Returns the instance of the GlobalsManager through this object,
88         the caller can access all the methods in this class.
89     */

90     public static GlobalsManager getInstance()
91     {
92         if( sGlobalsMgr == null )
93         {
94             sGlobalsMgr = new GlobalsManager();
95         }
96         return sGlobalsMgr;
97     }
98
99     /**
100         Returns the command environment that is set in in GlobalsManager.
101     */

102     public ICommandEnvironment getGlobalsEnv()
103     {
104         return mGlobalCommandEnvironment;
105     }
106
107     /**
108         Sets the global command environment. (global means the set of options
109         which are base/common options to all the subcommands).
110         @param env is the command environment to be set.
111     */

112     public void setGlobalsEnv( ICommandEnvironment env )
113     {
114         mGlobalCommandEnvironment = env;
115     }
116
117     /**
118         Returns the option value that is set in GlobalsManager.
119         @param optionName is the name of the option.
120      */

121     public String JavaDoc getOption( String JavaDoc optionName )
122     {
123         String JavaDoc optionValue = (String JavaDoc)mGlobalCommandEnvironment.getEnvironmentValue(optionName );
124         return optionValue;
125     }
126
127     /**
128         Sets the Option object in the GlobalsManager.
129         @param option is the Option object to be set.
130      */

131     public void setOption( String JavaDoc name, String JavaDoc value )
132     {
133         mGlobalCommandEnvironment.setEnvironment(name, value);
134     }
135
136     /**
137      *Removes the option from the options list
138      */

139     public void removeOption( String JavaDoc optionName)
140     {
141         mGlobalCommandEnvironment.removeEnvironment(optionName);
142     }
143
144     /*
145      * Returns the Localized string from the commands properties file
146      */

147     public static String JavaDoc getString(String JavaDoc key) throws CommandException
148     {
149         return getString(key, COMMANDS_BASE_PACKAGE,
150                             COMMANDS_PROPERTY_FILE_NAME);
151     }
152
153     /*
154      * Return the Localized string from the commands properties file
155      * with the inserted values
156      */

157     public static String JavaDoc getString(String JavaDoc key, Object JavaDoc[] toInsert) throws CommandException
158     {
159         return getString(key, toInsert, COMMANDS_BASE_PACKAGE,
160                             COMMANDS_PROPERTY_FILE_NAME);
161     }
162     
163     /*
164      * Returns the Localized string from the framework properties file
165      */

166     public static String JavaDoc getFrameworkString(String JavaDoc key) throws CommandException
167     {
168         return getString(key, FRAMEWORK_BASE_PACKAGE,
169                             FRAMEWORK_PROPERTY_FILE_NAME);
170     }
171
172     /*
173      * Return the Localized string from the commands properties file
174      * with the inserted values
175      */

176     public static String JavaDoc getFrameworkString(String JavaDoc key, Object JavaDoc[] toInsert)
177                             throws CommandException
178     {
179         return getString(key, toInsert, FRAMEWORK_BASE_PACKAGE,
180                             FRAMEWORK_PROPERTY_FILE_NAME);
181     }
182     
183     /*
184      * Returns the Localized string from the commands properties file
185      */

186     private static String JavaDoc getString(String JavaDoc key, String JavaDoc basePkg, String JavaDoc propertyFile)
187                     throws CommandException
188     {
189         return getResourceBundle(basePkg, propertyFile).getString(key);
190     }
191
192     /*
193      * Return the Localized string with the inserted values
194      */

195     private static String JavaDoc getString(String JavaDoc key, Object JavaDoc[] toInsert,
196                                     String JavaDoc basePkg, String JavaDoc propertyFile)
197                                     throws CommandException
198     {
199         String JavaDoc fmtStr = null;
200         try
201         {
202             ResourceBundle JavaDoc resBundle = getResourceBundle(basePkg, propertyFile);
203             MessageFormat JavaDoc msgFormat =
204                         new MessageFormat JavaDoc(resBundle.getString(key));
205             fmtStr = msgFormat.format(toInsert);
206         }
207         catch(Exception JavaDoc e)
208         {
209             throw new CommandException(e.getLocalizedMessage());
210         }
211         return fmtStr;
212     }
213     
214     private static ResourceBundle JavaDoc getResourceBundle(String JavaDoc packageName,
215                                                    String JavaDoc propertyFile)
216                                                     throws CommandException
217     {
218         ResourceBundle JavaDoc resBundle = (ResourceBundle JavaDoc) resourceBundles.get(packageName);
219         if (resBundle == null)
220         {
221             try
222             {
223                 resBundle = ResourceBundle.getBundle(
224                                 packageName + "." + propertyFile);
225                 resourceBundles.put(packageName, resBundle);
226             }
227             catch(java.util.MissingResourceException JavaDoc mre)
228             {
229                 throw new CommandException(mre.getLocalizedMessage());
230             }
231         }
232         return resBundle;
233     }
234     
235     /*
236      * Sets the base package for the commands module
237      */

238     public static void setBasePackage(String JavaDoc basePkg)
239     {
240         COMMANDS_BASE_PACKAGE = basePkg;
241     }
242     
243     /*
244      * Sets the property file for the commands module
245      */

246     public static void setPropertyFile(String JavaDoc fileName)
247     {
248         COMMANDS_PROPERTY_FILE_NAME = fileName;
249     }
250     
251     public static void main(String JavaDoc[] args)
252     {
253         try
254         {
255             GlobalsManager globalsMgr = GlobalsManager.getInstance();
256             System.out.println(globalsMgr.getFrameworkString("junk", new Object JavaDoc[]{"junk", "prashanth"}));
257             globalsMgr.setBasePackage("com.sun.enterprise.cli.commands");
258             System.out.println(globalsMgr.getString("junk", new Object JavaDoc[]{"junk", "prashanth"}));
259         }
260         catch(CommandException ce)
261         {
262             ce.printStackTrace();
263         }
264     }
265    
266 }
267
268
Popular Tags