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