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 | |
| 19 | import com.google.inject.Binder; |
| 20 | import com.google.inject.Key; |
| 21 | import com.google.inject.Module; |
| 22 | import com.google.inject.binder.AnnotatedConstantBindingBuilder; |
| 23 | import com.google.inject.binder.ConstantBindingBuilder; |
| 24 | import com.google.inject.binder.LinkedBindingBuilder; |
| 25 | import com.google.inject.binder.ScopedBindingBuilder; |
limpbizkit | 5a72c09 | 2008-05-18 02:25:11 +0000 | [diff] [blame] | 26 | import com.google.inject.spi.SourceProviders; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame^] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 28 | import org.aopalliance.intercept.MethodInterceptor; |
| 29 | |
| 30 | import java.util.List; |
| 31 | |
| 32 | /** |
| 33 | * Executes commands against a binder. |
| 34 | * |
| 35 | * @author jessewilson@google.com (Jesse Wilson) |
| 36 | */ |
| 37 | public class CommandReplayer { |
| 38 | |
| 39 | /** |
| 40 | * Returns a module that executes the specified commands |
| 41 | * using this executing visitor. |
| 42 | */ |
limpbizkit | 5a72c09 | 2008-05-18 02:25:11 +0000 | [diff] [blame] | 43 | public final Module createModule(final Iterable<Command> commands) { |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 44 | return new Module() { |
| 45 | public void configure(Binder binder) { |
| 46 | replay(binder, commands); |
| 47 | } |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Replays {@code commands} against {@code binder}. |
| 53 | */ |
limpbizkit | 5a72c09 | 2008-05-18 02:25:11 +0000 | [diff] [blame] | 54 | public final void replay(final Binder binder, Iterable<Command> commands) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame^] | 55 | checkNotNull(binder, "binder"); |
| 56 | checkNotNull(commands, "commands"); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 57 | |
| 58 | Command.Visitor<Void> visitor = new Command.Visitor<Void>() { |
| 59 | public Void visitAddMessageError(AddMessageErrorCommand command) { |
| 60 | replayAddMessageError(binder, command); |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | public Void visitAddError(AddThrowableErrorCommand command) { |
| 65 | replayAddError(binder, command); |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | public Void visitBindInterceptor(BindInterceptorCommand command) { |
| 70 | replayBindInterceptor(binder, command); |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | public Void visitBindScope(BindScopeCommand command) { |
| 75 | replayBindScope(binder, command); |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | public Void visitRequestStaticInjection(RequestStaticInjectionCommand command) { |
| 80 | replayRequestStaticInjection(binder, command); |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | public Void visitBindConstant(BindConstantCommand command) { |
| 85 | replayBindConstant(binder, command); |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | public Void visitConvertToTypes(ConvertToTypesCommand command) { |
| 90 | replayConvertToTypes(binder, command); |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | public <T> Void visitBind(BindCommand<T> command) { |
| 95 | replayBind(binder, command); |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | public <T> Void visitGetProvider(GetProviderCommand<T> command) { |
| 100 | replayGetProvider(binder, command); |
| 101 | return null; |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | for (Command command : commands) { |
| 106 | command.acceptVisitor(visitor); |
| 107 | } |
| 108 | } |
| 109 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 110 | public void replayAddMessageError(final Binder binder, final AddMessageErrorCommand command) { |
| 111 | SourceProviders.withDefault(command.getSource(), new Runnable() { |
| 112 | public void run() { |
| 113 | binder.addError(command.getMessage(), command.getArguments().toArray()); |
| 114 | } |
| 115 | }); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 116 | } |
| 117 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 118 | public void replayAddError(final Binder binder, final AddThrowableErrorCommand command) { |
| 119 | SourceProviders.withDefault(command.getSource(), new Runnable() { |
| 120 | public void run() { |
| 121 | binder.addError(command.getThrowable()); |
| 122 | } |
| 123 | }); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 124 | } |
| 125 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 126 | public void replayBindInterceptor(final Binder binder, final BindInterceptorCommand command) { |
| 127 | SourceProviders.withDefault(command.getSource(), new Runnable() { |
| 128 | public void run() { |
| 129 | List<MethodInterceptor> interceptors = command.getInterceptors(); |
| 130 | binder.bindInterceptor(command.getClassMatcher(), command.getMethodMatcher(), |
| 131 | interceptors.toArray(new MethodInterceptor[interceptors.size()])); |
| 132 | } |
| 133 | }); |
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 replayBindScope(final Binder binder, final BindScopeCommand command) { |
| 137 | SourceProviders.withDefault(command.getSource(), new Runnable() { |
| 138 | public void run() { |
| 139 | binder.bindScope(command.getAnnotationType(), command.getScope()); |
| 140 | } |
| 141 | }); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 142 | } |
| 143 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 144 | public void replayRequestStaticInjection(final Binder binder, |
| 145 | final RequestStaticInjectionCommand command) { |
| 146 | SourceProviders.withDefault(command.getSource(), new Runnable() { |
| 147 | public void run() { |
| 148 | List<Class> types = command.getTypes(); |
| 149 | binder.requestStaticInjection(types.toArray(new Class[types.size()])); |
| 150 | } |
| 151 | }); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 152 | } |
| 153 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 154 | public void replayBindConstant(final Binder binder, final BindConstantCommand command) { |
| 155 | SourceProviders.withDefault(command.getSource(), new Runnable() { |
| 156 | public void run() { |
| 157 | AnnotatedConstantBindingBuilder constantBindingBuilder = binder.bindConstant(); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 158 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 159 | Key<Object> key = command.getKey(); |
| 160 | ConstantBindingBuilder builder = key.getAnnotation() != null |
| 161 | ? constantBindingBuilder.annotatedWith(key.getAnnotation()) |
| 162 | : constantBindingBuilder.annotatedWith(key.getAnnotationType()); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 163 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 164 | command.getTarget().execute(builder); |
| 165 | } |
| 166 | }); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 167 | } |
| 168 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 169 | public void replayConvertToTypes(final Binder binder, final ConvertToTypesCommand command) { |
| 170 | SourceProviders.withDefault(command.getSource(), new Runnable() { |
| 171 | public void run() { |
| 172 | binder.convertToTypes(command.getTypeMatcher(), command.getTypeConverter()); |
| 173 | } |
| 174 | }); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 175 | } |
| 176 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 177 | public <T> void replayBind(final Binder binder, final BindCommand<T> command) { |
| 178 | SourceProviders.withDefault(command.getSource(), new Runnable() { |
| 179 | public void run() { |
| 180 | LinkedBindingBuilder<T> lbb = binder.bind(command.getKey()); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 181 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 182 | BindTarget<T> bindTarget = command.getTarget(); |
| 183 | ScopedBindingBuilder sbb = bindTarget != null |
| 184 | ? bindTarget.execute(lbb) |
| 185 | : lbb; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 186 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 187 | BindScoping scoping = command.getScoping(); |
| 188 | if (scoping != null) { |
| 189 | scoping.execute(sbb); |
| 190 | } |
| 191 | } |
| 192 | }); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 193 | } |
| 194 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 195 | public <T> void replayGetProvider(final Binder binder, final GetProviderCommand<T> command) { |
| 196 | SourceProviders.withDefault(command.getSource(), new Runnable() { |
| 197 | public void run() { |
| 198 | binder.getProvider(command.getKey()); |
| 199 | } |
| 200 | }); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 201 | } |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 202 | } |