1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.console.editor;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.StringTokenizer;
29
30 import net.grinder.common.GrinderException;
31 import net.grinder.console.distribution.AgentCacheState;
32
33
34
35
36
37
38 class ExternalEditor {
39
40 private static final ThreadGroup s_threadGroup =
41 new ThreadGroup("ExternalEditor");
42
43 static final ThreadGroup getThreadGroup() {
44 return s_threadGroup;
45 }
46
47 private final AgentCacheState m_agentCacheState;
48 private final EditorModel m_editorModel;
49 private final File m_command;
50 private final String m_arguments;
51
52 public ExternalEditor(AgentCacheState agentCacheState,
53 EditorModel editorModel,
54 File command,
55 String arguments) {
56 m_agentCacheState = agentCacheState;
57 m_editorModel = editorModel;
58 m_command = command;
59 m_arguments = arguments;
60 }
61
62
63 String[] fileToCommandLine(File file) {
64 final List<String> result = new ArrayList<String>();
65 result.add(m_command.getAbsolutePath());
66
67 boolean fileTemplateFound = false;
68
69 if (m_arguments != null) {
70 final StringTokenizer tokenizer = new StringTokenizer(m_arguments);
71
72 while (tokenizer.hasMoreElements()) {
73 final String token = tokenizer.nextToken();
74
75 final String argument = token.replaceAll("%f", file.getAbsolutePath());
76 result.add(argument);
77
78 fileTemplateFound |= !argument.equals(token);
79 }
80 }
81
82 if (!fileTemplateFound) {
83 result.add(file.getAbsolutePath());
84 }
85
86 return result.toArray(new String[result.size()]);
87 }
88
89 public void open(final File file) throws IOException {
90 final long originalModificationTime = file.lastModified();
91
92 final Process exec = Runtime.getRuntime().exec(
93 fileToCommandLine(file),
94 null,
95 file.getParentFile());
96
97 final Runnable handleCompletion = new Runnable() {
98 public void run() {
99 try {
100
101
102
103 exec.waitFor();
104 }
105 catch (InterruptedException e) {
106
107 return;
108 }
109
110
111 final long lastModified = file.lastModified();
112
113 if (lastModified > originalModificationTime) {
114 m_agentCacheState.setNewFileTime(lastModified);
115
116
117 final Buffer buffer = m_editorModel.getBufferForFile(file);
118
119 if (buffer != null && !buffer.isDirty()) {
120 try {
121 buffer.load();
122 }
123 catch (GrinderException e) {
124
125 }
126 }
127 }
128 }
129 };
130
131 final Thread thread = new Thread(getThreadGroup(),
132 handleCompletion,
133 "External edit of " + file);
134 thread.setDaemon(true);
135 thread.start();
136 }
137 }