1 /*
2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.tools.sjavac.server;
27 
28 import com.sun.tools.sjavac.Log;
29 
30 import java.io.IOException;
31 import java.util.Timer;
32 import java.util.TimerTask;
33 
34 /**
35  * Monitors the presence of a port file and shuts down the given SjavacServer
36  * whenever the port file is deleted or invalidated.
37  *
38  * TODO: JDK-8046882
39  *
40  *  <p><b>This is NOT part of any supported API.
41  *  If you write code that depends on this, you do so at your own risk.
42  *  This code and its internal interfaces are subject to change or
43  *  deletion without notice.</b>
44  */
45 public class PortFileMonitor {
46 
47     // Check if the portfile is gone, every 5 seconds.
48     private final static int CHECK_PORTFILE_INTERVAL = 5000;
49 
50     final private Timer timer = new Timer();
51     final private PortFile portFile;
52     final private SjavacServer server;
53 
PortFileMonitor(PortFile portFile, SjavacServer server)54     public PortFileMonitor(PortFile portFile,
55                            SjavacServer server) {
56         this.portFile = portFile;
57         this.server = server;
58     }
59 
start()60     public void start() {
61         Log log = Log.get();
62         TimerTask shutdownCheck = new TimerTask() {
63             public void run() {
64                 Log.setLogForCurrentThread(log);
65                 Log.debug("Checking port file status...");
66                 try {
67                     if (!portFile.exists()) {
68                         // Time to quit because the portfile was deleted by another
69                         // process, probably by the makefile that is done building.
70                         server.shutdown("Quitting because portfile was deleted!");
71                     } else if (portFile.markedForStop()) {
72                         // Time to quit because another process touched the file
73                         // server.port.stop to signal that the server should stop.
74                         // This is necessary on some operating systems that lock
75                         // the port file hard!
76                         server.shutdown("Quitting because a portfile.stop file was found!");
77                     } else if (!portFile.stillMyValues()) {
78                         // Time to quit because another build has started.
79                         server.shutdown("Quitting because portfile is now owned by another javac server!");
80                     }
81                 } catch (IOException e) {
82                     Log.error("IOException caught in PortFileMonitor.");
83                     Log.debug(e);
84                 } catch (InterruptedException e) {
85                     Thread.currentThread().interrupt();
86                     Log.error(e);
87                 }
88             }
89         };
90 
91         timer.schedule(shutdownCheck, 0, CHECK_PORTFILE_INTERVAL);
92     }
93 
shutdown()94     public void shutdown() {
95         timer.cancel();
96     }
97 }
98