• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc;
18 
19 import java.io.IOException;
20 import java.net.URL;
21 import java.util.Enumeration;
22 
23 /**
24  * A ClassLoader to help test service providers.
25  */
26 public class ReplacingClassLoader extends ClassLoader {
27   private final String resource;
28   private final String replacement;
29 
30   /**
31    * Construct an instance where {@code replacement} is loaded instead of {@code resource}.
32    */
ReplacingClassLoader(ClassLoader parent, String resource, String replacement)33   public ReplacingClassLoader(ClassLoader parent, String resource, String replacement) {
34     super(parent);
35     this.resource = resource;
36     this.replacement = replacement;
37   }
38 
39   @Override
getResource(String name)40   public URL getResource(String name) {
41     if (resource.equals(name)) {
42       return getParent().getResource(replacement);
43     }
44     return super.getResource(name);
45   }
46 
47   @Override
getResources(String name)48   public Enumeration<URL> getResources(String name) throws IOException {
49     if (resource.equals(name)) {
50       return getParent().getResources(replacement);
51     }
52     return super.getResources(name);
53   }
54 }
55 
56