001: /*
002: * Copyright (C) 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package com.bm.ejb3guice.inject;
018:
019: import java.lang.annotation.Annotation;
020: import java.util.Arrays;
021:
022: /**
023: * The entry point to the Guice framework. Creates {@link Injector}s from
024: * {@link Module}s.
025: *
026: * <p>
027: * Guice supports a model of development that draws clear boundaries between
028: * APIs, Implementations of these APIs, Modules which configure these
029: * implementations, and finally Applications which consist of a collection of
030: * Modules. It is the Application, which typically defines your {@code main()}
031: * method, that bootstraps the Guice Injector using the {@code Guice} class, as
032: * in this example:
033: *
034: * <pre>
035: * public class FooApplication {
036: * public static void main(String[] args) {
037: * Injector injector = Guice.createInjector(
038: * new ModuleA(),
039: * new ModuleB(),
040: * . . .
041: * new FooApplicationFlagsModule(args)
042: * );
043: *
044: * // Now just bootstrap the application and you're done
045: * MyStartClass starter = injector.getInstance(MyStartClass.class);
046: * starter.runApplication();
047: * }
048: * }
049: * </pre>
050: */
051: public final class Ejb3Guice {
052:
053: private Ejb3Guice() {
054: }
055:
056: /**
057: * Creates an injector for the given set of modules.
058: *
059: * @throws CreationException
060: * if one or more errors occur during Injector construction
061: */
062: public static Injector createInjector(Module... modules) {
063: return createInjector(Arrays.asList(modules));
064: }
065:
066: /**
067: * Creates an injector for the given set of modules.
068: *
069: * @throws CreationException
070: * if one or more errors occur during Injector construction
071: */
072: public static Injector createInjector(Iterable<Module> modules) {
073: return createInjector(Stage.DEVELOPMENT, modules);
074: }
075:
076: /**
077: * Creates an injector for the given set of modules, in a given development
078: * stage.
079: *
080: * @throws CreationException
081: * if one or more errors occur during Injector construction
082: */
083: public static Injector createInjector(Stage stage,
084: Module... modules) {
085: return createInjector(stage, Arrays.asList(modules));
086: }
087:
088: /**
089: * Creates an injector for the given set of modules, in a given development
090: * stage. Additionally own marker annotations can be provided.
091: *
092: * @throws CreationException
093: * if one or more errors occur during Injector construction
094: */
095: public static Injector createInjector(Stage stage,
096: Class<? extends Annotation>[] injectionMarkers,
097: CreationListner crListner, Module... modules) {
098: return createInjector(stage, Arrays.asList(modules),
099: injectionMarkers, crListner);
100: }
101:
102: /**
103: * Creates an injector for the given set of modules, in a given development
104: * stage.
105: *
106: * @throws CreationException
107: * if one or more errors occur during Injector construction
108: */
109: public static Injector createInjector(Stage stage,
110: Iterable<Module> modules) {
111: Class<? extends Annotation> defaults = Inject.class;
112: return createInjector(stage, modules, markerToArray(defaults),
113: null);
114: }
115:
116: /**
117: * Creates an injector for the given set of modules, in a given development
118: * stage.
119: *
120: * @throws CreationException
121: * if one or more errors occur during Injector construction
122: */
123: public static Injector createInjector(Stage stage,
124: Iterable<Module> modules,
125: Class<? extends Annotation>[] injectionMarkers,
126: CreationListner crListner) {
127: BinderImpl binder = new BinderImpl(stage, injectionMarkers,
128: crListner);
129: for (Module module : modules) {
130: binder.install(module);
131: }
132: return binder.createInjector();
133: }
134:
135: public static Class<? extends Annotation>[] markerToArray(
136: Class<? extends Annotation>... annotations) {
137: return annotations;
138:
139: }
140: }
|