KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > databinding > datagrid > runtime > util > ExtensionUtil


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.databinding.datagrid.runtime.util;
19
20 import org.apache.beehive.netui.databinding.datagrid.api.exceptions.DataGridExtensionException;
21 import org.apache.beehive.netui.util.logging.Logger;
22 import org.apache.beehive.netui.util.Bundle;
23
24 /**
25  *
26  */

27 public final class ExtensionUtil {
28
29     private static final Logger LOGGER = Logger.getInstance(ExtensionUtil.class);
30
31     private ExtensionUtil() {
32     }
33
34     /**
35      * Utility method that helps instantiate a class used to extend the data grid.
36      *
37      * @param className the name of a class to instantiate
38      * @return an instance of the given class
39      * @throws org.apache.beehive.netui.databinding.datagrid.api.exceptions.DataGridExtensionException
40      * when an error occurs creating an instance of the class
41      */

42     public static final Object JavaDoc instantiateClass(String JavaDoc className, Class JavaDoc assignableFrom) {
43         if(className == null)
44             throw new IllegalArgumentException JavaDoc(Bundle.getErrorString("DataGridUtil_CantCreateClass"));
45
46         Class JavaDoc clazz = null;
47         try {
48             clazz = Class.forName(className, false, Thread.currentThread().getContextClassLoader());
49         }
50         catch(Exception JavaDoc e) {
51             assert e instanceof IllegalAccessException JavaDoc ||
52                     e instanceof InstantiationException JavaDoc ||
53                     e instanceof ClassNotFoundException JavaDoc : "Caught exception of unexpected type: " + e.getClass().getName();
54
55             String JavaDoc msg = Bundle.getErrorString("DataGridUtil_CantInstantiateClass", new Object JavaDoc[]{e});
56             LOGGER.error(msg, e);
57             throw new DataGridExtensionException(msg, e);
58         }
59
60         return instantiateClass(clazz, assignableFrom);
61     }
62
63     /**
64      * Utility method that helps instantiate a class used to extend the data grid.
65      *
66      * @param clazz the name of a class to instantiate
67      * @return an instance of the given class
68      * @throws org.apache.beehive.netui.databinding.datagrid.api.exceptions.DataGridExtensionException
69      * when an error occurs creating an instance of the class
70      */

71     public static final Object JavaDoc instantiateClass(Class JavaDoc clazz, Class JavaDoc assignableFrom) {
72         if(clazz == null)
73             throw new IllegalArgumentException JavaDoc(Bundle.getErrorString("DataGridUtil_CantCreateClass"));
74
75         try {
76             Object JavaDoc obj = clazz.newInstance();
77
78             if(assignableFrom == null || assignableFrom.isAssignableFrom(clazz))
79                 return obj;
80             else
81                 throw new DataGridExtensionException(Bundle.getErrorString("DataGridUtil_InvalidParentClass", new Object JavaDoc[]{clazz.getName(), assignableFrom}));
82         }
83         catch(Exception JavaDoc e) {
84             assert
85                     e instanceof DataGridExtensionException ||
86                     e instanceof IllegalAccessException JavaDoc ||
87                     e instanceof InstantiationException JavaDoc ||
88                     e instanceof ClassNotFoundException JavaDoc : "Caught exception of unexpected type " + e.getClass().getName();
89
90             String JavaDoc msg = Bundle.getErrorString("DataGridUtil_CantInstantiateClass", new Object JavaDoc[]{e});
91             LOGGER.error(msg, e);
92             throw new DataGridExtensionException(msg, e);
93         }
94     }
95 }
96
Popular Tags