001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle. 005 * 006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package pt.ua.dicoogle.rGUI.server.controllers; 020 021import java.io.File; 022import java.io.FileInputStream; 023import java.io.FileOutputStream; 024import java.io.IOException; 025import java.io.PrintStream; 026import java.rmi.RemoteException; 027import java.text.DateFormat; 028import java.text.SimpleDateFormat; 029import java.util.ArrayList; 030import java.util.Date; 031import java.util.TimerTask; 032import java.util.concurrent.Semaphore; 033import org.slf4j.LoggerFactory; 034 035import pt.ua.dicoogle.rGUI.interfaces.controllers.ILogs; 036import pt.ua.dicoogle.DicomLog.LogDICOM; 037import pt.ua.dicoogle.DicomLog.LogLine; 038import pt.ua.dicoogle.rGUI.interfaces.signals.ILogsSignal; 039import pt.ua.dicoogle.server.users.UserSessionsLog; 040import pt.ua.dicoogle.server.FileWatcher; 041import pt.ua.dicoogle.sdk.Utils.Platform; 042 043/** 044 * Controller of Logs 045 * 046 * @author Samuel Campos <samuelcampos@ua.pt> 047 */ 048@Deprecated 049public class Logs implements ILogs { 050 // Server Log (activities in server) 051 052 private static String logfilename; 053 private static File logfile; 054 private ILogsSignal signalBack; 055 056 private String serverLog; 057 private static Semaphore sem = new Semaphore(1, true); 058 private static Logs instance = null; 059 060 // saves the Loglines that are pending to send to GUI client 061 private ArrayList<LogLine> partialDICOMLog; 062 private String partialServerLog; 063 064 public static synchronized Logs getInstance() { 065 try { 066 sem.acquire(); 067 if (instance == null) { 068 instance = new Logs(); 069 } 070 sem.release(); 071 072 } catch (InterruptedException ex) { 073 LoggerFactory.getLogger(Logs.class).error(ex.getMessage(), ex); 074 } 075 return instance; 076 } 077 078 private Logs() { 079 080 // defaul log file 081 logfilename = Platform.homePath() + "DICOMLOG.log"; 082 logfile = new File(logfilename); 083 084 partialDICOMLog = new ArrayList<LogLine>(); 085 partialServerLog = ""; 086 087 /** 088 * File watcher of Log File, just to keep updated the 089 * interface log 090 */ 091 TimerTask task = new FileWatcher(logfile) { 092 093 @Override 094 protected void onChange(File file) { 095 reloadtext(file); 096 } 097 }; 098 099 java.util.Timer timer = new java.util.Timer(); 100 // repeat the check every second 101 timer.schedule(task, new Date(), 1000); 102 } 103 104 public void resetSignalBack(){ 105 signalBack = null; 106 } 107 108 @Override 109 public void RegisterSignalBack(ILogsSignal signalBack) throws RemoteException { 110 this.signalBack = signalBack; 111 112 try { 113 partialDICOMLog.addAll(LogDICOM.getInstance().getLl()); 114 partialServerLog = UserSessionsLog.getInstance().readLog(); 115 116 if(signalBack != null){ 117 signalBack.sendLogSignal(0); 118 signalBack.sendLogSignal(2); 119 } 120 } catch (RemoteException ex) { 121 //Logger.getLogger(Logs.class.getName()).log(Level.SEVERE, null, ex); 122 //DebugManager.getInstance().debug("Problem sending signal to log: 4"); 123 } 124 125 serverLog = null; 126 reloadtext(logfile); 127 } 128 129 130 public void reloadtext(File file) { 131 try { 132 FileInputStream fis = new FileInputStream(file); 133 int x = fis.available(); 134 byte b[] = new byte[x]; 135 fis.read(b); 136 String content = new String(b); 137 if (!content.equals(serverLog)) { 138 serverLog = content; 139 140 if(signalBack != null) 141 signalBack.sendLogSignal(1); 142 } 143 } catch (IOException ex) { 144 //Logger.getLogger(Logs.class.getName()).log(Level.SEVERE, null, ex); 145 //DebugManager.getInstance().debug("Problem sending signal to log: 1"); 146 } 147 } 148 149 public void addLog(LogLine line) { 150 partialDICOMLog.add(line); 151 152 try { 153 if(signalBack != null) 154 signalBack.sendLogSignal(0); 155 } catch (RemoteException ex) { 156 //Logger.getLogger(Logs.class.getName()).log(Level.SEVERE, null, ex); 157 //DebugManager.getInstance().debug("Problem sending signal to log: 2"); 158 } 159 } 160 161 public void addServerLog(String text){ 162 FileOutputStream fos = null; 163 PrintStream pos = null; 164 165 try { 166 fos = new FileOutputStream(logfilename, true); 167 pos = new PrintStream(fos); 168 pos.print(getDateTime().concat(": ").concat(text).concat("\n")); 169 170 pos.close(); 171 fos.close(); 172 } catch (Exception e) { 173 e.printStackTrace(); 174 } 175 } 176 177 public void addSessionsLog(String line){ 178 partialServerLog = partialServerLog + line; 179 180 try { 181 if(signalBack != null) 182 signalBack.sendLogSignal(2); 183 } catch (RemoteException ex) { 184 //Logger.getLogger(Logs.class.getName()).log(Level.SEVERE, null, ex); 185 //DebugManager.getInstance().debug("Problem sending signal to log: 3"); 186 } 187 } 188 189 @Override 190 public void clearDICOMLog() throws RemoteException { 191 partialDICOMLog.clear(); 192 LogDICOM.getInstance().clearLog(); 193 194 //DebugManager.getInstance().debug("Clear DICOM Log"); 195 } 196 197 @Override 198 public void clearServerLog() throws RemoteException { 199 FileOutputStream fos = null; 200 PrintStream pos = null; 201 202 /* 203 * To clear the log, writes an empty string to the logfile 204 */ 205 try { 206 fos = new FileOutputStream(logfilename); 207 pos = new PrintStream(fos); 208 pos.print(""); 209 210 pos.close(); 211 fos.close(); 212 } catch (Exception e) { 213 e.printStackTrace(); 214 } 215 } 216 217 @Override 218 public void clearSessionsLog() throws RemoteException { 219 partialServerLog = ""; 220 UserSessionsLog.getInstance().cleanLog(); 221 } 222 223 @Override 224 public ArrayList<LogLine> getPendingDICOMLog() throws RemoteException { 225 ArrayList<LogLine> log = new ArrayList<LogLine>(partialDICOMLog); 226 227 partialDICOMLog.clear(); 228 229 return log; 230 } 231 232 @Override 233 public String getServerLog() throws RemoteException { 234 return serverLog; 235 } 236 237 @Override 238 public String getPendingSessionsLog() throws RemoteException { 239 String tmp = partialServerLog; 240 241 partialServerLog = ""; 242 243 return tmp; 244 } 245 246 private String getDateTime() { 247 DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 248 Date date = new Date(); 249 return dateFormat.format(date); 250 } 251}