| 49 | type JavaSource map[string]string |
| 50 | |
| 51 | func runJavaServer(ctx context.Context, sources JavaSource, port int) task.Signal { |
| 52 | signal, done := task.NewSignal() |
| 53 | go func() { |
| 54 | defer done(ctx) |
| 55 | tmp, err := ioutil.TempDir("", "jdwp") |
| 56 | if err != nil { |
| 57 | log.E(ctx, "Couldn't get temporary directory. Error: %v", err) |
| 58 | return |
| 59 | } |
| 60 | defer os.RemoveAll(tmp) |
| 61 | sourceNames := make([]string, 0, len(sources)) |
| 62 | for name, source := range sources { |
| 63 | if err := ioutil.WriteFile(filepath.Join(tmp, name), []byte(source), 0777); err != nil { |
| 64 | log.E(ctx, "Couldn't write to temporary source file. Error: %v", err) |
| 65 | return |
| 66 | } |
| 67 | sourceNames = append(sourceNames, name) |
| 68 | } |
| 69 | l := log.From(ctx) |
| 70 | if err := shell. |
| 71 | Command("javac", sourceNames...). |
| 72 | In(tmp). |
| 73 | Capture(nil, l.Writer(log.Error)). |
| 74 | Run(ctx); err != nil { |
| 75 | |
| 76 | log.E(ctx, "Couldn't compile java source. Error: %v", err) |
| 77 | return |
| 78 | } |
| 79 | agentlib := fmt.Sprintf("-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=%v", port) |
| 80 | if err := shell. |
| 81 | Command("java", agentlib, "main"). |
| 82 | In(tmp). |
| 83 | Capture(l.Writer(log.Info), l.Writer(log.Error)). |
| 84 | Run(ctx); err != nil { |
| 85 | |
| 86 | if !task.Stopped(ctx) { |
| 87 | log.E(ctx, "Couldn't start java server. Error: %v", err) |
| 88 | } |
| 89 | return |
| 90 | } |
| 91 | }() |
| 92 | return signal |
| 93 | } |
| 94 | |
| 95 | // BuildRunAndConnect builds all the source files with javac, executes them with |
| 96 | // java, attaches to it via JDWP, then calls onConnect with the connection. |