001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.databinding.datagrid.runtime.util;
020:
021: import org.apache.beehive.netui.databinding.datagrid.api.exceptions.DataGridExtensionException;
022: import org.apache.beehive.netui.util.logging.Logger;
023: import org.apache.beehive.netui.util.Bundle;
024:
025: /**
026: * Internal method used by the data grid to instantiate classes. This class should not be used elsewhere.
027: */
028: public final class ExtensionUtil {
029:
030: private static final Logger LOGGER = Logger
031: .getInstance(ExtensionUtil.class);
032:
033: private ExtensionUtil() {
034: }
035:
036: /**
037: * Utility method that helps instantiate a class used to extend the data grid.
038: *
039: * @param className the name of a class to instantiate
040: * @param assignableFrom the type that should be assignable from an instance of type <code>className</code>
041: * @return an instance of the given class
042: * @throws org.apache.beehive.netui.databinding.datagrid.api.exceptions.DataGridExtensionException
043: * when an error occurs creating an instance of the class
044: */
045: public static Object instantiateClass(String className,
046: Class assignableFrom) {
047: if (className == null)
048: throw new IllegalArgumentException(Bundle
049: .getErrorString("DataGridUtil_CantCreateClass"));
050:
051: Class clazz = null;
052: try {
053: clazz = Class.forName(className, false, Thread
054: .currentThread().getContextClassLoader());
055: } catch (Exception e) {
056: assert e instanceof IllegalAccessException
057: || e instanceof InstantiationException
058: || e instanceof ClassNotFoundException : "Caught exception of unexpected type: "
059: + e.getClass().getName();
060:
061: String msg = Bundle.getErrorString(
062: "DataGridUtil_CantInstantiateClass",
063: new Object[] { e });
064: LOGGER.error(msg, e);
065: throw new DataGridExtensionException(msg, e);
066: }
067:
068: return instantiateClass(clazz, assignableFrom);
069: }
070:
071: /**
072: * Utility method that helps instantiate a class used to extend the data grid.
073: *
074: * @param clazz the name of a class to instantiate
075: * @param assignableFrom the type that should be assignable from an instance of type <code>className</code>
076: * @return an instance of the given class
077: * @throws org.apache.beehive.netui.databinding.datagrid.api.exceptions.DataGridExtensionException
078: * when an error occurs creating an instance of the class
079: */
080: public static Object instantiateClass(Class clazz,
081: Class assignableFrom) {
082: if (clazz == null)
083: throw new IllegalArgumentException(Bundle
084: .getErrorString("DataGridUtil_CantCreateClass"));
085:
086: try {
087: Object obj = clazz.newInstance();
088:
089: if (assignableFrom == null
090: || assignableFrom.isAssignableFrom(clazz))
091: return obj;
092: else
093: throw new DataGridExtensionException(Bundle
094: .getErrorString(
095: "DataGridUtil_InvalidParentClass",
096: new Object[] { clazz.getName(),
097: assignableFrom }));
098: } catch (Exception e) {
099: assert e instanceof DataGridExtensionException
100: || e instanceof IllegalAccessException
101: || e instanceof InstantiationException
102: || e instanceof ClassNotFoundException : "Caught exception of unexpected type "
103: + e.getClass().getName();
104:
105: String msg = Bundle.getErrorString(
106: "DataGridUtil_CantInstantiateClass",
107: new Object[] { e });
108: LOGGER.error(msg, e);
109: throw new DataGridExtensionException(msg, e);
110: }
111: }
112: }
|