1 6 package fr.jayasoft.ivy; 7 8 import java.io.File ; 9 import java.lang.ref.WeakReference ; 10 import java.util.HashMap ; 11 import java.util.Map ; 12 13 import fr.jayasoft.ivy.circular.CircularDependencyStrategy; 14 import fr.jayasoft.ivy.util.IvyThread; 15 import fr.jayasoft.ivy.util.MessageImpl; 16 17 18 28 public class IvyContext { 29 30 private static ThreadLocal _current = new ThreadLocal (); 31 32 private Ivy _defaultIvy; 33 private WeakReference _ivy = new WeakReference (null); 34 private File _cache; 35 private MessageImpl _messageImpl; 36 37 private Map _contextMap = new HashMap (); 38 39 private Thread _operatingThread; 40 41 42 public static IvyContext getContext() { 43 IvyContext cur = (IvyContext)_current.get(); 44 if (cur == null) { 45 cur = new IvyContext(); 46 _current.set(cur); 47 } 48 return cur; 49 } 50 51 57 public static void setContext(IvyContext context) { 58 _current.set(context); 59 } 60 61 70 public Ivy getIvy() { 71 Ivy ivy = (Ivy)_ivy.get(); 72 return ivy == null ? getDefaultIvy() : ivy; 73 } 74 75 private Ivy getDefaultIvy() { 76 if (_defaultIvy == null) { 77 _defaultIvy = new Ivy(); 78 try { 79 getDefaultIvy().configureDefault(); 80 } catch (Exception e) { 81 } 82 } 83 return _defaultIvy; 84 } 85 void setIvy(Ivy ivy) { 86 _ivy = new WeakReference (ivy); 87 _operatingThread = Thread.currentThread(); 88 } 89 public File getCache() { 90 return _cache == null ? getIvy().getDefaultCache() : _cache; 91 } 92 void setCache(File cache) { 93 _cache = cache; 94 } 95 96 public CircularDependencyStrategy getCircularDependencyStrategy() { 97 return getIvy().getCircularDependencyStrategy(); 98 } 99 100 public Object get(String key) { 101 WeakReference ref = (WeakReference ) _contextMap.get(key); 102 return ref == null ? null : ref.get(); 103 } 104 105 public void set(String key, Object value) { 106 _contextMap.put(key, new WeakReference (value)); 107 } 108 109 public Thread getOperatingThread() { 110 return _operatingThread; 111 } 112 113 114 118 public MessageImpl getMessageImpl() { 119 return _messageImpl; 120 } 121 122 public void setMessageImpl(MessageImpl impl) { 123 _messageImpl = impl; 124 } 125 126 145 146 } 147 | Popular Tags |