66d90ed273a2289d2c1574d4cefc3b73b224c77c
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / impl / GeneratedClassLoadingStrategy.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.generator.impl;
9
10 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
11 import org.opendaylight.mdsal.binding.model.api.Type;
12 import org.opendaylight.yangtools.util.ClassLoaderUtils;
13
14 public abstract class GeneratedClassLoadingStrategy implements ClassLoadingStrategy {
15
16     private static final GeneratedClassLoadingStrategy TCCL_STRATEGY = new TCCLClassLoadingStrategy();
17
18     private static final GeneratedClassLoadingStrategy ALWAYS_FAIL_STRATEGY = new GeneratedClassLoadingStrategy() {
19         @Override
20         public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
21             throw new ClassNotFoundException(fullyQualifiedName);
22         }
23     };
24
25     @Override
26     public Class<?> loadClass(final Type type) throws ClassNotFoundException {
27         return loadClass(type.getFullyQualifiedName());
28     }
29
30     @Override
31     public abstract Class<?> loadClass(String fullyQualifiedName) throws ClassNotFoundException;
32
33     public static final GeneratedClassLoadingStrategy getTCCLClassLoadingStrategy() {
34         return TCCL_STRATEGY;
35     }
36
37     public static final GeneratedClassLoadingStrategy getAlwaysFailClassLoadingStrategy() {
38         return ALWAYS_FAIL_STRATEGY;
39     }
40
41     private static final class TCCLClassLoadingStrategy extends GeneratedClassLoadingStrategy {
42         @Override
43         public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
44             return ClassLoaderUtils.loadClassWithTCCL(fullyQualifiedName);
45         }
46     }
47 }