Save This Page
Home » tapestry-src-5.0.19 » org.apache.tapestry5.ioc.internal.util » [javadoc | source]
    1   // Copyright 2006, 2007 The Apache Software Foundation
    2   //
    3   // Licensed under the Apache License, Version 2.0 (the "License");
    4   // you may not use this file except in compliance with the License.
    5   // You may obtain a copy of the License at
    6   //
    7   //     http://www.apache.org/licenses/LICENSE-2.0
    8   //
    9   // Unless required by applicable law or agreed to in writing, software
   10   // distributed under the License is distributed on an "AS IS" BASIS,
   11   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12   // See the License for the specific language governing permissions and
   13   // limitations under the License.
   14   
   15   package org.apache.tapestry5.ioc.internal.util;
   16   
   17   /**
   18    * Logic for handling one shot semantics for classes; classes that include a method (or methods) that "locks down" the
   19    * instance, to prevent it from being used again in the future.
   20    */
   21   public class OneShotLock
   22   {
   23       private boolean lock;
   24   
   25       /**
   26        * Checks to see if the lock has been set (via {@link #lock()}).
   27        *
   28        * @throws IllegalStateException if the lock is set
   29        */
   30       public synchronized void check()
   31       {
   32           innerCheck();
   33       }
   34   
   35       private void innerCheck()
   36       {
   37           if (lock)
   38           {
   39               // The depth to find the caller of the check() or lock() method varies between JDKs.
   40   
   41   
   42               StackTraceElement[] elements = Thread.currentThread().getStackTrace();
   43   
   44               int i = 0;
   45               while (!elements[i].getMethodName().equals("innerCheck"))
   46                   i++;
   47   
   48               throw new IllegalStateException(UtilMessages.oneShotLock(elements[i + 2]));
   49           }
   50       }
   51   
   52       /**
   53        * Checks the lock, then sets it.
   54        */
   55       public synchronized void lock()
   56       {
   57           innerCheck();
   58   
   59           lock = true;
   60       }
   61   }

Save This Page
Home » tapestry-src-5.0.19 » org.apache.tapestry5.ioc.internal.util » [javadoc | source]