View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.server.handler;
15  
16  
17  import java.io.IOException;
18  
19  import org.eclipse.jetty.server.Handler;
20  import org.eclipse.jetty.server.Server;
21  import org.eclipse.jetty.util.component.AggregateLifeCycle;
22  import org.eclipse.jetty.util.log.Log;
23  import org.eclipse.jetty.util.log.Logger;
24  
25  
26  /* ------------------------------------------------------------ */
27  /** AbstractHandler.
28   * 
29   *
30   */
31  public abstract class AbstractHandler extends AggregateLifeCycle implements Handler
32  {
33      private static final Logger LOG = Log.getLogger(AbstractHandler.class);
34  
35      private Server _server;
36      
37      /* ------------------------------------------------------------ */
38      /**
39       * 
40       */
41      public AbstractHandler()
42      {
43      }
44  
45      /* ------------------------------------------------------------ */
46      /* 
47       * @see org.eclipse.thread.LifeCycle#start()
48       */
49      @Override
50      protected void doStart() throws Exception
51      {
52          LOG.debug("starting {}",this);
53          super.doStart();
54      }
55  
56      /* ------------------------------------------------------------ */
57      /* 
58       * @see org.eclipse.thread.LifeCycle#stop()
59       */
60      @Override
61      protected void doStop() throws Exception
62      {
63          LOG.debug("stopping {}",this);
64          super.doStop();
65      }
66  
67      /* ------------------------------------------------------------ */
68      public void setServer(Server server)
69      {
70          Server old_server=_server;
71          if (old_server!=null && old_server!=server)
72              old_server.getContainer().removeBean(this);
73          _server=server;
74          if (_server!=null && _server!=old_server)
75              _server.getContainer().addBean(this);
76      }
77  
78      /* ------------------------------------------------------------ */
79      public Server getServer()
80      {
81          return _server;
82      }
83  
84      /* ------------------------------------------------------------ */
85      public void destroy()
86      {
87          if (!isStopped())
88              throw new IllegalStateException("!STOPPED");
89          super.destroy();
90          if (_server!=null)
91              _server.getContainer().removeBean(this);
92      }
93  
94      /* ------------------------------------------------------------ */
95      public void dumpThis(Appendable out) throws IOException
96      {
97          out.append(toString()).append(' ').append(getState()).append('\n');
98      }
99      
100 }