Implemented explicit preloading. Updated copyright notices.

git-svn-id: https://google-guice.googlecode.com/svn/trunk@47 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/src/com/google/inject/AbstractErrorHandler.java b/src/com/google/inject/AbstractErrorHandler.java
index 747f342..6c77047 100644
--- a/src/com/google/inject/AbstractErrorHandler.java
+++ b/src/com/google/inject/AbstractErrorHandler.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * Copyright (C) 2006 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,
+ * 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.
+ */
 
 package com.google.inject;
 
diff --git a/src/com/google/inject/AbstractModule.java b/src/com/google/inject/AbstractModule.java
index e876e83..fe1e1f5 100644
--- a/src/com/google/inject/AbstractModule.java
+++ b/src/com/google/inject/AbstractModule.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * Copyright (C) 2006 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,
+ * 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.
+ */
 
 package com.google.inject;
 
diff --git a/src/com/google/inject/ContainerBuilder.java b/src/com/google/inject/ContainerBuilder.java
index c8cf940..c8bd224 100644
--- a/src/com/google/inject/ContainerBuilder.java
+++ b/src/com/google/inject/ContainerBuilder.java
@@ -315,7 +315,7 @@
 
     stopwatch.resetAndLog(logger, "Static validation");
 
-    // Blow up.
+    // Blow up if we encountered errors.
     if (!errorMessages.isEmpty()) {
       throw new ContainerCreationException(createErrorMessage());
     }
@@ -331,23 +331,25 @@
     stopwatch.resetAndLog(logger, "Static member injection");
 
     // Run preloading commands.
-    if (preload) {
-      container.callInContext(new ContainerImpl.ContextualCallable<Void>() {
-        public Void call(InternalContext context) {
-          for (ContainerImpl.ContextualCallable<Void> preloader
-              : preloaders) {
-            preloader.call(context);
-          }
-          return null;
-        }
-      });
-    }
+    runPreloaders(container, preloaders);
 
     stopwatch.resetAndLog(logger, "Preloading");
 
     return container;
   }
 
+  private void runPreloaders(ContainerImpl container,
+      final List<ContainerImpl.ContextualCallable<Void>> preloaders) {
+    container.callInContext(new ContainerImpl.ContextualCallable<Void>() {
+      public Void call(InternalContext context) {
+        for (ContainerImpl.ContextualCallable<Void> preloader : preloaders) {
+          preloader.call(context);
+        }
+        return null;
+      }
+    });
+  }
+
   private String createErrorMessage() {
     StringBuilder error = new StringBuilder();
     error.append("Guice configuration errors:\n\n");
@@ -396,20 +398,14 @@
       final InternalFactory<?> factory = builder.getInternalFactory(container);
       putFactory(builder.getSource(), factories, key, factory);
 
-      if (preload && builder.isInContainerScope()) {
-        preloaders.add(new ContainerImpl.ContextualCallable<Void>() {
-          public Void call(InternalContext context) {
-            context.setExternalContext(
-                ExternalContext.newInstance(null, key,
-                    context.getContainerImpl()));
-            try {
-              factory.get(context);
-              return null;
-            } finally {
-              context.setExternalContext(null);
-            }
-          }
-        });
+      if (builder.isInContainerScope()) {
+        if (preload || builder.shouldPreload()) {
+          preloaders.add(new BindingPreloader(key, factory));
+        }
+      } else {
+        if (builder.shouldPreload()) {
+          addError(builder.getSource(), ErrorMessage.PRELOAD_NOT_ALLOWED);
+        }
       }
     }
   }
@@ -459,6 +455,7 @@
     Key<T> key;
     InternalFactory<? extends T> factory;
     Scope scope;
+    boolean preload = false;
 
     BindingBuilder(Key<T> key) {
       this.key = nonNull(key, "key");
@@ -601,6 +598,20 @@
       }
     }
 
+    /**
+     * Instructs the builder to eagerly load this binding when it creates
+     * the container. Useful for application initialization logic. Currently
+     * only supported for container-scoped bindings.
+     */
+    public BindingBuilder<T> preload() {
+      this.preload = true;
+      return this;
+    }
+
+    boolean shouldPreload() {
+      return preload;
+    }
+
     InternalFactory<? extends T> getInternalFactory(
         final ContainerImpl container) {
       // If an implementation wasn't specified, use the injection type.
@@ -922,4 +933,28 @@
       });
     }
   }
