limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.inject.commands; |
| 18 | |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 20 | import com.google.inject.Binder; |
| 21 | import com.google.inject.Key; |
| 22 | import com.google.inject.Module; |
| 23 | import com.google.inject.binder.AnnotatedConstantBindingBuilder; |
| 24 | import com.google.inject.binder.ConstantBindingBuilder; |
| 25 | import com.google.inject.binder.LinkedBindingBuilder; |
| 26 | import com.google.inject.binder.ScopedBindingBuilder; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 27 | import java.util.List; |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 28 | import org.aopalliance.intercept.MethodInterceptor; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Executes commands against a binder. |
| 32 | * |
| 33 | * @author jessewilson@google.com (Jesse Wilson) |
| 34 | */ |
| 35 | public class CommandReplayer { |
| 36 | |
| 37 | /** |
| 38 | * Returns a module that executes the specified commands |
| 39 | * using this executing visitor. |
| 40 | */ |
limpbizkit | 5a72c09 | 2008-05-18 02:25:11 +0000 | [diff] [blame] | 41 | public final Module createModule(final Iterable<Command> commands) { |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 42 | return new Module() { |
| 43 | public void configure(Binder binder) { |
| 44 | replay(binder, commands); |
| 45 | } |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Replays {@code commands} against {@code binder}. |
| 51 | */ |
limpbizkit | 5a72c09 | 2008-05-18 02:25:11 +0000 | [diff] [blame] | 52 | public final void replay(final Binder binder, Iterable<Command> commands) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 53 | checkNotNull(binder, "binder"); |
| 54 | checkNotNull(commands, "commands"); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 55 | |
| 56 | Command.Visitor<Void> visitor = new Command.Visitor<Void>() { |
| 57 | public Void visitAddMessageError(AddMessageErrorCommand command) { |
| 58 | replayAddMessageError(binder, command); |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | public Void visitAddError(AddThrowableErrorCommand command) { |
| 63 | replayAddError(binder, command); |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | public Void visitBindInterceptor(BindInterceptorCommand command) { |
| 68 | replayBindInterceptor(binder, command); |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | public Void visitBindScope(BindScopeCommand command) { |
| 73 | replayBindScope(binder, command); |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | public Void visitRequestStaticInjection(RequestStaticInjectionCommand command) { |
| 78 | replayRequestStaticInjection(binder, command); |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | public Void visitBindConstant(BindConstantCommand command) { |
| 83 | replayBindConstant(binder, command); |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | public Void visitConvertToTypes(ConvertToTypesCommand command) { |
| 88 | replayConvertToTypes(binder, command); |
| 89 | return null; |
| 90 | } |
| 91 | |
| 92 | public <T> Void visitBind(BindCommand<T> command) { |
| 93 | replayBind(binder, command); |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | public <T> Void visitGetProvider(GetProviderCommand<T> command) { |
| 98 | replayGetProvider(binder, command); |
| 99 | return null; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | for (Command command : commands) { |
| 104 | command.acceptVisitor(visitor); |
| 105 | } |
| 106 | } |
| 107 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 108 | public void replayAddMessageError(final Binder binder, final AddMessageErrorCommand command) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 109 | binder.withSource(command.getSource()) |
| 110 | .addError(command.getMessage(), command.getArguments().toArray()); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 111 | } |
| 112 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 113 | public void replayAddError(final Binder binder, final AddThrowableErrorCommand command) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 114 | binder.withSource(command.getSource()).addError(command.getThrowable()); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 115 | } |
| 116 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 117 | public void replayBindInterceptor(final Binder binder, final BindInterceptorCommand command) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 118 | List<MethodInterceptor> interceptors = command.getInterceptors(); |
| 119 | binder.withSource(command.getSource()).bindInterceptor( |
| 120 | command.getClassMatcher(), command.getMethodMatcher(), |
| 121 | interceptors.toArray(new MethodInterceptor[interceptors.size()])); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 122 | } |
| 123 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 124 | public void replayBindScope(final Binder binder, final BindScopeCommand command) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 125 | binder.withSource(command.getSource()).bindScope( |
| 126 | command.getAnnotationType(), command.getScope()); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 127 | } |
| 128 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 129 | public void replayRequestStaticInjection(final Binder binder, |
| 130 | final RequestStaticInjectionCommand command) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 131 | List<Class> types = command.getTypes(); |
| 132 | binder.withSource(command.getSource()) |
| 133 | .requestStaticInjection(types.toArray(new Class[types.size()])); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 134 | } |
| 135 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 136 | public void replayBindConstant(final Binder binder, final BindConstantCommand command) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 137 | AnnotatedConstantBindingBuilder constantBindingBuilder |
| 138 | = binder.withSource(command.getSource()).bindConstant(); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 139 | |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 140 | Key<Object> key = command.getKey(); |
| 141 | ConstantBindingBuilder builder = key.getAnnotation() != null |
| 142 | ? constantBindingBuilder.annotatedWith(key.getAnnotation()) |
| 143 | : constantBindingBuilder.annotatedWith(key.getAnnotationType()); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 144 | |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 145 | command.getTarget().execute(builder); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 146 | } |
| 147 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 148 | public void replayConvertToTypes(final Binder binder, final ConvertToTypesCommand command) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 149 | binder.withSource(command.getSource()) |
| 150 | .convertToTypes(command.getTypeMatcher(), command.getTypeConverter()); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 151 | } |
| 152 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 153 | public <T> void replayBind(final Binder binder, final BindCommand<T> command) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 154 | LinkedBindingBuilder<T> lbb = binder.withSource(command.getSource()).bind(command.getKey()); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 155 | |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 156 | BindTarget<T> bindTarget = command.getTarget(); |
| 157 | ScopedBindingBuilder sbb = bindTarget != null |
| 158 | ? bindTarget.execute(lbb) |
| 159 | : lbb; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 160 | |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 161 | BindScoping scoping = command.getScoping(); |
| 162 | if (scoping != null) { |
| 163 | scoping.execute(sbb); |
| 164 | } |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 165 | } |
| 166 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 167 | public <T> void replayGetProvider(final Binder binder, final GetProviderCommand<T> command) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 168 | binder.withSource(command.getSource()).getProvider(command.getKey()); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 169 | } |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 170 | } |