blob: 99c5d3c48ec3203e685fcd3586bc36b7e0fff32a [file] [log] [blame]
Brian O'Connorbe95f682016-05-18 15:40:19 -07001/*
2 * Copyright 2016-present 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
17package org.onosproject.checkstyle;
18
19import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
20
21import java.io.IOException;
22import java.net.ServerSocket;
23import java.nio.ByteBuffer;
24import java.nio.channels.FileChannel;
25import java.nio.channels.FileLock;
26import java.nio.file.Files;
27import java.nio.file.Path;
28import java.nio.file.Paths;
29import java.util.Timer;
30import java.util.TimerTask;
31import java.util.concurrent.ExecutorService;
32import java.util.concurrent.Executors;
33
34import static java.nio.file.StandardOpenOption.*;
35
36/**
37 * Main program for executing scenario test warden.
38 */
39public final class Main {
40
41 private static long POLLING_INTERVAL = 1000; //ms
42
43 // Public construction forbidden
44 private Main(String[] args) {
45 }
46
47 /**
48 * Main entry point for the cell warden.
49 *
50 * @param args command-line arguments
51 */
52 public static void main(String[] args)
53 throws CheckstyleException, IOException {
54 startServer(args);
55 }
56
57 // Monitors another PID and exit when that process exits
58 private static void watchProcess(String pid) {
59 if (pid == null || pid.equals("0")) {
60 return;
61 }
62 Timer timer = new Timer(true); // start as a daemon, so we don't hang shutdown
63 timer.scheduleAtFixedRate(new TimerTask() {
64 private String cmd = "kill -s 0 " + pid;
65 @Override
66 public void run() {
67 try {
68 Process p = Runtime.getRuntime().exec(cmd);
69 p.waitFor();
70 if (p.exitValue() != 0) {
71 System.err.println("shutting down...");
72 System.exit(0);
73 }
74 } catch (IOException | InterruptedException e) {
75 //no-op
76 e.printStackTrace();
77 }
78 }
79 }, POLLING_INTERVAL, POLLING_INTERVAL);
80 }
81
82 // Initiates a server.
83 private static void startServer(String[] args) throws IOException, CheckstyleException {
84 String portLock = args[0];
85 String buckPid = args[1];
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
90 Path portLockPath = Paths.get(portLock);
91 FileChannel channel = FileChannel.open(portLockPath, WRITE, CREATE);
92 FileLock lock = channel.tryLock();
93 if (lock == null) {
94 System.out.println("Server is already running");
95 System.exit(1);
96 } //else, hold the lock until the JVM exits
97
98 // Start the server and bind it to a random port
99 ServerSocket server = new ServerSocket(0);
100
101 // Monitor the parent buck process
102 watchProcess(buckPid);
103
104 // Set up hook to clean up after ourselves
105 Runtime.getRuntime().addShutdownHook(new Thread(() -> {
106 try {
107 if (channel != null) {
108 channel.truncate(0);
109 channel.close();
110 }
111 System.err.println("tear down...");
112 Files.delete(portLockPath);
113 } catch (IOException e) {
114 //no-op: shutting down
115 e.printStackTrace();
116 }
117 }));
118
119 // Write the bound port to the port file
120 int port = server.getLocalPort();
121 channel.truncate(0);
122 channel.write(ByteBuffer.wrap(Integer.toString(port).getBytes()));
123
124 // Instantiate a Checkstyle runner and executor; serve until exit...
125 CheckstyleRunner runner = new CheckstyleRunner(checkstyleFile, suppressionsFile);
126 ExecutorService executor = Executors.newCachedThreadPool();
127 while (true) {
128 try {
129 executor.submit(runner.checkClass(server.accept()));
130 } catch (Exception e) {
131 e.printStackTrace();
132 //no-op
133 }
134 }
135 }
136}