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      @Override
45      protected void doStart() throws Exception
46      {
47          Log.debug("starting {}",this);
48      }
49  
50      /* ------------------------------------------------------------ */
51      /* 
52       * @see org.eclipse.thread.LifeCycle#stop()
53       */
54      @Override
55      protected void doStop() throws Exception
56      {
57          Log.debug("stopping {}",this);
58      }
59  
60      /* ------------------------------------------------------------ */
61      @Override
62      public String toString()
63      {
64          StringBuilder b=new StringBuilder();
65          String s=super.toString();
66          b.append(s,s.lastIndexOf('.')+1,s.length());
67          ContextHandler.Context ctx = ContextHandler.getCurrentContext();
68          if (ctx!=null && ctx.getContextPath()!=null && !(this instanceof ContextHandler))
69          {
70              b.append('@');
71              b.append(ctx.getContextPath());
72              
73          }
74          return b.toString();
75      }
76  
77      /* ------------------------------------------------------------ */
78      public void setServer(Server server)
79      {
80          Server old_server=_server;
81          if (old_server!=null && old_server!=server)
82              old_server.getContainer().removeBean(this);
83          _server=server;
84          if (_server!=null && _server!=old_server)
85              _server.getContainer().addBean(this);
86      }
87  
88      /* ------------------------------------------------------------ */
89      public Server getServer()
90      {
91          return _server;
92      }
93  
94  
95      /* ------------------------------------------------------------ */
96      public void destroy()
97      {
98          if (!isStopped())
99              throw new IllegalStateException("!STOPPED");
100         if (_server!=null)
101             _server.getContainer().removeBean(this);
102     }
103 
104 
105     /* ------------------------------------------------------------ */
106     public String dump()
107     {
108         StringBuilder b = new StringBuilder();
109         dump(b,"");
110         return b.toString();
111     }    
112 
113     /* ------------------------------------------------------------ */
114     protected void dump(StringBuilder b,String indent)
115     {
116         b.append(toString());
117         b.append(isStarted()?" started":" STOPPED");
118         b.append('\n');
119     }
120 
121 }