1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: CallContext.java 2099 2011-03-07 17:13:00Z dmsmith $
21   */
22  package org.crosswire.common.util;
23  
24  import org.crosswire.jsword.JSOtherMsg;
25  
26  /**
27   * This singleton class provides a way for a method to determine which class
28   * called it.
29   * <p>
30   * It has been tested to work in command line and WebStart environments.
31   * 
32   * @see gnu.lgpl.License for license details.<br>
33   *      The copyright to this program is held by it's authors.
34   * @author DM Smith [ dmsmith555 at yahoo dot com]
35   */
36  public final class CallContext extends SecurityManager {
37      /**
38       * Prevent instantiation
39       */
40      private CallContext() {
41      }
42  
43      /**
44       * Singleton accessor
45       */
46      public static CallContext instance() {
47          try {
48              if (resolver == null) {
49                  resolver = new CallContext();
50              }
51  
52              return resolver;
53          } catch (SecurityException se) {
54              throw new LucidRuntimeException(JSOtherMsg.lookupText("Could not create ClassResolver:"), se);
55          }
56      }
57  
58      /*
59       * (non-Javadoc)
60       * 
61       * @see java.lang.SecurityManager#getClassContext()
62       */
63      @Override
64      protected Class<?>[] getClassContext() {
65          return super.getClassContext();
66      }
67  
68      /**
69       * When called from a method it will return the class calling that method.
70       */
71      public static Class<?> getCallingClass() {
72          return getCallingClass(1); // add 1 for this method
73      }
74  
75      /**
76       * When called from a method it will return the i-th class calling that
77       * method, up the call chain. If used with a -1 it will return the class
78       * making the call -2 and -3 will return this class
79       * 
80       * @throws ArrayIndexOutOfBoundsException
81       *             if the index is not valid
82       */
83      public static Class<?> getCallingClass(int i) {
84          return instance().getClassContext()[CALL_CONTEXT_OFFSET + i];
85      }
86  
87      // may need to change if this class is redesigned
88      /**
89       * Offset needed to represent the caller of the method that called this
90       * method.
91       * 
92       */
93      private static final int CALL_CONTEXT_OFFSET = 3;
94  
95      private static volatile CallContext resolver;
96  }
97