KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > util > Util


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 package com.sun.enterprise.tools.jsfext.util;
24
25 import java.util.Locale JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Properties JavaDoc;
28
29
30 /**
31  * <p> This class is for general purpose utility methods.</p>
32  *
33  * @author Ken Paulsen (ken.paulsen@sun.com)
34  */

35 public class Util {
36
37     /**
38      * <p> This method returns the ContextClassLoader unless it is null, in
39      * which case it returns the ClassLoader that loaded "obj". Unless it
40      * is null, in which it will return the system ClassLoader.</p>
41      *
42      * @param obj May be null, if non-null when the Context ClassLoader is
43      * null, then the Classloader used to load this Object will be
44      * returned.
45      */

46     public static ClassLoader JavaDoc getClassLoader(Object JavaDoc obj) {
47     // Get the ClassLoader
48
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
49     if (loader == null) {
50         if (obj != null) {
51         loader = obj.getClass().getClassLoader();
52         } else {
53         loader = ClassLoader.getSystemClassLoader();
54         }
55     }
56     return loader;
57     }
58
59     /**
60      * <p> This method attempts load the requested Class. If obj is a
61      * String, it will use this value as the fully qualified class name.
62      * If it is a Class, it will return it. If it is anything else, it
63      * will return the Class for the given Object.</p>
64      *
65      * @param obj The Object describing the requested Class
66      */

67     public static Class JavaDoc getClass(Object JavaDoc obj) throws ClassNotFoundException JavaDoc {
68     if ((obj == null) || (obj instanceof Class JavaDoc)) {
69         return (Class JavaDoc) obj;
70     }
71     Class JavaDoc cls = null;
72     if (obj instanceof String JavaDoc) {
73         ClassLoader JavaDoc loader = getClassLoader(obj);
74         cls = loader.loadClass((String JavaDoc) obj);
75     } else {
76         cls = obj.getClass();
77     }
78     return cls;
79     }
80
81     /**
82      * <p> This method converts the given Map into a Properties Map (if it is
83      * already one, then it simply returns the given Map).</p>
84      */

85     public static Properties JavaDoc mapToProperties(Map JavaDoc map) {
86     if ((map == null) || (map instanceof Properties JavaDoc)) {
87         return (Properties JavaDoc) map;
88     }
89
90     // Create Properties and add all the values
91
Properties JavaDoc props = new Properties JavaDoc();
92     props.putAll(map);
93
94     // Return the result
95
return props;
96     }
97 }
98
Popular Tags