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;
22import java.util.ResourceBundle;
23
24import org.tp23.antinstaller.InstallException;
25import org.tp23.antinstaller.Installer;
26import org.tp23.antinstaller.InstallerContext;
27import org.tp23.antinstaller.page.Page;
28import org.tp23.antinstaller.page.SimpleInputPage;
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 ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
50
51    protected final InstallerContext ctx;
52    protected final Installer installer;
53    private final Logger logger;
54    protected final IfPropertyHelper ifHelper;
55
56    public TextRunner(InstallerContext ctx) throws IOException {
57        super(ctx);
58        this.ctx = ctx;
59        this.installer = ctx.getInstaller();
60        this.logger = ctx.getLogger();
61        ctx.setMessageRenderer(new TextMessageRenderer());
62        ctx.setAntOutputRenderer(new AntOutputRenderer(){
63            public PrintStream getErr() {
64                return System.err;
65            }
66            public PrintStream getOut() {
67                return System.out;
68            }
69
70        });
71        this.ifHelper = new IfPropertyHelper(ctx);
72    }
73
74    /**
75     * Renders the installer on the command line, this method blocks until
76     * the UI has finished
77     * @throws InstallException
78     * @return boolean false implies the install was aborted
79     */
80    public boolean runInstaller() throws InstallException {
81        try {
82            return renderPages(installer.getPages());
83        }
84        catch (Exception ex) {
85            logger.log("FATAL exception during installation:"+ex.getMessage());
86            logger.log(installer, ex);
87            
88            ctx.getMessageRenderer().printMessage(res.getString("installationFailed") + ":" + ex.getMessage());
89            //Fixed BUG: ctx.getMessageRenderer().printMessage("Installation failed:"+ex.getMessage());
90            throw new InstallException("Installation failed", ex);
91        }
92    }
93
94
95    private boolean renderPages(Page[] pages) throws ClassNotFoundException, InstallException{
96        Page next = null;
97        for (int i = 0; i < pages.length; i++) {
98            next = pages[i];
99
00            if(next instanceof SimpleInputPage){
01                // skip iftarget specified and missing
02                if(!ifHelper.ifTarget(next, pages))continue;
03                // skip page if ifProperty is specified and property is missing
04                if(!ifHelper.ifProperty(next))continue;
05            }
06
07            AbstractTextPageRenderer renderer = RendererFactory.getTextPageRenderer(next);
08            renderer.setContext(ctx);
09            renderer.init( new BufferedReader(new InputStreamReader(System.in)), System.out);
10            ctx.setCurrentPage(next);
11            renderer.renderPage(next);
12            if (next.isAbort()){
13                return false;
14            }
15            runPost(next);
16        }
17        return true;
18    }
19    public InstallerContext getInstallerContext() {
20        return ctx;
21    }
22
23
24
25    /**
26     * Called when Ant has finished its work
27     */
28    public void antFinished() {
29        System.out.println(res.getString("finished"));
30        //System.exit(0);
31    }
32    /**
33     * Called is Ant failed to install correctly
34     */
35    public void fatalError(){
36        System.out.println(res.getString("failed"));
37        //System.exit(1);
38    }
39
40    public String toString() {
41        return "TextRunner";
42    }
43}
44