Search in sources :

Example 1 with Described

use of io.micronaut.core.naming.Described in project micronaut-core by micronaut-projects.

the class DefaultInterceptorRegistry method resolveInterceptors.

@Override
@NonNull
public <T> Interceptor<T, ?>[] resolveInterceptors(@NonNull Executable<T, ?> method, @NonNull Collection<BeanRegistration<Interceptor<T, ?>>> interceptors, @NonNull InterceptorKind interceptorKind) {
    final AnnotationMetadata annotationMetadata = method.getAnnotationMetadata();
    if (interceptors.isEmpty()) {
        return resolveToNone((ExecutableMethod<?, ?>) method, interceptorKind, annotationMetadata);
    } else {
        instrumentAnnotationMetadata(beanContext, method);
        final Collection<AnnotationValue<?>> applicableBindings = AbstractInterceptorChain.resolveInterceptorValues(annotationMetadata, interceptorKind);
        if (applicableBindings.isEmpty()) {
            return resolveToNone((ExecutableMethod<?, ?>) method, interceptorKind, annotationMetadata);
        } else {
            @SuppressWarnings({ "unchecked", "rawtypes" }) final Interceptor[] resolvedInterceptors = (Interceptor[]) interceptorStream(method.getDeclaringType(), (Collection) interceptors, interceptorKind, applicableBindings).filter(bean -> (bean instanceof MethodInterceptor) || !(bean instanceof ConstructorInterceptor)).toArray(Interceptor[]::new);
            if (LOG.isTraceEnabled()) {
                LOG.trace("Resolved {} {} interceptors out of a possible {} for method: {} - {}", resolvedInterceptors.length, interceptorKind, interceptors.size(), method.getDeclaringType(), method instanceof Described ? ((Described) method).getDescription(true) : method.toString());
                for (int i = 0; i < resolvedInterceptors.length; i++) {
                    Interceptor<?, ?> resolvedInterceptor = resolvedInterceptors[i];
                    LOG.trace("Interceptor {} - {}", i, resolvedInterceptor);
                }
            }
            // noinspection unchecked
            return resolvedInterceptors;
        }
    }
}
Also used : Described(io.micronaut.core.naming.Described) MethodInterceptor(io.micronaut.aop.MethodInterceptor) ConstructorInterceptor(io.micronaut.aop.ConstructorInterceptor) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Collection(java.util.Collection) MethodInterceptor(io.micronaut.aop.MethodInterceptor) ConstructorInterceptor(io.micronaut.aop.ConstructorInterceptor) Interceptor(io.micronaut.aop.Interceptor) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) NonNull(io.micronaut.core.annotation.NonNull)

Example 2 with Described

use of io.micronaut.core.naming.Described in project micronaut-core by micronaut-projects.

the class Micronaut method start.

/**
 * @return Run this {@link Micronaut}
 */
