]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/component/FileNoticeLifeCycleListener.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / util / component / FileNoticeLifeCycleListener.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.util.component;
20
21 import java.io.FileWriter;
22 import java.io.Writer;
23
24 import org.eclipse.jetty.util.log.Log;
25 import org.eclipse.jetty.util.log.Logger;
26
27
28 /* ------------------------------------------------------------ */
29 /** A LifeCycle Listener that writes state changes to a file.
30  * <p>This can be used with the jetty.sh script to wait for successful startup.
31  */
32 public class FileNoticeLifeCycleListener implements LifeCycle.Listener
33 {
34     private static final Logger LOG = Log.getLogger(FileNoticeLifeCycleListener.class);
35     
36     private final String _filename;
37     
38     public FileNoticeLifeCycleListener(String filename)
39     {
40         _filename=filename;
41     }
42
43     private void writeState(String action, LifeCycle lifecycle)
44     {
45         try (Writer out = new FileWriter(_filename,true))
46         {
47             out.append(action).append(" ").append(lifecycle.toString()).append("\n");
48         }
49         catch(Exception e)
50         {
51             LOG.warn(e);
52         }
53     }
54     
55     public void lifeCycleStarting(LifeCycle event)
56     {  
57         writeState("STARTING",event);      
58     }
59
60     public void lifeCycleStarted(LifeCycle event)
61     {        
62         writeState("STARTED",event); 
63     }
64
65     public void lifeCycleFailure(LifeCycle event, Throwable cause)
66     {        
67         writeState("FAILED",event);
68     }
69
70     public void lifeCycleStopping(LifeCycle event)
71     {        
72         writeState("STOPPING",event);
73     }
74
75     public void lifeCycleStopped(LifeCycle event)
76     {        
77         writeState("STOPPED",event);
78     }
79 }