blob: b40f793bf602b3b4e226d1dc8d0895a0b8cb5bc1 [file] [log] [blame]
limpbizkita67e39f2008-02-18 20:10:59 +00001/**
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
17package com.google.inject.commands;
18
limpbizkit7d9991e2008-06-17 02:49:18 +000019import static com.google.common.base.Preconditions.checkNotNull;
limpbizkita67e39f2008-02-18 20:10:59 +000020import com.google.inject.Binder;
21import com.google.inject.Key;
22import com.google.inject.Module;
23import com.google.inject.binder.AnnotatedConstantBindingBuilder;
24import com.google.inject.binder.ConstantBindingBuilder;
25import com.google.inject.binder.LinkedBindingBuilder;
26import com.google.inject.binder.ScopedBindingBuilder;
limpbizkita67e39f2008-02-18 20:10:59 +000027import java.util.List;
limpbizkit7d9991e2008-06-17 02:49:18 +000028import org.aopalliance.intercept.MethodInterceptor;
limpbizkita67e39f2008-02-18 20:10:59 +000029
30/**
31 * Executes commands against a binder.
32 *
33 * @author jessewilson@google.com (Jesse Wilson)
34 */
35public class CommandReplayer {
36
37 /**
38 * Returns a module that executes the specified commands
39 * using this executing visitor.
40 */
limpbizkit5a72c092008-05-18 02:25:11 +000041 public final Module createModule(final Iterable<Command> commands) {
limpbizkita67e39f2008-02-18 20:10:59 +000042 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 */
limpbizkit5a72c092008-05-18 02:25:11 +000052 public final void replay(final Binder binder, Iterable<Command> commands) {
kevinb9n1601ae52008-06-03 22:21:04 +000053 checkNotNull(binder, "binder");
54 checkNotNull(commands, "commands");
limpbizkita67e39f2008-02-18 20:10:59 +000055
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
limpbizkit3d58d6b2008-03-08 16:11:47 +0000108 public void replayAddMessageError(final Binder binder, final AddMessageErrorCommand command) {
limpbizkit7d9991e2008-06-17 02:49:18 +0000109 binder.withSource(command.getSource())
110 .addError(command.getMessage(), command.getArguments().toArray());
limpbizkita67e39f2008-02-18 20:10:59 +0000111 }
112
limpbizkit3d58d6b2008-03-08 16:11:47 +0000113 public void replayAddError(final Binder binder, final AddThrowableErrorCommand command) {
limpbizkit7d9991e2008-06-17 02:49:18 +0000114 binder.withSource(command.getSource()).addError(command.getThrowable());
limpbizkita67e39f2008-02-18 20:10:59 +0000115 }
116
limpbizkit3d58d6b2008-03-08 16:11:47 +0000117 public void replayBindInterceptor(final Binder binder, final BindInterceptorCommand command) {
limpbizkit7d9991e2008-06-17 02:49:18 +0000118 List<MethodInterceptor> interceptors = command.getInterceptors();
119 binder.withSource(command.getSource()).bindInterceptor(
120 command.getClassMatcher(), command.getMethodMatcher(),
121 interceptors.toArray(new MethodInterceptor[interceptors.size()]));
limpbizkita67e39f2008-02-18 20:10:59 +0000122 }
123
limpbizkit3d58d6b2008-03-08 16:11:47 +0000124 public void replayBindScope(final Binder binder, final BindScopeCommand command) {
limpbizkit7d9991e2008-06-17 02:49:18 +0000125 binder.withSource(command.getSource()).bindScope(
126 command.getAnnotationType(), command.getScope());
limpbizkita67e39f2008-02-18 20:10:59 +0000127 }
128
limpbizkit3d58d6b2008-03-08 16:11:47 +0000129 public void replayRequestStaticInjection(final Binder binder,
130 final RequestStaticInjectionCommand command) {
limpbizkit7d9991e2008-06-17 02:49:18 +0000131 List<Class> types = command.getTypes();
132 binder.withSource(command.getSource())
133 .requestStaticInjection(types.toArray(new Class[types.size()]));
limpbizkita67e39f2008-02-18 20:10:59 +0000134 }
135
limpbizkit3d58d6b2008-03-08 16:11:47 +0000136 public void replayBindConstant(final Binder binder, final BindConstantCommand command) {
limpbizkit7d9991e2008-06-17 02:49:18 +0000137 AnnotatedConstantBindingBuilder constantBindingBuilder
138 = binder.withSource(command.getSource()).bindConstant();
limpbizkita67e39f2008-02-18 20:10:59 +0000139
limpbizkit7d9991e2008-06-17 02:49:18 +0000140 Key<Object> key = command.getKey();
141 ConstantBindingBuilder builder = key.getAnnotation() != null
142 ? constantBindingBuilder.annotatedWith(key.getAnnotation())
143 : constantBindingBuilder.annotatedWith(key.getAnnotationType());
limpbizkita67e39f2008-02-18 20:10:59 +0000144
limpbizkit7d9991e2008-06-17 02:49:18 +0000145 command.getTarget().execute(builder);
limpbizkita67e39f2008-02-18 20:10:59 +0000146 }
147
limpbizkit3d58d6b2008-03-08 16:11:47 +0000148 public void replayConvertToTypes(final Binder binder, final ConvertToTypesCommand command) {
limpbizkit7d9991e2008-06-17 02:49:18 +0000149 binder.withSource(command.getSource())
150 .convertToTypes(command.getTypeMatcher(), command.getTypeConverter());
limpbizkita67e39f2008-02-18 20:10:59 +0000151 }
152
limpbizkit3d58d6b2008-03-08 16:11:47 +0000153 public <T> void replayBind(final Binder binder, final BindCommand<T> command) {
limpbizkit7d9991e2008-06-17 02:49:18 +0000154 LinkedBindingBuilder<T> lbb = binder.withSource(command.getSource()).bind(command.getKey());
limpbizkita67e39f2008-02-18 20:10:59 +0000155
limpbizkit7d9991e2008-06-17 02:49:18 +0000156 BindTarget<T> bindTarget = command.getTarget();
157 ScopedBindingBuilder sbb = bindTarget != null
158 ? bindTarget.execute(lbb)
159 : lbb;
limpbizkita67e39f2008-02-18 20:10:59 +0000160
limpbizkit7d9991e2008-06-17 02:49:18 +0000161 BindScoping scoping = command.getScoping();
162 if (scoping != null) {
163 scoping.execute(sbb);
164 }
limpbizkita67e39f2008-02-18 20:10:59 +0000165 }
166
limpbizkit3d58d6b2008-03-08 16:11:47 +0000167 public <T> void replayGetProvider(final Binder binder, final GetProviderCommand<T> command) {
limpbizkit7d9991e2008-06-17 02:49:18 +0000168 binder.withSource(command.getSource()).getProvider(command.getKey());
limpbizkita67e39f2008-02-18 20:10:59 +0000169 }
limpbizkita67e39f2008-02-18 20:10:59 +0000170}