[1] | 1 | package genius.domains;
|
---|
| 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 | import java.net.URISyntaxException;
|
---|
| 10 | import java.net.URL;
|
---|
| 11 | import java.nio.file.Files;
|
---|
| 12 | import java.nio.file.Path;
|
---|
| 13 | import java.nio.file.Paths;
|
---|
| 14 | import java.security.CodeSource;
|
---|
| 15 | import java.util.zip.ZipEntry;
|
---|
| 16 | import java.util.zip.ZipInputStream;
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * Installs domain xml files either from jar file or from file system (if you
|
---|
| 20 | * run from eclipse). Nothing happens if /etc is already there.
|
---|
| 21 | *
|
---|
| 22 | */
|
---|
| 23 | public class DomainInstaller {
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Installs the domain files for use in Genius. Specifically, copies
|
---|
| 27 | * "resources/genius/repositories/domains/domainrepository.xml" in "."
|
---|
| 28 | * (current dir) and the entire directory "resources/genius/template" to
|
---|
| 29 | * "./etc/". Files that are already exist are not touched. This means that
|
---|
| 30 | * eg "./etc" already exists, nothing happens.
|
---|
| 31 | *
|
---|
| 32 | * @throws IOException
|
---|
| 33 | * if failes to copy the files
|
---|
| 34 | */
|
---|
| 35 | public static void run() throws IOException {
|
---|
| 36 | copyRecursively("genius/templates/", "etc/templates/");
|
---|
| 37 | copyRecursively("genius/repositories/domains/domainrepository.xml", "domainrepository.xml");
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | *
|
---|
| 42 | * @param dir
|
---|
| 43 | * the head part of all files needed, eg "genius/templates/".
|
---|
| 44 | * @param target
|
---|
| 45 | * the head part to be used for the target files. We replace
|
---|
| 46 | * "dir" with "target" in all matching files. Remember to add
|
---|
| 47 | * trailing "/" if you have trailing "/" in dir as well
|
---|
| 48 | * @throws IOException
|
---|
| 49 | * @throws URISyntaxException
|
---|
| 50 | */
|
---|
| 51 | protected static void copyRecursively(String dir, String target) throws IOException {
|
---|
| 52 | // are we running from a jar?
|
---|
| 53 | CodeSource src = DomainInstaller.class.getProtectionDomain().getCodeSource();
|
---|
| 54 | if (src == null)
|
---|
| 55 | throw new FileNotFoundException("code base not found");
|
---|
| 56 | URL uri = src.getLocation();
|
---|
| 57 | // file://... but the file may be a jar after all..
|
---|
| 58 | if (uri.toString().endsWith("jar")) {
|
---|
| 59 | copyJarSystem(dir, target, uri);
|
---|
| 60 | } else {
|
---|
| 61 | // assume it's a normal file
|
---|
| 62 | copyFileSystem("/" + dir, target);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * Copy a resource inside a JAR recursively
|
---|
| 69 | *
|
---|
| 70 | * @param dir
|
---|
| 71 | * resource name, class path string like "/genius/repositories"
|
---|
| 72 | * @param target
|
---|
| 73 | * target directory eg "test" (writes in current directory
|
---|
| 74 | * "./test"). Do not end the target with "/". If you copy just a
|
---|
| 75 | * file, give the exact target filename here.
|
---|
| 76 | * @throws IOException
|
---|
| 77 | * if problem with reading or writing the file
|
---|
| 78 | * @throws URISyntaxException
|
---|
| 79 | * if string can not be converted to valid file path
|
---|
| 80 | */
|
---|
| 81 | private static void copyFileSystem(String dir, String target) throws IOException {
|
---|
| 82 |
|
---|
| 83 | final Path destpath = Paths.get(target);
|
---|
| 84 | if (destpath.toFile().exists()) {
|
---|
| 85 | return;
|
---|
| 86 | }
|
---|
| 87 | final URL url = DomainInstaller.class.getResource(dir);
|
---|
| 88 | if (url == null) {
|
---|
| 89 | throw new FileNotFoundException("Could not find resource " + dir);
|
---|
| 90 | }
|
---|
| 91 | try {
|
---|
| 92 | final File file = new File(url.toURI());
|
---|
| 93 | if (file.isDirectory()) {
|
---|
| 94 | destpath.toFile().getParentFile().mkdirs();
|
---|
| 95 | Files.copy(Paths.get(url.toURI()), destpath);
|
---|
| 96 | for (File sub : file.listFiles()) {
|
---|
| 97 | copyFileSystem(dir + "/" + sub.getName(), target + "/" + sub.getName());
|
---|
| 98 | }
|
---|
| 99 | } else {
|
---|
| 100 | Files.copy(Paths.get(url.toURI()), Paths.get(target));
|
---|
| 101 | }
|
---|
| 102 | } catch (URISyntaxException e) {
|
---|
| 103 | throw new IOException("Encountered wrong filename while copying resources", e);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * Copy all files recursively where the source is inside a jar.
|
---|
| 110 | *
|
---|
| 111 | * @param dir
|
---|
| 112 | * @param target
|
---|
| 113 | * @param uri
|
---|
| 114 | * @throws IOException
|
---|
| 115 | */
|
---|
| 116 | private static void copyJarSystem(String dir, String target, URL uri) throws IOException {
|
---|
| 117 | final Path destpath = Paths.get(target);
|
---|
| 118 | if (destpath.toFile().exists()) {
|
---|
| 119 | return;
|
---|
| 120 | }
|
---|
| 121 | ZipInputStream zip = new ZipInputStream(uri.openStream());
|
---|
| 122 | while (true) {
|
---|
| 123 | ZipEntry e = zip.getNextEntry();
|
---|
| 124 | if (e == null)
|
---|
| 125 | break;
|
---|
| 126 | String name = e.getName();
|
---|
| 127 | if (name.startsWith(dir)) {
|
---|
| 128 | copyResource("/" + name, name.replace(dir, target));
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * Copy 1 resource inside a jar. If given name actually ends with "/" it is
|
---|
| 135 | * assumed a directory and the directory is created if it does not yet
|
---|
| 136 | * exist.
|
---|
| 137 | *
|
---|
| 138 | * @param name
|
---|
| 139 | * the file/directory name.Name should end with "/" iff it is a
|
---|
| 140 | * directory.
|
---|
| 141 | * @param target
|
---|
| 142 | * the target name/directory.
|
---|
| 143 | * @throws IOException
|
---|
| 144 | */
|
---|
| 145 | private static void copyResource(String name, String target) throws IOException {
|
---|
| 146 | /*
|
---|
| 147 | * make the directory that should hold this file. Notice: sometimes the
|
---|
| 148 | * zip file contains the directories in "nice" order so that dir names
|
---|
| 149 | * are before files inside these dirs. And at other times, they are
|
---|
| 150 | * not...
|
---|
| 151 | */
|
---|
| 152 | createDir(new File(target));
|
---|
| 153 | if (name.endsWith("/")) {
|
---|
| 154 | return;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | InputStream stream = null;
|
---|
| 158 | OutputStream resStreamOut = null;
|
---|
| 159 | try {
|
---|
| 160 | stream = DomainInstaller.class.getResourceAsStream(name);
|
---|
| 161 | if (stream == null) {
|
---|
| 162 | throw new FileNotFoundException("file not found " + name);
|
---|
| 163 | }
|
---|
| 164 | int readBytes;
|
---|
| 165 | byte[] buffer = new byte[4096];
|
---|
| 166 | resStreamOut = new FileOutputStream(target);
|
---|
| 167 | while ((readBytes = stream.read(buffer)) > 0) {
|
---|
| 168 | resStreamOut.write(buffer, 0, readBytes);
|
---|
| 169 | }
|
---|
| 170 | } finally {
|
---|
| 171 | if (stream != null) {
|
---|
| 172 | stream.close();
|
---|
| 173 | }
|
---|
| 174 | if (resStreamOut != null) {
|
---|
| 175 | resStreamOut.close();
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | private static void createDir(File dir) {
|
---|
| 181 | if (dir == null)
|
---|
| 182 | return;
|
---|
| 183 | if (!dir.isDirectory()) {
|
---|
| 184 | dir = dir.getParentFile();
|
---|
| 185 | if (dir == null)
|
---|
| 186 | return;
|
---|
| 187 | }
|
---|
| 188 | dir.mkdirs();
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | } |
---|