001: /*
002: * Copyright 2005 Paul Hinds
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: package org.tp23.antinstaller.runtime;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.InputStreamReader;
021: import java.io.PrintStream;
022:
023: import org.tp23.antinstaller.InstallException;
024: import org.tp23.antinstaller.Installer;
025: import org.tp23.antinstaller.InstallerContext;
026: import org.tp23.antinstaller.page.Page;
027: import org.tp23.antinstaller.page.SimpleInputPage;
028: import org.tp23.antinstaller.renderer.AIResourceBundle;
029: import org.tp23.antinstaller.renderer.AntOutputRenderer;
030: import org.tp23.antinstaller.renderer.RendererFactory;
031: import org.tp23.antinstaller.renderer.text.AbstractTextPageRenderer;
032: import org.tp23.antinstaller.renderer.text.TextMessageRenderer;
033:
034: /**
035: *
036: * <p>Runs the installer from the text only command line (console) </p>
037: * <p>This class uses the Installer object tree as its data source and renderers
038: * from the org.tp23.antinstaller.renderer.text package </p>
039: * <p>Copyright (c) 2004</p>
040: * <p>Company: tp23</p>
041: * @author Paul Hinds
042: * @version $Id: TextRunner.java,v 1.10 2007/01/19 00:24:36 teknopaul Exp $
043: */
044: public class TextRunner extends AntRunner implements Runner {
045:
046: private static final AIResourceBundle res = new AIResourceBundle();
047:
048: protected final Installer installer;
049: private final Logger logger;
050: protected final IfPropertyHelper ifHelper;
051:
052: public TextRunner(InstallerContext ctx) throws IOException {
053: super (ctx);
054: this .installer = ctx.getInstaller();
055: this .logger = ctx.getLogger();
056: ctx.setMessageRenderer(new TextMessageRenderer());
057: ctx.setAntOutputRenderer(new AntOutputRenderer() {
058: public PrintStream getErr() {
059: return System.err;
060: }
061:
062: public PrintStream getOut() {
063: return System.out;
064: }
065:
066: });
067: this .ifHelper = new IfPropertyHelper(ctx);
068: }
069:
070: /**
071: * Renders the installer on the command line, this method blocks until
072: * the UI has finished
073: * @throws InstallException
074: * @return boolean false implies the install was aborted
075: */
076: public boolean runInstaller() throws InstallException {
077: try {
078: return renderPages(installer.getPages());
079: } catch (Exception ex) {
080: logger.log("FATAL exception during installation:"
081: + ex.getMessage());
082: logger.log(installer, ex);
083:
084: ctx.getMessageRenderer().printMessage(
085: res.getString("installation.failed") + ":"
086: + ex.getMessage());
087: //Fixed BUG: ctx.getMessageRenderer().printMessage("Installation failed:"+ex.getMessage());
088: throw new InstallException("Installation failed", ex);
089: }
090: }
091:
092: private boolean renderPages(Page[] pages)
093: throws ClassNotFoundException, InstallException {
094: Page next = null;
095: for (int i = 0; i < pages.length; i++) {
096: next = pages[i];
097:
098: if (next instanceof SimpleInputPage) {
099: // skip iftarget specified and missing
100: if (!ifHelper.ifTarget(next, pages))
101: continue;
102: // skip page if ifProperty is specified and property is missing
103: if (!ifHelper.ifProperty(next))
104: continue;
105: }
106:
107: AbstractTextPageRenderer renderer = RendererFactory
108: .getTextPageRenderer(next);
109: renderer.setContext(ctx);
110: renderer.init(new BufferedReader(new InputStreamReader(
111: System.in)), System.out);
112: ctx.setCurrentPage(next);
113: renderer.renderPage(next);
114: if (next.isAbort()) {
115: return false;
116: }
117: runPost(next);
118: }
119: return true;
120: }
121:
122: public InstallerContext getInstallerContext() {
123: return ctx;
124: }
125:
126: /**
127: * Called when Ant has finished its work
128: */
129: public void antFinished() {
130: System.out.println(res.getString("finished"));
131: //System.exit(0);
132: }
133:
134: /**
135: * Called is Ant failed to install correctly
136: */
137: public void fatalError() {
138: System.out.println(res.getString("failed.view.errors"));
139: //System.exit(1);
140: }
141:
142: public String toString() {
143: return "TextRunner";
144: }
145: }
|