1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * 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 */
17 package org.apache.commons.discovery.tools;
18
19 import org.apache.commons.discovery.ResourceClass;
20 import org.apache.commons.discovery.ResourceClassIterator;
21 import org.apache.commons.discovery.resource.classes.DiscoverClasses;
22 import org.apache.commons.discovery.resource.ClassLoaders;
23
24 /**
25 * Holder for a default class.
26 *
27 * Class may be specified by name (String) or class (Class).
28 * Using the holder complicates the users job, but minimized # of API's.
29 */
30 public class DefaultClassHolder<T> {
31
32 private Class<? extends T> defaultClass;
33
34 private final String defaultName;
35
36 /**
37 * Creates a new holder implementation given
38 * the input SPI implementation/extension class.
39 *
40 * @param <S> Any type extends the SPI type
41 * @param defaultClass The hold class
42 */
43 public <S extends T> DefaultClassHolder(Class<S> defaultClass) {
44 this.defaultClass = defaultClass;
45 this.defaultName = defaultClass.getName();
46 }
47
48 /**
49 * Creates a new holder implementation given
50 * the input SPI implementation/extension class name.
51 *
52 * @param defaultName The hold class name
53 */
54 public DefaultClassHolder(String defaultName) {
55 this.defaultClass = null;
56 this.defaultName = defaultName;
57 }
58
59 /**
60 * Returns the default class, loading it if necessary
61 * and verifying that it implements the SPI
62 * (this forces the check, no way out..).
63 *
64 * @param <S> Any type extends the SPI type
65 * @param spi non-null SPI
66 * @param loaders Used only if class needs to be loaded.
67 * @return The default Class.
68 */
69 public <S extends T> Class<S> getDefaultClass(SPInterface<T> spi, ClassLoaders loaders) {
70 if (defaultClass == null) {
71 DiscoverClasses<T> classDiscovery = new DiscoverClasses<T>(loaders);
72 ResourceClassIterator<T> classes = classDiscovery.findResourceClasses(getDefaultName());
73 if (classes.hasNext()) {
74 ResourceClass<T> info = classes.nextResourceClass();
75 try {
76 defaultClass = info.loadClass();
77 } catch (Exception e) {
78 // ignore
79 }
80 }
81 }
82
83 if (defaultClass != null) {
84 spi.verifyAncestory(defaultClass);
85 }
86
87 @SuppressWarnings("unchecked") // the SPInterface.verifyAncestory already asserted
88 Class<S> returned = (Class<S>) defaultClass;
89 return returned;
90 }
91
92 /**
93 * Returns the hold class name.
94 *
95 * @return The hold class name
96 */
97 public String getDefaultName() {
98 return defaultName;
99 }
100
101 }