001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log;
056:
057: import java.util.Stack;
058:
059: /**
060: * The ContextStack records the nested context of an application.
061: * The context is an application defined characteristeric. For instance
062: * a webserver context may be defined as the session that is currently
063: * used to connect to server. A application may have context
064: * defined by current thread. A applet may have it's context defined
065: * by the name of the applet etc.
066: *
067: * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
068: * @deprecated This class has been deprecated as it encouraged use of bad
069: * design practices. Use org.apache.log.ContextMap instead.
070: */
071: public class ContextStack {
072: ///Thread local for holding instance of stack associated with current thread
073: private static final ThreadLocal c_context = new ThreadLocal();
074:
075: ///Container to hold stack of elements
076: private Stack m_stack = new Stack();
077:
078: /**
079: * Get the Current ContextStack.
080: * This method returns a ContextStack associated with current thread. If the
081: * thread doesn't have a ContextStack associated with it then a new
082: * ContextStack is created with the name of thread as base context.
083: *
084: * @return the current ContextStack
085: */
086: public static final ContextStack getCurrentContext() {
087: return getCurrentContext(true);
088: }
089:
090: /**
091: * Get the Current ContextStack.
092: * This method returns a ContextStack associated with current thread.
093: * If the thread doesn't have a ContextStack associated with it and
094: * autocreate is true then a new ContextStack is created with the name
095: * of thread as base context.
096: *
097: * @param autocreate true if a ContextStack is to be created if it doesn't exist
098: * @return the current ContextStack
099: */
100: static final ContextStack getCurrentContext(final boolean autocreate) {
101: ContextStack context = (ContextStack) c_context.get();
102:
103: if (null == context && autocreate) {
104: context = new ContextStack();
105: context.push(Thread.currentThread().getName());
106: c_context.set(context);
107: }
108:
109: return context;
110: }
111:
112: /**
113: * Empty the context stack.
114: *
115: */
116: public void clear() {
117: m_stack.setSize(0);
118: }
119:
120: /**
121: * Get the context at a particular depth.
122: *
123: * @param index the depth of the context to retrieve
124: * @return the context
125: */
126: public Object get(final int index) {
127: return m_stack.elementAt(index);
128: }
129:
130: /**
131: * Remove a context from top of stack and return it.
132: *
133: * @return the context that was on top of stack
134: */
135: public Object pop() {
136: return m_stack.pop();
137: }
138:
139: /**
140: * Push the context onto top of context stack.
141: *
142: * @param context the context to place on stack
143: */
144: public void push(final Object context) {
145: m_stack.push(context);
146: }
147:
148: /**
149: * Set the current ContextSet to be equl to other ContextStack.
150: *
151: * @param stack the value to copy
152: */
153: public void set(final ContextStack stack) {
154: clear();
155: final int size = stack.m_stack.size();
156:
157: for (int i = 0; i < size; i++) {
158: m_stack.push(stack.m_stack.elementAt(i));
159: }
160: }
161:
162: /**
163: * Get the number of contexts in stack.
164: *
165: * @return the number of contexts in stack
166: */
167: public int getSize() {
168: return m_stack.size();
169: }
170:
171: /**
172: * Format context stack into a string.
173: * Each element in stack is printed out, separated by a '.' character.
174: *
175: * @return the string describing context stack
176: */
177: public String toString() {
178: return toString(getSize());
179: }
180:
181: /**
182: * Format context stack into a string.
183: * Only write a maximum of count elements, separated by '.' separator.
184: * Note that elements in stack will have toString() called and every occurence
185: * of spearator character '.' replaced with '_'.
186: *
187: * @return the string describing context stack
188: */
189: public String toString(final int count) {
190: final StringBuffer sb = new StringBuffer();
191:
192: final int end = getSize() - 1;
193: final int start = Math.max(end - count + 1, 0);
194:
195: for (int i = start; i < end; i++) {
196: sb.append(fix(get(i).toString()));
197: sb.append('.');
198: }
199:
200: sb.append(fix(get(end).toString()));
201:
202: return sb.toString();
203: }
204:
205: /**
206: * Correct a context string by replacing separators '.' with a '_'.
207: *
208: * @param context the un-fixed context
209: * @return the fixed context
210: */
211: private String fix(final String context) {
212: return context.replace('.', '_');
213: }
214: }
|