KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > runtime > internal > CFlowCounter


1 /* *******************************************************************
2  * Copyright (c) 2004 IBM Corporation
3  *
4  * All rights reserved.
5  * This program and the accompanying materials are made available
6  * under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * Andy Clement initial implementation
12  * ******************************************************************/

13
14
15 package org.aspectj.runtime.internal;
16
17 import org.aspectj.runtime.internal.cflowstack.ThreadCounter;
18 import org.aspectj.runtime.internal.cflowstack.ThreadStackFactory;
19 import org.aspectj.runtime.internal.cflowstack.ThreadStackFactoryImpl;
20 import org.aspectj.runtime.internal.cflowstack.ThreadStackFactoryImpl11;
21
22
23 public class CFlowCounter {
24     
25     private static ThreadStackFactory tsFactory;
26     private ThreadCounter flowHeightHandler;
27
28     static {
29         selectFactoryForVMVersion();
30     }
31     
32     public CFlowCounter() {
33         flowHeightHandler = tsFactory.getNewThreadCounter();
34     }
35     
36     public void inc() {
37         flowHeightHandler.inc();
38     }
39
40     public void dec() {
41         flowHeightHandler.dec();
42     }
43     
44     public boolean isValid() {
45         return flowHeightHandler.isNotZero();
46     }
47
48
49     private static ThreadStackFactory getThreadLocalStackFactory() { return new ThreadStackFactoryImpl(); }
50     private static ThreadStackFactory getThreadLocalStackFactoryFor11() { return new ThreadStackFactoryImpl11(); }
51     
52     private static void selectFactoryForVMVersion() {
53         String JavaDoc override = getSystemPropertyWithoutSecurityException("aspectj.runtime.cflowstack.usethreadlocal","unspecified");
54         boolean useThreadLocalImplementation = false;
55         if (override.equals("unspecified")) {
56             String JavaDoc v = System.getProperty("java.class.version","0.0");
57             // Java 1.2 is version 46.0 and above
58
useThreadLocalImplementation = (v.compareTo("46.0") >= 0);
59         } else {
60             useThreadLocalImplementation = override.equals("yes") || override.equals("true");
61         }
62         // System.err.println("Trying to use thread local implementation? "+useThreadLocalImplementation);
63
if (useThreadLocalImplementation) {
64             tsFactory = getThreadLocalStackFactory();
65         } else {
66             tsFactory = getThreadLocalStackFactoryFor11();
67         }
68     }
69     
70     
71     private static String JavaDoc getSystemPropertyWithoutSecurityException (String JavaDoc aPropertyName, String JavaDoc aDefaultValue) {
72         try {
73             return System.getProperty(aPropertyName, aDefaultValue);
74         }
75         catch (SecurityException JavaDoc ex) {
76             return aDefaultValue;
77         }
78     }
79     
80     // For debug ...
81
public static String JavaDoc getThreadStackFactoryClassName() {
82         return tsFactory.getClass().getName();
83     }
84
85 }
86
Popular Tags