home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (ProvisionListenerStackCallback.java)

This example Java source code file (ProvisionListenerStackCallback.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

already, binding, empty_callback, empty_listener, errorsexception, illegalstateexception, internalcontext, override, provisioncallback, provisionlistener, provisionlistenerstackcallback, set, suppresswarnings, util

The ProvisionListenerStackCallback.java Java example source code

 * Copyright (C) 2011 Google Inc.
 * 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package com.google.inject.internal;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.inject.Binding;
import com.google.inject.ProvisionException;
import com.google.inject.spi.DependencyAndSource;
import com.google.inject.spi.ProvisionListener;

import java.util.List;
import java.util.Set;

 * Intercepts provisions with a stack of listeners.
 * @author sameb@google.com (Sam Berlin)
final class ProvisionListenerStackCallback<T> {
  private static final ProvisionListener EMPTY_LISTENER[] = new ProvisionListener[0];
  private static final ProvisionListenerStackCallback<?> EMPTY_CALLBACK =
      new ProvisionListenerStackCallback(null /* unused, so ok */, ImmutableList.of());

  private final ProvisionListener[] listeners;
  private final Binding<T> binding;
  public static <T> ProvisionListenerStackCallback emptyListener() {
    return (ProvisionListenerStackCallback<T>) EMPTY_CALLBACK;

  public ProvisionListenerStackCallback(Binding<T> binding, List listeners) {
    this.binding = binding;
    if (listeners.isEmpty()) {
      this.listeners = EMPTY_LISTENER;
    } else {
      Set<ProvisionListener> deDuplicated = Sets.newLinkedHashSet(listeners);
      this.listeners = deDuplicated.toArray(new ProvisionListener[deDuplicated.size()]);
  public boolean hasListeners() {
    return listeners.length > 0;

  public T provision(Errors errors, InternalContext context, ProvisionCallback<T> callable)
      throws ErrorsException {
    Provision provision = new Provision(errors, context, callable);
    RuntimeException caught = null;
    try {
    } catch(RuntimeException t) {
      caught = t;
    if (provision.exceptionDuringProvision != null) {
      throw provision.exceptionDuringProvision;
    } else if (caught != null) {
      Object listener = provision.erredListener != null ?
          provision.erredListener.getClass() : "(unknown)";
      throw errors
          .errorInUserCode(caught, "Error notifying ProvisionListener %s of %s.%n"
              + " Reason: %s", listener, binding.getKey(), caught)
    } else {
      return provision.result;

  // TODO(sameb): Can this be more InternalFactory-like?
  public interface ProvisionCallback<T> {
    public T call() throws ErrorsException;

  private class Provision extends ProvisionListener.ProvisionInvocation<T> {

    final Errors errors;
    final int numErrorsBefore;
    final InternalContext context;
    final ProvisionCallback<T> callable;
    int index = -1;
    T result;
    ErrorsException exceptionDuringProvision;
    ProvisionListener erredListener;

    public Provision(Errors errors, InternalContext context, ProvisionCallback<T> callable) {
      this.callable = callable;
      this.context = context;
      this.errors = errors;
      this.numErrorsBefore = errors.size();

    public T provision() {
      if (index == listeners.length) {
        try {
          result = callable.call();
          // Make sure we don't return the provisioned object if there were any errors
          // injecting its field/method dependencies.
        } catch(ErrorsException ee) {
          exceptionDuringProvision = ee;
          throw new ProvisionException(errors.merge(ee.getErrors()).getMessages());
      } else if (index < listeners.length) {
        int currentIdx = index;
        try {
        } catch(RuntimeException re) {
          erredListener = listeners[currentIdx];
          throw re;
        if (currentIdx == index) {
          // Our listener didn't provision -- do it for them.
      } else {
        throw new IllegalStateException("Already provisioned in this listener.");
      return result;
    public Binding<T> getBinding() {
      // TODO(sameb): Because so many places cast directly to BindingImpl & subclasses,
      // we can't decorate this to prevent calling getProvider().get(), which means
      // if someone calls that they'll get strange errors.
      return binding;
    public List<DependencyAndSource> getDependencyChain() {
      return context.getDependencyChain();

Other Java examples (source code examples)

Here is a short list of links related to this Java ProvisionListenerStackCallback.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.