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.server.users;
020
021import java.io.BufferedReader;
022import java.io.BufferedWriter;
023import java.io.FileReader;
024import java.io.FileWriter;
025import java.io.IOException;
026import org.slf4j.LoggerFactory;
027
028import pt.ua.dicoogle.sdk.Utils.Platform;
029
030/**
031 *
032 * @author Samuel Campos <samuelcampos@ua.pt>
033 */
034@Deprecated
035public class UserSessionsLog {
036
037    private String sessionsLogFile;
038    private static UserSessionsLog instance = null;
039
040    public static synchronized UserSessionsLog getInstance() {
041        if (instance == null) {
042            instance = new UserSessionsLog();
043        }
044        return instance;
045    }
046
047    private UserSessionsLog() {
048        sessionsLogFile = Platform.homePath() + "sessions.log";
049    }
050
051    public void login(String username, String host, boolean admin) {
052        String line;
053
054        if (admin)
055            line = host + " - " + username + ": administrator logged";
056        else
057            line = host + " - " + username + ": user logged";
058  
059        writeLine(line, true);
060    }
061
062    public void loginFailed(String username, String host, boolean admin) {
063        String line;
064
065        if (admin) {
066            line = host + " - " + username + ": administrator login failed";
067        } else {
068            line = host + " - " + username + ": user login failed";
069        }
070
071        writeLine(line, true);
072    }
073
074    public void logout(String username, String host, boolean admin) {
075        String line;
076
077        if (admin) {
078            line = host + " - " + username + ": administrator logged out";
079            writeLine(line, false);
080        } else {
081            line = host + " - " + username + ": user logged out";
082            writeLine(line, true);
083        }
084    }
085
086    /**
087     *
088     * @param line
089     * @param send - indicates whether or not to send to the controller
090     */
091    private void writeLine(String line, boolean send) {
092        BufferedWriter out = null;
093
094        //DebugManager.getInstance().debug(line);
095
096//        Date now = new Date();
097//        String tmp = now.toString() + ": " + line + "\n";
098
099        if(send) {
100            LoggerFactory.getLogger(UserSessions.class).info(line);
101//            Logs.getInstance().addSessionsLog(tmp);
102        }
103        
104//        try {
105//            semFile.acquire();
106//            out = new BufferedWriter(new FileWriter(sessionsLogFile, true));
107//            out.write(tmp);
108//            out.close();
109//        } catch (InterruptedException ex) {
110//            Logger.getLogger(UserSessionsLog.class.getName()).log(Level.SEVERE, null, ex);
111//        } catch (IOException ex) {
112//            Logger.getLogger(UserSessionsLog.class.getName()).log(Level.SEVERE, null, ex);
113//        } finally {
114//            try {
115//                out.close();
116//            } catch (IOException ex) {
117//                Logger.getLogger(UserSessionsLog.class.getName()).log(Level.SEVERE, null, ex);
118//            }
119//        }
120//
121//        semFile.release();
122    }
123
124    public synchronized void cleanLog() {
125        BufferedWriter out = null;
126        try {
127            out = new BufferedWriter(new FileWriter(sessionsLogFile));
128            out.write("");
129            out.close();
130
131        } catch (IOException ex) {
132            LoggerFactory.getLogger(UserSessions.class).error("Failed to clean log", ex);
133        } finally {
134            try {
135                out.close();
136            } catch (IOException ex) {
137                LoggerFactory.getLogger(UserSessions.class).error("Failed to clean log", ex);
138            }
139        }
140    }
141
142    public synchronized String readLog() {
143        String ret = "";
144
145        BufferedReader in = null;
146        try {
147            String tmp;
148
149
150            in = new BufferedReader(new FileReader(sessionsLogFile));
151
152            while ((tmp = in.readLine()) != null) {
153                ret = ret + "\n" + tmp;
154            }
155
156            in.close();
157
158        } catch (IOException ex) {
159            LoggerFactory.getLogger(UserSessions.class).error("Failed to read log", ex);
160        } finally {
161            try {
162                in.close();
163
164            } catch (IOException ex) {
165                LoggerFactory.getLogger(UserSessions.class).error("Failed to read log", ex);
166            }
167        }
168        
169        ret = ret + "\n";
170
171        return ret;
172    }
173}