1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.server.core.invocation;
21
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.IdentityHashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.directory.server.core.interceptor.context.OperationContext;
30
31
32 /**
33 * Keeps track of recursive {@link Invocation}s. This stack assumes an invocation
34 * occurs in the same thread since it is called first, so we manages stacks
35 * for each invocation in {@link ThreadLocal}-like manner. You can just use
36 * {@link #getInstance()} to get current invocation stack.
37 *
38 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39 * @version $Rev: 662440 $, $Date: 2008-06-02 16:00:23 +0200 (Mo, 02 Jun 2008) $
40 */
41 public final class InvocationStack
42 {
43 // I didn't use ThreadLocal to release contexts explicitly.
44 // It seems like JDK 1.5 supports explicit release by introducing
45 // <tt>ThreadLocal.remove()</tt>, but we're still targetting 1.4.
46 private static final Map<Thread, InvocationStack> stacks =
47 Collections.synchronizedMap( new IdentityHashMap<Thread, InvocationStack>() );
48
49 private final Thread thread;
50 private final List<OperationContext> stack = new ArrayList<OperationContext>();
51
52
53 /**
54 * Returns the invocation stack of current thread.
55 */
56 public static InvocationStack getInstance()
57 {
58 Thread currentThread = Thread.currentThread();
59 InvocationStack ctx;
60 ctx = stacks.get( currentThread );
61
62 if ( ctx == null )
63 {
64 ctx = new InvocationStack( currentThread );
65 }
66
67 return ctx;
68 }
69
70
71 private InvocationStack( Thread currentThread )
72 {
73 thread = currentThread;
74 stacks.put( currentThread, this );
75 }
76
77
78 /**
79 * Returns an array of {@link Invocation}s. 0th element is the
80 * latest invocation.
81 */
82 public OperationContext[] toArray()
83 {
84 OperationContext[] result = new OperationContext[stack.size()];
85 result = stack.toArray( result );
86 return result;
87 }
88
89
90 /**
91 * Returns the latest invocation.
92 */
93 public OperationContext peek()
94 {
95 return stack.get( 0 );
96 }
97
98
99 /**
100 * Returns true if the stack is empty false otherwise.
101 */
102 public boolean isEmpty()
103 {
104 return stack.isEmpty();
105 }
106
107
108 /**
109 * Pushes the specified invocation to this stack.
110 */
111 public void push( OperationContext opContext )
112 {
113 stack.add( 0, opContext );
114 }
115
116
117 /**
118 * Pops the latest invocation from this stack. This stack is released
119 * automatically if you pop all items from this stack.
120 */
121 public OperationContext pop()
122 {
123 OperationContext invocation = stack.remove( 0 );
124
125 if ( stack.size() == 0 )
126 {
127 stacks.remove( thread );
128 }
129
130 return invocation;
131 }
132 }