private static void executeMain()

in webbeans-se/src/main/java/org/apache/openwebbeans/se/CDILauncher.java [56:138]


    private static void executeMain(final Config config, final SeContainer container)
    {
        final BeanManager manager = container.getBeanManager();
        Set<Bean<?>> beans = manager.getBeans(config.main);
        if (beans == null || beans.isEmpty())
        {
            try
            {
                beans = manager.getBeans(Thread.currentThread().getContextClassLoader().loadClass(config.main));
            }
            catch (final ClassNotFoundException e)
            {
                throw new IllegalArgumentException("No bean '" + config.main + "' found");
            }
        }
        final Bean<?> bean = manager.resolve(beans);
        final CreationalContext<Object> context = manager.createCreationalContext(null);
        final Object reference = manager.getReference(bean, selectProxyType(bean.getTypes()), context);
        try
        {
            if (Runnable.class.isInstance(reference))
            {
                Runnable.class.cast(reference).run();
            }
            else // by reflection
            {
                // try a main method taking parameters
                // then just a main or run method
                Method method = null;
                try
                {
                    method = bean.getBeanClass()
                            .getMethod("main", String[].class);
                }
                catch (final Exception e)
                {
                    try
                    {
                        method = bean.getBeanClass()
                                .getMethod("main");
                    }
                    catch (final Exception e2)
                    {
                        try
                        {
                            method = bean.getBeanClass()
                                    .getMethod("main");
                        }
                        catch (final Exception e3)
                        {
                            throw new IllegalArgumentException(
                                    bean + " does not implements Runnable or has a public main method");
                        }
                    }
                }
                try
                {
                    final Object output = method.invoke(
                            Modifier.isStatic(method.getModifiers()) ? null : reference,
                            method.getParameterCount() == 1 ? new Object[] { config.args } : new Object[0]);
                    if (output != null)
                    {
                        System.out.println(output);
                    }
                }
                catch (final IllegalAccessException e)
                {
                    throw new IllegalStateException(e);
                }
                catch (final InvocationTargetException e)
                {
                    throw new IllegalStateException(e.getTargetException());
                }
            }
        }
        finally
        {
            if (!manager.isNormalScope(bean.getScope()))
            {
                context.release();
            }
        }
    }