View Javadoc
1   package org.apache.maven.doxia.sink.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.InvocationHandler;
23  import java.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  import java.lang.reflect.Proxy;
26  
27  import java.util.List;
28  
29  import org.apache.maven.doxia.sink.Sink;
30  
31  /**
32   * May be used to invoke the same method on a List of Sinks.
33   *
34   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
35   */
36  public class PipelineSink
37      implements InvocationHandler
38  {
39      private List<Sink> pipeline;
40  
41      /**
42       * Constructs a PipelineSink for a given List of Sinks.
43       *
44       * @param pipeline A List of Sinks.
45       */
46      public PipelineSink( List<Sink> pipeline )
47      {
48          this.pipeline = pipeline;
49      }
50  
51      /**
52       * Add a Sink to the List of Sinks.
53       *
54       * @param sink the Sink to add.
55       */
56      public void addSink( Sink sink )
57      {
58          pipeline.add( sink );
59      }
60  
61      /**
62       * {@inheritDoc}
63       *
64       * Invoke a Method on this PipelineSink.
65       *
66       * @throws IllegalAccessException if any.
67       * @throws InvocationTargetException if any.
68       * @param proxy a {@link java.lang.Object} object.
69       * @param method a {@link java.lang.reflect.Method} object.
70       * @param args an array of {@link java.lang.Object} objects.
71       * @return a {@link java.lang.Object} object.
72       */
73      public Object invoke( Object proxy, Method method, Object[] args )
74              throws IllegalAccessException, InvocationTargetException
75      {
76          for ( Sink sink : pipeline )
77          {
78              method.invoke( sink, args );
79          }
80  
81          return null;
82      }
83  
84      /**
85       * Returns an instance of a PipelineSink as a Sink.
86       *
87       * @param pipeline A List of Sinks.
88       * @return a {@link org.apache.maven.doxia.sink.Sink} object.
89       */
90      public static Sink newInstance( List<Sink> pipeline )
91      {
92          return (Sink) Proxy.newProxyInstance( PipelineSink.class.getClassLoader(),
93                                                new Class<?>[]{Sink.class},
94                                                new PipelineSink( pipeline ) );
95      }
96  }