Simplify ClassLoadingStrategy implementations
[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.yangtools.util.ClassLoaderUtils;
12
13 public abstract class GeneratedClassLoadingStrategy implements ClassLoadingStrategy {
14
15     private static final GeneratedClassLoadingStrategy TCCL_STRATEGY = new TCCLClassLoadingStrategy();
16
17     private static final GeneratedClassLoadingStrategy ALWAYS_FAIL_STRATEGY = new GeneratedClassLoadingStrategy() {
18         @Override
19         public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
20             throw new ClassNotFoundException(fullyQualifiedName);
21         }
22     };
23
24     @Override
25     public abstract Class<?> loadClass(String fullyQualifiedName) throws ClassNotFoundException;
26
27     public static final GeneratedClassLoadingStrategy getTCCLClassLoadingStrategy() {
28         return TCCL_STRATEGY;
29     }
30
31     public static final GeneratedClassLoadingStrategy getAlwaysFailClassLoadingStrategy() {
32         return ALWAYS_FAIL_STRATEGY;
33     }
34
35     private static final class TCCLClassLoadingStrategy extends GeneratedClassLoadingStrategy {
36         @Override
37         public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
38             return ClassLoaderUtils.loadClassWithTCCL(fullyQualifiedName);
39         }
40     }
41 }