Committed by
Gerrit Code Review
Refactoring checkstyle daemon into a more general purpose buck daemon.
Change-Id: I999541e8894f07061141b3a62414e491369f8d08
Showing
7 changed files
with
324 additions
and
153 deletions
... | @@ -37,10 +37,11 @@ def checkstyle( | ... | @@ -37,10 +37,11 @@ def checkstyle( |
37 | 37 | ||
38 | sh_test( | 38 | sh_test( |
39 | name = name + '-checkstyle', | 39 | name = name + '-checkstyle', |
40 | - test = '//tools/build/conf:start-checkstyle', | 40 | + test = '//tools/build/conf:start-buck-daemon', |
41 | deps = [ jar_target ], | 41 | deps = [ jar_target ], |
42 | args = [ | 42 | args = [ |
43 | - '$(location //tools/build/conf:checkstyle-jar)', | 43 | + '$(location //tools/build/conf:buck-daemon-jar)', |
44 | + 'checkstyle', | ||
44 | '$(location :' + name + '-checkstyle-files)', | 45 | '$(location :' + name + '-checkstyle-files)', |
45 | '$(location //tools/build/conf:checkstyle-xml)', | 46 | '$(location //tools/build/conf:checkstyle-xml)', |
46 | '$(location //tools/build/conf:suppressions-xml)', | 47 | '$(location //tools/build/conf:suppressions-xml)', | ... | ... |
... | @@ -14,7 +14,7 @@ export_file ( | ... | @@ -14,7 +14,7 @@ export_file ( |
14 | ) | 14 | ) |
15 | 15 | ||
16 | export_file ( | 16 | export_file ( |
17 | - name = 'start-checkstyle', | 17 | + name = 'start-buck-daemon', |
18 | visibility = [ 'PUBLIC' ], | 18 | visibility = [ 'PUBLIC' ], |
19 | ) | 19 | ) |
20 | 20 | ||
... | @@ -38,9 +38,9 @@ java_library ( | ... | @@ -38,9 +38,9 @@ java_library ( |
38 | ) | 38 | ) |
39 | 39 | ||
40 | java_binary ( | 40 | java_binary ( |
41 | - name = 'checkstyle-jar', | 41 | + name = 'buck-daemon-jar', |
42 | deps = [ ':checkstyle' ] + RUN, | 42 | deps = [ ':checkstyle' ] + RUN, |
43 | - main_class = 'org.onosproject.checkstyle.Main', | 43 | + main_class = 'org.onosproject.buckdaemon.BuckDaemon', |
44 | blacklist = [ 'META-INF/.*' ], | 44 | blacklist = [ 'META-INF/.*' ], |
45 | visibility = [ 'PUBLIC' ], | 45 | visibility = [ 'PUBLIC' ], |
46 | ) | 46 | ) | ... | ... |
1 | /* | 1 | /* |
2 | - * Copyright 2016-present Open Networking Laboratory | 2 | + * Copyright 2016 Open Networking Laboratory |
3 | * | 3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | 5 | * you may not use this file except in compliance with the License. |
... | @@ -14,54 +14,80 @@ | ... | @@ -14,54 +14,80 @@ |
14 | * limitations under the License. | 14 | * limitations under the License. |
15 | */ | 15 | */ |
16 | 16 | ||
17 | -package org.onosproject.checkstyle; | 17 | +package org.onosproject.buckdaemon; |
18 | 18 | ||
19 | +import com.google.common.io.ByteStreams; | ||
19 | import com.puppycrawl.tools.checkstyle.api.CheckstyleException; | 20 | import com.puppycrawl.tools.checkstyle.api.CheckstyleException; |
21 | +import org.onosproject.checkstyle.CheckstyleRunner; | ||
20 | 22 | ||
21 | import java.io.IOException; | 23 | import java.io.IOException; |
22 | import java.net.ServerSocket; | 24 | import java.net.ServerSocket; |
25 | +import java.net.Socket; | ||
23 | import java.nio.ByteBuffer; | 26 | import java.nio.ByteBuffer; |
24 | import java.nio.channels.FileChannel; | 27 | import java.nio.channels.FileChannel; |
25 | import java.nio.channels.FileLock; | 28 | import java.nio.channels.FileLock; |
26 | import java.nio.file.Files; | 29 | import java.nio.file.Files; |
27 | import java.nio.file.Path; | 30 | import java.nio.file.Path; |
28 | import java.nio.file.Paths; | 31 | import java.nio.file.Paths; |
32 | +import java.util.HashMap; | ||
33 | +import java.util.Map; | ||
29 | import java.util.Timer; | 34 | import java.util.Timer; |
30 | import java.util.TimerTask; | 35 | import java.util.TimerTask; |
36 | +import java.util.concurrent.Callable; | ||
31 | import java.util.concurrent.ExecutorService; | 37 | import java.util.concurrent.ExecutorService; |
32 | import java.util.concurrent.Executors; | 38 | import java.util.concurrent.Executors; |
33 | 39 | ||
34 | import static java.nio.file.StandardOpenOption.*; | 40 | import static java.nio.file.StandardOpenOption.*; |
35 | 41 | ||
36 | /** | 42 | /** |
37 | - * Main program for executing scenario test warden. | 43 | + * Buck daemon process. |
38 | */ | 44 | */ |
39 | -public final class Main { | 45 | +public final class BuckDaemon { |
40 | 46 | ||
41 | private static long POLLING_INTERVAL = 1000; //ms | 47 | private static long POLLING_INTERVAL = 1000; //ms |
42 | 48 | ||
49 | + private final Map<String, BuckTask> tasks = new HashMap<>(); | ||
50 | + private final String portLock; | ||
51 | + private final String buckPid; | ||
52 | + | ||
43 | // Public construction forbidden | 53 | // Public construction forbidden |
44 | - private Main(String[] args) { | 54 | + private BuckDaemon(String[] args) { |
55 | + portLock = args[0]; | ||
56 | + buckPid = args[1]; | ||
45 | } | 57 | } |
46 | 58 | ||
47 | /** | 59 | /** |
48 | - * Main entry point for the cell warden. | 60 | + * Main entry point for the daemon. |
49 | * | 61 | * |
50 | * @param args command-line arguments | 62 | * @param args command-line arguments |
51 | */ | 63 | */ |
52 | public static void main(String[] args) | 64 | public static void main(String[] args) |
53 | throws CheckstyleException, IOException { | 65 | throws CheckstyleException, IOException { |
54 | - startServer(args); | 66 | + BuckDaemon daemon = new BuckDaemon(args); |
67 | + daemon.registerTasks(); | ||
68 | + daemon.startServer(); | ||
69 | + } | ||
70 | + | ||
71 | + /** | ||
72 | + * Registers re-entrant tasks by their task name. | ||
73 | + */ | ||
74 | + private void registerTasks() { | ||
75 | + tasks.put("checkstyle", new CheckstyleRunner(System.getProperty("checkstyle.config"), | ||
76 | + System.getProperty("checkstyle.suppressions"))); | ||
77 | + // tasks.put("swagger", new SwaggerGenerator()); | ||
55 | } | 78 | } |
56 | 79 | ||
57 | - // Monitors another PID and exit when that process exits | 80 | + /** |
58 | - private static void watchProcess(String pid) { | 81 | + * Monitors another PID and exit when that process exits. |
82 | + */ | ||
83 | + private void watchProcess(String pid) { | ||
59 | if (pid == null || pid.equals("0")) { | 84 | if (pid == null || pid.equals("0")) { |
60 | return; | 85 | return; |
61 | } | 86 | } |
62 | Timer timer = new Timer(true); // start as a daemon, so we don't hang shutdown | 87 | Timer timer = new Timer(true); // start as a daemon, so we don't hang shutdown |
63 | timer.scheduleAtFixedRate(new TimerTask() { | 88 | timer.scheduleAtFixedRate(new TimerTask() { |
64 | private String cmd = "kill -s 0 " + pid; | 89 | private String cmd = "kill -s 0 " + pid; |
90 | + | ||
65 | @Override | 91 | @Override |
66 | public void run() { | 92 | public void run() { |
67 | try { | 93 | try { |
... | @@ -79,13 +105,10 @@ public final class Main { | ... | @@ -79,13 +105,10 @@ public final class Main { |
79 | }, POLLING_INTERVAL, POLLING_INTERVAL); | 105 | }, POLLING_INTERVAL, POLLING_INTERVAL); |
80 | } | 106 | } |
81 | 107 | ||
82 | - // Initiates a server. | 108 | + /** |
83 | - private static void startServer(String[] args) throws IOException, CheckstyleException { | 109 | + * Initiates a server. |
84 | - String portLock = args[0]; | 110 | + */ |
85 | - String buckPid = args[1]; | 111 | + private void startServer() throws IOException, CheckstyleException { |
86 | - String checkstyleFile = args[2]; | ||
87 | - String suppressionsFile = args[3]; | ||
88 | - | ||
89 | // Use a file lock to ensure only one copy of the daemon runs | 112 | // Use a file lock to ensure only one copy of the daemon runs |
90 | Path portLockPath = Paths.get(portLock); | 113 | Path portLockPath = Paths.get(portLock); |
91 | FileChannel channel = FileChannel.open(portLockPath, WRITE, CREATE); | 114 | FileChannel channel = FileChannel.open(portLockPath, WRITE, CREATE); |
... | @@ -104,10 +127,8 @@ public final class Main { | ... | @@ -104,10 +127,8 @@ public final class Main { |
104 | // Set up hook to clean up after ourselves | 127 | // Set up hook to clean up after ourselves |
105 | Runtime.getRuntime().addShutdownHook(new Thread(() -> { | 128 | Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
106 | try { | 129 | try { |
107 | - if (channel != null) { | 130 | + channel.truncate(0); |
108 | - channel.truncate(0); | 131 | + channel.close(); |
109 | - channel.close(); | ||
110 | - } | ||
111 | System.err.println("tear down..."); | 132 | System.err.println("tear down..."); |
112 | Files.delete(portLockPath); | 133 | Files.delete(portLockPath); |
113 | } catch (IOException e) { | 134 | } catch (IOException e) { |
... | @@ -122,15 +143,58 @@ public final class Main { | ... | @@ -122,15 +143,58 @@ public final class Main { |
122 | channel.write(ByteBuffer.wrap(Integer.toString(port).getBytes())); | 143 | channel.write(ByteBuffer.wrap(Integer.toString(port).getBytes())); |
123 | 144 | ||
124 | // Instantiate a Checkstyle runner and executor; serve until exit... | 145 | // Instantiate a Checkstyle runner and executor; serve until exit... |
125 | - CheckstyleRunner runner = new CheckstyleRunner(checkstyleFile, suppressionsFile); | ||
126 | ExecutorService executor = Executors.newCachedThreadPool(); | 146 | ExecutorService executor = Executors.newCachedThreadPool(); |
127 | while (true) { | 147 | while (true) { |
128 | try { | 148 | try { |
129 | - executor.submit(runner.checkClass(server.accept())); | 149 | + executor.submit(new BuckTaskRunner(server.accept())); |
130 | } catch (Exception e) { | 150 | } catch (Exception e) { |
131 | e.printStackTrace(); | 151 | e.printStackTrace(); |
132 | //no-op | 152 | //no-op |
133 | } | 153 | } |
134 | } | 154 | } |
135 | } | 155 | } |
156 | + | ||
157 | + /** | ||
158 | + * Runnable capable of invoking the appropriate Buck task with input | ||
159 | + * consumed form the specified socket and output produced back to that | ||
160 | + * socket. | ||
161 | + */ | ||
162 | + private class BuckTaskRunner implements Runnable { | ||
163 | + | ||
164 | + private final Socket socket; | ||
165 | + | ||
166 | + public BuckTaskRunner(Socket socket) { | ||
167 | + this.socket = socket; | ||
168 | + } | ||
169 | + | ||
170 | + @Override | ||
171 | + public void run() { | ||
172 | + try { | ||
173 | + BuckTaskContext context = new BuckTaskContext(socket.getInputStream()); | ||
174 | + String taskName = context.taskName(); | ||
175 | + if (!taskName.isEmpty()) { | ||
176 | + BuckTask task = tasks.get(taskName); | ||
177 | + if (task != null) { | ||
178 | + System.out.println(String.format("Executing task '%s'", taskName)); | ||
179 | + task.execute(context); | ||
180 | + for (String line : context.output()) { | ||
181 | + output(socket, line); | ||
182 | + } | ||
183 | + } else { | ||
184 | + String message = String.format("No task named '%s'", taskName); | ||
185 | + System.out.print(message); | ||
186 | + output(socket, message); | ||
187 | + } | ||
188 | + } | ||
189 | + socket.getOutputStream().flush(); | ||
190 | + socket.close(); | ||
191 | + } catch (IOException e) { | ||
192 | + e.printStackTrace(); | ||
193 | + } | ||
194 | + } | ||
195 | + | ||
196 | + private void output(Socket socket, String line) throws IOException { | ||
197 | + socket.getOutputStream().write((line + "\n").getBytes()); | ||
198 | + } | ||
199 | + } | ||
136 | } | 200 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +package org.onosproject.buckdaemon; | ||
18 | + | ||
19 | +/** | ||
20 | + * Abstraction of a Buck task that can be spawned by the Buck daemon | ||
21 | + */ | ||
22 | +public interface BuckTask { | ||
23 | + | ||
24 | + /** | ||
25 | + * Executes the task, consuming the specified input and producing output. | ||
26 | + * | ||
27 | + * @param context context for the tast operation | ||
28 | + */ | ||
29 | + void execute(BuckTaskContext context); | ||
30 | + | ||
31 | +} |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +package org.onosproject.buckdaemon; | ||
18 | + | ||
19 | +import com.google.common.collect.ImmutableList; | ||
20 | +import com.google.common.io.ByteStreams; | ||
21 | + | ||
22 | +import java.io.IOException; | ||
23 | +import java.io.InputStream; | ||
24 | +import java.util.ArrayList; | ||
25 | +import java.util.List; | ||
26 | + | ||
27 | +import static com.google.common.base.Preconditions.checkArgument; | ||
28 | + | ||
29 | +/** | ||
30 | + * Context for executing a single Buck task. | ||
31 | + */ | ||
32 | +public class BuckTaskContext { | ||
33 | + | ||
34 | + private final String taskName; | ||
35 | + private final ImmutableList<String> input; | ||
36 | + private final List<String> output = new ArrayList<>(); | ||
37 | + | ||
38 | + BuckTaskContext(InputStream inputString) throws IOException { | ||
39 | + String[] split = new String(ByteStreams.toByteArray(inputString)).split("\n"); | ||
40 | + checkArgument(split.length >= 1, "Request must contain at least task type"); | ||
41 | + this.taskName = split[0]; | ||
42 | + ImmutableList.Builder<String> builder = ImmutableList.builder(); | ||
43 | + for (int i = 1; i < split.length; i++) { | ||
44 | + builder.add(split[i]); | ||
45 | + } | ||
46 | + input = builder.build(); | ||
47 | + } | ||
48 | + | ||
49 | + /** | ||
50 | + * Returns the symbolic task name. | ||
51 | + */ | ||
52 | + public String taskName() { | ||
53 | + return taskName; | ||
54 | + } | ||
55 | + | ||
56 | + /** | ||
57 | + * Returns the input data a list of strings. | ||
58 | + * | ||
59 | + * @return input data | ||
60 | + */ | ||
61 | + public List<String> input() { | ||
62 | + return ImmutableList.copyOf(input); | ||
63 | + } | ||
64 | + | ||
65 | + /** | ||
66 | + * Returns the output data a list of strings. | ||
67 | + * | ||
68 | + * @return output data | ||
69 | + */ | ||
70 | + List<String> output() { | ||
71 | + return ImmutableList.copyOf(output); | ||
72 | + } | ||
73 | + | ||
74 | + /** | ||
75 | + * Adds a line to the output data. | ||
76 | + * | ||
77 | + * @param line line of output data | ||
78 | + */ | ||
79 | + public void output(String line) { | ||
80 | + output.add(line); | ||
81 | + } | ||
82 | + | ||
83 | +} |
... | @@ -15,7 +15,6 @@ | ... | @@ -15,7 +15,6 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.checkstyle; | 16 | package org.onosproject.checkstyle; |
17 | 17 | ||
18 | -import com.google.common.io.ByteStreams; | ||
19 | import com.puppycrawl.tools.checkstyle.Checker; | 18 | import com.puppycrawl.tools.checkstyle.Checker; |
20 | import com.puppycrawl.tools.checkstyle.ConfigurationLoader; | 19 | import com.puppycrawl.tools.checkstyle.ConfigurationLoader; |
21 | import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | 20 | import com.puppycrawl.tools.checkstyle.DefaultConfiguration; |
... | @@ -24,64 +23,51 @@ import com.puppycrawl.tools.checkstyle.api.AuditEvent; | ... | @@ -24,64 +23,51 @@ import com.puppycrawl.tools.checkstyle.api.AuditEvent; |
24 | import com.puppycrawl.tools.checkstyle.api.AuditListener; | 23 | import com.puppycrawl.tools.checkstyle.api.AuditListener; |
25 | import com.puppycrawl.tools.checkstyle.api.CheckstyleException; | 24 | import com.puppycrawl.tools.checkstyle.api.CheckstyleException; |
26 | import com.puppycrawl.tools.checkstyle.api.Configuration; | 25 | import com.puppycrawl.tools.checkstyle.api.Configuration; |
26 | +import org.onosproject.buckdaemon.BuckTask; | ||
27 | +import org.onosproject.buckdaemon.BuckTaskContext; | ||
27 | 28 | ||
28 | import java.io.File; | 29 | import java.io.File; |
29 | -import java.io.IOException; | ||
30 | -import java.net.Socket; | ||
31 | import java.util.List; | 30 | import java.util.List; |
32 | import java.util.concurrent.CountDownLatch; | 31 | import java.util.concurrent.CountDownLatch; |
33 | import java.util.stream.Collectors; | 32 | import java.util.stream.Collectors; |
34 | -import java.util.stream.Stream; | ||
35 | 33 | ||
36 | import static com.google.common.base.Strings.isNullOrEmpty; | 34 | import static com.google.common.base.Strings.isNullOrEmpty; |
37 | 35 | ||
38 | -public class CheckstyleRunner { | 36 | +/** |
37 | + * Buck task for executing checkstyle on the specified project files. | ||
38 | + */ | ||
39 | +public class CheckstyleRunner implements BuckTask { | ||
39 | 40 | ||
40 | private final Configuration config; | 41 | private final Configuration config; |
41 | 42 | ||
42 | - public CheckstyleRunner(String configLocation, String suppressionLocation) | 43 | + public CheckstyleRunner(String configLocation, String suppressionLocation) { |
43 | - throws CheckstyleException { | 44 | + try { |
44 | - // create a configuration | 45 | + // create a configuration |
45 | - DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader.loadConfiguration( | 46 | + DefaultConfiguration config = (DefaultConfiguration) ConfigurationLoader |
46 | - configLocation, new PropertiesExpander(System.getProperties())); | 47 | + .loadConfiguration(configLocation, new PropertiesExpander(System.getProperties())); |
47 | - | 48 | + |
48 | - // add the suppression file to the configuration | 49 | + // add the suppression file to the configuration |
49 | - DefaultConfiguration suppressions = new DefaultConfiguration("SuppressionFilter"); | 50 | + DefaultConfiguration suppressions = new DefaultConfiguration("SuppressionFilter"); |
50 | - suppressions.addAttribute("file", suppressionLocation); | 51 | + suppressions.addAttribute("file", suppressionLocation); |
51 | - config.addChild(suppressions); | 52 | + config.addChild(suppressions); |
52 | - | 53 | + |
53 | - this.config = config; | 54 | + this.config = config; |
54 | - } | 55 | + } catch (CheckstyleException e) { |
55 | - | 56 | + throw new RuntimeException(e); |
56 | - public Runnable checkClass(Socket socket) { | 57 | + } |
57 | - return () -> { | ||
58 | - try { | ||
59 | - String input = new String(ByteStreams.toByteArray(socket.getInputStream())); | ||
60 | - String output = checkClass(input); | ||
61 | - socket.getOutputStream().write(output.getBytes()); | ||
62 | - socket.getOutputStream().flush(); | ||
63 | - socket.close(); | ||
64 | - } catch (IOException e) { | ||
65 | - e.printStackTrace(); | ||
66 | - } catch (CheckstyleException e) { | ||
67 | - e.printStackTrace(); | ||
68 | - } catch (InterruptedException e) { | ||
69 | - e.printStackTrace(); | ||
70 | - } | ||
71 | - }; | ||
72 | } | 58 | } |
73 | 59 | ||
74 | - public String checkClass(String input) throws CheckstyleException, InterruptedException { | 60 | + @Override |
75 | - String[] split = input.split("\n", 3); | 61 | + public void execute(BuckTaskContext context) { |
76 | - if (split.length < 3 || split[2].length() == 0) { | 62 | + List<String> input = context.input(); |
77 | - return ""; | 63 | + if (input.size() < 3 || input.get(2).length() == 0) { |
64 | + return; | ||
78 | } | 65 | } |
79 | - String project = split[0]; | 66 | + String project = input.get(0); |
80 | - String baseDir = split[1]; | 67 | + String baseDir = input.get(1); |
81 | - String files = split[2]; | ||
82 | 68 | ||
83 | // create a listener for output | 69 | // create a listener for output |
84 | - StringAuditor listener = new StringAuditor(); | 70 | + StringAuditor listener = new StringAuditor(context); |
85 | listener.setProjectName(project); | 71 | listener.setProjectName(project); |
86 | listener.setBaseDir(baseDir); | 72 | listener.setBaseDir(baseDir); |
87 | 73 | ||
... | @@ -91,102 +77,102 @@ public class CheckstyleRunner { | ... | @@ -91,102 +77,102 @@ public class CheckstyleRunner { |
91 | checker.setModuleClassLoader(moduleClassLoader); | 77 | checker.setModuleClassLoader(moduleClassLoader); |
92 | 78 | ||
93 | try { | 79 | try { |
94 | - | ||
95 | checker.configure(config); | 80 | checker.configure(config); |
96 | checker.addListener(listener); | 81 | checker.addListener(listener); |
97 | 82 | ||
98 | // run Checker | 83 | // run Checker |
99 | - List<File> fileList = Stream.of(files.split("\n")) | 84 | + List<File> fileList = input.subList(2, input.size() - 1).stream() |
100 | - .map(File::new) | 85 | + .map(File::new) |
101 | - .collect(Collectors.toList()); | 86 | + .collect(Collectors.toList()); |
102 | int errorCounter = checker.process(fileList); | 87 | int errorCounter = checker.process(fileList); |
103 | if (errorCounter > 0) { | 88 | if (errorCounter > 0) { |
104 | - listener.append("CHECKSTYLE ERROR\n"); | 89 | + context.output("CHECKSTYLE ERROR"); |
105 | } | 90 | } |
91 | + | ||
92 | + listener.await(); | ||
93 | + } catch (CheckstyleException | InterruptedException e) { | ||
94 | + throw new RuntimeException(e); | ||
106 | } finally { | 95 | } finally { |
107 | checker.destroy(); | 96 | checker.destroy(); |
108 | } | 97 | } |
109 | 98 | ||
110 | - return listener.getAudit(); | ||
111 | } | 99 | } |
112 | -} | ||
113 | - | ||
114 | -class StringAuditor implements AuditListener { | ||
115 | - | ||
116 | - private CountDownLatch finishedLatch = new CountDownLatch(1); | ||
117 | - private StringBuilder output = new StringBuilder(); | ||
118 | - private String baseDir = ""; | ||
119 | - private String project = ""; | ||
120 | 100 | ||
121 | - public void setBaseDir(String base) { | 101 | + static class StringAuditor implements AuditListener { |
122 | - this.baseDir = base; | ||
123 | - } | ||
124 | 102 | ||
125 | - public void setProjectName(String projectName) { | 103 | + private final BuckTaskContext context; |
126 | - this.project = projectName; | 104 | + private CountDownLatch finishedLatch = new CountDownLatch(1); |
127 | - } | 105 | + private String baseDir = ""; |
106 | + private String project = ""; | ||
128 | 107 | ||
129 | - public void append(String s) { | 108 | + StringAuditor(BuckTaskContext context) { |
130 | - output.append(s); | 109 | + this.context = context; |
131 | - } | 110 | + } |
132 | - | ||
133 | - public String getAudit() throws InterruptedException { | ||
134 | - finishedLatch.await(); | ||
135 | - return output.toString(); | ||
136 | - } | ||
137 | 111 | ||
138 | - @Override | 112 | + public void setBaseDir(String base) { |
139 | - public void auditStarted(AuditEvent evt) { | 113 | + this.baseDir = base; |
114 | + } | ||
140 | 115 | ||
141 | - } | 116 | + public void setProjectName(String projectName) { |
117 | + this.project = projectName; | ||
118 | + } | ||
142 | 119 | ||
143 | - @Override | 120 | + public void await() throws InterruptedException { |
144 | - public void auditFinished(AuditEvent evt) { | 121 | + finishedLatch.await(); |
145 | - finishedLatch.countDown(); | 122 | + } |
146 | - } | ||
147 | 123 | ||
148 | - @Override | 124 | + @Override |
149 | - public void fileStarted(AuditEvent evt) { | 125 | + public void auditStarted(AuditEvent evt) { |
126 | + } | ||
150 | 127 | ||
151 | - } | 128 | + @Override |
129 | + public void auditFinished(AuditEvent evt) { | ||
130 | + finishedLatch.countDown(); | ||
131 | + } | ||
152 | 132 | ||
153 | - @Override | 133 | + @Override |
154 | - public void fileFinished(AuditEvent evt) { | 134 | + public void fileStarted(AuditEvent evt) { |
135 | + } | ||
155 | 136 | ||
156 | - } | 137 | + @Override |
138 | + public void fileFinished(AuditEvent evt) { | ||
139 | + } | ||
157 | 140 | ||
158 | - @Override | 141 | + @Override |
159 | - public void addError(AuditEvent evt) { | 142 | + public void addError(AuditEvent evt) { |
160 | - switch (evt.getSeverityLevel()) { | 143 | + switch (evt.getSeverityLevel()) { |
161 | - case ERROR: | 144 | + case ERROR: |
162 | - String fileName = evt.getFileName(); | 145 | + StringBuilder output = new StringBuilder(); |
163 | - if (!isNullOrEmpty(baseDir)) { | 146 | + String fileName = evt.getFileName(); |
164 | - int index = fileName.indexOf(baseDir); | 147 | + if (!isNullOrEmpty(baseDir)) { |
165 | - if (index >= 0) { | 148 | + int index = fileName.indexOf(baseDir); |
166 | - fileName = fileName.substring(index + baseDir.length() + 1); | 149 | + if (index >= 0) { |
167 | - if (!isNullOrEmpty(project)) { | 150 | + fileName = fileName.substring(index + baseDir.length() + 1); |
168 | - output.append(project).append(':'); | 151 | + if (!isNullOrEmpty(project)) { |
152 | + output.append(project).append(':'); | ||
153 | + } | ||
169 | } | 154 | } |
170 | } | 155 | } |
171 | - } | 156 | + output.append(fileName).append(':').append(evt.getLine()); |
172 | - output.append(fileName).append(':').append(evt.getLine()); | 157 | + if (evt.getColumn() > 0) { |
173 | - if (evt.getColumn() > 0) { | 158 | + output.append(':').append(evt.getColumn()); |
174 | - output.append(':').append(evt.getColumn()); | 159 | + } |
175 | - } | 160 | + output.append(": ").append(evt.getMessage()); |
176 | - output.append(": ").append(evt.getMessage()); | 161 | + context.output(output.toString()); |
177 | - output.append('\n'); | 162 | + break; |
178 | - break; | 163 | + case IGNORE: |
179 | - case IGNORE: | 164 | + case INFO: |
180 | - case INFO: | 165 | + case WARNING: |
181 | - case WARNING: | 166 | + default: |
182 | - default: | 167 | + break; |
183 | - break; | 168 | + } |
184 | } | 169 | } |
185 | - } | ||
186 | 170 | ||
187 | - @Override | 171 | + @Override |
188 | - public void addException(AuditEvent evt, Throwable throwable) { | 172 | + public void addException(AuditEvent evt, Throwable throwable) { |
189 | - addError(evt); | 173 | + addError(evt); |
190 | - output.append(throwable.getMessage()); | 174 | + context.output(throwable.getMessage()); |
175 | + } | ||
191 | } | 176 | } |
192 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
177 | + | ||
178 | +} | ... | ... |
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | -CHECKSTYLE=$1 | 2 | +# ----------------------------------------------------------------------------- |
3 | -FILES=$2 | 3 | +# Launches Buck daemon if not already running and requests Buck task execution. |
4 | -CONFIG=$3 | 4 | +# ----------------------------------------------------------------------------- |
5 | -SUPPRESSIONS=$4 | 5 | + |
6 | +BUCK_DAEMON=$1 | ||
7 | +TASK=${2:-unspecified} | ||
8 | +DATA=${3} | ||
9 | + | ||
10 | +# TODO: Figure out how to parametrize better | ||
11 | +BUCK_PROPS="-Dcheckstyle.config=$4 -Dcheckstyle.suppressions=$5" | ||
6 | 12 | ||
7 | PORT_FILE="$1.port" | 13 | PORT_FILE="$1.port" |
8 | 14 | ||
... | @@ -39,15 +45,15 @@ function check_socket() { | ... | @@ -39,15 +45,15 @@ function check_socket() { |
39 | return $? | 45 | return $? |
40 | } | 46 | } |
41 | 47 | ||
42 | -# check to see if checkstyle daemon is running; if not, start it | 48 | +# check to see if buck daemon is running; if not, start it |
43 | if ! check_socket; then | 49 | if ! check_socket; then |
44 | - # Starting checkstyle server... | 50 | + # Starting buck daemon... |
45 | #FIXME change to /dev/null if/when we are confident | 51 | #FIXME change to /dev/null if/when we are confident |
46 | - nohup java -jar $CHECKSTYLE $PORT_FILE $(buck_pid) $3 $4 >>/tmp/checkstyle.daemon 2>&1 & | 52 | + nohup java $BUCK_PROPS -jar $BUCK_DAEMON $PORT_FILE $(buck_pid) >>/tmp/buck.daemon 2>&1 & |
47 | 53 | ||
48 | TRIES=20 | 54 | TRIES=20 |
49 | i=0 | 55 | i=0 |
50 | - # Wait for checkstyle server to start for 2 seconds | 56 | + # Wait for buck daemon to start for 2 seconds |
51 | while [ $i -lt $TRIES ]; do | 57 | while [ $i -lt $TRIES ]; do |
52 | if check_socket; then | 58 | if check_socket; then |
53 | CONNECTED=true | 59 | CONNECTED=true |
... | @@ -57,15 +63,15 @@ if ! check_socket; then | ... | @@ -57,15 +63,15 @@ if ! check_socket; then |
57 | sleep 0.1 | 63 | sleep 0.1 |
58 | done | 64 | done |
59 | if [ -z "$CONNECTED" ]; then | 65 | if [ -z "$CONNECTED" ]; then |
60 | - echo "Failed to start checkstyle server" | 66 | + echo "Failed to start buck daemon" |
61 | exit 3 | 67 | exit 3 |
62 | fi | 68 | fi |
63 | fi | 69 | fi |
64 | 70 | ||
65 | -# run the actual checkstyle client | 71 | +# run the actual buck daemon client |
66 | -OUT=$(cat $FILES | nc localhost $(port)) | 72 | +OUT=$((printf "%s\n" $TASK; cat $DATA) | nc localhost $(port)) |
67 | if [ $? -ne 0 ]; then | 73 | if [ $? -ne 0 ]; then |
68 | - echo "Error connecting to checkstyle server" | 74 | + echo "Error connecting to buck daemon server" |
69 | exit 2 | 75 | exit 2 |
70 | fi | 76 | fi |
71 | if [ -n "$OUT" ]; then | 77 | if [ -n "$OUT" ]; then | ... | ... |
-
Please register or login to post a comment