001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2017 MicroBean.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
014 * implied.  See the License for the specific language governing
015 * permissions and limitations under the License.
016 */
017package org.microbean.main;
018
019import javax.enterprise.inject.Produces;
020
021import javax.enterprise.inject.se.SeContainer;
022import javax.enterprise.inject.se.SeContainerInitializer;
023
024import javax.inject.Named;
025import javax.inject.Singleton;
026
027/**
028 * A class whose {@linkplain #main(String[]) <code>main</code> method}
029 * {@linkplain SeContainerInitializer#initialize() initializes} a new
030 * {@link SeContainer}.
031 *
032 * <h2>Thread Safety</h2>
033 *
034 * <p>This class is not safe for concurrent use by multiple
035 * threads.</p>
036 *
037 * @author <a href="http://about.me/lairdnelson"
038 * target="_parent">Laird Nelson</a>
039 *
040 * @see SeContainerInitializer#initialize()
041 */
042@Singleton
043public class Main {
044
045
046  /*
047   * Static fields.
048   */
049
050  
051  /**
052   * Any command line arguments installed by the last invocation of
053   * the {@link #main(String[])} method.
054   *
055   * <p>This field may be {@code null}.</p>
056   *
057   * @see #getCommandLineArguments()
058   *
059   * @see #main(String[])
060   */
061  private static String[] commandLineArguments;
062
063
064  /*
065   * Constructors.
066   */
067
068  
069  /**
070   * Creates a new {@link Main}.
071   */
072  public Main() {
073    super();
074  }
075
076
077  /*
078   * Static methods.
079   */
080
081  
082  /**
083   * A <a
084   * href="http://docs.jboss.org/cdi/spec/2.0.Beta1/cdi-spec.html#producer_method">producer
085   * method</a> that returns the command line arguments stored in the
086   * {@link #commandLineArguments} field by the {@link
087   * #main(String[])} method.
088   *
089   * <p>This method may return {@code null}.</p>
090   *
091   * @return a {@link String} array of command line arguments, or
092   * {@code null}
093   *
094   * @see #commandLineArguments
095   *
096   * @see #main(String[])
097   */
098  @Produces
099  @Named("commandLineArguments")
100  @Singleton
101  private static final String[] getCommandLineArguments() {
102    return commandLineArguments;
103  }
104
105  /**
106   * {@linkplain SeContainerInitializer#initialize() Initializes} a
107   * new {@link SeContainer} and then {@linkplain SeContainer#close()
108   * closes} it.
109   *
110   * <p>This method has a deliberate side effect of making the {@code
111   * args} parameter value available in the CDI container in {@link
112   * Singleton} scope with a qualifier of {@link
113   * Named @Named("commandLineArguments")}.  It also causes an
114   * instance of this class to be created by the CDI container in
115   * {@link Singleton} scope.</p>
116   *
117   * @param args command-line arguments; may be {@code null}
118   *
119   * @see SeContainerInitializer#newInstance()
120   *
121   * @see SeContainerInitializer#initialize()
122   *
123   * @see SeContainer#close()
124   */
125  public static final void main(final String[] args) {
126    commandLineArguments = args;
127    final SeContainerInitializer containerInitializer = SeContainerInitializer.newInstance();
128    assert containerInitializer != null;
129    containerInitializer.addBeanClasses(Main.class);
130    try (final SeContainer container = containerInitializer.initialize()) {
131      assert container != null;
132      assert container.select(Main.class).get() != null;
133    }
134  }
135  
136}