001package org.apache.maven.doxia.sink;
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
029/**
030 * May be used to invoke the same method on a List of Sinks.
031 *
032 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
033 * @version $Id$
034 */
035public class PipelineSink
036    implements InvocationHandler
037{
038    private List<Sink> pipeline;
039
040    /**
041     * Constructs a PipelineSink for a given List of Sinks.
042     *
043     * @param pipeline A List of Sinks.
044     */
045    public PipelineSink( List<Sink> pipeline )
046    {
047        this.pipeline = pipeline;
048    }
049
050    /**
051     * Add a Sink to the List of Sinks.
052     *
053     * @param sink the Sink to add.
054     */
055    public void addSink( Sink sink )
056    {
057        pipeline.add( sink );
058    }
059
060    /**
061     * {@inheritDoc}
062     *
063     * Invoke a Method on this PipelineSink.
064     *
065     * @throws IllegalAccessException if any.
066     * @throws InvocationTargetException if any.
067     */
068    public Object invoke( Object proxy, Method method, Object[] args )
069            throws IllegalAccessException, InvocationTargetException
070    {
071        for ( Sink sink : pipeline )
072        {
073            method.invoke( sink, args );
074        }
075
076        return null;
077    }
078
079    /**
080     * Returns an instance of a PipelineSink as a Sink.
081     *
082     * @param pipeline A List of Sinks.
083     * @return a {@link org.apache.maven.doxia.sink.Sink} object.
084     */
085    public static Sink newInstance( List<Sink> pipeline )
086    {
087        return (Sink) Proxy.newProxyInstance( PipelineSink.class.getClassLoader(),
088                                              new Class<?>[]{Sink.class},
089                                              new PipelineSink( pipeline ) );
090    }
091}