(final String injectionMethod, final String[] args)
| 115 | } |
| 116 | |
| 117 | public static void injectedMain(final String injectionMethod, final String[] args) throws InterruptedException, IOException, InvocationTargetException { |
| 118 | final boolean useUI = args.length == 0 && !GraphicsEnvironment.isHeadless(); |
| 119 | final boolean useConfig = args.length == 2 && args[0].equals("config"); |
| 120 | final boolean useCLI = args.length > 0 && args[0].equals("cli"); |
| 121 | |
| 122 | final List<File> potentialCwds = new ArrayList<>(); |
| 123 | if (System.getenv("VP_RUN_DIR") != null) { |
| 124 | potentialCwds.add(new File(System.getenv("VP_RUN_DIR"))); |
| 125 | } |
| 126 | potentialCwds.add(new File(System.getProperty("user.dir"))); |
| 127 | potentialCwds.add(new File(".")); |
| 128 | JarUtil.getJarFile().map(File::getParentFile).ifPresent(potentialCwds::add); |
| 129 | |
| 130 | final List<File> failedCwds = new ArrayList<>(); |
| 131 | for (File potentialCwd : potentialCwds) { |
| 132 | if (potentialCwd.isDirectory()) { |
| 133 | if (Files.isWritable(potentialCwd.toPath())) { |
| 134 | CWD = potentialCwd; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | failedCwds.add(potentialCwd); |
| 139 | } |
| 140 | if (CWD == null) { // Backup strategy for weird permission setups: Attempt to write a dummy file to check if the directory is writable |
| 141 | for (File potentialCwd : potentialCwds) { |
| 142 | if (potentialCwd.isDirectory()) { |
| 143 | try { |
| 144 | final Path testFile = new File(potentialCwd, "viaproxy_writable_test.txt").toPath(); |
| 145 | Files.deleteIfExists(testFile); |
| 146 | Files.writeString(testFile, "This is just a test. This file can be deleted."); |
| 147 | Files.deleteIfExists(testFile); |
| 148 | CWD = potentialCwd; |
| 149 | break; |
| 150 | } catch (IOException ignored) { |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | if (CWD != null) { |
| 156 | System.setProperty("user.dir", CWD.getAbsolutePath()); |
| 157 | } else if (useUI) { |
| 158 | JOptionPane.showMessageDialog(null, "Could not find a suitable directory to use as working directory. Make sure that the current folder is writeable.", "ViaProxy", JOptionPane.ERROR_MESSAGE); |
| 159 | System.exit(1); |
| 160 | } else { |
| 161 | System.err.println("Could not find a suitable directory to use as working directory. Make sure that the current folder is writeable."); |
| 162 | System.err.println("Attempted to use the following directories:"); |
| 163 | for (File failedCwd : failedCwds) { |
| 164 | System.err.println("\t- " + failedCwd.getAbsolutePath()); |
| 165 | } |
| 166 | System.exit(1); |
| 167 | } |
| 168 | |
| 169 | Logger.setup(); |
| 170 | if (!useUI && !useConfig && !useCLI) { |
| 171 | final String fileName = JarUtil.getJarFile().map(File::getName).orElse("ViaProxy.jar"); |
| 172 | Logger.LOGGER.info("Usage: java -jar " + fileName + " | Starts ViaProxy in graphical mode if available"); |
| 173 | Logger.LOGGER.info("Usage: java -jar " + fileName + " config <config file> | Starts ViaProxy with the specified config file"); |
| 174 | Logger.LOGGER.info("Usage: java -jar " + fileName + " cli --help | Starts ViaProxy in CLI mode"); |
no test coverage detected