| 1 | package tmcsim.paramicslog; |
|---|
| 2 | |
|---|
| 3 | import java.io.FileInputStream; |
|---|
| 4 | import java.io.IOException; |
|---|
| 5 | import java.rmi.Naming; |
|---|
| 6 | import java.util.Properties; |
|---|
| 7 | import java.util.logging.Level; |
|---|
| 8 | import java.util.logging.LogRecord; |
|---|
| 9 | import java.util.logging.Logger; |
|---|
| 10 | import javax.swing.JOptionPane; |
|---|
| 11 | import tmcsim.common.SimulationException; |
|---|
| 12 | import tmcsim.interfaces.CoordinatorInterface; |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * Logs communication from ParamicsCommunicator to ParamicsSimulator. |
|---|
| 16 | * |
|---|
| 17 | * The system property "PARAMICS_LOG_CONFIG" should be set to the path where the |
|---|
| 18 | * properties file for this class is located. <br><br> |
|---|
| 19 | * The data for the properties file follows. <br> |
|---|
| 20 | * <code> |
|---|
| 21 | * -----------------------------------------------------------------<br> |
|---|
| 22 | * Log File The file to write the communication to. CAD Simulator Host The host |
|---|
| 23 | * that runs the CAD Simulator. CAD Simulator RMI Port The port on the host that |
|---|
| 24 | * runs the CAD Simulator where the RMI Coordinator object is registered to. |
|---|
| 25 | * -----------------------------------------------------------------<br> |
|---|
| 26 | * Example File: <br> |
|---|
| 27 | * LogFile=c:\\log.txt CADSimulatorHost=localhost CADSimulatorRMIPort=4445 |
|---|
| 28 | * -----------------------------------------------------------------<br> |
|---|
| 29 | * </code> |
|---|
| 30 | * |
|---|
| 31 | * @author Nathaniel Lehrer |
|---|
| 32 | * @version |
|---|
| 33 | */ |
|---|
| 34 | public class ParamicsLogFileHandler extends java.util.logging.FileHandler |
|---|
| 35 | { |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Enmeration containing property names. |
|---|
| 39 | * |
|---|
| 40 | * @author Nathaniel Lehrer |
|---|
| 41 | */ |
|---|
| 42 | private enum PROPERTIES |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 45 | LOG_FILE("LogFile"), |
|---|
| 46 | CAD_SIM_HOST("CADSimulatorHost"), |
|---|
| 47 | CAD_SIM_PORT("CADSimulatorRMIPort"); |
|---|
| 48 | public String name; |
|---|
| 49 | |
|---|
| 50 | private PROPERTIES(String n) |
|---|
| 51 | { |
|---|
| 52 | name = n; |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | /** |
|---|
| 56 | * Error logger. |
|---|
| 57 | */ |
|---|
| 58 | private static Logger paramLogger = Logger.getLogger("tmcsim.paramicslog"); |
|---|
| 59 | /** |
|---|
| 60 | * Static instance. |
|---|
| 61 | */ |
|---|
| 62 | private static ParamicsLogFileHandler instance; |
|---|
| 63 | private static String propertiesFile; |
|---|
| 64 | /** |
|---|
| 65 | * Properties object. |
|---|
| 66 | */ |
|---|
| 67 | private static Properties paramicsLogProp; |
|---|
| 68 | /** |
|---|
| 69 | * File log entries are written to |
|---|
| 70 | */ |
|---|
| 71 | // private static String logFile; |
|---|
| 72 | /** |
|---|
| 73 | * Stores the log entries. This contains the same information logFile. |
|---|
| 74 | */ |
|---|
| 75 | private StringBuilder log; |
|---|
| 76 | /** |
|---|
| 77 | * Remote reference to the simulation |
|---|
| 78 | */ |
|---|
| 79 | private CoordinatorInterface theCoorInt; |
|---|
| 80 | /** |
|---|
| 81 | * Object for synchronizing IO |
|---|
| 82 | */ |
|---|
| 83 | Object lock; |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * Creates the singleton instance of this class. |
|---|
| 87 | */ |
|---|
| 88 | public static ParamicsLogFileHandler getInstance() throws IOException |
|---|
| 89 | { |
|---|
| 90 | System.setProperty("PARAMICS_LOG_PROPERTIES", "config/logging_paramics_communicator.properties"); |
|---|
| 91 | // Has an instance been created yet? |
|---|
| 92 | if (instance == null) |
|---|
| 93 | { |
|---|
| 94 | try |
|---|
| 95 | { |
|---|
| 96 | if (System.getProperty("PARAMICS_LOG_PROPERTIES") != null) |
|---|
| 97 | { |
|---|
| 98 | propertiesFile = System.getProperty("PARAMICS_LOG_PROPERTIES"); |
|---|
| 99 | |
|---|
| 100 | paramicsLogProp = new Properties(); |
|---|
| 101 | paramicsLogProp.load(new FileInputStream(propertiesFile)); |
|---|
| 102 | |
|---|
| 103 | if ((paramicsLogProp.getProperty(PROPERTIES.LOG_FILE.name)) == null) |
|---|
| 104 | { |
|---|
| 105 | throw new Exception("Properties file missing log file location."); |
|---|
| 106 | } |
|---|
| 107 | String logFile = paramicsLogProp.getProperty(PROPERTIES.LOG_FILE.name); |
|---|
| 108 | instance = new ParamicsLogFileHandler(logFile); |
|---|
| 109 | } |
|---|
| 110 | else |
|---|
| 111 | { |
|---|
| 112 | throw new Exception("PARAMICS_LOG_PROPERTIES system property not defined."); |
|---|
| 113 | } |
|---|
| 114 | } catch (Exception e) |
|---|
| 115 | { |
|---|
| 116 | instance = new ParamicsLogFileHandler(); |
|---|
| 117 | |
|---|
| 118 | paramLogger.logp(Level.WARNING, "ParamicsLog", "static initializer", |
|---|
| 119 | "Error occured initializing application", e); |
|---|
| 120 | |
|---|
| 121 | JOptionPane.showMessageDialog(null, e.getMessage(), |
|---|
| 122 | "Error - ParamicsLog will not save log to file.", |
|---|
| 123 | JOptionPane.ERROR_MESSAGE); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | return instance; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | * Creates an instance of ParamicsLog that does not write to a file when |
|---|
| 131 | * writeToLog is called. |
|---|
| 132 | */ |
|---|
| 133 | private ParamicsLogFileHandler() throws IOException |
|---|
| 134 | { |
|---|
| 135 | lock = new Object(); |
|---|
| 136 | log = new StringBuilder(""); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | * Creates an instance of ParamicsLog that writes to a file when writeToLog |
|---|
| 141 | * is called. |
|---|
| 142 | * |
|---|
| 143 | * @param propertiesFile |
|---|
| 144 | */ |
|---|
| 145 | private ParamicsLogFileHandler(String logFile) throws IOException |
|---|
| 146 | { |
|---|
| 147 | super(logFile); |
|---|
| 148 | lock = new Object(); |
|---|
| 149 | log = new StringBuilder(""); |
|---|
| 150 | |
|---|
| 151 | //String logFile = null; |
|---|
| 152 | String CADSIMHost = null; |
|---|
| 153 | String CADSIMPort = null; |
|---|
| 154 | |
|---|
| 155 | try |
|---|
| 156 | { |
|---|
| 157 | paramicsLogProp = new Properties(); |
|---|
| 158 | paramicsLogProp.load(new FileInputStream(propertiesFile)); |
|---|
| 159 | |
|---|
| 160 | if ((CADSIMHost = paramicsLogProp.getProperty(PROPERTIES.CAD_SIM_HOST.name)) == null) |
|---|
| 161 | { |
|---|
| 162 | throw new Exception("Properties file missing CAD Simulator host."); |
|---|
| 163 | } |
|---|
| 164 | else if ((CADSIMPort = paramicsLogProp.getProperty(PROPERTIES.CAD_SIM_PORT.name)) == null) |
|---|
| 165 | { |
|---|
| 166 | throw new Exception("Properties file missing CAD Simulator RMI port."); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | try |
|---|
| 170 | { |
|---|
| 171 | connect(CADSIMHost, CADSIMPort); |
|---|
| 172 | } catch (Exception e) |
|---|
| 173 | { |
|---|
| 174 | JOptionPane.showMessageDialog(null, |
|---|
| 175 | "ParamicsLog: Could not connect to remote Coordinator object.", |
|---|
| 176 | "Network Error", JOptionPane.ERROR_MESSAGE); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | // try |
|---|
| 180 | // { |
|---|
| 181 | // createLogFile(logFile); |
|---|
| 182 | // } catch (Exception e) |
|---|
| 183 | // { |
|---|
| 184 | // JOptionPane.showMessageDialog(null, |
|---|
| 185 | // "ParamicsLog: Could not create new log file.", |
|---|
| 186 | // "File Error", JOptionPane.ERROR_MESSAGE); |
|---|
| 187 | // } |
|---|
| 188 | |
|---|
| 189 | } catch (Exception e) |
|---|
| 190 | { |
|---|
| 191 | |
|---|
| 192 | paramLogger.logp(Level.WARNING, "ParamicsLog", "ParamicsLog constructor", |
|---|
| 193 | "Properties file incorrect or missing.", e); |
|---|
| 194 | |
|---|
| 195 | JOptionPane.showMessageDialog(null, |
|---|
| 196 | "ParamicsLog: Properties file invalid.", |
|---|
| 197 | "Invalid Configuration", JOptionPane.ERROR_MESSAGE); |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | /** |
|---|
| 202 | * Connect to the Coordinator's RMI object. |
|---|
| 203 | * |
|---|
| 204 | * @param hostname Host name of the CAD Simulator. |
|---|
| 205 | * @param portNumber Port number of the CAD Simulator RMI communication. |
|---|
| 206 | * @throws SimulationException if there is an error creating the RMI |
|---|
| 207 | * connection. |
|---|
| 208 | */ |
|---|
| 209 | private void connect(String hostname, String portNumber) |
|---|
| 210 | throws SimulationException |
|---|
| 211 | { |
|---|
| 212 | |
|---|
| 213 | String coorIntURL = ""; |
|---|
| 214 | |
|---|
| 215 | try |
|---|
| 216 | { |
|---|
| 217 | coorIntURL = "rmi://" + hostname + ":" + portNumber + "/coordinator"; |
|---|
| 218 | |
|---|
| 219 | theCoorInt = (CoordinatorInterface) Naming.lookup(coorIntURL); |
|---|
| 220 | } catch (Exception e) |
|---|
| 221 | { |
|---|
| 222 | paramLogger.logp(Level.WARNING, "ParamicsLog", |
|---|
| 223 | "establishRMIConnection", "Unable to establish RMI " |
|---|
| 224 | + "communication with the CAD Simulator. URL <" + coorIntURL + ">", e); |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | /** |
|---|
| 229 | * Accessor to the entries in the log. No file IO is used. |
|---|
| 230 | * |
|---|
| 231 | * @return The entries in the log. |
|---|
| 232 | */ |
|---|
| 233 | public String getLog() |
|---|
| 234 | { |
|---|
| 235 | |
|---|
| 236 | return log.toString(); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | /** |
|---|
| 240 | * Writes an entry to the log. The simulator time when the message was sent |
|---|
| 241 | * is prepended to the entry. Entries are padded by a blank line before and |
|---|
| 242 | * after them. TODO: Ensure output is written in order of request. |
|---|
| 243 | * |
|---|
| 244 | * @param entry |
|---|
| 245 | */ |
|---|
| 246 | public void publish(LogRecord rec) |
|---|
| 247 | { |
|---|
| 248 | |
|---|
| 249 | String time = "?"; |
|---|
| 250 | String formattedEntry; |
|---|
| 251 | String entry = rec.getMessage(); |
|---|
| 252 | |
|---|
| 253 | if (theCoorInt != null) |
|---|
| 254 | { |
|---|
| 255 | try |
|---|
| 256 | { |
|---|
| 257 | time = formatTime(theCoorInt.getCurrentSimulationTime()); |
|---|
| 258 | } catch (Exception e) |
|---|
| 259 | { |
|---|
| 260 | paramLogger.logp(Level.WARNING, "ParamicsLog", |
|---|
| 261 | "RMICommunication", "Unable to communicate with RMI object", e); |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | formattedEntry = "\n" + "<!-- Time written to file: " + time + " -->\n" + entry + "\n"; |
|---|
| 266 | log.append(formattedEntry); |
|---|
| 267 | rec.setMessage(formattedEntry); |
|---|
| 268 | super.publish(rec); |
|---|
| 269 | |
|---|
| 270 | //ParamicsLogGUI.getInstance().update(this, entry); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | /** |
|---|
| 274 | * Formats the time given in seconds to hh:mm:ss format. |
|---|
| 275 | * |
|---|
| 276 | * @param time The time in seconds. |
|---|
| 277 | */ |
|---|
| 278 | public String formatTime(long time) |
|---|
| 279 | { |
|---|
| 280 | long seconds = time % 60; |
|---|
| 281 | long minutes = (time - seconds) / 60; |
|---|
| 282 | long hours = (time - seconds - minutes * 60) / 60; |
|---|
| 283 | |
|---|
| 284 | return padr(hours) + ":" + padr(minutes) + ":" + padr(seconds); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | private String padr(long n) |
|---|
| 288 | { |
|---|
| 289 | if (n < 10) |
|---|
| 290 | { |
|---|
| 291 | return "0" + n; |
|---|
| 292 | } |
|---|
| 293 | { |
|---|
| 294 | return "" + n; |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | /** |
|---|
| 298 | * Accessor for static instance of this class. |
|---|
| 299 | * |
|---|
| 300 | * @return The instance of this class. |
|---|
| 301 | */ |
|---|
| 302 | // public static ParamicsLogFileHandler getInstance() |
|---|
| 303 | // { |
|---|
| 304 | // |
|---|
| 305 | // return instance; |
|---|
| 306 | // } |
|---|
| 307 | } |
|---|