KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Id: CommandEnvironment.java,v 1.4 2005/12/25 03:46:56 tcfujii Exp $
26  */

27
28
29 package com.sun.enterprise.cli.framework;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 /**
36    This class creates an environment for the options
37  */

38
39 public class CommandEnvironment implements ICommandEnvironment
40 {
41     //static instance of itself
42
private static CommandEnvironment commandEnvironment;
43
44     //Environment Prefix
45
//this value is temporary here -- will move this to CLIDescriptor.xml
46
private static final String JavaDoc ENVIRONMENT_PREFIX = "AS_ADMIN_";
47
48
49     private HashMap JavaDoc environments;
50
51     /**
52     Default constructor.
53     */

54     protected CommandEnvironment()
55     {
56         environments = (HashMap JavaDoc)getSystemEnvironment();
57     }
58
59
60     /** This method gets environment from os system environment
61      * @param strArray - the string array to convert
62      * @return
63      */

64     private Map JavaDoc getSystemEnvironmentNative()
65     {
66         HashMap JavaDoc hashMap = new HashMap JavaDoc();
67         try
68         {
69             CLIDescriptorsReader cdr = CLIDescriptorsReader.getInstance();
70             String JavaDoc envPrefix = cdr.getEnvironmentPrefix();
71             //if not specified on descriptor file, then use the default value
72
envPrefix = envPrefix==null?ENVIRONMENT_PREFIX:envPrefix;
73
74             //get Global Env
75
final String JavaDoc[] strArray = new CliUtil().getEnv(envPrefix);
76
77             for (int ii=0; ii<strArray.length; ii++)
78             {
79                 final int index = strArray[ii].indexOf("=");
80                 // need to break up the string in strArray
81
// since the format is in name=value
82
final String JavaDoc sOptionName = strArray[ii].substring(envPrefix.length(),
83                                                                   index).toLowerCase();
84                 hashMap.put( sOptionName.replace('_', '-'),
85                              strArray[ii].substring(index+1));
86             }
87         }
88         catch (java.lang.UnsatisfiedLinkError JavaDoc e)
89         {
90             CLILogger.getInstance().printDebugMessage(e.getLocalizedMessage());
91             // Emit the warning message
92
CLILogger.getInstance().printWarning(getLocalizedString("UnableToReadEnv"));
93         }
94         return hashMap;
95     }
96
97
98     /**
99      * This method uses System.getenv() (resurrected in jdk 1.5) to gets os environments.
100      * @return Map of the system environment.
101      */

102     private Map JavaDoc getSystemEnvironment()
103     {
104         //get environment prefix from CLI Descriptor file.
105
final CLIDescriptorsReader cdr = CLIDescriptorsReader.getInstance();
106         String JavaDoc sPrefix = cdr.getEnvironmentPrefix();
107         //if not specified in descriptor file, then use the default value
108
sPrefix = sPrefix==null?ENVIRONMENT_PREFIX:sPrefix;
109
110         final Map JavaDoc<String JavaDoc, String JavaDoc> mEnv = System.getenv();
111         HashMap JavaDoc<String JavaDoc, String JavaDoc> hmEnv = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
112         final Iterator JavaDoc iterEnv = mEnv.keySet().iterator();
113
114         while( iterEnv.hasNext() )
115         {
116             final String JavaDoc sEnvKey = ( String JavaDoc )iterEnv.next();
117             if (sEnvKey.startsWith(sPrefix)) {
118                     //extract the option name and convert it to lower case.
119
final String JavaDoc sOptionName = sEnvKey.substring(sPrefix.length()).toLowerCase();
120                     //replace occurrences of '_' to '-' since option name can only contain '-'
121
//this is a CLIP requirement.
122
hmEnv.put(sOptionName.replace('_','-'), (String JavaDoc)mEnv.get(sEnvKey));
123             }
124         }
125         return hmEnv;
126     }
127     
128
129
130     /**
131      * returns the instance of the CommandEnvironment
132      */

133     public static CommandEnvironment getInstance()
134     {
135         if (commandEnvironment == null)
136         {
137             commandEnvironment = new CommandEnvironment();
138         }
139         return commandEnvironment;
140     }
141
142
143     /**
144      * This method adds a name and value to the environment.
145      * @param name - option name
146      * @param value - option value
147      */

148     public void setEnvironment(String JavaDoc name, String JavaDoc value)
149     {
150         if ( environments.containsKey(name) )
151         {
152             environments.remove( name );
153         }
154         environments.put(name, value );
155     }
156
157
158     /**
159      * This method removes environment
160      * from the environment.
161      * @param name is the name of the environment to be removed.
162     */

163     public Object JavaDoc removeEnvironment( String JavaDoc name )
164     {
165         return environments.remove(name);
166     }
167
168     /**
169      * returns the envrionment value by the given key
170      */

171     public Object JavaDoc getEnvironmentValue(String JavaDoc key)
172     {
173         return environments.get(key);
174     }
175
176     /**
177     Returns an iterator over collection of Option objects.
178     */

179     public HashMap JavaDoc getEnvironments()
180     {
181         return environments;
182     }
183
184
185     /**
186        This method returns a String format for list of environments in the
187        environment with its argument values.
188        @return A String object.
189     */

190     public String JavaDoc toString()
191     {
192         String JavaDoc description = "";
193         final Iterator JavaDoc environIter = environments.keySet().iterator();
194         while( environIter.hasNext() )
195         {
196             String JavaDoc environKey = ( String JavaDoc )environIter.next();
197             description += " " + environKey + " = " +
198                                     (String JavaDoc)environments.get(environKey);
199         }
200         return description;
201     }
202
203
204     /**
205     Returns the number of environments
206     */

207     public int getNumEnvironments()
208     {
209         return environments.size();
210     }
211
212     /** returns the localized string from framework's LocalStrings.properties.
213      * Calls the LocalStringsManagerFactory.getFrameworkLocalStringsManager()
214      * method, returns "Key not found" if it cannot find the key
215      * @param key, the string to be localized
216      */

217     private String JavaDoc getLocalizedString(String JavaDoc key)
218     {
219         try
220         {
221             final LocalStringsManager lsm =
222             LocalStringsManagerFactory.getFrameworkLocalStringsManager();
223             return lsm.getString(key);
224         }
225         catch (CommandValidationException cve)
226         {
227             return LocalStringsManager.DEFAULT_STRING_VALUE;
228         }
229     }
230
231 }
232
233
Popular Tags