|
1
|
|
/*
|
|
2
|
|
* JBind
|
|
3
|
|
*
|
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
|
5
|
|
*
|
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
|
8
|
|
* 'Apache Software License V 1.1'.
|
|
9
|
|
*/
|
|
10
|
|
package org.jbind.xml.facade;
|
|
11
|
|
|
|
12
|
|
import java.net.MalformedURLException;
|
|
13
|
|
import java.net.URL;
|
|
14
|
|
|
|
15
|
|
import org.jbind.base.Console;
|
|
16
|
|
import org.jbind.xml.code.IApplication;
|
|
17
|
|
import org.jbind.xml.code.IApplicationCode;
|
|
18
|
|
import org.jbind.xml.core.data.IAnyTypeData;
|
|
19
|
|
import org.jbind.xml.core.data.IDataContext;
|
|
20
|
|
import org.jbind.xml.msg.XmlException;
|
|
21
|
|
import org.jbind.xml.msg.XmlMessages;
|
|
22
|
|
import org.xml.sax.InputSource;
|
|
23
|
|
|
|
24
|
|
public class ApplicationCode extends ConfigurationCode implements IApplicationCode {
|
|
25
|
|
|
|
26
|
1
|
public ApplicationCode(URL aUrl, IDataContext aDataContext) {
|
|
27
|
1
|
super(aUrl, aDataContext);
|
|
28
|
|
}
|
|
29
|
0
|
public ApplicationCode(InputSource anInputSource, IDataContext aDataContext) {
|
|
30
|
0
|
super(anInputSource, aDataContext);
|
|
31
|
|
}
|
|
32
|
|
|
|
33
|
2
|
public IApplication getApplication() throws XmlException {
|
|
34
|
2
|
IAnyTypeData code = getCode();
|
|
35
|
2
|
if (!(code instanceof IApplication)) {
|
|
36
|
0
|
throw new XmlException(XmlMessages.notAnApplicationInstance(getUrl(), code.getClass().getName(), IApplication.class.getName(), null));
|
|
37
|
|
}
|
|
38
|
2
|
return (IApplication)code;
|
|
39
|
|
}
|
|
40
|
|
|
|
41
|
|
/*
|
|
42
|
|
* Template method that executes the application. The hook methods called in
|
|
43
|
|
* this template method have reasonable default implementations.
|
|
44
|
|
* The execution consists of the following steps:
|
|
45
|
|
* <ol>
|
|
46
|
|
* <li>Gets the application by calling the {@link #getApplication} method.</li>
|
|
47
|
|
* <li>Setup the application by calling the method {@link #setup}.</li>
|
|
48
|
|
* <li>Execute the application by calling the hook method {@link #doExecuteApplication}.</li>
|
|
49
|
|
* <li>Tear down the application by calling the method {@link #tearDown}.</li>
|
|
50
|
|
* <ol>
|
|
51
|
|
*
|
|
52
|
|
* @return The error code returned by the execute method of the application.
|
|
53
|
|
* @throws XmlException raised if the application could not be instantiated
|
|
54
|
|
* properly.
|
|
55
|
|
*/
|
|
56
|
1
|
public int execute() throws XmlException {
|
|
57
|
1
|
IApplication app = getApplication();
|
|
58
|
1
|
setup();
|
|
59
|
1
|
int errorCode = doExecuteApplication(app);
|
|
60
|
1
|
tearDown();
|
|
61
|
|
|
|
62
|
1
|
return errorCode;
|
|
63
|
|
}
|
|
64
|
|
|
|
65
|
|
/**
|
|
66
|
|
* Hook method for executing the application. The default implementation simply
|
|
67
|
|
* calls the execute method of the application.
|
|
68
|
|
*
|
|
69
|
|
* @return The error code returned by the execute method of the application.
|
|
70
|
|
*/
|
|
71
|
1
|
protected int doExecuteApplication(IApplication anApplication) {
|
|
72
|
1
|
return anApplication.execute();
|
|
73
|
|
}
|
|
74
|
|
|
|
75
|
0
|
private static void usage() {
|
|
76
|
0
|
Console.err("usage:");
|
|
77
|
0
|
Console.err("\tjava " + ApplicationCode.class.getName() + " -url <aUrl> [-nbLevels <int>]");
|
|
78
|
0
|
Console.err("\t\t\turl : url of XML instance document that contains an application");
|
|
79
|
0
|
Console.err("\t\t\tnbLevels: number of setup / tear down levels.");
|
|
80
|
|
}
|
|
81
|
|
|
|
82
|
|
/**
|
|
83
|
|
* Executes an XML application.
|
|
84
|
|
*
|
|
85
|
|
* @param anArgs <i>(required)</i>. The recognized arguments are:
|
|
86
|
|
* <ul>
|
|
87
|
|
* <li>-url <aUrl> <i>(url of an XML instance document that contains an application)</i></li>
|
|
88
|
|
* </ul>
|
|
89
|
|
*/
|
|
90
|
0
|
public static void main(String[] anArgs) {
|
|
91
|
0
|
URL url = null;
|
|
92
|
0
|
for (int i = 0; i < anArgs.length; i++) {
|
|
93
|
0
|
String a = anArgs[i];
|
|
94
|
0
|
if ("-url".equals(a)) {
|
|
95
|
0
|
if (i + 1 == anArgs.length) {
|
|
96
|
0
|
Console.err("Missing url");
|
|
97
|
0
|
usage();
|
|
98
|
0
|
System.exit(-1);
|
|
99
|
|
}
|
|
100
|
0
|
try {
|
|
101
|
0
|
url = new URL(anArgs[++i]);
|
|
102
|
|
} catch (MalformedURLException e) {
|
|
103
|
0
|
Console.err("invalid url: " + anArgs[i]);
|
|
104
|
0
|
usage();
|
|
105
|
0
|
System.exit(-1);
|
|
106
|
|
}
|
|
107
|
|
} else {
|
|
108
|
0
|
Console.err("unknown option: " + a);
|
|
109
|
0
|
usage();
|
|
110
|
0
|
System.exit(-1);
|
|
111
|
|
}
|
|
112
|
|
}
|
|
113
|
0
|
if (null == url) {
|
|
114
|
0
|
Console.err("missing url");
|
|
115
|
0
|
usage();
|
|
116
|
0
|
System.exit(-1);
|
|
117
|
|
}
|
|
118
|
0
|
ApplicationCode applicationCode = new ApplicationCode(url, null);
|
|
119
|
0
|
try {
|
|
120
|
0
|
int errorCode = applicationCode.execute();
|
|
121
|
0
|
System.exit(errorCode);
|
|
122
|
|
} catch (Exception e) {
|
|
123
|
0
|
e.printStackTrace();
|
|
124
|
0
|
System.exit(-1);
|
|
125
|
|
}
|
|
126
|
|
}
|
|
127
|
|
}
|
|
128
|
|
|