KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > util > Message


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.util;
7
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 import fr.jayasoft.ivy.IvyContext;
17
18 /**
19  *
20  * @author Xavier Hanin
21  * @author Gilles Scokart
22  */

23 public class Message {
24     // messages level copied from ant project, to avoid dependency on ant
25
/** Message priority of "error". */
26     public static final int MSG_ERR = 0;
27     /** Message priority of "warning". */
28     public static final int MSG_WARN = 1;
29     /** Message priority of "information". */
30     public static final int MSG_INFO = 2;
31     /** Message priority of "verbose". */
32     public static final int MSG_VERBOSE = 3;
33     /** Message priority of "debug". */
34     public static final int MSG_DEBUG = 4;
35
36
37     private static List JavaDoc _problems = new ArrayList JavaDoc();
38     private static List JavaDoc _warns = new ArrayList JavaDoc();
39     private static List JavaDoc _errors = new ArrayList JavaDoc();
40     
41     private static boolean _showProgress = true;
42     
43     private static boolean _showedInfo = false;
44     
45     public static void init(MessageImpl impl) {
46         IvyContext.getContext().setMessageImpl(impl);
47         showInfo();
48     }
49
50     /**
51      * same as init, but without displaying info
52      * @param impl
53      */

54     public static void setImpl(MessageImpl impl) {
55         IvyContext.getContext().setMessageImpl(impl);
56     }
57     
58     public static MessageImpl getImpl() {
59         return IvyContext.getContext().getMessageImpl();
60     }
61     
62     public static boolean isInitialised() {
63         return IvyContext.getContext().getMessageImpl() != null;
64     }
65
66     private static void showInfo() {
67         if (!_showedInfo ) {
68             Properties JavaDoc props = new Properties JavaDoc();
69             URL JavaDoc moduleURL = Message.class.getResource("/module.properties");
70             if (moduleURL != null) {
71                 try {
72                     InputStream JavaDoc module = moduleURL.openStream();
73                     props.load(module);
74                     debug("version information loaded from "+moduleURL);
75                     info(":: Ivy "+props.getProperty("version")+" - "+props.getProperty("date")+" :: http://ivy.jayasoft.org/ ::");
76                     module.close();
77                 } catch (IOException JavaDoc e) {
78                     info(":: Ivy non official version :: http://ivy.jayasoft.org/ ::");
79                 }
80             } else {
81                 info(":: Ivy non official version :: http://ivy.jayasoft.org/ ::");
82             }
83             _showedInfo = true;
84         }
85     }
86
87     public static void debug(String JavaDoc msg) {
88         MessageImpl messageImpl = IvyContext.getContext().getMessageImpl();
89         if (messageImpl != null) {
90             messageImpl.log(msg, MSG_DEBUG);
91         } else {
92             System.err.println(msg);
93         }
94     }
95     public static void verbose(String JavaDoc msg) {
96         MessageImpl messageImpl = IvyContext.getContext().getMessageImpl();
97         if (messageImpl != null) {
98             messageImpl.log(msg, MSG_VERBOSE);
99         } else {
100             System.err.println(msg);
101         }
102     }
103     public static void info(String JavaDoc msg) {
104         MessageImpl messageImpl = IvyContext.getContext().getMessageImpl();
105         if (messageImpl != null) {
106             messageImpl.log(msg, MSG_INFO);
107         } else {
108             System.err.println(msg);
109         }
110     }
111     public static void rawinfo(String JavaDoc msg) {
112         MessageImpl messageImpl = IvyContext.getContext().getMessageImpl();
113         if (messageImpl != null) {
114             messageImpl.rawlog(msg, MSG_INFO);
115         } else {
116             System.err.println(msg);
117         }
118     }
119     public static void warn(String JavaDoc msg) {
120         MessageImpl messageImpl = IvyContext.getContext().getMessageImpl();
121         if (messageImpl != null) {
122             messageImpl.log("WARN: "+msg, MSG_VERBOSE);
123         } else {
124             System.err.println(msg);
125         }
126         _problems.add("WARN: "+msg);
127         _warns.add(msg);
128     }
129     public static void error(String JavaDoc msg) {
130         MessageImpl messageImpl = IvyContext.getContext().getMessageImpl();
131         if (messageImpl != null) {
132             // log in verbose mode because message is appended as a problem, and will be
133
// logged at the end at error level
134
messageImpl.log("ERROR: "+msg, MSG_VERBOSE);
135         } else {
136             System.err.println(msg);
137         }
138         _problems.add("\tERROR: "+msg);
139         _errors.add(msg);
140     }
141
142     public static List JavaDoc getProblems() {
143         return _problems;
144     }
145     
146     public static void sumupProblems() {
147         if (_problems.size() > 0) {
148             info("\n:: problems summary ::");
149             MessageImpl messageImpl = IvyContext.getContext().getMessageImpl();
150             if (_warns.size() > 0) {
151                 info(":::: WARNINGS");
152                 for (Iterator JavaDoc iter = _warns.iterator(); iter.hasNext();) {
153                     String JavaDoc msg = (String JavaDoc) iter.next();
154                     if (messageImpl != null) {
155                         messageImpl.log("\t"+msg+"\n", MSG_WARN);
156                     } else {
157                         System.err.println(msg);
158                     }
159                 }
160             }
161             if (_errors.size() > 0) {
162                 info(":::: ERRORS");
163                 for (Iterator JavaDoc iter = _errors.iterator(); iter.hasNext();) {
164                     String JavaDoc msg = (String JavaDoc) iter.next();
165                     if (messageImpl != null) {
166                         messageImpl.log("\t"+msg+"\n", MSG_ERR);
167                     } else {
168                         System.err.println(msg);
169                     }
170                 }
171             }
172             info("\n:: USE VERBOSE OR DEBUG MESSAGE LEVEL FOR MORE DETAILS");
173             _problems.clear();
174             _warns.clear();
175             _errors.clear();
176         }
177     }
178
179     public static void progress() {
180         if (_showProgress) {
181             MessageImpl messageImpl = IvyContext.getContext().getMessageImpl();
182             if (messageImpl != null) {
183                 messageImpl.progress();
184             } else {
185                 System.out.println(".");
186             }
187         }
188     }
189
190     public static void endProgress() {
191         endProgress("");
192     }
193
194     public static void endProgress(String JavaDoc msg) {
195         if (_showProgress) {
196             MessageImpl messageImpl = IvyContext.getContext().getMessageImpl();
197             if (messageImpl != null) {
198                 messageImpl.endProgress(msg);
199             }
200         }
201     }
202
203     public static boolean isShowProgress() {
204         return _showProgress;
205     }
206     public static void setShowProgress(boolean progress) {
207         _showProgress = progress;
208     }
209
210     public static void uninit() {
211         IvyContext.getContext().setMessageImpl(null);
212     }
213 }
214
Popular Tags