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   * @version $Id$
36   */
37  public class PipelineSink
38      implements InvocationHandler
39  {
40      private List<Sink> pipeline;
41  
42      /**
43       * Constructs a PipelineSink for a given List of Sinks.
44       *
45       * @param pipeline A List of Sinks.
46       */
47      public PipelineSink( List<Sink> pipeline )
48      {
49          this.pipeline = pipeline;
50      }
51  
52      /**
53       * Add a Sink to the List of Sinks.
54       *
55       * @param sink the Sink to add.
56       */
57      public void addSink( Sink sink )
58      {
59          pipeline.add( sink );
60      }
61  
62      /**
63       * {@inheritDoc}
64       *
65       * Invoke a Method on this PipelineSink.
66       *
67       * @throws IllegalAccessException if any.
68       * @throws InvocationTargetException if any.
69       */
70      public Object invoke( Object proxy, Method method, Object[] args )
71              throws IllegalAccessException, InvocationTargetException
72      {
73          for ( Sink sink : pipeline )
74          {
75              method.invoke( sink, args );
76          }
77  
78          return null;
79      }
80  
81      /**
82       * Returns an instance of a PipelineSink as a Sink.
83       *
84       * @param pipeline A List of Sinks.
85       * @return a {@link org.apache.maven.doxia.sink.Sink} object.
86       */
87      public static Sink newInstance( List<Sink> pipeline )
88      {
89          return (Sink) Proxy.newProxyInstance( PipelineSink.class.getClassLoader(),
90                                                new Class<?>[]{Sink.class},
91                                                new PipelineSink( pipeline ) );
92      }
93  }