67public class LaunchRemote {
69 public static Logger LOGGER = Logger.getLogger(
"");
70 public static Logger CLIENT_LOGGER = Logger.getLogger(
"> Client: ");
74 public void invoke() {
75 System.out.print(getReceived());
79 private InetSocketAddress remoteAgentSocketAddress;
81 @SuppressWarnings(
"unused")
84 private Socket socket;
85 private InputStream inputStream;
86 private DataInputStream dataInputStream;
87 private OutputStream outputStream;
88 private DataOutputStream dataOutputStream;
103 private Set<String> excludeClassPath =
new LinkedHashSet<String>();
105 public LaunchRemote() {
108 public InetSocketAddress getRemoteAgentSocketAddress() {
109 return remoteAgentSocketAddress;
112 public void setRemoteAgentSocketAddress(InetSocketAddress remoteAgentSocketAddress) {
113 this.remoteAgentSocketAddress = remoteAgentSocketAddress;
116 public void setup()
throws IOException {
118 socket =
new Socket(remoteAgentSocketAddress.getAddress(), remoteAgentSocketAddress.getPort());
119 }
catch (ConnectException e) {
120 System.err.println(
"Cannot connect to " + remoteAgentSocketAddress.getAddress() +
":"
121 + remoteAgentSocketAddress.getPort());
122 System.err.println(
"The Agent is probably not running on the remote machine.");
123 System.err.println(
"Get agent.jar and run it with: ");
124 System.err.println(
" java -jar agent.jar ");
125 if (remoteAgentSocketAddress.getPort() == 8999) {
126 System.err.println(
"for default port 8999. Or if you want to run it at alternative port run it with:");
127 System.err.println(
" java -jar agent.jar -l <new-port>");
128 System.err.println(
"But then you need to run this side launcher with that port as well.");
131 "for default port 8999. Or if you want to run it at alternative port (as specified here) run it with:");
132 System.err.println(
" java -jar agent.jar -l " + remoteAgentSocketAddress.getPort());
137 inputStream = socket.getInputStream();
138 dataInputStream =
new DataInputStream(inputStream);
140 outputStream = socket.getOutputStream();
141 dataOutputStream =
new DataOutputStream(outputStream);
146 stateMachine.getProcessorMap().put(
ReadyProcessor.ID, readyProcessor);
148 stateMachine.getProcessorMap().put(
"S0", streamProcessor);
152 public void processRequest(String pathId, String resourceName)
throws IOException {
153 String pathString = localClasspathProcessor.getLocalClasspath().get(pathId);
154 File path =
new File(pathString);
155 File resource =
new File(path, resourceName);
156 if (!resource.exists()) {
157 resourceProcessor.send(dataOutputStream,
null, -1);
159 FileInputStream fis =
new FileInputStream(resource);
161 resourceProcessor.send(dataOutputStream, fis, (
int) resource.length());
171 public void start()
throws IOException {
172 LOGGER.info(
"Collecting classpath...");
174 LOGGER.info(
"Collecting resources...");
176 LOGGER.info(
"Starting remote VM...");
178 startRemoteVMProcessor.send(dataOutputStream);
179 if (LOGGER.isLoggable(Level.FINE)) {
180 LOGGER.fine(
" With remote VM arguments:");
181 for (String a : startRemoteVMProcessor.getVmArgs()) {
182 LOGGER.fine(
" " + a);
184 LOGGER.fine(
" With launcher arguments:");
185 for (String a : startRemoteVMProcessor.getLauncherArgs()) {
186 LOGGER.fine(
" " + a);
190 LOGGER.info(
"Waiting for remote client to start...");
191 synchronized (readyProcessor) {
192 while (!readyProcessor.isReady()) {
193 stateMachine.processInput(dataInputStream);
196 LOGGER.info(
"Remote client started.");
198 LOGGER.info(
"Sending remote classpath...");
199 remoteClasspathProcessor.send(dataOutputStream);
201 LOGGER.info(
"Sending classpath...");
202 localClasspathProcessor.send(dataOutputStream);
204 LOGGER.info(
"Sending resource details...");
205 cacheResponseProcessor.send(dataOutputStream);
207 LOGGER.info(
"Sending arguments...");
208 argumentsProcessor.send(dataOutputStream);
209 if (LOGGER.isLoggable(Level.FINE)) {
210 for (String a : argumentsProcessor.getArguments()) {
211 LOGGER.fine(
" " + a);
215 LOGGER.info(
"Sending main class \"" + mainClassProcessor.getMainClass() +
"\"...");
216 mainClassProcessor.send(dataOutputStream);
218 LOGGER.info(
"Sending signal for client to start application...");
219 startApplicationProcessor.send(dataOutputStream);
221 LOGGER.info(
"Output form the client:");
222 LOGGER.info(
"----------------------------------------------------------------------------------------------");
226 stateMachine.processInput(dataInputStream);
228 }
catch (IOException e) {
231 LOGGER.info(
"----------------------------------------------------------------------------------------------");
232 LOGGER.info(
"Finished.");
235 private URI[] getClassList() {
236 String path = System.getProperty(
"user.dir").replace(
'\\',
'/');
237 int idx = path.lastIndexOf(
'/');
238 String root = path.substring(idx) +
"/";
239 List<URI> classpath =
new ClassGraph().getClasspathURIs();
245 for (
int i = classpath.size() - 1; i >= 0; i--) {
246 URI uri = classpath.get(i);
247 String file = uri.getPath();
249 if ((file ==
null) || !file.contains(root)) {
258 URI[] items =
new URI[classpath.size()];
259 return (classpath.toArray(items));
262 protected int addFile(String[] rootComponents, String filePath,
int nextId) {
263 filePath = filePath.replace(
"\\",
"/");
264 String[] filePathComponents = filePath.split(
"/");
265 String relPath =
PathUtils.relativisePath(rootComponents, filePathComponents);
268 String pathId = localClasspathProcessor.getLocalClasspath().get(relPath);
269 if (pathId ==
null) {
270 pathId = Integer.toString(nextId);
271 localClasspathProcessor.getLocalClasspath().put(pathId, relPath);
278 protected int collectDeployFiles(String[] rootComponents, String path,
int nextId) {
279 File folder =
new File(path);
282 File[] files = folder.listFiles();
284 for (File file : files) {
286 nextId = addFile(rootComponents, file.getAbsolutePath(), nextId);
287 if (file.isDirectory()) {
288 nextId = collectDeployFiles(rootComponents, file.getAbsolutePath(), nextId);
297 protected void collectClasspath()
throws IOException {
301 File hereDir =
new File(
"");
302 String herePath = hereDir.getAbsolutePath().replace(
"\\",
"/");
304 String[] herePathComponents = herePath.split(
"/");
305 String launchersPath =
ClasspathUtils.obtainPathOfClass(getClass());
306 if (!launchersPath.endsWith(
".jar")) {
307 launchersPath =
null;
314 URI[] urls = getClassList();
315 for (URI url : urls) {
320 File file =
new File(url.getPath());
321 String filePath = file.getAbsolutePath().replace(
"\\",
"/");
323 if (!filePath.equals(launchersPath)) {
324 String[] filePathComponents = filePath.split(
"/");
325 String relativisedPath =
PathUtils.relativisePath(herePathComponents, filePathComponents);
327 if (!excludeClassPath.contains(relativisedPath) && !excludeClassPath.contains(filePath)) {
328 String pathId = localClasspathProcessor.getLocalClasspath().get(relativisedPath);
329 if (pathId ==
null) {
330 pathId = Integer.toString(nextId);
331 localClasspathProcessor.getLocalClasspath().put(pathId, relativisedPath);
337 if (LOGGER.isLoggable(Level.FINE)) {
338 for (Map.Entry<String, String> cpe : localClasspathProcessor.getLocalClasspath().entrySet()) {
339 LOGGER.fine(
" " + cpe.getKey() +
": " + cpe.getValue());
344 nextId = addFile(herePathComponents, herePath +
"/deploy", nextId);
345 collectDeployFiles(herePathComponents, herePath +
"/deploy", nextId);
352 protected void collectResources() {
353 for (Map.Entry<String, String> entry : localClasspathProcessor.getLocalClasspath().entrySet()) {
354 collectResources(entry.getKey(),
new File(entry.getValue()),
"");
356 if (LOGGER.isLoggable(Level.FINER)) {
357 for (ResourceResponse r : cacheResponseProcessor.getResources()) {
358 LOGGER.finer(
" " + r.pathId +
": (" + r.hash +
", " + r.length +
") " + r.name);
363 protected void collectResources(String pathId, File path, String resource) {
364 File file =
new File(path, resource);
365 if (file.isDirectory()) {
366 for (File f : file.listFiles()) {
367 if (!f.equals(
".") && !f.equals(
"..")) {
369 if (resource.equals(
"")) {
370 resourceFile =
new File(f.getName());
372 resourceFile =
new File(
new File(resource), f.getName());
374 collectResources(pathId, path, resourceFile.getPath());
378 ResourceResponse rr =
new ResourceResponse();
380 rr.name = resource.replace(
"\\",
"/");
381 rr.hash = file.lastModified();
382 rr.length = file.length();
383 cacheResponseProcessor.getResources().add(rr);
387 public void run(String[] args)
throws Exception {
388 LaunchRemote client =
this;
392 boolean remoteClasspath =
false;
393 boolean excludeClasspath =
false;
394 boolean addressAndPort =
true;
395 boolean mainClass =
true;
396 boolean arguments =
false;
397 boolean allRead =
false;
398 boolean debugLevelFlag =
false;
399 boolean remoteDebugPortFlag =
false;
400 boolean remoteVMarg =
false;
401 for (String arg : args) {
403 if (remoteClasspath) {
404 client.remoteClasspathProcessor.getRemoteClasspath().add(arg);
405 remoteClasspath =
false;
406 }
else if (excludeClasspath) {
407 client.excludeClassPath.add(arg);
408 excludeClasspath =
false;
409 }
else if (debugLevelFlag) {
410 debugLevel = Integer.parseInt(arg);
411 client.startRemoteVMProcessor.getLauncherArgs().add(
"-d");
412 client.startRemoteVMProcessor.getLauncherArgs().add(Integer.toString(Integer.parseInt(arg)));
413 debugLevelFlag =
false;
414 }
else if (remoteDebugPortFlag) {
415 client.startRemoteVMProcessor.setRemoteDebugPort(Integer.parseInt(arg));
416 remoteDebugPortFlag =
false;
417 }
else if (remoteVMarg) {
418 client.startRemoteVMProcessor.getVmArgs().add(arg);
420 }
else if (
"-rcp".equals(arg) ||
"--remote-classpath".equals(arg)) {
421 remoteClasspath =
true;
422 }
else if (
"-ecp".equals(arg) ||
"--exclude-classpath".equals(arg)) {
423 excludeClasspath =
true;
424 }
else if (
"-d".equals(arg) ||
"--debug".equals(arg)) {
425 debugLevelFlag =
true;
426 }
else if (
"-rdp".equals(arg) ||
"--remote-debug-port".equals(arg)) {
427 remoteDebugPortFlag =
true;
428 }
else if (
"-rds".equals(arg) ||
"--remote-debug-suspend".equals(arg)) {
429 client.startRemoteVMProcessor.setRemoteDebugSuspendAtStart(
true);
430 }
else if (
"-rvma".equals(arg) ||
"--remote-VM-argment".equals(arg)) {
432 }
else if (
"-h".equals(arg) ||
"--help".equals(arg)) {
435 }
else if (addressAndPort) {
436 int i = arg.indexOf(
':');
438 client.setRemoteAgentSocketAddress(
new InetSocketAddress(Integer.parseInt(arg)));
440 client.setRemoteAgentSocketAddress(
441 new InetSocketAddress(arg.substring(0, i), Integer.parseInt(arg.substring(i + 1))));
443 addressAndPort =
false;
445 }
else if (mainClass) {
446 client.mainClassProcessor.setMainClass(arg);
449 }
else if (allRead) {
450 if (
"--".equals(arg)) {
454 System.out.println(
"ERROR: unknown argument '" + arg +
"'");
455 System.out.println();
460 System.out.println(
"ERROR: unknown argument '" + arg +
"'");
461 System.out.println();
466 client.argumentsProcessor.getArguments().add(arg);
469 if (addressAndPort) {
470 System.out.println(
"ERROR: missing address and port argument.");
471 System.out.println();
476 System.out.println(
"ERROR: missing main class argument.");
477 System.out.println();
489 public static void printHelp() {
490 System.out.println(
"Remote VM Launcher usage:");
491 System.out.println(
"");
492 System.out.println(
"java -jar remotevmlauncher-client.jar {options} [address:]port mainClass");
493 System.out.println(
"");
494 System.out.println(
"Options:");
495 System.out.println(
"");
496 System.out.println(
" -rcp|--remote-classpath classpath-entry");
497 System.out.println(
" classpath entry as seen from remote machine");
498 System.out.println(
" where agent is running.");
499 System.out.println(
" -ecp|--exclude-classpath classpath-entry");
500 System.out.println(
" classpath entry not to be send to the remote");
501 System.out.println(
" machine where agent is running.");
502 System.out.println(
" -d|--debug level debug level from 0 to 4. Default: 0");
503 System.out.println(
" -rdp|--remote-debug-port port ");
504 System.out.println(
" If port is specified remote VM will be launched");
505 System.out.println(
" in debug mode at the specified port.");
506 System.out.println(
" -rds|--remote-debug-suspend ");
507 System.out.println(
" If port is specified and if this flag as well");
508 System.out.println(
" remote VM will be suspended.");
509 System.out.println(
" -h|--help this help.");
510 System.out.println(
"");
511 System.out.println(
"If launcher is used from an IDE, and ");
512 System.out.println(
" org.ah.java.remotevmlauncher.client.LaunchRemote class is directly used as");
513 System.out.println(
"main class, then supplied classpath will automatically used (provided that it");
514 System.out.println(
"is defined as URLClassLoader or descendant class) on the remote side.");
515 System.out.println(
"");
516 System.out.println(
"Note: if remote debug port is specified launcher's VM is started with:");
517 System.out.println(
"-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=(y|n),address=<port>");
518 System.out.println(
"depending on port and if suspend is specified or not.");