use of org.talend.sdk.component.api.component.Components in project component-runtime by Talend.
the class ComponentManager method instance.
/**
* Creates a default manager with default maven local repository,
* TALEND-INF/dependencies.txt file to find the dependencies of the plugins and
* a default JMX pattern for plugins. It also adds the caller as a plugin.
*
* @return the contextual manager instance.
*/
public static ComponentManager instance() {
ComponentManager manager = CONTEXTUAL_INSTANCE.get();
if (manager == null) {
synchronized (CONTEXTUAL_INSTANCE) {
if (CONTEXTUAL_INSTANCE.get() == null) {
final Thread shutdownHook = new Thread(ComponentManager.class.getName() + "-" + ComponentManager.class.hashCode()) {
@Override
public void run() {
ofNullable(CONTEXTUAL_INSTANCE.get()).ifPresent(ComponentManager::close);
}
};
manager = new ComponentManager(findM2(), "TALEND-INF/dependencies.txt", "org.talend.sdk.component:type=component,value=%s") {
private final AtomicBoolean closed = new AtomicBoolean(false);
{
info("Creating the contextual ComponentManager instance " + getIdentifiers());
if (!Boolean.getBoolean("component.manager.callers.skip")) {
addCallerAsPlugin();
}
// alternatively we could capture based on TALEND-INF/dependencies.txt jars
if (!Boolean.getBoolean("component.manager.classpath.skip")) {
final String componentClasspath = findClasspath().replace(File.pathSeparatorChar, ';');
if (!componentClasspath.isEmpty()) {
final String[] jars = componentClasspath.split(";");
if (jars.length > 1) {
Stream.of(jars).map(FileArchive::decode).map(File::new).filter(File::exists).filter(f -> !f.isDirectory() && f.getName().endsWith(".jar")).filter(f -> KnownJarsFilter.INSTANCE.test(f.getName())).filter(f -> !hasPlugin(container.buildAutoIdFromName(f.getName()))).forEach(jar -> addPlugin(jar.getAbsolutePath()));
}
}
}
container.getDefinedNestedPlugin().stream().filter(p -> !hasPlugin(p)).forEach(this::addPlugin);
info("Components: " + availablePlugins());
}
@Override
public void close() {
if (!closed.compareAndSet(false, true)) {
return;
}
try {
synchronized (CONTEXTUAL_INSTANCE) {
if (CONTEXTUAL_INSTANCE.compareAndSet(this, null)) {
try {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
} catch (final IllegalStateException ise) {
// already shutting down
}
}
}
} finally {
CONTEXTUAL_INSTANCE.set(null);
super.close();
info("Released the contextual ComponentManager instance " + getIdentifiers());
}
}
Object readResolve() throws ObjectStreamException {
return new SerializationReplacer();
}
};
Runtime.getRuntime().addShutdownHook(shutdownHook);
manager.info("Created the contextual ComponentManager instance " + getIdentifiers());
if (!CONTEXTUAL_INSTANCE.compareAndSet(null, manager)) {
// unlikely it fails in a synch block
manager = CONTEXTUAL_INSTANCE.get();
}
}
}
}
return manager;
}
Aggregations