001/*
002 *   Copyright (C) Christian Schulte, 2005-206
003 *   All rights reserved.
004 *
005 *   Redistribution and use in source and binary forms, with or without
006 *   modification, are permitted provided that the following conditions
007 *   are met:
008 *
009 *     o Redistributions of source code must retain the above copyright
010 *       notice, this list of conditions and the following disclaimer.
011 *
012 *     o Redistributions in binary form must reproduce the above copyright
013 *       notice, this list of conditions and the following disclaimer in
014 *       the documentation and/or other materials provided with the
015 *       distribution.
016 *
017 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 *   $JOMC: ClassFileProcessorTask.java 4613 2012-09-22 10:07:08Z schulte $
029 *
030 */
031package org.jomc.ant;
032
033import org.apache.tools.ant.BuildException;
034import org.apache.tools.ant.Project;
035import org.jomc.tools.ClassFileProcessor;
036
037/**
038 * Base class for executing class file processor based tasks.
039 *
040 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
041 * @version $JOMC: ClassFileProcessorTask.java 4613 2012-09-22 10:07:08Z schulte $
042 * @see #processClassFiles()
043 */
044public class ClassFileProcessorTask extends JomcToolTask
045{
046
047    /** Controls processing of class files. */
048    private boolean classProcessingEnabled = true;
049
050    /** Class of the {@code ClassFileProcessor} backing the task. */
051    private Class<? extends ClassFileProcessor> classFileProcessorClass;
052
053    /** Creates a new {@code ClassFileProcessorTask} instance. */
054    public ClassFileProcessorTask()
055    {
056        super();
057    }
058
059    /**
060     * Gets a flag indicating the processing of classes is enabled.
061     *
062     * @return {@code true}, if processing of classes is enabled; {@code false}, else.
063     *
064     * @see #setClassProcessingEnabled(boolean)
065     */
066    public final boolean isClassProcessingEnabled()
067    {
068        return this.classProcessingEnabled;
069    }
070
071    /**
072     * Sets the flag indicating the processing of classes is enabled.
073     *
074     * @param value {@code true}, to enable processing of classes; {@code false}, to disable processing of classes.
075     *
076     * @see #isClassProcessingEnabled()
077     */
078    public final void setClassProcessingEnabled( final boolean value )
079    {
080        this.classProcessingEnabled = value;
081    }
082
083    /**
084     * Gets the class of the {@code ClassFileProcessor} backing the task.
085     *
086     * @return The class of the {@code ClassFileProcessor} backing the task.
087     *
088     * @see #setClassFileProcessorClass(java.lang.Class)
089     */
090    public final Class<? extends ClassFileProcessor> getClassFileProcessorClass()
091    {
092        if ( this.classFileProcessorClass == null )
093        {
094            this.classFileProcessorClass = ClassFileProcessor.class;
095        }
096
097        return this.classFileProcessorClass;
098    }
099
100    /**
101     * Sets the class of the {@code ClassFileProcessor} backing the task.
102     *
103     * @param value The new class of the {@code ClassFileProcessor} backing the task or {@code null}.
104     *
105     * @see #getClassFileProcessorClass()
106     */
107    public final void setClassFileProcessorClass( final Class<? extends ClassFileProcessor> value )
108    {
109        this.classFileProcessorClass = value;
110    }
111
112    /**
113     * Creates a new {@code ClassFileProcessor} instance setup using the properties of the instance.
114     *
115     * @return A new {@code ClassFileProcessor} instance.
116     *
117     * @throws BuildException if creating a new {@code ClassFileProcessor} instance fails.
118     *
119     * @see #getClassFileProcessorClass()
120     * @see #configureClassFileProcessor(org.jomc.tools.ClassFileProcessor)
121     */
122    public ClassFileProcessor newClassFileProcessor() throws BuildException
123    {
124        try
125        {
126            final ClassFileProcessor classFileProcessor = this.getClassFileProcessorClass().newInstance();
127            this.configureClassFileProcessor( classFileProcessor );
128            return classFileProcessor;
129        }
130        catch ( final InstantiationException e )
131        {
132            throw new BuildException( Messages.getMessage( "failedCreatingObject",
133                                                           this.getClassFileProcessorClass().getName() ),
134                                      e, this.getLocation() );
135
136        }
137        catch ( final IllegalAccessException e )
138        {
139            throw new BuildException( Messages.getMessage( "failedCreatingObject",
140                                                           this.getClassFileProcessorClass().getName() ),
141                                      e, this.getLocation() );
142
143        }
144    }
145
146    /**
147     * Configures a given {@code ClassFileProcessor} instance using the properties of the instance.
148     *
149     * @param classFileProcessor The class file processor to configure.
150     *
151     * @throws NullPointerException if {@code classFileProcessor} is {@code null}.
152     * @throws BuildException if configuring {@code classFileProcessor} fails.
153     *
154     * @see #configureJomcTool(org.jomc.tools.JomcTool)
155     */
156    public void configureClassFileProcessor( final ClassFileProcessor classFileProcessor ) throws BuildException
157    {
158        if ( classFileProcessor == null )
159        {
160            throw new NullPointerException( "classFileProcessor" );
161        }
162
163        this.configureJomcTool( classFileProcessor );
164    }
165
166    /**
167     * Calls the {@code processClassFiles} method if class processing is enabled.
168     *
169     * @throws BuildException if processing class files fails.
170     *
171     * @see #processClassFiles()
172     */
173    @Override
174    public final void executeTask() throws BuildException
175    {
176        if ( this.isClassProcessingEnabled() )
177        {
178            this.processClassFiles();
179            this.log( Messages.getMessage( "classProcessingSuccess" ) );
180        }
181        else
182        {
183            this.log( Messages.getMessage( "classProcessingDisabled" ) );
184        }
185    }
186
187    /**
188     * Processes class files.
189     *
190     * @throws BuildException if processing class files fails.
191     *
192     * @see #executeTask()
193     */
194    public void processClassFiles() throws BuildException
195    {
196        this.log( Messages.getMessage( "unimplementedTask", this.getClass().getName(), "processClassFiles" ),
197                  Project.MSG_ERR );
198
199    }
200
201    /** {@inheritDoc} */
202    @Override
203    public ClassFileProcessorTask clone()
204    {
205        return (ClassFileProcessorTask) super.clone();
206    }
207
208}