PortFileMonitor.java revision 2697:10100ecb0c97
1/*
2 * Copyright (c) 2014, 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 */
25package com.sun.tools.sjavac.server;
26
27import java.io.IOException;
28import java.util.Timer;
29import java.util.TimerTask;
30
31/**
32 * Monitors the presence of a port file and shuts down the given SjavacServer
33 * whenever the port file is deleted or invalidated.
34 *
35 * TODO: JDK-8046882
36 *
37 *  <p><b>This is NOT part of any supported API.
38 *  If you write code that depends on this, you do so at your own risk.
39 *  This code and its internal interfaces are subject to change or
40 *  deletion without notice.</b>
41 */
42public class PortFileMonitor {
43
44    // Check if the portfile is gone, every 5 seconds.
45    private final static int CHECK_PORTFILE_INTERVAL = 5000;
46
47    final private Timer timer = new Timer();
48    final private PortFile portFile;
49    final private SjavacServer server;
50
51    public PortFileMonitor(PortFile portFile,
52                           SjavacServer server) {
53        this.portFile = portFile;
54        this.server = server;
55    }
56
57    public void start() {
58        TimerTask shutdownCheck = new TimerTask() {
59            public void run() {
60                try {
61                    if (!portFile.exists()) {
62                        // Time to quit because the portfile was deleted by another
63                        // process, probably by the makefile that is done building.
64                        server.shutdown("Quitting because portfile was deleted!");
65                    } else if (portFile.markedForStop()) {
66                        // Time to quit because another process touched the file
67                        // server.port.stop to signal that the server should stop.
68                        // This is necessary on some operating systems that lock
69                        // the port file hard!
70                        server.shutdown("Quitting because a portfile.stop file was found!");
71                    } else if (!portFile.stillMyValues()) {
72                        // Time to quit because another build has started.
73                        server.shutdown("Quitting because portfile is now owned by another javac server!");
74                    }
75                } catch (IOException e) {
76                    e.printStackTrace(server.theLog);
77                    server.flushLog();
78                } catch (InterruptedException e) {
79                    Thread.currentThread().interrupt();
80                    e.printStackTrace(server.theLog);
81                    server.flushLog();
82                }
83            }
84        };
85
86        timer.schedule(shutdownCheck, 0, CHECK_PORTFILE_INTERVAL);
87    }
88
89    public void shutdown() {
90        timer.cancel();
91    }
92}
93