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