01: /*
02: * Copyright 2005 Paul Hinds
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */
16: package org.tp23.antinstaller.runtime;
17:
18: import org.tp23.antinstaller.InstallerContext;
19: import org.tp23.antinstaller.antmod.RuntimeLauncher;
20: import org.tp23.antinstaller.page.Page;
21:
22: /**
23: * Abstract Runner superclass that handles the runPost(page) to execute
24: * ant tasks mid display.
25: * Initialising is now done in the background to reduce the wait to reparse
26: * @author teknopaul
27: */
28: public abstract class AntRunner implements Runner {
29:
30: private RuntimeLauncher launcher = null;
31: protected InstallerContext ctx;
32: // initialisation locking
33: private Object prepareLock = new Object();
34: private boolean preparing = false; // trap race conditions
35:
36: public AntRunner(InstallerContext ctx) {
37: this .ctx = ctx;
38:
39: if (ctx.getInstaller().hasPostDisplayTargets()) {
40: preparing = true; // run post cant be run until the constructor has finished
41: new PrepareThread().start();
42: }
43: }
44:
45: /**
46: * Runs a target during the UI
47: * @param page
48: * @return 0 for success 1 for exception
49: */
50: protected int runPost(Page page) {
51: String postTarget = page.getPostDisplayTarget();
52: if (postTarget != null) {
53: if (preparing) {
54: // may get here first
55: synchronized (prepareLock) {
56: try {
57: prepareLock.wait();
58: } catch (InterruptedException e) {
59: // normal
60: }
61: }
62: }
63:
64: launcher.updateProps();
65: return launcher.run(postTarget);
66: }
67: return 0;
68: }
69:
70: /**
71: * This should never get run if there are no postTargets
72: */
73: private void prepareLauncher() {
74: launcher = new RuntimeLauncher(ctx);
75: launcher.parseProject();
76: }
77:
78: private class PrepareThread extends Thread {
79:
80: public void run() {
81: synchronized (prepareLock) {
82: prepareLauncher();
83: preparing = false;
84: prepareLock.notifyAll();
85: }
86: }
87:
88: }
89: }
|