1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.antinstaller.runtime;
17
18import java.io.BufferedReader;
19import java.io.IOException;
20import java.io.InputStreamReader;
21import java.io.PrintStream;
22
23import org.tp23.antinstaller.InstallException;
24import org.tp23.antinstaller.Installer;
25import org.tp23.antinstaller.InstallerContext;
26import org.tp23.antinstaller.page.Page;
27import org.tp23.antinstaller.page.SimpleInputPage;
28import org.tp23.antinstaller.renderer.AIResourceBundle;
29import org.tp23.antinstaller.renderer.AntOutputRenderer;
30import org.tp23.antinstaller.renderer.RendererFactory;
31import org.tp23.antinstaller.renderer.text.AbstractTextPageRenderer;
32import org.tp23.antinstaller.renderer.text.TextMessageRenderer;
33
34
35
36/**
37 *
38 * <p>Runs the installer from the text only command line (console) </p>
39 * <p>This class uses the Installer object tree as its data source and renderers
40 * from the org.tp23.antinstaller.renderer.text package </p>
41 * <p>Copyright (c) 2004</p>
42 * <p>Company: tp23</p>
43 * @author Paul Hinds
44 * @version $Id: TextRunner.java,v 1.10 2007/01/19 00:24:36 teknopaul Exp $
45 */
46public class TextRunner extends AntRunner 
47    implements Runner {
48    
49    private static final AIResourceBundle res = new AIResourceBundle();
50
51    protected final Installer installer;
52    private final Logger logger;
53    protected final IfPropertyHelper ifHelper;
54
55    public TextRunner(InstallerContext ctx) throws IOException {
56        super(ctx);
57        this.installer = ctx.getInstaller();
58        this.logger = ctx.getLogger();
59        ctx.setMessageRenderer(new TextMessageRenderer());
60        ctx.setAntOutputRenderer(new AntOutputRenderer(){
61            public PrintStream getErr() {
62                return System.err;
63            }
64            public PrintStream getOut() {
65                return System.out;
66            }
67
68        });
69        this.ifHelper = new IfPropertyHelper(ctx);
70    }
71
72    /**
73     * Renders the installer on the command line, this method blocks until
74     * the UI has finished
75     * @throws InstallException
76     * @return boolean false implies the install was aborted
77     */
78    public boolean runInstaller() throws InstallException {
79        try {
80            return renderPages(installer.getPages());
81        }
82        catch (Exception ex) {
83            logger.log("FATAL exception during installation:"+ex.getMessage());
84            logger.log(installer, ex);
85            
86            ctx.getMessageRenderer().printMessage(res.getString("installation.failed") + ":" + ex.getMessage());
87            //Fixed BUG: ctx.getMessageRenderer().printMessage("Installation failed:"+ex.getMessage());
88            throw new InstallException("Installation failed", ex);
89        }
90    }
91
92
93    private boolean renderPages(Page[] pages) throws ClassNotFoundException, InstallException{
94        Page next = null;
95        for (int i = 0; i < pages.length; i++) {
96            next = pages[i];
97
98            if(next instanceof SimpleInputPage){
99                // skip iftarget specified and missing
00                if(!ifHelper.ifTarget(next, pages))continue;
01                // skip page if ifProperty is specified and property is missing
02                if(!ifHelper.ifProperty(next))continue;
03            }
04
05            AbstractTextPageRenderer renderer = RendererFactory.getTextPageRenderer(next);
06            renderer.setContext(ctx);
07            renderer.init( new BufferedReader(new InputStreamReader(System.in)), System.out);
08            ctx.setCurrentPage(next);
09            renderer.renderPage(next);
10            if (next.isAbort()){
11                return false;
12            }
13            runPost(next);
14        }
15        return true;
16    }
17    public InstallerContext getInstallerContext() {
18        return ctx;
19    }
20
21
22
23    /**
24     * Called when Ant has finished its work
25     */
26    public void antFinished() {
27        System.out.println(res.getString("finished"));
28        //System.exit(0);
29    }
30    /**
31     * Called is Ant failed to install correctly
32     */
33    public void fatalError(){
34        System.out.println(res.getString("failed.view.errors"));
35        //System.exit(1);
36    }
37
38    public String toString() {
39        return "TextRunner";
40    }
41}
42