01: /* ThreadLocals.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Thu Sep 21 10:07:46 2006, Created by henrichen
10: }}IS_NOTE
11:
12: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: }}IS_RIGHT
16: */
17: package org.zkoss.lang;
18:
19: import java.lang.reflect.Field;
20:
21: /**
22: * ThreadLocal related utilties. This implementation get the static
23: * ThreadLocal via reflection.
24: *
25: * @author henrichen
26: */
27: public class ThreadLocals {
28: /** Given class name and static ThreadLocal field name, return the associated ThreadLocal.
29: * @param clsname the class name
30: * @param fldname the ThreadLocal field name
31: */
32: public static ThreadLocal getThreadLocal(String clsname,
33: String fldname) {
34: try {
35: Class cls = Classes.forNameByThread(clsname);
36: return getThreadLocal(cls, fldname);
37: } catch (ClassNotFoundException ex) {
38: throw SystemException.Aide.wrap(ex);
39: }
40: }
41:
42: /** Given class and static ThreadLocal field name, return the associated ThreadLocal.
43: * @param cls the class
44: * @param fldname the ThreadLocal field name.
45: */
46: public static ThreadLocal getThreadLocal(Class cls, String fldname) {
47: try {
48: Field fld = cls.getDeclaredField(fldname);
49: fld.setAccessible(true);
50: return (ThreadLocal) fld.get(cls); //class static field, a ThreadLocal
51: } catch (java.lang.NoSuchFieldException ex) {
52: throw SystemException.Aide.wrap(ex);
53: } catch (java.lang.IllegalAccessException ex) {
54: throw SystemException.Aide.wrap(ex);
55: }
56: }
57: }
|