001package net.sf.logdistiller.publishers;
002
003/*
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017import java.io.*;
018
019import net.sf.logdistiller.*;
020import net.sf.logdistiller.LogDistillation.Group;
021import net.sf.logdistiller.LogDistiller.Report;
022
023/**
024 * An empty publisher, delegating everything to a delegate publisher. The delegate publisher gets instantiated on the
025 * first use, then its libraries dependencies are needed only if the publisher is really used, not when the publisher
026 * is registered.
027 *
028 * @since 1.1
029 */
030public class DelegatePublisher
031    extends Publisher
032{
033    private final String id;
034
035    private final String className;
036
037    private Publisher delegate;
038
039    public DelegatePublisher( String id, String className )
040    {
041        this.id = id;
042        this.className = className;
043    }
044
045    public String getId()
046    {
047        return id;
048    }
049
050    protected Publisher getDelegate()
051    {
052        if ( delegate == null )
053        {
054            try
055            {
056                delegate = (Publisher) Class.forName( className ).newInstance();
057            }
058            catch ( InstantiationException ie )
059            {
060                throw new RuntimeException( "unable to instantiate " + className, ie );
061            }
062            catch ( IllegalAccessException iae )
063            {
064                throw new RuntimeException( "unable to instantiate " + className, iae );
065            }
066            catch ( ClassNotFoundException cnfe )
067            {
068                throw new RuntimeException( "unable to instantiate " + className, cnfe );
069            }
070        }
071
072        return delegate;
073    }
074
075    public void publish( LogDistillation logdistillation, LogDistiller.Report report )
076        throws IOException, PublishException
077    {
078        getDelegate().publish( logdistillation, report );
079    }
080
081    public void publish( Group group, Report report )
082        throws IOException, PublishException
083    {
084        getDelegate().publish( group, report );
085    }
086}