1414import io .jooby .ServiceRegistry ;
1515import io .jooby .annotations .Path ;
1616
17+ import javax .annotation .Nonnull ;
1718import javax .enterprise .context .ApplicationScoped ;
1819import javax .enterprise .event .Observes ;
1920import javax .enterprise .inject .literal .NamedLiteral ;
2526import javax .enterprise .inject .spi .ProcessAnnotatedType ;
2627import javax .enterprise .inject .spi .WithAnnotations ;
2728import javax .enterprise .inject .spi .configurator .BeanConfigurator ;
28- import javax .inject .Inject ;
2929import javax .inject .Provider ;
3030import java .lang .reflect .Type ;
3131import java .util .List ;
3232import java .util .Map ;
3333import java .util .Set ;
3434import java .util .function .Function ;
3535
36+ /**
37+ * Weld extension. Exposes {@link Environment}, {@link Config} and application services into weld
38+ * container.
39+ *
40+ * Also, scan and register MVC routes annotated with {@link Path} annotation at top level class.
41+ *
42+ * @author edgar
43+ * @since 2.0.0
44+ */
3645public class JoobyExtension implements Extension {
3746 private final Jooby app ;
3847
39- @ Inject
40- public JoobyExtension (Jooby application ) {
48+ /**
49+ * Creates a new Jooby extension.
50+ *
51+ * @param application Application.
52+ */
53+ public JoobyExtension (@ Nonnull Jooby application ) {
4154 this .app = application ;
4255 }
4356
44- public void registerMvc (
45- @ Observes @ WithAnnotations (Path .class ) ProcessAnnotatedType <?> controller ) {
46- this .app .mvc (controller .getAnnotatedType ().getJavaClass ());
57+ /**
58+ * Register MVC routes annotated with {@link Path}.
59+ *
60+ * @param c Weld callback.
61+ */
62+ public void registerMvc (@ Observes @ WithAnnotations (Path .class ) ProcessAnnotatedType <?> c ) {
63+ this .app .mvc (c .getAnnotatedType ().getJavaClass ());
4764 }
4865
49- public void configureServices (@ Observes AfterBeanDiscovery beanDiscovery ,
50- BeanManager beanManager ) {
66+ /**
67+ * Configure application services into a weld container.
68+ *
69+ * @param beanDiscovery Bean discovery.
70+ * @param beanManager Bean Manager.
71+ */
72+ public void configureServices (@ Nonnull @ Observes AfterBeanDiscovery beanDiscovery ,
73+ @ Nonnull BeanManager beanManager ) {
5174 ServiceRegistry registry = app .getServices ();
5275 Set <Map .Entry <ServiceKey <?>, Provider <?>>> entries = registry .entrySet ();
5376 for (Map .Entry <ServiceKey <?>, Provider <?>> entry : entries ) {
@@ -56,13 +79,18 @@ public void configureServices(@Observes AfterBeanDiscovery beanDiscovery,
5679 }
5780 }
5881
59- public void configureEnv (@ Observes AfterBeanDiscovery beanDiscovery ,
60- BeanManager beanManager ) {
82+ /**
83+ * Configure {@link Environment} and {@link Config} into a weld container.
84+ *
85+ * @param abd Bean discovery.
86+ * @param bm Bean Manager.
87+ */
88+ public void configureEnv (@ Observes AfterBeanDiscovery abd , BeanManager bm ) {
6189 Environment environment = app .getEnvironment ();
6290 Config config = environment .getConfig ();
6391
64- registerSingleton (beanDiscovery , beanManager , Config .class , null , config );
65- registerSingleton (beanDiscovery , beanManager , Environment .class , null , environment );
92+ registerSingleton (abd , bm , Config .class , null , config );
93+ registerSingleton (abd , bm , Environment .class , null , environment );
6694
6795 for (Map .Entry <String , ConfigValue > configEntry : config .entrySet ()) {
6896 final String configKey = configEntry .getKey ();
@@ -78,9 +106,9 @@ public void configureEnv(@Observes AfterBeanDiscovery beanDiscovery,
78106 configType = boolean .class ;
79107 }
80108 NamedLiteral literal = NamedLiteral .of (configKey );
81- AnnotatedType <?> annotatedType = beanManager .createAnnotatedType (configClass );
82- InjectionTarget <?> target = beanManager .createInjectionTarget (annotatedType );
83- beanDiscovery .addBean ()
109+ AnnotatedType <?> annotatedType = bm .createAnnotatedType (configClass );
110+ InjectionTarget <?> target = bm .createInjectionTarget (annotatedType );
111+ abd .addBean ()
84112 .addQualifier (literal )
85113 .addTypes (configType , Object .class )
86114 .name (configKey )
0 commit comments