View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.proxy2.stub;
19  
20  import java.util.Collections;
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import org.apache.commons.lang3.ArrayUtils;
25  import org.apache.commons.lang3.Validate;
26  import org.apache.commons.lang3.builder.Builder;
27  import org.apache.commons.proxy2.Invoker;
28  import org.apache.commons.proxy2.ObjectProvider;
29  import org.apache.commons.proxy2.ProxyFactory;
30  import org.apache.commons.proxy2.interceptor.SwitchInterceptor;
31  import org.apache.commons.proxy2.invoker.NullInvoker;
32  import org.apache.commons.proxy2.provider.ConstantProvider;
33  
34  public class StubBuilder<T> implements Builder<T>
35  {
36      //******************************************************************************************************************
37      // Fields
38      //******************************************************************************************************************
39  
40      private final ProxyFactory proxyFactory;
41      private final T target;
42      private final SwitchInterceptor switchInterceptor = new SwitchInterceptor();
43      private final Set<Class<?>> proxyTypes = new HashSet<Class<?>>();
44  
45      //******************************************************************************************************************
46      // Constructors
47      //******************************************************************************************************************
48  
49      public StubBuilder(ProxyFactory proxyFactory, Class<T> type)
50      {
51          this(proxyFactory, type, NullInvoker.INSTANCE);
52      }
53  
54      public StubBuilder(ProxyFactory proxyFactory, Class<T> type, Invoker invoker)
55      {
56          this.proxyFactory = proxyFactory;
57          this.target = proxyFactory.createInvokerProxy(invoker, type);
58          this.proxyTypes.add(Validate.notNull(type));
59      }
60  
61      public StubBuilder(ProxyFactory proxyFactory, Class<T> type, ObjectProvider<? extends T> provider)
62      {
63          this.proxyFactory = proxyFactory;
64          this.target = proxyFactory.createDelegatorProxy(provider, type);
65          this.proxyTypes.add(Validate.notNull(type));
66      }
67  
68      public StubBuilder(ProxyFactory proxyFactory, Class<T> type, T target)
69      {
70          this.proxyFactory = proxyFactory;
71          this.target = proxyFactory.createDelegatorProxy(new ConstantProvider<T>(target), type);
72          this.proxyTypes.add(Validate.notNull(type));
73      }
74  
75      //******************************************************************************************************************
76      // Other Methods
77      //******************************************************************************************************************
78  
79      @Override
80      public T build()
81      {
82          return proxyFactory.createInterceptorProxy(target, switchInterceptor,
83                  proxyTypes.toArray(ArrayUtils.EMPTY_CLASS_ARRAY));
84      }
85  
86      public <O> StubBuilder<T> train(BaseTrainer<?, O> trainer)
87      {
88          final TrainingContext trainingContext = TrainingContext.join(proxyFactory);
89          try
90          {
91              final O trainee = trainingContext.push(trainer.traineeType, switchInterceptor);
92              trainer.train(trainee);
93              proxyTypes.add(trainer.traineeType);
94          }
95          finally
96          {
97              trainingContext.part();
98          }
99          return this;
100     }
101 
102     public StubBuilder<T> addProxyTypes(Class<?>... proxyTypes)
103     {
104         Collections.addAll(this.proxyTypes, Validate.noNullElements(proxyTypes));
105         return this;
106     }
107 }