KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > jmx > remote > StringManagerFactory


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 package com.sun.enterprise.admin.jmx.remote;
25
26 import com.sun.enterprise.admin.jmx.remote.IStringManager;
27 import com.sun.enterprise.admin.jmx.remote.StringManager;
28 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration;
29
30 import java.lang.reflect.Constructor JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Map JavaDoc;
34
35 public class StringManagerFactory {
36
37     /** cache for all the local string managers (per pkg) */
38     private static Hashtable JavaDoc managers = new Hashtable JavaDoc();
39     
40     private static final Logger JavaDoc logger = Logger.getLogger(
41         DefaultConfiguration.JMXCONNECTOR_LOGGER);/*,
42         DefaultConfiguration.LOGGER_RESOURCE_BUNDLE_NAME );
43     
44     /**
45      * packageName stands for the input fully qualified class name against which
46      * a string manager is stored
47      */

48     public static IStringManager getServerStringManager(String JavaDoc packageName) {
49         String JavaDoc stringMgrClassName = System.getProperty(DefaultConfiguration.STRING_MANAGER_CLASS_NAME);
50         return getManager(packageName, stringMgrClassName);
51     }
52
53     public static IStringManager getClientStringManager(String JavaDoc packageName, Map JavaDoc env) {
54         String JavaDoc stringMgrClassName = null;
55         if (env != null)
56             stringMgrClassName = (String JavaDoc)env.get(DefaultConfiguration.STRING_MANAGER_CLASS_NAME);
57         return getManager(packageName, stringMgrClassName);
58     }
59
60     /**
61      * inputClass is the Class against whose package a string manager is stored
62      */

63     public static IStringManager getServerStringManager(Class JavaDoc inputClass) {
64         String JavaDoc packageName = inputClass.getPackage().getName();
65         String JavaDoc stringMgrClassName = System.getProperty(DefaultConfiguration.STRING_MANAGER_CLASS_NAME);
66         return getManager(packageName, stringMgrClassName);
67     }
68
69     public static IStringManager getClientStringManager(Class JavaDoc inputClass, Map JavaDoc env) {
70         String JavaDoc packageName = inputClass.getPackage().getName();
71         String JavaDoc stringMgrClassName = null;
72         if (env != null)
73             stringMgrClassName = (String JavaDoc)env.get(DefaultConfiguration.STRING_MANAGER_CLASS_NAME);
74         return getManager(packageName, stringMgrClassName);
75     }
76
77     public static IStringManager getManager(String JavaDoc packageName, String JavaDoc stringMgrClassName) {
78         
79         IStringManager mgr = (IStringManager) managers.get(packageName);
80         
81         if (mgr != null) return mgr;
82         
83         if (stringMgrClassName == null) mgr = new StringManager(packageName);
84         else {
85             try {
86                 Class JavaDoc customClass = Class.forName(stringMgrClassName);
87                 Constructor JavaDoc constructor =
88                     customClass.getConstructor(new Class JavaDoc[] { String JavaDoc.class });
89                 mgr = (IStringManager) constructor.newInstance(new Object JavaDoc[] {packageName});
90             } catch (Exception JavaDoc e) {
91                 logger.severe("StringManager could not be configured");
92             }
93         }
94         
95         if (mgr != null) managers.put(packageName, mgr);
96         else logger.severe("Custom StringManager Class could not be instantiated");
97         
98         return mgr;
99     }
100 }
Popular Tags