下面列出了com.google.inject.servlet.RequestScoped#com.google.inject.binder.AnnotatedBindingBuilder 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static List<Module> newModuleList(final Application app, final Class<? extends Application> appClass,
Module... guiceModules) {
List<Module> lst = new LinkedList<>();
lst.addAll(Arrays.asList(guiceModules));
lst.add(new AbstractModule() {
@Override
public void configure() {
AnnotatedBindingBuilder<Application> builder = bind(Application.class);
if (app != null) {
builder.toInstance(app);
} else {
builder.to(appClass);
}
}
});
return lst;
}
@Override
public Injector getInjector() {
Injector _xblockexpression = null;
{
TerminalsStandaloneSetup.doSetup();
XtextRuntimeModule _xtextRuntimeModule = new XtextRuntimeModule();
_xblockexpression = Guice.createInjector(_xtextRuntimeModule, new com.google.inject.Module() {
@Override
public void configure(final Binder binder) {
AnnotatedBindingBuilder<IXtextProjectConfig> _bind = binder.<IXtextProjectConfig>bind(IXtextProjectConfig.class);
StandardProjectConfig _standardProjectConfig = new StandardProjectConfig();
_bind.toInstance(_standardProjectConfig);
}
});
}
return _xblockexpression;
}
@SuppressWarnings({"unchecked", "rawtypes"})
@Before
public void before() {
PrivateBinder binder = mock(PrivateBinder.class);
AnnotatedBindingBuilder ab = mock(AnnotatedBindingBuilder.class);
when(binder.bind(any(Class.class))).thenReturn(ab);
when(ab.annotatedWith(any(Annotation.class))).thenReturn(ab);
AnnotatedElementBuilder aeb = mock(AnnotatedElementBuilder.class);
when(binder.expose(any(Class.class))).thenReturn(aeb);
ScopedBindingBuilder sb = mock(ScopedBindingBuilder.class);
when(ab.toProvider(any(Provider.class))).thenReturn(sb);
when(binder.bind(any(TypeLiteral.class))).thenReturn(ab);
when(binder.skipSources(any(Class.class), any(Class.class))).thenReturn(binder);
when(binder.skipSources(any(Class.class))).thenReturn(binder);
securityConfigurer = mock(SecurityConfigurer.class);
underTest = new SecurityInternalModule(securityConfigurer, new HashMap<>());
Whitebox.setInternalState(underTest, "binder", binder);
}
void apply(Binder binder) {
if (from != null) {
AnnotatedBindingBuilder<T> bind = binder.bind(from);
if (!from.isAssignableFrom(target)) {
throw SeedException.createNew(CoreErrorCode.INVALID_BINDING)
.put("key", from)
.put("target", target);
}
if (qualifier != null) {
LOGGER.debug("Binding {} annotated with {} to {}", from.getName(), qualifier, target.getName());
bind.annotatedWith(qualifier).to(target);
} else {
LOGGER.debug("Binding {} to {}", from.getName(), target.getName());
bind.to(target);
}
} else {
if (qualifier != null) {
LOGGER.debug("Binding {} annotated with {} to itself", target.getName(), qualifier);
binder.bind(target).annotatedWith(qualifier);
} else {
LOGGER.debug("Binding {} to itself", target.getName());
binder.bind(target);
}
}
}
@Override
protected void bindSessionManager(AnnotatedBindingBuilder<SessionManager> bind) {
shiroSessionListeners = Multibinder.newSetBinder(binder(), SessionListener.class, Names.named(GuicySessionManager.PROP_SESSION_LISTENERS));
shiroSessionListeners.addBinding().to(ShiroSessionRegistryExpirer.class).asEagerSingleton();
bind.to(GuicySessionManager.class).asEagerSingleton();
bind(GuicySessionManager.class);
bindRealm().to(AppHandoffRealm.class);
}
@Override
protected void bindSessionManager(AnnotatedBindingBuilder<SessionManager> bind) {
bind.to(DefaultSessionManager.class).asEagerSingleton();
bind(DefaultSessionManager.class);
bindConstant().annotatedWith(Names.named("shiro.globalSessionTimeout")).to(globalSessionTimeoutInSecs * 1000L);
bindConstant().annotatedWith(Names.named("shiro.sessionValidationSchedulerEnabled")).to(false);
}
@Override
protected void bindSecurityManager(AnnotatedBindingBuilder<? super SecurityManager> bind) {
try {
bind.toConstructor(DefaultSecurityManager.class.getConstructor(Collection.class)).asEagerSingleton();
} catch (NoSuchMethodException e) {
throw new ConfigurationException("This really shouldn't happen. Either something has changed in Shiro, or there's a bug in " + ShiroModule.class.getSimpleName(), e);
}
}
@Override
public com.google.inject.Module getServerModule() {
ServerModule _serverModule = new ServerModule();
final com.google.inject.Module _function = (Binder it) -> {
AnnotatedBindingBuilder<IMultiRootWorkspaceConfigFactory> _bind = it.<IMultiRootWorkspaceConfigFactory>bind(IMultiRootWorkspaceConfigFactory.class);
_bind.toInstance(new MultiRootWorkspaceConfigFactory() {
@Override
public void addProjectsForWorkspaceFolder(final WorkspaceConfig workspaceConfig, final WorkspaceFolder workspaceFolder, final Set<String> existingNames) {
String _uri = null;
if (workspaceFolder!=null) {
_uri=workspaceFolder.getUri();
}
boolean _tripleNotEquals = (_uri != null);
if (_tripleNotEquals) {
URI _uri_1 = this.getUriExtensions().toUri(workspaceFolder.getUri());
String _uniqueProjectName = this.getUniqueProjectName(workspaceFolder.getName(), existingNames);
final FileProjectConfig project = new FileProjectConfig(_uri_1, _uniqueProjectName) {
@Override
public boolean isIndexOnly() {
return true;
}
};
project.addSourceFolder(".");
workspaceConfig.addProject(project);
}
}
});
};
return Modules2.mixin(_serverModule, _function);
}
@Override
protected void bindWebSecurityManager(final AnnotatedBindingBuilder<? super WebSecurityManager> bind) {
bind(NexusWebSecurityManager.class).asEagerSingleton();
// bind RealmSecurityManager and WebSecurityManager to _same_ component
bind(RealmSecurityManager.class).to(NexusWebSecurityManager.class);
bind.to(NexusWebSecurityManager.class);
// bindings used by external modules
expose(RealmSecurityManager.class);
expose(WebSecurityManager.class);
}
@Override
protected void bindSessionManager(final AnnotatedBindingBuilder<SessionManager> bind) {
// use native web session management instead of delegating to servlet container
// workaround for NEXUS-5727, see NexusDefaultWebSessionManager javadoc for clues
bind.to(NexusWebSessionManager.class).asEagerSingleton();
// this is a PrivateModule, so explicitly binding the NexusDefaultSessionManager class
bind(NexusWebSessionManager.class);
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void testShiroModule() {
SecurityGuiceConfigurer securityGuiceConfigurer = mock(SecurityGuiceConfigurer.class);
DefaultSecurityModule underTest = new DefaultSecurityModule(securityGuiceConfigurer);
PrivateBinder b = mock(PrivateBinder.class);
AnnotatedBindingBuilder ab = mock(AnnotatedBindingBuilder.class);
when(b.bind(any(Class.class))).thenReturn(ab);
when(ab.annotatedWith(any(Annotation.class))).thenReturn(ab);
AnnotatedElementBuilder aeb = mock(AnnotatedElementBuilder.class);
when(b.expose(any(Class.class))).thenReturn(aeb);
Whitebox.setInternalState(underTest, "binder", b);
underTest.configureShiro();
}
@SuppressWarnings("unchecked")
public void apply(Binder binder) {
AnnotatedBindingBuilder<T> bind = binder.bind((TypeLiteral<T>) TypeLiteral.get(from));
if (qualifier != null) {
LOGGER.debug("Binding {} annotated with {} to provider {}",
from.getTypeName(),
qualifier,
target.getName());
bind.annotatedWith(qualifier).toProvider(target);
} else {
LOGGER.debug("Binding {} to provider {}", from.getTypeName(), target.getName());
bind.toProvider(target);
}
}
protected <T> AnnotatedBindingBuilder<T> bindAndExpose(final Class<T> type) {
this.expose(type);
return this.bind(type);
}
protected <T> AnnotatedBindingBuilder<T> bindAndExpose(final Class<T> type) {
this.expose(type);
return this.bind(type);
}
@Override
protected void bindAuthenticationDAO(AnnotatedBindingBuilder<AuthenticationDAO> bind) {
bind.to(PersonDAOImpl.class);
}
protected void bindSessionDAO(AnnotatedBindingBuilder<SessionDAO> bind) {
bind.to(GuicedCassandraSessionDAO.class);
}
protected void bindCacheManager(AnnotatedBindingBuilder<CacheManager> bind) {
bind.to(MemoryConstrainedCacheManager.class);
}
protected void bindAuthorizationDAO(AnnotatedBindingBuilder<AuthorizationDAO> bind) {
bind.to(NoopAuthorizationDAOImpl.class);
}
protected void bindPrincipalResolver(AnnotatedBindingBuilder<PrincipalResolver> bind) {
bind.to(DefaultPrincipalResolver.class);
}
protected void bindCredentialsHashingStrategy(AnnotatedBindingBuilder<CredentialsHashingStrategy> bind) {
bind.to(Sha256CredentialsHashingStrategy.class);
}
@Override
protected void bindAuthenticationDAO(AnnotatedBindingBuilder<AuthenticationDAO> bind) {
bind.toInstance(EasyMock.createMock(AuthenticationDAO.class));
expose(AuthenticationDAO.class);
}
@Override
protected void bindSessionDAO(AnnotatedBindingBuilder<SessionDAO> bind) {
bind.toInstance(EasyMock.createMock(SessionDAO.class));
expose(SessionDAO.class);
}
@Override
protected void bindCredentialsHashingStrategy(
AnnotatedBindingBuilder<CredentialsHashingStrategy> bind) {
bind.to(PlainCredentialsHashingStrategy.class).asEagerSingleton();
}
protected <T extends BaseComponent> AnnotatedBindingBuilder<ComponentRenderer<? super T>> bindComponent(Class<T> componentType) {
return bind(ComponentRenderers.rendererType(componentType));
}
public void configureIXtextProjectConfig(final Binder binder) {
AnnotatedBindingBuilder<IXtextProjectConfig> _bind = binder.<IXtextProjectConfig>bind(IXtextProjectConfig.class);
AbstractGeneratorFragmentTests.FakeProjectConfig _fakeProjectConfig = new AbstractGeneratorFragmentTests.FakeProjectConfig();
_bind.toInstance(_fakeProjectConfig);
}
private static void bindHealthCheckInvocationStrategy(AnnotatedBindingBuilder<HealthCheckInvocationStrategy> bindingBuilder) {
bindingBuilder.to(SyncHealthCheckInvocationStrategy.class);
}
/**
* Bind to guice context. Singleton scope will be forced if it's not disabled (
* {@link ru.vyarus.dropwizard.guice.module.installer.InstallersOptions#ForceSingletonForJerseyExtensions}) and
* if no explicit scope is declared with annotation on bean.
*
* @param binder guice binder
* @param type extension type
*/
protected void bindInGuice(final Binder binder, final Class<?> type) {
final AnnotatedBindingBuilder<?> binding = binder.bind(type);
if (isForceSingleton(type, false)) {
// force singleton only if no explicit scope annotation present
binding.in(Singleton.class);
}
}
protected abstract void bindAuthenticationDAO(AnnotatedBindingBuilder<AuthenticationDAO> bind);