Search in sources :

Example 1 with Components

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;
}
Also used : Manifest(java.util.jar.Manifest) PartitionMapper(org.talend.sdk.component.api.input.PartitionMapper) JsonBuilderFactory(javax.json.JsonBuilderFactory) KnownClassesFilter(org.talend.sdk.component.runtime.manager.xbean.KnownClassesFilter) Optional.of(java.util.Optional.of) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) LocalPartitionMapper(org.talend.sdk.component.runtime.input.LocalPartitionMapper) KnownJarsFilter(org.talend.sdk.component.runtime.manager.xbean.KnownJarsFilter) Arrays.asList(java.util.Arrays.asList) Document(org.w3c.dom.Document) Map(java.util.Map) Executable(java.lang.reflect.Executable) ContainerListenerExtension(org.talend.sdk.component.runtime.manager.spi.ContainerListenerExtension) LocalCache(org.talend.sdk.component.api.service.cache.LocalCache) Set(java.util.Set) JsonGeneratorFactory(javax.json.stream.JsonGeneratorFactory) Collectors.joining(java.util.stream.Collectors.joining) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Service(org.talend.sdk.component.api.service.Service) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Constructors.findConstructor(org.talend.sdk.component.runtime.manager.reflect.Constructors.findConstructor) MigrationHandlerFactory(org.talend.sdk.component.runtime.manager.reflect.MigrationHandlerFactory) ReflectionService(org.talend.sdk.component.runtime.manager.reflect.ReflectionService) ComponentExtension(org.talend.sdk.component.spi.component.ComponentExtension) InterceptorHandlerFacade(org.talend.sdk.component.runtime.manager.interceptor.InterceptorHandlerFacade) AnnotatedElement(java.lang.reflect.AnnotatedElement) Components(org.talend.sdk.component.api.component.Components) Proxy(java.lang.reflect.Proxy) XPath(javax.xml.xpath.XPath) ComponentContexts(org.talend.sdk.component.runtime.manager.extension.ComponentContexts) ModelListener(org.talend.sdk.component.runtime.visitor.ModelListener) JsonParserFactory(javax.json.stream.JsonParserFactory) LightContainer(org.talend.sdk.component.runtime.serialization.LightContainer) Constructor(java.lang.reflect.Constructor) Supplier(java.util.function.Supplier) JsonbConfig(javax.json.bind.JsonbConfig) ArrayList(java.util.ArrayList) Filters(org.apache.xbean.finder.filter.Filters) Processor(org.talend.sdk.component.api.processor.Processor) ParameterModelService(org.talend.sdk.component.runtime.manager.reflect.ParameterModelService) PreComputedJsonpProvider(org.talend.sdk.component.runtime.manager.json.PreComputedJsonpProvider) LocalConfiguration(org.talend.sdk.component.api.service.configuration.LocalConfiguration) StreamSupport(java.util.stream.StreamSupport) ManagementFactory(java.lang.management.ManagementFactory) Request(org.talend.sdk.component.api.service.http.Request) InternationalizationServiceFactory(org.talend.sdk.component.runtime.internationalization.InternationalizationServiceFactory) HttpClientFactory(org.talend.sdk.component.api.service.http.HttpClientFactory) Properties(java.util.Properties) ContainerManager(org.talend.sdk.component.container.ContainerManager) IOException(java.io.IOException) Filter(org.apache.xbean.finder.filter.Filter) File(java.io.File) HttpClientFactoryImpl(org.talend.sdk.component.runtime.manager.service.HttpClientFactoryImpl) ObjectStreamException(java.io.ObjectStreamException) MvnDependencyListLocalRepositoryResolver(org.talend.sdk.component.dependencies.maven.MvnDependencyListLocalRepositoryResolver) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Jsonb(javax.json.bind.Jsonb) ActionType(org.talend.sdk.component.api.service.ActionType) JsonbProvider(javax.json.bind.spi.JsonbProvider) ExcludeIncludeFilter(org.apache.xbean.finder.filter.ExcludeIncludeFilter) JsonReaderFactory(javax.json.JsonReaderFactory) JsonWriterFactory(javax.json.JsonWriterFactory) ClasspathArchive(org.apache.xbean.finder.archive.ClasspathArchive) URL(java.net.URL) Spliterators(java.util.Spliterators) RequiredArgsConstructor(lombok.RequiredArgsConstructor) JarFile(java.util.jar.JarFile) JsonProvider(javax.json.spi.JsonProvider) Version(org.talend.sdk.component.api.component.Version) PreDestroy(javax.annotation.PreDestroy) LocalConfigurationService(org.talend.sdk.component.runtime.manager.service.LocalConfigurationService) Internationalized(org.talend.sdk.component.api.internationalization.Internationalized) Method(java.lang.reflect.Method) Collectors.toSet(java.util.stream.Collectors.toSet) Emitter(org.talend.sdk.component.api.input.Emitter) ContainerListener(org.talend.sdk.component.container.ContainerListener) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) ConfigurableClassLoader(org.talend.sdk.component.classloader.ConfigurableClassLoader) ResolverImpl(org.talend.sdk.component.runtime.manager.service.ResolverImpl) ProcessorImpl(org.talend.sdk.component.runtime.output.ProcessorImpl) Delegated(org.talend.sdk.component.runtime.base.Delegated) ServiceLoader(java.util.ServiceLoader) Attributes(java.util.jar.Attributes) Objects(java.util.Objects) LocalCacheService(org.talend.sdk.component.runtime.manager.service.LocalCacheService) List(java.util.List) FileArchive.decode(org.apache.xbean.finder.archive.FileArchive.decode) Modifier(java.lang.reflect.Modifier) Function.identity(java.util.function.Function.identity) Annotation(java.lang.annotation.Annotation) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ComponentContextImpl(org.talend.sdk.component.runtime.manager.extension.ComponentContextImpl) Spliterator(java.util.Spliterator) HttpClient(org.talend.sdk.component.api.service.http.HttpClient) Getter(lombok.Getter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Icon(org.talend.sdk.component.api.component.Icon) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) FilterFactory(org.talend.sdk.component.runtime.manager.xbean.FilterFactory) FileArchive(org.apache.xbean.finder.archive.FileArchive) Level(java.util.logging.Level) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) JavaProxyEnricherFactory(org.talend.sdk.component.runtime.manager.proxy.JavaProxyEnricherFactory) JarInputStream(java.util.jar.JarInputStream) Comparator.comparing(java.util.Comparator.comparing) JmxManager(org.talend.sdk.component.jmx.JmxManager) InvocationExceptionWrapper.toRuntimeException(org.talend.sdk.component.runtime.base.lang.exception.InvocationExceptionWrapper.toRuntimeException) Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptySet(java.util.Collections.emptySet) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) Container(org.talend.sdk.component.container.Container) PartitionMapperImpl(org.talend.sdk.component.runtime.input.PartitionMapperImpl) Optional.ofNullable(java.util.Optional.ofNullable) ProxyGenerator(org.talend.sdk.component.runtime.manager.asm.ProxyGenerator) ModelVisitor(org.talend.sdk.component.runtime.visitor.ModelVisitor) NestedJarArchive(org.talend.sdk.component.runtime.manager.xbean.NestedJarArchive) ClassFinder(org.apache.xbean.finder.ClassFinder) Consumer(java.util.function.Consumer) Resolver(org.talend.sdk.component.api.service.dependency.Resolver) XPathFactory(javax.xml.xpath.XPathFactory) Collectors.toList(java.util.stream.Collectors.toList) Mapper(org.talend.sdk.component.runtime.input.Mapper) Data(lombok.Data) AllArgsConstructor(lombok.AllArgsConstructor) Archive(org.apache.xbean.finder.archive.Archive) InputStream(java.io.InputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ObjectStreamException(java.io.ObjectStreamException) File(java.io.File) JarFile(java.util.jar.JarFile)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ObjectStreamException (java.io.ObjectStreamException)1 Serializable (java.io.Serializable)1 Annotation (java.lang.annotation.Annotation)1 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)1 ManagementFactory (java.lang.management.ManagementFactory)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Constructor (java.lang.reflect.Constructor)1 Executable (java.lang.reflect.Executable)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 Proxy (java.lang.reflect.Proxy)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1