001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.server.core.invocation; 021 022 023 import java.util.ArrayList; 024 import java.util.Collections; 025 import java.util.IdentityHashMap; 026 import java.util.List; 027 import java.util.Map; 028 029 import org.apache.directory.server.core.interceptor.context.OperationContext; 030 031 032 /** 033 * Keeps track of recursive {@link Invocation}s. This stack assumes an invocation 034 * occurs in the same thread since it is called first, so we manages stacks 035 * for each invocation in {@link ThreadLocal}-like manner. You can just use 036 * {@link #getInstance()} to get current invocation stack. 037 * 038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 039 * @version $Rev: 896599 $, $Date: 2010-01-06 19:26:43 +0100 (Wed, 06 Jan 2010) $ 040 */ 041 public final class InvocationStack 042 { 043 // I didn't use ThreadLocal to release contexts explicitly. 044 // It seems like JDK 1.5 supports explicit release by introducing 045 // <tt>ThreadLocal.remove()</tt>, but we're still targetting 1.4. 046 private static final Map<Thread, InvocationStack> stacks = 047 Collections.synchronizedMap( new IdentityHashMap<Thread, InvocationStack>() ); 048 049 private final Thread thread; 050 private final List<OperationContext> stack = new ArrayList<OperationContext>(); 051 052 053 /** 054 * Returns the invocation stack of current thread. 055 */ 056 public static InvocationStack getInstance() 057 { 058 Thread currentThread = Thread.currentThread(); 059 InvocationStack ctx; 060 ctx = stacks.get( currentThread ); 061 062 if ( ctx == null ) 063 { 064 ctx = new InvocationStack( currentThread ); 065 } 066 067 return ctx; 068 } 069 070 071 private InvocationStack( Thread currentThread ) 072 { 073 thread = currentThread; 074 stacks.put( currentThread, this ); 075 } 076 077 078 /** 079 * Returns an array of {@link Invocation}s. 0th element is the 080 * latest invocation. 081 */ 082 public OperationContext[] toArray() 083 { 084 OperationContext[] result = new OperationContext[stack.size()]; 085 result = stack.toArray( result ); 086 return result; 087 } 088 089 090 /** 091 * Returns the latest invocation. 092 */ 093 public OperationContext peek() 094 { 095 return stack.get( 0 ); 096 } 097 098 099 /** 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 }