Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.cognifide.slice.core.internal.injector;
   * #%L
   * Slice - Core
   * $Id:$
   * $HeadURL:$
   * %%
   * Copyright (C) 2012 Cognifide Limited
  * %%
  * 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
  * 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%
 // @formatter:off

RafaƂ Malinowski
InjectorWithContext Decoration for Guice Injector class with simple access to modyfing and restoring ContextProvider. For use in Servlets or Services use following idiom: final InjectorWithContext injector = InjectorsRepository.getInjector(APP_NAME); final SimpleContextProvider simpleContextProvider = new SimpleContextProvider(); simpleContextProvider.setContext(ContextFactory.getServletRequestContext(request, response)); injector.pushContextProvider(simpleContextProvider); try { ... } finally { injector.popContextProvider(); } to ensure that injector is left in a state that is was before. This decoration has two delegate getInstance() methods added for convenience.
 // @formatter:on
 public class InjectorWithContextImpl implements InjectorWithContext {
 	private final Injector injector;
 	private final ContextScope contextScope;
Create decoration for Injector.

injector Injector to decorate
 	public InjectorWithContextImpl(final Injector injector) {
 		this. = injector;
 		this. = injector.getInstance(ContextScope.class);

Return decorated Injector.

decorated Injector.
 	public Injector getInjector() {
 		return ;

Set new ContextProvider for decorated Injector. Push previous one on internal stack.

contextProvider new ContextProvider
 	public void pushContextProvider(final ContextProvider contextProvider) {

Restore previous ContextProvider for decorated Injector. Returns current one.

current ContextProvider
		return result;
	public <T> T getInstance(final Class<T> clazz) {
		return .getInstance(clazz);
	public Object getInstance(final Key<?> key) {
		return .getInstance(key);
New to GrepCode? Check out our FAQ X