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 */
036public class PipelineSink
037    implements InvocationHandler
038{
039    private List<Sink> pipeline;
040
041    /**
042     * Constructs a PipelineSink for a given List of Sinks.
043     *
044     * @param pipeline A List of Sinks.
045     */
046    public PipelineSink( List<Sink> pipeline )
047    {
048        this.pipeline = pipeline;
049    }
050
051    /**
052     * Add a Sink to the List of Sinks.
053     *
054     * @param sink the Sink to add.
055     */
056    public void addSink( Sink sink )
057    {
058        pipeline.add( sink );
059    }
060
061    /**
062     * {@inheritDoc}
063     *
064     * Invoke a Method on this PipelineSink.
065     *
066     * @throws IllegalAccessException if any.
067     * @throws InvocationTargetException if any.
068     * @param proxy a {@link java.lang.Object} object.
069     * @param method a {@link java.lang.reflect.Method} object.
070     * @param args an array of {@link java.lang.Object} objects.
071     * @return a {@link java.lang.Object} object.
072     */
073    public Object invoke( Object proxy, Method method, Object[] args )
074            throws IllegalAccessException, InvocationTargetException
075    {
076        for ( Sink sink : pipeline )
077        {
078            method.invoke( sink, args );
079        }
080
081        return null;
082    }
083
084    /**
085     * Returns an instance of a PipelineSink as a Sink.
086     *
087     * @param pipeline A List of Sinks.
088     * @return a {@link org.apache.maven.doxia.sink.Sink} object.
089     */
090    public static Sink newInstance( List<Sink> pipeline )
091    {
092        return (Sink) Proxy.newProxyInstance( PipelineSink.class.getClassLoader(),
093                                              new Class<?>[]{Sink.class},
094                                              new PipelineSink( pipeline ) );
095    }
096}