|
80 | 80 | import org.jooby.Session.Store; |
81 | 81 | import org.jooby.handlers.AssetHandler; |
82 | 82 | import org.jooby.internal.AppPrinter; |
| 83 | +import org.jooby.internal.AssetProxy; |
83 | 84 | import org.jooby.internal.BuiltinParser; |
84 | 85 | import org.jooby.internal.BuiltinRenderer; |
85 | 86 | import org.jooby.internal.DefaulErrRenderer; |
86 | | -import org.jooby.internal.AssetProxy; |
87 | 87 | import org.jooby.internal.HttpHandlerImpl; |
88 | 88 | import org.jooby.internal.JvmInfo; |
89 | 89 | import org.jooby.internal.LifecycleProcessor; |
@@ -2896,152 +2896,155 @@ private Injector bootstrap() throws Exception { |
2896 | 2896 | callback.getValue().forEach(it -> it.accept(config)); |
2897 | 2897 | } |
2898 | 2898 | } |
2899 | | - // dependency injection |
| 2899 | + /** dependency injection */ |
2900 | 2900 | @SuppressWarnings("unchecked") |
2901 | 2901 | Injector injector = Guice.createInjector(stage, binder -> { |
2902 | 2902 |
|
2903 | | - // type converters |
2904 | | - new TypeConverters().configure(binder); |
2905 | | - |
2906 | | - // bind config |
2907 | | - bindConfig(binder, config); |
2908 | | - |
2909 | | - // bind env |
2910 | | - binder.bind(Env.class).toInstance(env); |
2911 | | - |
2912 | | - // bind charset |
2913 | | - binder.bind(Charset.class).toInstance(charset); |
2914 | | - |
2915 | | - // bind locale |
2916 | | - binder.bind(Locale.class).toInstance(locale); |
2917 | | - |
2918 | | - // bind time zone |
2919 | | - binder.bind(ZoneId.class).toInstance(zoneId); |
2920 | | - binder.bind(TimeZone.class).toInstance(TimeZone.getTimeZone(zoneId)); |
2921 | | - |
2922 | | - // bind date format |
2923 | | - binder.bind(DateTimeFormatter.class).toInstance(dateTimeFormatter); |
2924 | | - |
2925 | | - // bind number format |
2926 | | - binder.bind(NumberFormat.class).toInstance(numberFormat); |
2927 | | - binder.bind(DecimalFormat.class).toInstance(numberFormat); |
2928 | | - |
2929 | | - // bind managed |
2930 | | - LifecycleProcessor lifecycleProcessor = new LifecycleProcessor(); |
2931 | | - binder.bind(LifecycleProcessor.class).toInstance(lifecycleProcessor); |
2932 | | - binder.bindListener(Matchers.any(), lifecycleProcessor); |
2933 | | - |
2934 | | - // Routes |
2935 | | - Multibinder<Route.Definition> definitions = Multibinder |
2936 | | - .newSetBinder(binder, Route.Definition.class); |
2937 | | - |
2938 | | - // Web Sockets |
2939 | | - Multibinder<WebSocket.Definition> sockets = Multibinder |
2940 | | - .newSetBinder(binder, WebSocket.Definition.class); |
2941 | | - |
2942 | | - // tmp dir |
2943 | | - File tmpdir = new File(config.getString("application.tmpdir")); |
2944 | | - tmpdir.mkdirs(); |
2945 | | - binder.bind(File.class).annotatedWith(Names.named("application.tmpdir")) |
2946 | | - .toInstance(tmpdir); |
2947 | | - |
2948 | | - RouteMetadata classInfo = new RouteMetadata(env); |
2949 | | - binder.bind(ParameterNameProvider.class).toInstance(classInfo); |
2950 | | - |
2951 | | - // err handler |
2952 | | - Multibinder<Err.Handler> ehandlers = Multibinder |
2953 | | - .newSetBinder(binder, Err.Handler.class); |
2954 | | - |
2955 | | - // parsers & renderers |
2956 | | - Multibinder<Parser> parsers = Multibinder |
2957 | | - .newSetBinder(binder, Parser.class); |
2958 | | - |
2959 | | - Multibinder<Renderer> renderers = Multibinder |
2960 | | - .newSetBinder(binder, Renderer.class); |
2961 | | - |
2962 | | - // modules, routes, parsers, renderers and websockets |
2963 | | - bag.forEach(candidate -> { |
2964 | | - if (candidate instanceof Jooby.Module) { |
2965 | | - install((Jooby.Module) candidate, env, config, binder); |
2966 | | - } else if (candidate instanceof Route.Definition) { |
2967 | | - definitions.addBinding().toInstance((Route.Definition) candidate); |
2968 | | - } else if (candidate instanceof Route.Namespace) { |
2969 | | - ((Route.Namespace) candidate).routes() |
2970 | | - .forEach(r -> definitions.addBinding().toInstance(r)); |
2971 | | - } else if (candidate instanceof WebSocket.Definition) { |
2972 | | - sockets.addBinding().toInstance((WebSocket.Definition) candidate); |
2973 | | - } else if (candidate instanceof Parser) { |
2974 | | - parsers.addBinding().toInstance((Parser) candidate); |
2975 | | - } else if (candidate instanceof Renderer) { |
2976 | | - renderers.addBinding().toInstance((Renderer) candidate); |
2977 | | - } else if (candidate instanceof Err.Handler) { |
2978 | | - ehandlers.addBinding().toInstance((Err.Handler) candidate); |
2979 | | - } else { |
2980 | | - binder.bind((Class<?>) candidate); |
2981 | | - MvcRoutes.routes(env, classInfo, (Class<?>) candidate) |
2982 | | - .forEach(route -> definitions.addBinding().toInstance(route)); |
2983 | | - } |
2984 | | - }); |
2985 | | - |
2986 | | - parsers.addBinding().toInstance(BuiltinParser.Basic); |
2987 | | - parsers.addBinding().toInstance(BuiltinParser.Collection); |
2988 | | - parsers.addBinding().toInstance(BuiltinParser.Optional); |
2989 | | - parsers.addBinding().toInstance(BuiltinParser.Enum); |
2990 | | - parsers.addBinding().toInstance(BuiltinParser.Upload); |
2991 | | - parsers.addBinding().toInstance(BuiltinParser.Bytes); |
2992 | | - parsers.addBinding().toInstance(new DateParser(dateFormat)); |
2993 | | - parsers.addBinding().toInstance(new LocalDateParser(dateTimeFormatter)); |
2994 | | - parsers.addBinding().toInstance(new LocaleParser()); |
2995 | | - parsers.addBinding().toInstance(new BeanParser()); |
2996 | | - parsers.addBinding().toInstance(new StaticMethodParser("valueOf")); |
2997 | | - parsers.addBinding().toInstance(new StaticMethodParser("fromString")); |
2998 | | - parsers.addBinding().toInstance(new StaticMethodParser("forName")); |
2999 | | - parsers.addBinding().toInstance(new StringConstructorParser()); |
3000 | | - |
3001 | | - binder.bind(ParserExecutor.class).in(Singleton.class); |
3002 | | - |
3003 | | - // renderer |
3004 | | - renderers.addBinding().toInstance(BuiltinRenderer.Asset); |
3005 | | - renderers.addBinding().toInstance(BuiltinRenderer.Bytes); |
3006 | | - renderers.addBinding().toInstance(BuiltinRenderer.ByteBuffer); |
3007 | | - renderers.addBinding().toInstance(BuiltinRenderer.File); |
3008 | | - renderers.addBinding().toInstance(BuiltinRenderer.CharBuffer); |
3009 | | - renderers.addBinding().toInstance(BuiltinRenderer.InputStream); |
3010 | | - renderers.addBinding().toInstance(BuiltinRenderer.Reader); |
3011 | | - renderers.addBinding().toInstance(BuiltinRenderer.FileChannel); |
3012 | | - renderers.addBinding().toInstance(new DefaulErrRenderer()); |
3013 | | - renderers.addBinding().toInstance(BuiltinRenderer.ToString); |
3014 | | - |
3015 | | - binder.bind(HttpHandler.class).to(HttpHandlerImpl.class).in(Singleton.class); |
3016 | | - |
3017 | | - RequestScope requestScope = new RequestScope(); |
3018 | | - binder.bind(RequestScope.class).toInstance(requestScope); |
3019 | | - binder.bindScope(RequestScoped.class, requestScope); |
3020 | | - |
3021 | | - // session manager |
3022 | | - binder.bind(SessionManager.class).asEagerSingleton(); |
3023 | | - binder.bind(Session.Definition.class).toInstance(session); |
3024 | | - Object sstore = session.store(); |
3025 | | - if (sstore instanceof Class) { |
3026 | | - binder.bind(Session.Store.class).to((Class<? extends Store>) sstore) |
3027 | | - .asEagerSingleton(); |
| 2903 | + /** type converters */ |
| 2904 | + new TypeConverters().configure(binder); |
| 2905 | + |
| 2906 | + /** bind config */ |
| 2907 | + bindConfig(binder, config); |
| 2908 | + |
| 2909 | + /** bind env */ |
| 2910 | + binder.bind(Env.class).toInstance(env); |
| 2911 | + |
| 2912 | + /** bind charset */ |
| 2913 | + binder.bind(Charset.class).toInstance(charset); |
| 2914 | + |
| 2915 | + /** bind locale */ |
| 2916 | + binder.bind(Locale.class).toInstance(locale); |
| 2917 | + |
| 2918 | + /** bind time zone */ |
| 2919 | + binder.bind(ZoneId.class).toInstance(zoneId); |
| 2920 | + binder.bind(TimeZone.class).toInstance(TimeZone.getTimeZone(zoneId)); |
| 2921 | + |
| 2922 | + /** bind date format */ |
| 2923 | + binder.bind(DateTimeFormatter.class).toInstance(dateTimeFormatter); |
| 2924 | + |
| 2925 | + /** bind number format */ |
| 2926 | + binder.bind(NumberFormat.class).toInstance(numberFormat); |
| 2927 | + binder.bind(DecimalFormat.class).toInstance(numberFormat); |
| 2928 | + |
| 2929 | + /** bind managed */ |
| 2930 | + LifecycleProcessor lifecycleProcessor = new LifecycleProcessor(); |
| 2931 | + binder.bind(LifecycleProcessor.class).toInstance(lifecycleProcessor); |
| 2932 | + binder.bindListener(Matchers.any(), lifecycleProcessor); |
| 2933 | + |
| 2934 | + /** routes */ |
| 2935 | + Multibinder<Route.Definition> definitions = Multibinder |
| 2936 | + .newSetBinder(binder, Route.Definition.class); |
| 2937 | + |
| 2938 | + /** web sockets */ |
| 2939 | + Multibinder<WebSocket.Definition> sockets = Multibinder |
| 2940 | + .newSetBinder(binder, WebSocket.Definition.class); |
| 2941 | + |
| 2942 | + /** tmp dir */ |
| 2943 | + File tmpdir = new File(config.getString("application.tmpdir")); |
| 2944 | + tmpdir.mkdirs(); |
| 2945 | + binder.bind(File.class).annotatedWith(Names.named("application.tmpdir")) |
| 2946 | + .toInstance(tmpdir); |
| 2947 | + |
| 2948 | + RouteMetadata classInfo = new RouteMetadata(env); |
| 2949 | + binder.bind(ParameterNameProvider.class).toInstance(classInfo); |
| 2950 | + |
| 2951 | + /** err handler */ |
| 2952 | + Multibinder<Err.Handler> ehandlers = Multibinder |
| 2953 | + .newSetBinder(binder, Err.Handler.class); |
| 2954 | + |
| 2955 | + /** parsers & renderers */ |
| 2956 | + Multibinder<Parser> parsers = Multibinder |
| 2957 | + .newSetBinder(binder, Parser.class); |
| 2958 | + |
| 2959 | + Multibinder<Renderer> renderers = Multibinder |
| 2960 | + .newSetBinder(binder, Renderer.class); |
| 2961 | + |
| 2962 | + /** basic parser */ |
| 2963 | + parsers.addBinding().toInstance(BuiltinParser.Basic); |
| 2964 | + parsers.addBinding().toInstance(BuiltinParser.Collection); |
| 2965 | + parsers.addBinding().toInstance(BuiltinParser.Optional); |
| 2966 | + parsers.addBinding().toInstance(BuiltinParser.Enum); |
| 2967 | + parsers.addBinding().toInstance(BuiltinParser.Upload); |
| 2968 | + parsers.addBinding().toInstance(BuiltinParser.Bytes); |
| 2969 | + |
| 2970 | + /** basic render */ |
| 2971 | + renderers.addBinding().toInstance(BuiltinRenderer.Asset); |
| 2972 | + renderers.addBinding().toInstance(BuiltinRenderer.Bytes); |
| 2973 | + renderers.addBinding().toInstance(BuiltinRenderer.ByteBuffer); |
| 2974 | + renderers.addBinding().toInstance(BuiltinRenderer.File); |
| 2975 | + renderers.addBinding().toInstance(BuiltinRenderer.CharBuffer); |
| 2976 | + renderers.addBinding().toInstance(BuiltinRenderer.InputStream); |
| 2977 | + renderers.addBinding().toInstance(BuiltinRenderer.Reader); |
| 2978 | + renderers.addBinding().toInstance(BuiltinRenderer.FileChannel); |
| 2979 | + |
| 2980 | + /** modules, routes, parsers, renderers and websockets */ |
| 2981 | + bag.forEach(candidate -> { |
| 2982 | + if (candidate instanceof Jooby.Module) { |
| 2983 | + install((Jooby.Module) candidate, env, config, binder); |
| 2984 | + } else if (candidate instanceof Route.Definition) { |
| 2985 | + definitions.addBinding().toInstance((Route.Definition) candidate); |
| 2986 | + } else if (candidate instanceof Route.Namespace) { |
| 2987 | + ((Route.Namespace) candidate).routes() |
| 2988 | + .forEach(r -> definitions.addBinding().toInstance(r)); |
| 2989 | + } else if (candidate instanceof WebSocket.Definition) { |
| 2990 | + sockets.addBinding().toInstance((WebSocket.Definition) candidate); |
| 2991 | + } else if (candidate instanceof Parser) { |
| 2992 | + parsers.addBinding().toInstance((Parser) candidate); |
| 2993 | + } else if (candidate instanceof Renderer) { |
| 2994 | + renderers.addBinding().toInstance((Renderer) candidate); |
| 2995 | + } else if (candidate instanceof Err.Handler) { |
| 2996 | + ehandlers.addBinding().toInstance((Err.Handler) candidate); |
3028 | 2997 | } else { |
3029 | | - binder.bind(Session.Store.class).toInstance((Store) sstore); |
3030 | | - ; |
| 2998 | + binder.bind((Class<?>) candidate); |
| 2999 | + MvcRoutes.routes(env, classInfo, (Class<?>) candidate) |
| 3000 | + .forEach(route -> definitions.addBinding().toInstance(route)); |
3031 | 3001 | } |
| 3002 | + }); |
| 3003 | + |
| 3004 | + parsers.addBinding().toInstance(new DateParser(dateFormat)); |
| 3005 | + parsers.addBinding().toInstance(new LocalDateParser(dateTimeFormatter)); |
| 3006 | + parsers.addBinding().toInstance(new LocaleParser()); |
| 3007 | + parsers.addBinding().toInstance(new BeanParser()); |
| 3008 | + parsers.addBinding().toInstance(new StaticMethodParser("valueOf")); |
| 3009 | + parsers.addBinding().toInstance(new StaticMethodParser("fromString")); |
| 3010 | + parsers.addBinding().toInstance(new StaticMethodParser("forName")); |
| 3011 | + parsers.addBinding().toInstance(new StringConstructorParser()); |
| 3012 | + |
| 3013 | + binder.bind(ParserExecutor.class).in(Singleton.class); |
| 3014 | + |
| 3015 | + /** override(able) renderer */ |
| 3016 | + renderers.addBinding().toInstance(new DefaulErrRenderer()); |
| 3017 | + renderers.addBinding().toInstance(BuiltinRenderer.ToString); |
| 3018 | + |
| 3019 | + binder.bind(HttpHandler.class).to(HttpHandlerImpl.class).in(Singleton.class); |
| 3020 | + |
| 3021 | + RequestScope requestScope = new RequestScope(); |
| 3022 | + binder.bind(RequestScope.class).toInstance(requestScope); |
| 3023 | + binder.bindScope(RequestScoped.class, requestScope); |
| 3024 | + |
| 3025 | + /** session manager */ |
| 3026 | + binder.bind(SessionManager.class).asEagerSingleton(); |
| 3027 | + binder.bind(Session.Definition.class).toInstance(session); |
| 3028 | + Object sstore = session.store(); |
| 3029 | + if (sstore instanceof Class) { |
| 3030 | + binder.bind(Session.Store.class).to((Class<? extends Store>) sstore) |
| 3031 | + .asEagerSingleton(); |
| 3032 | + } else { |
| 3033 | + binder.bind(Session.Store.class).toInstance((Store) sstore); |
| 3034 | + } |
3032 | 3035 |
|
3033 | | - binder.bind(Request.class).toProvider(Providers.outOfScope(Request.class)) |
3034 | | - .in(RequestScoped.class); |
| 3036 | + binder.bind(Request.class).toProvider(Providers.outOfScope(Request.class)) |
| 3037 | + .in(RequestScoped.class); |
3035 | 3038 |
|
3036 | | - binder.bind(Response.class).toProvider(Providers.outOfScope(Response.class)) |
3037 | | - .in(RequestScoped.class); |
| 3039 | + binder.bind(Response.class).toProvider(Providers.outOfScope(Response.class)) |
| 3040 | + .in(RequestScoped.class); |
3038 | 3041 |
|
3039 | | - binder.bind(Session.class).toProvider(Providers.outOfScope(Session.class)) |
3040 | | - .in(RequestScoped.class); |
| 3042 | + binder.bind(Session.class).toProvider(Providers.outOfScope(Session.class)) |
| 3043 | + .in(RequestScoped.class); |
3041 | 3044 |
|
3042 | | - // def err |
3043 | | - ehandlers.addBinding().toInstance(new Err.DefHandler()); |
3044 | | - }); |
| 3045 | + /** def err */ |
| 3046 | + ehandlers.addBinding().toInstance(new Err.DefHandler()); |
| 3047 | + }); |
3045 | 3048 |
|
3046 | 3049 | return injector; |
3047 | 3050 | } |
|
0 commit comments