1 6 package fr.jayasoft.ivy.util; 7 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.net.URL ; 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Properties ; 15 16 import fr.jayasoft.ivy.IvyContext; 17 18 23 public class Message { 24 26 public static final int MSG_ERR = 0; 27 28 public static final int MSG_WARN = 1; 29 30 public static final int MSG_INFO = 2; 31 32 public static final int MSG_VERBOSE = 3; 33 34 public static final int MSG_DEBUG = 4; 35 36 37 private static List _problems = new ArrayList (); 38 private static List _warns = new ArrayList (); 39 private static List _errors = new ArrayList (); 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 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 props = new Properties (); 69 URL moduleURL = Message.class.getResource("/module.properties"); 70 if (moduleURL != null) { 71 try { 72 InputStream 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 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 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 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 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 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 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 msg) { 130 MessageImpl messageImpl = IvyContext.getContext().getMessageImpl(); 131 if (messageImpl != null) { 132 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 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 iter = _warns.iterator(); iter.hasNext();) { 153 String msg = (String ) 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 iter = _errors.iterator(); iter.hasNext();) { 164 String msg = (String ) 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 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 |