org.apache.commons.discovery.tools.DefaultClassHolder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-discovery Show documentation
Show all versions of commons-discovery Show documentation
Its an port to fix bugs in OSGi support. ... The Apache Commons Discovery 0.5 component is about discovering, or finding,
implementations for pluggable interfaces.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.discovery.tools;
import org.apache.commons.discovery.ResourceClass;
import org.apache.commons.discovery.ResourceClassIterator;
import org.apache.commons.discovery.resource.classes.DiscoverClasses;
import org.apache.commons.discovery.resource.ClassLoaders;
/**
* Holder for a default class.
*
* Class may be specified by name (String) or class (Class).
* Using the holder complicates the users job, but minimized # of API's.
*/
public class DefaultClassHolder {
private Class extends T> defaultClass;
private final String defaultName;
/**
* Creates a new holder implementation given
* the input SPI implementation/extension class.
*
* @param Any type extends the SPI type
* @param defaultClass The hold class
*/
public DefaultClassHolder(Class defaultClass) {
this.defaultClass = defaultClass;
this.defaultName = defaultClass.getName();
}
/**
* Creates a new holder implementation given
* the input SPI implementation/extension class name.
*
* @param defaultName The hold class name
*/
public DefaultClassHolder(String defaultName) {
this.defaultClass = null;
this.defaultName = defaultName;
}
/**
* Returns the default class, loading it if necessary
* and verifying that it implements the SPI
* (this forces the check, no way out..).
*
* @param Any type extends the SPI type
* @param spi non-null SPI
* @param loaders Used only if class needs to be loaded.
* @return The default Class.
*/
public Class getDefaultClass(SPInterface spi, ClassLoaders loaders) {
if (defaultClass == null) {
DiscoverClasses classDiscovery = new DiscoverClasses(loaders);
ResourceClassIterator classes = classDiscovery.findResourceClasses(getDefaultName());
if (classes.hasNext()) {
ResourceClass info = classes.nextResourceClass();
try {
defaultClass = info.loadClass();
} catch (Exception e) {
// ignore
}
}
}
if (defaultClass != null) {
spi.verifyAncestory(defaultClass);
}
@SuppressWarnings("unchecked") // the SPInterface.verifyAncestory already asserted
Class returned = (Class) defaultClass;
return returned;
}
/**
* Returns the hold class name.
*
* @return The hold class name
*/
public String getDefaultName() {
return defaultName;
}
}