KickJava   Java API By Example, From Geeks To Geeks.

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


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  * LocalStringsManagerFactory.java
26  *
27  * Created on July 29, 2003, 4:48 PM
28  */

29
30 package com.sun.enterprise.cli.framework;
31
32 //imports
33
import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Vector JavaDoc;
36 import java.util.Properties JavaDoc;
37
38 /**
39  *
40  * This is a factory class that creates the LocalStringsManager
41  * @author pa100654
42  */

43 public class LocalStringsManagerFactory {
44     
45     private static HashMap JavaDoc stringManagers = new HashMap JavaDoc();
46     private static String JavaDoc PROPERTY_FILE = "LocalStrings";
47     private final static String JavaDoc FRAMEWORK_BASE_PACKAGE = LocalStringsManagerFactory.class.getPackage().getName();
48     private static LocalStringsManager commandLocalStringsManager = null;
49     
50     /** Creates a new instance of LocalStringsManagerFactory */
51     protected LocalStringsManagerFactory()
52     {
53     }
54     
55     /**
56         Sets the LocalStringsManager object.
57         @param packageName name of the package used by the LocalStringsManager.
58         @param stringsManager LocalStringsManager object
59     */

60     public static void setInstance(String JavaDoc packageName,
61                                     LocalStringsManager stringsManager)
62     {
63         stringManagers.put(packageName, stringsManager);
64     }
65
66     /**
67         Returns the instance of the LocalStringsManager object,
68         the caller can access all the methods in this class.
69         @param packageName name of the package
70     */

71     public static LocalStringsManager getLocalStringsManager(
72                                             String JavaDoc packageName,
73                                             String JavaDoc propertiesFileName)
74             throws CommandValidationException
75     {
76         LocalStringsManager stringsManager =
77                     (LocalStringsManager) stringManagers.get(packageName);
78         if ((stringsManager == null) ||
79             (!stringsManager.getPropertiesFile().equals(propertiesFileName)))
80         {
81             try
82             {
83                 stringsManager = new LocalStringsManager(packageName,
84                                                             propertiesFileName);
85                 stringManagers.put(packageName, stringsManager);
86             }
87             catch(java.util.MissingResourceException JavaDoc mre)
88             {
89                 //throw new CommandException(mre.getLocalizedMessage());
90
throw new CommandValidationException(mre.getLocalizedMessage(), mre);
91             }
92         }
93         return stringsManager;
94     }
95
96
97     /**
98      * Returns the instance of the LocalStringsManager object for
99      * the CLI framework module.
100      * the caller can access all the methods in this class.
101      * @param packageName name of the package
102      */

103     public static LocalStringsManager getFrameworkLocalStringsManager()
104             throws CommandValidationException
105     {
106         return getLocalStringsManager(FRAMEWORK_BASE_PACKAGE, PROPERTY_FILE);
107     }
108
109     /**
110      * sets the list of base_package and property_file_name properties for
111      * the CLI Commands module.
112      * @param properties vector of java.util.Properties (name/value pairs of
113                             base-package and property-file-name) objects
114      */

115     public static void setCommandLocalStringsManagerProperties(
116                                                 Iterator JavaDoc propertiesIter)
117             throws CommandValidationException
118     {
119         Vector JavaDoc localizePropertiesList = new Vector JavaDoc();
120         while (propertiesIter.hasNext())
121         {
122             Properties JavaDoc localizeProperties = new Properties JavaDoc();
123             Properties JavaDoc properties = (Properties JavaDoc) propertiesIter.next();
124             String JavaDoc basePackage = properties.getProperty("base-package");
125             String JavaDoc propertyFile = properties.getProperty("property-file-name",
126                                     LocalStringsManagerFactory.PROPERTY_FILE);
127             if ((basePackage != null) && (!basePackage.equals("")))
128             {
129         CLILogger.getInstance().printDebugMessage("basePackage: " + basePackage);
130         CLILogger.getInstance().printDebugMessage("propertyFile: " + propertyFile);
131                 localizeProperties.setProperty("base-package", basePackage);
132                 localizeProperties.setProperty("property-file-name", propertyFile);
133                 localizePropertiesList.add(localizeProperties);
134             }
135         }
136         if (localizePropertiesList.size() > 0)
137         {
138             commandLocalStringsManager =
139                                new LocalStringsManager(localizePropertiesList);
140         }
141     }
142
143     /**
144      * Returns the instance of the LocalStringsManager object for
145      * the CLI Commands module.
146      * the caller can access all the methods in this class.
147      */

148     public static LocalStringsManager getCommandLocalStringsManager()
149             throws CommandValidationException
150     {
151         if (commandLocalStringsManager == null)
152         {
153             LocalStringsManager lsm =
154                 LocalStringsManagerFactory.getFrameworkLocalStringsManager();
155             throw new CommandValidationException(
156                         lsm.getString("CouldNotFindLocalStringsProperties"));
157         }
158         return commandLocalStringsManager;
159     }
160
161 }
162
Popular Tags