KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > ContextStack


1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgment may appear in the software
24  * itself, if and wherever such third-party acknowledgments
25  * normally appear.
26  *
27  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
28  * must not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.apache.log;
56
57 import java.util.Stack JavaDoc;
58
59 /**
60  * The ContextStack records the nested context of an application.
61  * The context is an application defined characteristeric. For instance
62  * a webserver context may be defined as the session that is currently
63  * used to connect to server. A application may have context
64  * defined by current thread. A applet may have it's context defined
65  * by the name of the applet etc.
66  *
67  * @author <a HREF="mailto:donaldp@apache.org">Peter Donald</a>
68  * @deprecated This class has been deprecated as it encouraged use of bad
69  * design practices. Use org.apache.log.ContextMap instead.
70  */

71 public class ContextStack
72 {
73     ///Thread local for holding instance of stack associated with current thread
74
private static final ThreadLocal JavaDoc c_context = new ThreadLocal JavaDoc();
75
76     ///Container to hold stack of elements
77
private Stack JavaDoc m_stack = new Stack JavaDoc();
78
79     /**
80      * Get the Current ContextStack.
81      * This method returns a ContextStack associated with current thread. If the
82      * thread doesn't have a ContextStack associated with it then a new
83      * ContextStack is created with the name of thread as base context.
84      *
85      * @return the current ContextStack
86      */

87     public static final ContextStack getCurrentContext()
88     {
89         return getCurrentContext( true );
90     }
91
92     /**
93      * Get the Current ContextStack.
94      * This method returns a ContextStack associated with current thread.
95      * If the thread doesn't have a ContextStack associated with it and
96      * autocreate is true then a new ContextStack is created with the name
97      * of thread as base context.
98      *
99      * @param autocreate true if a ContextStack is to be created if it doesn't exist
100      * @return the current ContextStack
101      */

102     static final ContextStack getCurrentContext( final boolean autocreate )
103     {
104         ContextStack context = (ContextStack)c_context.get();
105
106         if( null == context && autocreate )
107         {
108             context = new ContextStack();
109             context.push( Thread.currentThread().getName() );
110             c_context.set( context );
111         }
112
113         return context;
114     }
115
116     /**
117      * Empty the context stack.
118      *
119      */

120     public void clear()
121     {
122         m_stack.setSize( 0 );
123     }
124
125     /**
126      * Get the context at a particular depth.
127      *
128      * @param index the depth of the context to retrieve
129      * @return the context
130      */

131     public Object JavaDoc get( final int index )
132     {
133         return m_stack.elementAt( index );
134     }
135
136     /**
137      * Remove a context from top of stack and return it.
138      *
139      * @return the context that was on top of stack
140      */

141     public Object JavaDoc pop()
142     {
143         return m_stack.pop();
144     }
145
146     /**
147      * Push the context onto top of context stack.
148      *
149      * @param context the context to place on stack
150      */

151     public void push( final Object JavaDoc context )
152     {
153         m_stack.push( context );
154     }
155
156     /**
157      * Set the current ContextSet to be equl to other ContextStack.
158      *
159      * @param stack the value to copy
160      */

161     public void set( final ContextStack stack )
162     {
163         clear();
164         final int size = stack.m_stack.size();
165
166         for( int i = 0; i < size; i++ )
167         {
168             m_stack.push( stack.m_stack.elementAt( i ) );
169         }
170     }
171
172     /**
173      * Get the number of contexts in stack.
174      *
175      * @return the number of contexts in stack
176      */

177     public int getSize()
178     {
179         return m_stack.size();
180     }
181
182     /**
183      * Format context stack into a string.
184      * Each element in stack is printed out, separated by a '.' character.
185      *
186      * @return the string describing context stack
187      */

188     public String JavaDoc toString()
189     {
190         return toString( getSize() );
191     }
192
193     /**
194      * Format context stack into a string.
195      * Only write a maximum of count elements, separated by '.' separator.
196      * Note that elements in stack will have toString() called and every occurence
197      * of spearator character '.' replaced with '_'.
198      *
199      * @return the string describing context stack
200      */

201     public String JavaDoc toString( final int count )
202     {
203         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
204
205         final int end = getSize() - 1;
206         final int start = Math.max( end - count + 1, 0 );
207
208         for( int i = start; i < end; i++ )
209         {
210             sb.append( fix( get( i ).toString() ) );
211             sb.append( '.' );
212         }
213
214         sb.append( fix( get( end ).toString() ) );
215
216         return sb.toString();
217     }
218
219     /**
220      * Correct a context string by replacing separators '.' with a '_'.
221      *
222      * @param context the un-fixed context
223      * @return the fixed context
224      */

225     private String JavaDoc fix( final String JavaDoc context )
226     {
227         return context.replace( '.', '_' );
228     }
229 }
230
Popular Tags