001package org.apache.maven.doxia.sink.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.lang.reflect.InvocationHandler;
023import java.lang.reflect.InvocationTargetException;
024import java.lang.reflect.Method;
025import java.lang.reflect.Proxy;
026
027import java.util.List;
028
029import org.apache.maven.doxia.sink.Sink;
030
031/**
032 * May be used to invoke the same method on a List of Sinks.
033 *
034 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
035 * @version $Id$
036 */
037public class PipelineSink
038    implements InvocationHandler
039{
040    private List<Sink> pipeline;
041
042    /**
043     * Constructs a PipelineSink for a given List of Sinks.
044     *
045     * @param pipeline A List of Sinks.
046     */
047    public PipelineSink( List<Sink> pipeline )
048    {
049        this.pipeline = pipeline;
050    }
051
052    /**
053     * Add a Sink to the List of Sinks.
054     *
055     * @param sink the Sink to add.
056     */
057    public void addSink( Sink sink )
058    {
059        pipeline.add( sink );
060    }
061
062    /**
063     * {@inheritDoc}
064     *
065     * Invoke a Method on this PipelineSink.
066     *
067     * @throws IllegalAccessException if any.
068     * @throws InvocationTargetException if any.
069     */
070    public Object invoke( Object proxy, Method method, Object[] args )
071            throws IllegalAccessException, InvocationTargetException
072    {
073        for ( Sink sink : pipeline )
074        {
075            method.invoke( sink, args );
076        }
077
078        return null;
079    }
080
081    /**
082     * Returns an instance of a PipelineSink as a Sink.
083     *
084     * @param pipeline A List of Sinks.
085     * @return a {@link org.apache.maven.doxia.sink.Sink} object.
086     */
087    public static Sink newInstance( List<Sink> pipeline )
088    {
089        return (Sink) Proxy.newProxyInstance( PipelineSink.class.getClassLoader(),
090                                              new Class<?>[]{Sink.class},
091                                              new PipelineSink( pipeline ) );
092    }
093}