@Override public String toString() { return providerKey.toString(); } }
void error(Key<T> key, Throwable x) { if (verbose) { LOGGER.log(Level.WARNING, "Failed to instantiate " + key + "; skipping this component", x); } else { LOGGER.log(Level.INFO, "Failed to instantiate optional component {0}; skipping", key.getTypeLiteral()); LOGGER.log(Level.FINE, key.toString(), x); } } };
public static <T> Provider<T> outOfScope(final Key<T> key) { return () -> { throw new OutOfScopeException(key.toString()); }; }
@Override public String getServletName() { return servletKey.toString(); }
String getKey() { return servletKey.toString(); } }
@Override public String getFilterName() { return filterKey.toString(); }
@Override public <T> Provider<T> scope(final Key<T> key, final Provider<T> creator) { final String name = key.toString(); return new Provider<T>() { @Override public T get() { HttpSession session = GuiceFilter.getRequest(key).getSession(); synchronized (session) { Object obj = session.getAttribute(name); if (NullObject.INSTANCE == obj) { return null; } @SuppressWarnings("unchecked") T t = (T) obj; if (t == null) { t = creator.get(); if (!Scopes.isCircularProxy(t)) { session.setAttribute(name, (t != null) ? t : NullObject.INSTANCE); } } return t; } } @Override public String toString() { return String.format("%s[%s]", creator, SESSION); } }; }
private static RequestScoper continueRequest(Map<Key<?>, Object> seedMap) { Preconditions.checkArgument( null != seedMap, "Seed map cannot be null, try passing in Collections.emptyMap() instead."); // Snapshot the seed map and add all the instances to our continuing HTTP request. final ContinuingHttpServletRequest continuingRequest = new ContinuingHttpServletRequest(GuiceFilter.getRequest(Key.get(HttpServletRequest.class))); for (Map.Entry<Key<?>, Object> entry : seedMap.entrySet()) { Object value = validateAndCanonicalizeValue(entry.getKey(), entry.getValue()); continuingRequest.setAttribute(entry.getKey().toString(), value); } return new RequestScoper() { @Override public CloseableScope open() { checkScopingState( null == GuiceFilter.localContext.get(), "Cannot continue request in the same thread as a HTTP request!"); return new GuiceFilter.Context(continuingRequest, continuingRequest, null).open(); } }; }
String name = key.toString(); synchronized (request) { Object obj = request.getAttribute(name);
assertEquals(Key.get(HttpServlet.class).toString(), mockServlet.getServletName());
assertTrue(null != filterConfig); assertEquals(contextName, filterConfig.getServletContext().getServletContextName()); assertEquals(filterConfig.getFilterName(), Key.get(Filter.class).toString());
public void testHttpSessionIsSerializable() throws Exception { final Injector injector = createInjector(); final HttpServletRequest request = newFakeHttpServletRequest(); final HttpSession session = request.getSession(); GuiceFilter filter = new GuiceFilter(); final boolean[] invoked = new boolean[1]; FilterChain filterChain = new FilterChain() { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) { invoked[0] = true; assertNotNull(injector.getInstance(InSession.class)); assertNull(injector.getInstance(IN_SESSION_NULL_KEY)); } }; filter.doFilter(request, null, filterChain); assertTrue(invoked[0]); HttpSession deserializedSession = reserialize(session); String inSessionKey = IN_SESSION_KEY.toString(); String inSessionNullKey = IN_SESSION_NULL_KEY.toString(); assertTrue(deserializedSession.getAttribute(inSessionKey) instanceof InSession); assertEquals(NullObject.INSTANCE, deserializedSession.getAttribute(inSessionNullKey)); }
@Override public String toString() { return providerKey.toString(); } }
@Override public String toString() { return providerKey.toString(); } }
@Override public synchronized String toString() { if (toString == null) { toString = String.format("ThreadDelegatedScoped provider (Key: %s) of %s", key.toString(), unscoped.toString()); } return toString; } }
@Override public synchronized String toString() { if (toString == null) { toString = String.format("ThreadDelegatedScoped provider (Key: %s) of %s", key.toString(), unscoped.toString()); } return toString; } }
void error(Key<T> key, Throwable x) { if (verbose) { LOGGER.log(Level.WARNING, "Failed to instantiate " + key + "; skipping this component", x); } else { LOGGER.log(Level.INFO, "Failed to instantiate optional component {0}; skipping", key.getTypeLiteral()); LOGGER.log(Level.FINE, key.toString(), x); } } };
@Override public String getKey() { return binding.getKey().toString(); }
@Override public void visit(Element entry) { final List<Node> children = new ArrayList<Node>(); stack.peek().add(new Node(entry.getKey().toString(), children, entry.getTotalDuration(TimeUnit.MILLISECONDS))); stack.push(children); entry.accept(this); stack.pop(); } });
@Override public Void visit(PrivateElements privateElements) { for (Key<?> key : privateElements.getExposedKeys()) { if (key.getAnnotation() != null) { throw new IllegalStateException("Non singleton service " + service.getName() + " cannot expose " + key.toString() + ". Only services marked with @Singleton" + " can expose bindings with annotation."); } } return null; } });