KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > IvyContext


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy;
7
8 import java.io.File JavaDoc;
9 import java.lang.ref.WeakReference JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
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 /**
19  * This class represents an execution context of an Ivy action.
20  * It contains several getters to retrieve information, like the used Ivy instance, the
21  * cache location...
22  *
23  * @see IvyThread
24  *
25  * @author Xavier Hanin
26  * @author Maarten Coene
27  */

28 public class IvyContext {
29
30     private static ThreadLocal JavaDoc _current = new ThreadLocal JavaDoc();
31     
32     private Ivy _defaultIvy;
33     private WeakReference JavaDoc _ivy = new WeakReference JavaDoc(null);
34     private File JavaDoc _cache;
35     private MessageImpl _messageImpl;
36     
37     private Map JavaDoc _contextMap = new HashMap JavaDoc();
38
39     private Thread JavaDoc _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     /**
52      * Changes the context associated with this thread.
53      * This is especially useful when launching a new thread, to associate it with the same context as the initial one.
54      *
55      * @param context the new context to use in this thread.
56      */

57     public static void setContext(IvyContext context) {
58         _current.set(context);
59     }
60     
61     /**
62      * Returns the current ivy instance.
63      * When calling any public ivy method on an ivy instance, a reference to this instance is
64      * put in this context, and thus accessible using this method, until no code reference
65      * this instance and the garbage collector collects it.
66      * Then, or if no ivy method has been called, a default ivy instance is returned
67      * by this method, so that it never returns null.
68      * @return the current ivy instance
69      */

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 JavaDoc e) {
81             }
82         }
83         return _defaultIvy;
84     }
85     void setIvy(Ivy ivy) {
86         _ivy = new WeakReference JavaDoc(ivy);
87         _operatingThread = Thread.currentThread();
88     }
89     public File JavaDoc getCache() {
90         return _cache == null ? getIvy().getDefaultCache() : _cache;
91     }
92     void setCache(File JavaDoc cache) {
93         _cache = cache;
94     }
95
96     public CircularDependencyStrategy getCircularDependencyStrategy() {
97         return getIvy().getCircularDependencyStrategy();
98     }
99
100     public Object JavaDoc get(String JavaDoc key) {
101         WeakReference JavaDoc ref = (WeakReference JavaDoc) _contextMap.get(key);
102         return ref == null ? null : ref.get();
103     }
104
105     public void set(String JavaDoc key, Object JavaDoc value) {
106         _contextMap.put(key, new WeakReference JavaDoc(value));
107     }
108
109     public Thread JavaDoc getOperatingThread() {
110         return _operatingThread;
111     }
112
113     
114     /* NB : The messageImpl is only used by Message. It should be better to place it there.
115      * Alternatively, the Message itself could be placed here, bu this is has a major impact
116      * because Message is used at a lot of place.
117      */

118     public MessageImpl getMessageImpl() {
119         return _messageImpl;
120     }
121     
122     public void setMessageImpl(MessageImpl impl) {
123         _messageImpl = impl;
124     }
125     
126     // should be better to use context to store this kind of information, but not yet ready to do so...
127
// private WeakReference _root = new WeakReference(null);
128
// private String _rootModuleConf = null;
129
// public IvyNode getRoot() {
130
// return (IvyNode) _root.get();
131
// }
132
//
133
// public void setRoot(IvyNode root) {
134
// _root = new WeakReference(root);
135
// }
136
//
137
// public String getRootModuleConf() {
138
// return _rootModuleConf;
139
// }
140
//
141
// public void setRootModuleConf(String rootModuleConf) {
142
// _rootModuleConf = rootModuleConf;
143
// }
144

145     
146 }
147
Popular Tags