+
+  static class BindingPreloader
+      implements ContainerImpl.ContextualCallable<Void> {
+
+    private final Key<?> key;
+    private final InternalFactory<?> factory;
+
+    public BindingPreloader(Key<?> key, InternalFactory<?> factory) {
+      this.key = key;
+      this.factory = factory;
+    }
+
+    public Void call(InternalContext context) {
+      ExternalContext<?> externalContext =
+          ExternalContext.newInstance(null, key, context.getContainerImpl());
+      context.setExternalContext(externalContext);
+      try {
+        factory.get(context);
+        return null;
+      } finally {
+        context.setExternalContext(null);
+      }
+    }
+  }
 }
diff --git a/src/com/google/inject/ContainerCreationException.java b/src/com/google/inject/ContainerCreationException.java
index 1cbf14f..08374e9 100644
--- a/src/com/google/inject/ContainerCreationException.java
+++ b/src/com/google/inject/ContainerCreationException.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * Copyright (C) 2006 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,
+ * 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.
+ */
 
 package com.google.inject;
 
diff --git a/src/com/google/inject/ContainerScope.java b/src/com/google/inject/ContainerScope.java
index e6dcd40..35c8e11 100644
--- a/src/com/google/inject/ContainerScope.java
+++ b/src/com/google/inject/ContainerScope.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * Copyright (C) 2006 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,
+ * 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.
+ */
 
 package com.google.inject;
 
diff --git a/src/com/google/inject/ErrorHandler.java b/src/com/google/inject/ErrorHandler.java
index a8a54c7..a082210 100644
--- a/src/com/google/inject/ErrorHandler.java
+++ b/src/com/google/inject/ErrorHandler.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * Copyright (C) 2006 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,
+ * 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.
+ */
 
 package com.google.inject;
 
diff --git a/src/com/google/inject/ErrorMessage.java b/src/com/google/inject/ErrorMessage.java
index dc565a4..fd2ed5d 100644
--- a/src/com/google/inject/ErrorMessage.java
+++ b/src/com/google/inject/ErrorMessage.java
@@ -80,9 +80,13 @@
       + " between @Inject on member and parameter: %s. Please remove the name"
       + " from the member-level @Inject or remove @Inject from the parameter.";
 
+  static final String PRELOAD_NOT_ALLOWED = "Preloading is only supported for"
+      + " container-scoped bindings.";
+
   final Object source;
   final String message;
 
+
   public ErrorMessage(Object source, String message) {
     this.source = nonNull(source, "source");
     this.message = nonNull(message, "message");
diff --git a/src/com/google/inject/FactoryToInternalFactoryAdapter.java b/src/com/google/inject/FactoryToInternalFactoryAdapter.java
index a7bca2b..818d1b5 100644
--- a/src/com/google/inject/FactoryToInternalFactoryAdapter.java
+++ b/src/com/google/inject/FactoryToInternalFactoryAdapter.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * Copyright (C) 2006 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,
+ * 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.
+ */
 
 package com.google.inject;
 
diff --git a/src/com/google/inject/InternalFactoryToFactoryAdapter.java b/src/com/google/inject/InternalFactoryToFactoryAdapter.java
index 318be41..9abae5f 100644
--- a/src/com/google/inject/InternalFactoryToFactoryAdapter.java
+++ b/src/com/google/inject/InternalFactoryToFactoryAdapter.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * Copyright (C) 2006 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,
+ * 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.
+ */
 
 package com.google.inject;
 
diff --git a/src/com/google/inject/InternalToContextualFactoryAdapter.java b/src/com/google/inject/InternalToContextualFactoryAdapter.java
index 9c2bbf5..1f916d3 100644
--- a/src/com/google/inject/InternalToContextualFactoryAdapter.java
+++ b/src/com/google/inject/InternalToContextualFactoryAdapter.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * Copyright (C) 2006 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,
+ * 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.
+ */
 
 package com.google.inject;
 
diff --git a/src/com/google/inject/Scope.java b/src/com/google/inject/Scope.java
index c912189..a966612 100644
--- a/src/com/google/inject/Scope.java
+++ b/src/com/google/inject/Scope.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * Copyright (C) 2006 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,
+ * 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.
+ */
 
 package com.google.inject;
 
diff --git a/src/com/google/inject/Scopes.java b/src/com/google/inject/Scopes.java
index d9330a6..513f2c7 100644
--- a/src/com/google/inject/Scopes.java
+++ b/src/com/google/inject/Scopes.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * Copyright (C) 2006 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,
+ * 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.
+ */
 
 package com.google.inject;
 
diff --git a/src/com/google/inject/util/Stopwatch.java b/src/com/google/inject/util/Stopwatch.java
index 62850e8..9a46ee4 100644
--- a/src/com/google/inject/util/Stopwatch.java
+++ b/src/com/google/inject/util/Stopwatch.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * Copyright (C) 2006 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,
+ * 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.
+ */
 
 package com.google.inject.util;