act.inject.ActProviders Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of act Show documentation
Show all versions of act Show documentation
The ACT full stack MVC framework
package act.inject;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.Act;
import act.app.*;
import act.cli.CliContext;
import act.cli.CliOverHttpContext;
import act.cli.CliSession;
import act.conf.AppConfig;
import act.crypto.AppCrypto;
import act.db.DB;
import act.db.Dao;
import act.db.DbPlugin;
import act.db.DbService;
import act.event.EventBus;
import act.job.JobContext;
import act.mail.MailerContext;
import act.plugin.PrincipalProvider;
import act.route.Router;
import act.util.ActContext;
import act.util.ClassInfoRepository;
import act.util.ProgressGauge;
import act.util.SimpleProgressGauge;
import act.view.ViewManager;
import act.ws.SecureTicketCodec;
import act.ws.WebSocketContext;
import org.osgl.$;
import org.osgl.cache.CacheService;
import org.osgl.exception.NotAppliedException;
import org.osgl.http.H;
import org.osgl.inject.NamedProvider;
import org.osgl.logging.Logger;
import org.osgl.util.E;
import org.osgl.util.S;
import org.osgl.web.util.UserAgent;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import javax.inject.Provider;
/**
* Name space of built in providers
*/
@SuppressWarnings("unused")
public final class ActProviders {
private ActProviders() {}
private static Set providedTypes;
public static void classInit(App app) {
providedTypes = app.createSet();
init(providedTypes);
}
public static void testClassInit() {
providedTypes = new HashSet<>();
init(providedTypes);
}
private static void init(final Set providedTypes) {
registerBuiltInNamedProviders(ActProviders.class, new $.F2() {
@Override
public Void apply(Class aClass, NamedProvider provider) throws NotAppliedException, $.Break {
providedTypes.add(aClass);
return null;
}
});
registerBuiltInProviders(ActProviders.class, new $.F2() {
@Override
public Void apply(Class aClass, Provider provider) throws NotAppliedException, $.Break {
providedTypes.add(aClass);
return null;
}
});
}
public static final Provider APP = new Provider() {
@Override
public App get() {
return app();
}
};
public static final Provider ACTION_CONTEXT = new Provider() {
@Override
public ActionContext get() {
return ActionContext.current();
}
};
public static final Provider SESSION = new Provider() {
@Override
public H.Session get() {
return ActionContext.current().session();
}
};
public static final Provider FLASH = new Provider() {
@Override
public H.Flash get() {
return ActionContext.current().flash();
}
};
public static final Provider REQUEST = new Provider() {
@Override
public H.Request get() {
return ActionContext.current().req();
}
};
public static final Provider RESPONSE = new Provider() {
@Override
public H.Response get() {
return ActionContext.current().resp();
}
};
public static final Provider CLI_CONTEXT = new Provider() {
@Override
public CliContext get() {
return CliContext.current();
}
};
public static final Provider CLI_SESSION = new Provider() {
@Override
public CliSession get() {
CliContext context = CliContext.current();
return null == context ? null : context.session();
}
};
public static final Provider CLASS_INFO_REPO = new Provider() {
@Override
public ClassInfoRepository get() {
return Act.app().classLoader().classInfoRepository();
}
};
public static final Provider PROGRESS_GAUGE = new Provider() {
@Override
public ProgressGauge get() {
ActContext.Base context = ActContext.Base.currentContext();
if (null != context) {
return context.progress();
}
return new SimpleProgressGauge();
}
};
public static final Provider MAILER_CONTEXT = new Provider() {
@Override
public MailerContext get() {
return MailerContext.current();
}
};
public static final Provider WEB_SOCKET_CONTEXT = new Provider() {
@Override
public WebSocketContext get() {
return WebSocketContext.current();
}
};
public static final Provider ACT_CONTEXT = new Provider() {
@Override
public ActContext get() {
ActContext ctx = MailerContext.current();
if (null != ctx) {
return ctx;
}
ctx = WebSocketContext.current();
if (null != ctx) {
return ctx;
}
ctx = ActionContext.current();
if (null != ctx) {
return ctx;
}
ctx = CliContext.current();
if (null != ctx) {
return ctx;
}
ctx = CliOverHttpContext.current();
if (null != ctx) {
return ctx;
}
return JobContext.current();
}
};
public static final Provider LOGGER = new Provider() {
@Override
public Logger get() {
return Act.LOGGER;
}
};
public static final Provider USER_AGENT = new Provider() {
@Override
public UserAgent get() {
ActionContext actionContext = ActionContext.current();
if (null == actionContext) {
throw new IllegalStateException();
}
return actionContext.userAgent();
}
};
public static final Provider APP_CONFIG = new Provider() {
@Override
public AppConfig get() {
return app().config();
}
};
public static final Provider APP_CRYPTO = new Provider() {
@Override
public AppCrypto get() {
return app().crypto();
}
};
public static final Provider APP_CACHE_SERVICE = new Provider() {
@Override
public CacheService get() {
return app().cache();
}
};
public static final Provider DB_SERVICE_PROVIDER = new Provider() {
@Override
public DbService get() {
return app().dbServiceManager().dbService(DB.DEFAULT);
}
};
public static final NamedProvider NAMED_DB_SERVICE_PROVIDER = new NamedProvider() {
@Override
public DbService get(String name) {
return app().dbServiceManager().dbService(name);
}
};
public static final NamedProvider APP_NAMED_CACHE_SERVICE = new NamedProvider() {
@Override
public CacheService get(String name) {
return app().cache(name);
}
};
public static final NamedProvider COOKIE = new NamedProvider() {
@Override
public H.Cookie get(String s) {
ActionContext ctx = ActionContext.current();
if (null == ctx) {
return null;
}
H.Request req = ctx.req();
return null == req ? null : req.cookie(S.ensure(s).strippedOff("", "Cookie"));
}
};
public static final Provider ROUTER_PROVIDER = new Provider() {
@Override
public Router get() {
ActionContext ctx = ActionContext.current();
return null == ctx ? Act.app().router() : ctx.router();
}
};
public static final NamedProvider NAMED_ROUTER_PROVIDER = new NamedProvider() {
@Override
public Router get(String name) {
Router router = Act.app().router(name);
return null == router ? (Router) Act.injector().get(Router.class) : router;
}
};
public static final Provider VIEW_MANAGER = new Provider() {
@Override
public ViewManager get() {
return Act.viewManager();
}
};
public static final Provider EVENT_BUS = new Provider() {
@Override
public EventBus get() {
return app().eventBus();
}
};
public static final Provider LOCALE = new Provider() {
@Override
public Locale get() {
ActContext context = ActContext.Base.currentContext();
return null != context ? context.locale(true) : app().config().locale();
}
};
public static final Provider SECURE_TICKET_CODEC_PROVIDER = new Provider() {
@Override
public SecureTicketCodec get() {
AppConfig config = Act.appConfig();
return config.secureTicketCodec();
}
};
public static final Provider PRINCIPAL_PROVIDER = new Provider() {
@Override
public PrincipalProvider get() {
return Act.app().principalProvider();
}
};
public static void registerBuiltInProviders(Class providersClass, $.Func2 register) {
for (Field field : providersClass.getDeclaredFields()) {
try {
if (Provider.class.isAssignableFrom(field.getType())) {
field.setAccessible(true);
Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType ptype = $.cast(field.getGenericType());
Provider provider = $.cast(field.get(null));
register.apply((Class) ptype.getActualTypeArguments()[0], provider);
}
}
} catch (Exception e) {
throw E.unexpected(e);
}
}
}
public static void registerBuiltInNamedProviders(Class providersClass, $.Func2 register) {
for (Field field : providersClass.getDeclaredFields()) {
try {
if (NamedProvider.class.isAssignableFrom(field.getType())) {
field.setAccessible(true);
Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType ptype = $.cast(field.getGenericType());
NamedProvider provider = $.cast(field.get(null));
register.apply((Class) ptype.getActualTypeArguments()[0], provider);
}
}
} catch (Exception e) {
throw E.unexpected(e);
}
}
}
public static boolean isProvided(Class aClass) {
return providedTypes.contains(aClass) || Dao.class.isAssignableFrom(aClass);
}
private static App app() {
return App.instance();
}
public static void addProvidedType(Class aClass) {
providedTypes.add(aClass);
}
}