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 org.eclipse.jetty.server.Handler;
18  import org.eclipse.jetty.server.Server;
19  import org.eclipse.jetty.util.component.AbstractLifeCycle;
20  import org.eclipse.jetty.util.log.Log;
21  
22  
23  /* ------------------------------------------------------------ */
24  /** AbstractHandler.
25   * 
26   *
27   */
28  public abstract class AbstractHandler extends AbstractLifeCycle implements Handler
29  {
30      private Server _server;
31      
32      /* ------------------------------------------------------------ */
33      /**
34       * 
35       */
36      public AbstractHandler()
37      {
38      }
39  
40      /* ------------------------------------------------------------ */
41      /* 
42       * @see org.eclipse.thread.LifeCycle#start()
43       */
44      protected void doStart() throws Exception
45      {
46          Log.debug("starting {}",this);
47      }
48  
49      /* ------------------------------------------------------------ */
50      /* 
51       * @see org.eclipse.thread.LifeCycle#stop()
52       */
53      protected void doStop() throws Exception
54      {
55          Log.debug("stopping {}",this);
56      }
57  
58      /* ------------------------------------------------------------ */
59      public String toString()
60      {
61          StringBuilder b=new StringBuilder();
62          String s=super.toString();
63          b.append(s,s.lastIndexOf('.')+1,s.length());
64          ContextHandler.Context ctx = ContextHandler.getCurrentContext();
65          if (ctx!=null && ctx.getContextPath()!=null && !(this instanceof ContextHandler))
66          {
67              b.append('@');
68              b.append(ctx.getContextPath());
69              
70          }
71          return b.toString();
72      }
73  
74      /* ------------------------------------------------------------ */
75      public void setServer(Server server)
76      {
77          Server old_server=_server;
78          if (old_server!=null && old_server!=server)
79              old_server.getContainer().removeBean(this);
80          _server=server;
81          if (_server!=null && _server!=old_server)
82              _server.getContainer().addBean(this);
83      }
84  
85      /* ------------------------------------------------------------ */
86      public Server getServer()
87      {
88          return _server;
89      }
90  
91  
92      /* ------------------------------------------------------------ */
93      public void destroy()
94      {
95          if (!isStopped())
96              throw new IllegalStateException("!STOPPED");
97          if (_server!=null)
98              _server.getContainer().removeBean(this);
99      }
100 
101 
102     /* ------------------------------------------------------------ */
103     public String dump()
104     {
105         StringBuilder b = new StringBuilder();
106         dump(b,"");
107         return b.toString();
108     }    
109 
110     /* ------------------------------------------------------------ */
111     protected void dump(StringBuilder b,String indent)
112     {
113         b.append(toString());
114         b.append(isStarted()?" started":" STOPPED");
115         b.append('\n');
116     }
117 
118 }