[SDNC-5] Rebase sdnc-core
[sdnc/core.git] / dblib / common / src / main / java / org / apache / tomcat / jdbc / pool / ClassLoaderUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openecomp
4  * ================================================================================
5  * Copyright (C) 2016 - 2017 AT&T
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /*
22  * Licensed to the Apache Software Foundation (ASF) under one or more
23  * contributor license agreements.  See the NOTICE file distributed with
24  * this work for additional information regarding copyright ownership.
25  * The ASF licenses this file to You under the Apache License, Version 2.0
26  * (the "License"); you may not use this file except in compliance with
27  * the License.  You may obtain a copy of the License at
28  *
29  *      http://www.apache.org/licenses/LICENSE-2.0
30  *
31  * Unless required by applicable law or agreed to in writing, software
32  * distributed under the License is distributed on an "AS IS" BASIS,
33  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34  * See the License for the specific language governing permissions and
35  * limitations under the License.
36  */
37 package org.apache.tomcat.jdbc.pool;
38
39
40 import org.apache.juli.logging.Log;
41 import org.apache.juli.logging.LogFactory;
42
43 public class ClassLoaderUtil {
44     private static final Log log = LogFactory.getLog(ClassLoaderUtil.class);
45
46     private static final boolean onlyAttemptFirstLoader =
47         Boolean.parseBoolean(System.getProperty("org.apache.tomcat.jdbc.pool.onlyAttemptCurrentClassLoader", "false"));
48
49     public static Class<?> loadClass(String className, ClassLoader... classLoaders) throws ClassNotFoundException {
50         ClassNotFoundException last = null;
51         StringBuilder errorMsg = null;
52         for (ClassLoader cl : classLoaders) {
53             try {
54                 if (cl!=null) {
55                     if (log.isDebugEnabled()) {
56                         log.debug("Attempting to load class["+className+"] from "+cl);
57                     }
58                     return Class.forName(className, true, cl);
59                 } else {
60                     throw new ClassNotFoundException("Classloader is null");
61                 }
62             } catch (ClassNotFoundException x) {
63                 last = x;
64                 if (errorMsg==null) {
65                     errorMsg = new StringBuilder();
66                 } else {
67                     errorMsg.append(';');
68                 }
69                 errorMsg.append("ClassLoader:");
70                 errorMsg.append(cl);
71             }
72             if (onlyAttemptFirstLoader) {
73                 break;
74             }
75         }
76         throw new ClassNotFoundException("Unable to load class: "+className+" from "+errorMsg, last);
77     }
78
79
80
81 }