@Override
@NonNull
public ApplicationContext start() {
    long start = System.nanoTime();
    printBanner();
    ApplicationContext applicationContext = super.build();
    try {
        applicationContext.start();
        Optional<EmbeddedApplication> embeddedContainerBean = applicationContext.findBean(EmbeddedApplication.class);
        embeddedContainerBean.ifPresent((embeddedApplication -> {
            try {
                embeddedApplication.start();
                boolean keepAlive = false;
                if (embeddedApplication instanceof Described) {
                    if (LOG.isInfoEnabled()) {
                        long took = elapsedMillis(start);
                        String desc = ((Described) embeddedApplication).getDescription();
                        LOG.info("Startup completed in {}ms. Server Running: {}", took, desc);
                    }
                    keepAlive = embeddedApplication.isServer();
                } else {
                    if (embeddedApplication instanceof EmbeddedServer) {
                        final EmbeddedServer embeddedServer = (EmbeddedServer) embeddedApplication;
                        if (LOG.isInfoEnabled()) {
                            long took = elapsedMillis(start);
                            URL url = embeddedServer.getURL();
                            LOG.info("Startup completed in {}ms. Server Running: {}", took, url);
                        }
                        keepAlive = embeddedServer.isKeepAlive();
                    } else {
                        if (LOG.isInfoEnabled()) {
                            long took = elapsedMillis(start);
                            LOG.info("Startup completed in {}ms.", took);
                        }
                        keepAlive = embeddedApplication.isServer();
                    }
                }
                Thread mainThread = Thread.currentThread();
                boolean finalKeepAlive = keepAlive;
                CountDownLatch countDownLatch = new CountDownLatch(1);
                Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                    if (LOG.isInfoEnabled()) {
                        LOG.info("Embedded Application shutting down");
                    }
                    if (embeddedApplication.isRunning()) {
                        embeddedApplication.stop();
                        countDownLatch.countDown();
                        if (finalKeepAlive) {
                            mainThread.interrupt();
                        }
                    }
                }));
                if (keepAlive) {
                    new Thread(() -> {
                        try {
                            if (!embeddedApplication.isRunning()) {
                                countDownLatch.countDown();
                                Thread.sleep(1000);
                            }
                        } catch (InterruptedException e) {
                        // ignore
                        }
                    }, SHUTDOWN_MONITOR_THREAD).start();
                    boolean interrupted = false;
                    while (true) {
                        try {
                            countDownLatch.await();
                            break;
                        } catch (InterruptedException e) {
                            interrupted = true;
                        }
                    }
                    if (interrupted) {
                        Thread.currentThread().interrupt();
                    }
                    if (LOG.isInfoEnabled()) {
                        LOG.info("Embedded Application shutting down");
                    }
                }
                if (embeddedApplication.isForceExit()) {
                    System.exit(0);
                }
            } catch (Throwable e) {
                handleStartupException(applicationContext.getEnvironment(), e);
            }
        }));
        if (LOG.isInfoEnabled() && !embeddedContainerBean.isPresent()) {
            LOG.info("No embedded container found. Running as CLI application");
        }
        return applicationContext;
    } catch (Throwable e) {
        handleStartupException(applicationContext.getEnvironment(), e);
        return null;
    }
}
Also used : PrintStream(java.io.PrintStream) ResourceBanner(io.micronaut.context.banner.ResourceBanner) Environment(io.micronaut.context.env.Environment) Logger(org.slf4j.Logger) ApplicationContextBuilder(io.micronaut.context.ApplicationContextBuilder) PropertySource(io.micronaut.context.env.PropertySource) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) ApplicationStartupException(io.micronaut.runtime.exceptions.ApplicationStartupException) Function(java.util.function.Function) LinkedHashMap(java.util.LinkedHashMap) TimeUnit(java.util.concurrent.TimeUnit) NonNull(io.micronaut.core.annotation.NonNull) CountDownLatch(java.util.concurrent.CountDownLatch) ApplicationContext(io.micronaut.context.ApplicationContext) Nullable(io.micronaut.core.annotation.Nullable) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) Map(java.util.Map) Described(io.micronaut.core.naming.Described) Optional(java.util.Optional) MicronautBanner(io.micronaut.context.banner.MicronautBanner) DefaultApplicationContextBuilder(io.micronaut.context.DefaultApplicationContextBuilder) Described(io.micronaut.core.naming.Described) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) ApplicationContext(io.micronaut.context.ApplicationContext) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

NonNull (io.micronaut.core.annotation.NonNull)2 Described (io.micronaut.core.naming.Described)2 ConstructorInterceptor (io.micronaut.aop.ConstructorInterceptor)1 Interceptor (io.micronaut.aop.Interceptor)1 MethodInterceptor (io.micronaut.aop.MethodInterceptor)1 ApplicationContext (io.micronaut.context.ApplicationContext)1 ApplicationContextBuilder (io.micronaut.context.ApplicationContextBuilder)1 DefaultApplicationContextBuilder (io.micronaut.context.DefaultApplicationContextBuilder)1 MicronautBanner (io.micronaut.context.banner.MicronautBanner)1 ResourceBanner (io.micronaut.context.banner.ResourceBanner)1 Environment (io.micronaut.context.env.Environment)1 PropertySource (io.micronaut.context.env.PropertySource)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 Nullable (io.micronaut.core.annotation.Nullable)1 ApplicationStartupException (io.micronaut.runtime.exceptions.ApplicationStartupException)1 EmbeddedServer (io.micronaut.runtime.server.EmbeddedServer)1 PrintStream (java.io.PrintStream)1 URL (java.net.URL)1 Collection (java.util.Collection)1