001    /*
002     * $RCSfile: MCRExecuteCommandTask.java,v $
003     * $Revision: 1.2 $ $Date: 2006/11/22 10:50:21 $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    package org.mycore.buildtools.anttasks;
024    
025    import java.io.BufferedReader;
026    import java.io.IOException;
027    import java.io.StringReader;
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    import org.apache.tools.ant.BuildException;
032    import org.apache.tools.ant.Task;
033    import org.apache.tools.ant.types.LogLevel;
034    import org.mycore.frontend.cli.MCRCommandLineInterface;
035    
036    /**
037     * This class is an ant tasks to call the MyCoRe CLI in build process
038     * 
039     * @author Robert Stephan
040     * 
041     * @version $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
042     * 
043     */
044    public class MCRExecuteCommandTask extends Task {
045            String commands;
046            String basedir = null;
047    
048            /**
049             * method used, to read the body of an ant task xml element
050             * @param commands
051             */
052            public void addText(String commands) {
053                    this.commands = commands;
054            }
055    
056            /**
057             * set the base directory, that should be used while executing the commands
058             * @param basedir - the directory
059             */                     
060            public void setBasedir(String basedir) {
061                    this.basedir = basedir;
062            }
063            
064            
065            /* (non-Javadoc)
066             * @see org.apache.tools.ant.Task#execute()
067             */
068            public void execute() throws BuildException {
069                    commands = getProject().replaceProperties(commands);
070                    BufferedReader reader = new BufferedReader(new StringReader(commands));
071                    String line;
072                    List<String> list = new ArrayList<String>();
073                    try {
074                            while ((line = reader.readLine()) != null) {
075                                    line = line.trim();
076    
077                                    if (line.startsWith("#") || (line.length() == 0)) {
078                                            continue;
079                                    }
080                                    list.add(line);
081                            }
082                    } catch (IOException e) {
083                            // do nothing
084                    }
085                    StringBuffer sbCommands = new StringBuffer();
086                    for (String s : list) {
087                        getProject().log(s, LogLevel.INFO.getLevel());
088                            sbCommands.append(s).append(";;");
089                    }
090                                    
091                    try{
092                            MCRCommandLineInterface.main(new String[] { sbCommands.toString() });
093                    }
094                    catch(SecurityException e){
095                            //catches System.exit() in MCRCommandLineInterface.main()
096                    }
097            }
098    }