01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.util;
17:
18: import org.apache.openejb.OpenEJBException;
19:
20: import java.util.HashMap;
21: import java.util.Properties;
22:
23: public class SafeToolkit {
24: public static final Messages messages = new Messages(
25: "org.apache.openejb.util.resources");
26: public static final HashMap codebases = new HashMap();
27:
28: public static SafeToolkit getToolkit(String systemLocation) {
29: return new SafeToolkit(systemLocation);
30: }
31:
32: private String systemLocation;
33:
34: private SafeToolkit(String systemLocation) {
35: this .systemLocation = systemLocation;
36: }
37:
38: private Class forName(String className) throws OpenEJBException {
39: Class clazz = null;
40: try {
41: clazz = Class.forName(className);
42: } catch (ClassNotFoundException cnfe) {
43: OpenEJBErrorHandler
44: .classNotFound(systemLocation, className);
45: }
46: return clazz;
47: }
48:
49: public Object newInstance(String className) throws OpenEJBException {
50: return newInstance(forName(className));
51: }
52:
53: public Object newInstance(Class clazz) throws OpenEJBException {
54: Object instance = null;
55: try {
56: instance = clazz.newInstance();
57: } catch (InstantiationException ie) {
58: OpenEJBErrorHandler.classNotIntantiateable(systemLocation,
59: clazz.getName());
60: } catch (IllegalAccessException iae) {
61: OpenEJBErrorHandler.classNotAccessible(systemLocation,
62: clazz.getName());
63: }
64:
65: catch (Throwable exception) {
66: exception.printStackTrace();
67: ClassLoader classLoader = clazz.getClassLoader();
68: if (classLoader instanceof java.net.URLClassLoader) {
69: OpenEJBErrorHandler
70: .classNotIntantiateableFromCodebaseForUnknownReason(
71: systemLocation,
72: clazz.getName(),
73: getCodebase((java.net.URLClassLoader) classLoader),
74: exception.getClass().getName(),
75: exception.getMessage());
76: } else {
77: OpenEJBErrorHandler
78: .classNotIntantiateableForUnknownReason(
79: systemLocation, clazz.getName(),
80: exception.getClass().getName(),
81: exception.getMessage());
82: }
83: }
84: return instance;
85:
86: }
87:
88: private static String getCodebase(
89: java.net.URLClassLoader urlClassLoader) {
90: StringBuffer codebase = new StringBuffer();
91: java.net.URL urlList[] = urlClassLoader.getURLs();
92: codebase.append(urlList[0].toString());
93: for (int i = 1; i < urlList.length; ++i) {
94: codebase.append(';');
95: codebase.append(urlList[i].toString());
96: }
97: return codebase.toString();
98: }
99: }
|