1 package dolda.jsvc.j2ee;
6 import java.util.zip.*;
7 import java.util.jar.*;
10 private static void usage(PrintStream out) {
11 out.println("usage: dolda.jsvc.j2ee.Archive [-h] [-p PROPFILE] [-n DISPLAY-NAME] WAR-FILE JAR-FILE...");
14 private static void jarprops(String[] jars, String propres, Properties props) throws IOException {
15 URL[] urls = new URL[jars.length];
17 for(int i = 0; i < jars.length; i++)
18 urls[i] = new URL("file", "", jars[i]);
19 } catch(MalformedURLException e) {
22 ClassLoader cl = new URLClassLoader(urls);
23 InputStream in = cl.getResourceAsStream(propres);
33 private static Properties defprops() {
34 Properties props = new Properties();
35 props.put("jsvc.j2ee.webxml.coding", "UTF-8");
39 private static void cpstream(InputStream in, OutputStream out) throws IOException {
40 byte[] buf = new byte[4096];
42 int ret = in.read(buf, 0, buf.length);
45 out.write(buf, 0, ret);
49 private static String subst(String ln, Properties props) {
51 while((p = ln.indexOf("${", p)) >= 0) {
52 int p2 = ln.indexOf('}', p + 2);
53 String pn = ln.substring(p + 2, p2);
54 String pv = (String)props.get(pn);
56 throw(new RuntimeException("Missing required property " + pn));
57 ln = ln.substring(0, p) + pv + ln.substring(p2 + 1);
63 private static void writewebxml(Properties props, OutputStream out) throws IOException {
64 InputStream tmpl = Archive.class.getResourceAsStream("web.xml.template");
65 String cs = (String)props.get("jsvc.j2ee.webxml.coding");
67 BufferedReader r = new BufferedReader(new InputStreamReader(tmpl, "US-ASCII"));
68 BufferedWriter w = new BufferedWriter(new OutputStreamWriter(out, cs));
70 while((ln = r.readLine()) != null) {
71 w.write(subst(ln, props));
80 public static void makewar(String[] jars, Properties props, OutputStream out) throws IOException {
81 Manifest man = new Manifest();
82 man.getMainAttributes().put(new Attributes.Name("Manifest-Version"), "1.0");
83 man.getMainAttributes().put(new Attributes.Name("Created-By"), "jsvc");
84 JarOutputStream zip = new JarOutputStream(out, man);
85 zip.putNextEntry(new ZipEntry("WEB-INF/"));
86 zip.putNextEntry(new ZipEntry("WEB-INF/lib/"));
87 for(String jar : jars) {
89 int p = bn.lastIndexOf('/');
91 bn = bn.substring(p + 1);
92 zip.putNextEntry(new ZipEntry("WEB-INF/lib/" + bn));
93 InputStream jarin = new FileInputStream(jar);
100 zip.putNextEntry(new ZipEntry("WEB-INF/web.xml"));
101 writewebxml(props, zip);
105 public static void main(String[] args) throws IOException {
106 PosixArgs opt = PosixArgs.getopt(args, "hp:n:");
111 if(opt.rest.length < 2) {
115 String war = opt.rest[0];
116 String[] jars = Arrays.copyOfRange(opt.rest, 1, opt.rest.length);
118 Properties props = defprops();
119 jarprops(jars, "/jsvc.properties", props);
121 for(char c : opt.parsed()) {
125 InputStream in = new FileInputStream(opt.arg);
134 props.put("jsvc.j2ee.appname", opt.arg);
142 OutputStream out = new FileOutputStream(war);
144 makewar(jars, props, out);