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