1 | package genius;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileNotFoundException;
|
---|
5 | import java.io.FileOutputStream;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.io.OutputStream;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Installs the protocol xml files for use in Genius. Specifically, copies
|
---|
12 | * "resources/genius/repositories/domains/boapartyrepo.xml" and
|
---|
13 | * "resources/genius/repositories/parties/partyrepository.xml" to ".". Files
|
---|
14 | * that are already exist are not touched. This means that eg "./etc" already
|
---|
15 | * exists, nothing happens.
|
---|
16 | */
|
---|
17 | public class ProtocolsInstaller {
|
---|
18 | /**
|
---|
19 | * run the installer
|
---|
20 | *
|
---|
21 | * @throws IOException
|
---|
22 | * if fails to copy the files
|
---|
23 | */
|
---|
24 | public static void run() throws IOException {
|
---|
25 | copy("genius/repositories/protocols/protocolrepository.xml");
|
---|
26 | copy("genius/repositories/multipartyprotocols/multipartyprotocolrepository.xml");
|
---|
27 |
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Copy file in given repository to the current directory.
|
---|
32 | *
|
---|
33 | * @param filename
|
---|
34 | * the filename in our repository
|
---|
35 | * @throws IOException
|
---|
36 | * if copy fails
|
---|
37 | */
|
---|
38 | private static void copy(String filename) throws IOException {
|
---|
39 | // copy only takes the name but uses current directory
|
---|
40 | File copy = new File(new File(filename).getName());
|
---|
41 | if (copy.exists())
|
---|
42 | return;
|
---|
43 | copyResource(filename, copy.getName());
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Copy of DomainInstaller copyResource... Copy 1 resource inside a jar. If
|
---|
48 | * given name actually ends with "/" it is assumed a directory and the
|
---|
49 | * directory is created if it does not yet exist.
|
---|
50 | *
|
---|
51 | * @param name
|
---|
52 | * the file/directory name.Name should end with "/" iff it is a
|
---|
53 | * directory. Resourcess are assumed to be absolute refs, a
|
---|
54 | * leading "/" is added before trying to resolve.
|
---|
55 | * @param target
|
---|
56 | * the target name/directory. Usually does not start with "/"
|
---|
57 | * because that would imply the root of the file system while
|
---|
58 | * installs are usually done in the current directory.
|
---|
59 | * @throws IOException
|
---|
60 | * if copy fails
|
---|
61 | */
|
---|
62 | private static void copyResource(String name, String target) throws IOException {
|
---|
63 | if (name.endsWith("/")) {
|
---|
64 | new File(target).mkdirs();
|
---|
65 | return;
|
---|
66 | }
|
---|
67 | InputStream stream = null;
|
---|
68 | OutputStream resStreamOut = null;
|
---|
69 | try {
|
---|
70 | stream = ProtocolsInstaller.class.getResourceAsStream("/" + name);
|
---|
71 | if (stream == null) {
|
---|
72 | throw new FileNotFoundException("file not found " + name);
|
---|
73 | }
|
---|
74 | int readBytes;
|
---|
75 | byte[] buffer = new byte[4096];
|
---|
76 | resStreamOut = new FileOutputStream(target);
|
---|
77 | while ((readBytes = stream.read(buffer)) > 0) {
|
---|
78 | resStreamOut.write(buffer, 0, readBytes);
|
---|
79 | }
|
---|
80 | } finally {
|
---|
81 | if (stream != null) {
|
---|
82 | stream.close();
|
---|
83 | }
|
---|
84 | if (resStreamOut != null) {
|
---|
85 | resStreamOut.close();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | }
|
---|