Example usage for java.util.logging Handler close

List of usage examples for java.util.logging Handler close

Introduction

In this page you can find the example usage for java.util.logging Handler close.

Prototype

public abstract void close() throws SecurityException;

Source Link

Document

Close the Handler and free all associated resources.

Usage

From source file:BSxSB.Controllers.StudentController.java

@RequestMapping(value = "/studentgeneratecourses", method = RequestMethod.GET)
public String generateCourses(Model model) {
    try {/*from w  ww. java  2s.co  m*/
        //Initialize the file that the logger writes to.
        Handler handler = new FileHandler("%tBSxSBStudentGenerateCourses.log", true);
        handler.setFormatter(new SimpleFormatter());
        logger.addHandler(handler);
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName();
        Students currentStudent = StudentDAO.getStudent(name);
        Schools currentSchool = SchoolDAO.getSchool(currentStudent.getSchoolid());
        List<Courses> courses = CourseDAO.getCourseOfferingForSchool(currentSchool.getSchoolid());
        List<Scheduleblocks> scheduleblocks = new ArrayList<>();
        for (Courses course : courses) {
            scheduleblocks.add(ScheduleBlockDAO.getScheduleBlock(course.getScheduleblockid()));
        }
        List<Schools> schoolyears = SchoolDAO.getSchoolSameName(currentSchool.getSchoolname());
        Generationcriteria gencriteria = GenerationcriteriaDAO
                .getGenerationCriteria(currentStudent.getStudentid());

        if (gencriteria != null) {
            String[] courseids = gencriteria.getCourseids().split(",");
            List<Courses> genCourses = new ArrayList<>();
            List<Scheduleblocks> genscheduleblocks = new ArrayList<>();
            if (!gencriteria.getCourseids().isEmpty()) {
                for (String courseid : courseids) {
                    Courses genCourse = CourseDAO.getCourse(Integer.parseInt(courseid));
                    genCourses.add(genCourse);
                    genscheduleblocks.add(ScheduleBlockDAO.getScheduleBlock(genCourse.getScheduleblockid()));
                }
            }
            if (gencriteria.getLunch() != null && !gencriteria.getLunch().isEmpty()) {
                String[] lunch = gencriteria.getLunch().split(",");
                model.addAttribute("lunch", lunch);
            }
            String lunchrange = currentSchool.getLunchrange();
            model.addAttribute("lunchrange", lunchrange);
            int numdays = currentSchool.getNumdays();
            String lunchdays = lunchToText(numdays);
            String[] lunchdays2 = lunchdays.split(",");
            model.addAttribute("lunchdays", lunchdays2);
            model.addAttribute("genscheduleblocks", genscheduleblocks);
            model.addAttribute("gencourses", genCourses);
            model.addAttribute("schoolyears", schoolyears);
            model.addAttribute("scheduleblocks", scheduleblocks);
            model.addAttribute("courses", courses);
            logger.info("Successfully loaded generation criteria.");
            handler.close();
            logger.removeHandler(handler);
        }
    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
    } catch (SecurityException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

    return "studentgeneratecourses";
}

From source file:BSxSB.Controllers.StudentController.java

@RequestMapping(value = "/removedesiredcourse", method = RequestMethod.POST)
public String removedesiredcourses(Model model, @RequestParam("id") String id) {
    try {// w  w w .ja v  a 2s. co  m
        //Initialize the file that the logger writes to.
        Handler handler = new FileHandler("%tBSxSBStudentGenerateCourses.log", true);
        handler.setFormatter(new SimpleFormatter());
        logger.addHandler(handler);
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName();
        Students currentStudent = StudentDAO.getStudent(name);
        GenerationcriteriaDAO.removeDesiredCourses(currentStudent.getStudentid(), id);
        logger.info("Course sucessfully removed from generation criteria.");
        Schools currentSchool = SchoolDAO.getSchool(currentStudent.getSchoolid());
        List<Courses> courses = CourseDAO.getCourseOfferingForSchool(currentSchool.getSchoolid());
        List<Scheduleblocks> scheduleblocks = new ArrayList<>();
        for (Courses course : courses) {
            scheduleblocks.add(ScheduleBlockDAO.getScheduleBlock(course.getScheduleblockid()));
        }
        List<Schools> schoolyears = SchoolDAO.getSchoolSameName(currentSchool.getSchoolname());
        Generationcriteria gencriteria = GenerationcriteriaDAO
                .getGenerationCriteria(currentStudent.getStudentid());
        String[] courseids = gencriteria.getCourseids().split(",");
        List<Courses> genCourses = new ArrayList<>();
        List<Scheduleblocks> genscheduleblocks = new ArrayList<>();
        if (!gencriteria.getCourseids().isEmpty()) {
            for (String courseid : courseids) {
                Courses genCourse = CourseDAO.getCourse(Integer.parseInt(courseid));
                genCourses.add(genCourse);
                genscheduleblocks.add(ScheduleBlockDAO.getScheduleBlock(genCourse.getScheduleblockid()));
            }
        }
        if (gencriteria.getLunch() != null && !gencriteria.getLunch().isEmpty()) {
            String[] lunch = gencriteria.getLunch().split(",");
            model.addAttribute("lunch", lunch);
        }
        String lunchrange = currentSchool.getLunchrange();
        model.addAttribute("lunchrange", lunchrange);
        int numdays = currentSchool.getNumdays();
        String lunchdays = lunchToText(numdays);
        String[] lunchdays2 = lunchdays.split(",");
        model.addAttribute("lunchdays", lunchdays2);
        model.addAttribute("genscheduleblocks", genscheduleblocks);
        model.addAttribute("gencourses", genCourses);
        model.addAttribute("schoolyears", schoolyears);
        model.addAttribute("scheduleblocks", scheduleblocks);
        model.addAttribute("courses", courses);
        logger.info("Generation criteria successfully updated.");
        handler.close();
        logger.removeHandler(handler);
    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
    } catch (SecurityException ex) {
        logger.log(Level.SEVERE, null, ex);
    }
    return "studentgeneratecourses";
}

From source file:BSxSB.Controllers.StudentController.java

@RequestMapping(value = "/studentassignedcourses", method = RequestMethod.GET)
public String assignedCourses(Model model) {
    try {//from w  w  w .  jav  a2  s .c om
        //Initialize the file that the logger writes to.
        Handler handler = new FileHandler("%tBSxSBStudentAssignedCourses.log", true);
        handler.setFormatter(new SimpleFormatter());
        logger.addHandler(handler);
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName();
        Students currentStudent = StudentDAO.getStudent(name);
        List<Courses> studentCourses = CourseDAO.getCoursesForStudent(currentStudent.getStudentid());
        Schools school = SchoolDAO.getSchool(currentStudent.getSchoolid());
        List<List<Courses[]>> semesters = new ArrayList<>();
        List<Students> friends = StudentDAO.getFriends(currentStudent.getStudentid());
        for (Courses course : studentCourses) {
            for (Students friend : friends) {
                List<Courses> friendCourses = CourseDAO.getCoursesForStudent(friend.getStudentid());
                for (Courses friendCourse : friendCourses) {
                    if (friendCourse.getCourseid() == (course.getCourseid())) {
                        if (course.getFriends() != null) {
                            course.setFriends(course.getFriends() + " " + friend.getFirstname() + " "
                                    + friend.getLastname());
                        } else {
                            course.setFriends(friend.getFirstname() + " " + friend.getLastname());
                        }
                    }
                }
            }
        }
        logger.info("Successfully retrieved all friends for each course.");
        for (int s = 0; s < school.getNumsemesters(); s++) {
            List<Courses[]> schedule = new ArrayList<>();
            for (int i = 0; i < school.getNumperiods(); i++) {
                Courses[] period = new Courses[7];
                for (Courses course : studentCourses) {
                    Scheduleblocks sb = ScheduleBlockDAO.getScheduleBlock(course.getScheduleblockid());
                    if (sb.getPeriod() == i + 1) {
                        String[] days = sb.getDays().split(",");
                        String[] semester = course.getSemester().split(",");
                        for (String sem : semester) {
                            if (Integer.parseInt(sem) == s + 1) {
                                for (String day : days) {
                                    period[Integer.parseInt(day) - 1] = course;
                                }
                            }
                        }
                    }
                }
                schedule.add(period);
            }
            semesters.add(schedule);
        }
        logger.info("Successfully retrieved schedule for each semester.");
        Schools currentSchool = SchoolDAO.getSchool(currentStudent.getSchoolid());
        List<Schools> schoolyears = SchoolDAO.getSchoolSameName(currentSchool.getSchoolname());
        model.addAttribute("schoolyears", schoolyears);
        model.addAttribute("semester", semesters);
        handler.close();
        logger.removeHandler(handler);
    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
    } catch (SecurityException ex) {
        logger.log(Level.SEVERE, null, ex);
    }
    return "studentassignedcourses";
}

From source file:BSxSB.Controllers.StudentController.java

@RequestMapping(value = "/generateschedule", method = RequestMethod.GET)
public String generateSchedule(Model model, @RequestParam("instructors") String instructors) {
    try {//from   w  w w  .  jav  a 2 s  . c  o  m
        //Initialize the file that the logger writes to.
        Handler handler = new FileHandler("%tBSxSBStudentGenerateCourses.log", true);
        handler.setFormatter(new SimpleFormatter());
        logger.addHandler(handler);
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName();
        Students currentStudent = StudentDAO.getStudent(name);
        Schools currentSchool = SchoolDAO.getSchool(currentStudent.getSchoolid());
        Generationcriteria gencriteria = GenerationcriteriaDAO
                .getGenerationCriteria(currentStudent.getStudentid());
        String[] courseids = gencriteria.getCourseids().split(",");
        List<Courses> genCourses = new ArrayList<>();
        List<Scheduleblocks> genscheduleblocks = new ArrayList<>();
        if (!gencriteria.getCourseids().isEmpty()) {
            for (String courseid : courseids) {
                Courses genCourse = CourseDAO.getCourse(Integer.parseInt(courseid));
                genCourses.add(genCourse);
                genscheduleblocks.add(ScheduleBlockDAO.getScheduleBlock(genCourse.getScheduleblockid()));
            }
        }
        List<List<Courses>> conflictCourses = new ArrayList<>();
        String lunches = gencriteria.getLunch();
        String[] lunch = lunches.split(",");
        for (int i = 0; i < lunch.length; i++) {
            if (lunch[i].equals("monday")) {
                lunch[i] = "1";
            } else if (lunch[i].equals("tuesday")) {
                lunch[i] = "2";
            } else if (lunch[i].equals("wednesday")) {
                lunch[i] = "3";
            } else if (lunch[i].equals("thursday")) {
                lunch[i] = "4";
            } else if (lunch[i].equals("friday")) {
                lunch[i] = "5";
            } else if (lunch[i].equals("saturday")) {
                lunch[i] = "6";
            } else if (lunch[i].equals("sunday")) {
                lunch[i] = "7";
            }
        }
        String lunchperiods = currentSchool.getLunchrange();
        String[] temp = lunchperiods.split("-");
        int length = Integer.parseInt(temp[1]) - Integer.parseInt(temp[0]);
        int[] lunchperiod = new int[length + 1];
        int low = Integer.parseInt(temp[0]);
        for (int i = 0; i <= length; i++) {
            lunchperiod[i] = low;
            low++;
        }
        int conlunchcount = 0;
        List<List<Courses>> conflictLunches = new ArrayList<>();
        for (int i = 0; i < genscheduleblocks.size(); i++) {
            Scheduleblocks firstBlock = genscheduleblocks.get(i);
            conflictLunch: {
                for (int period : lunchperiod) {
                    if (firstBlock.getPeriod().equals(period)) {
                        String[] gendays = firstBlock.getDays().split(",");
                        for (String days : lunch) {
                            for (String genday : gendays) {
                                if (days.equals(genday)) {
                                    List<Courses> conflictCourse = new ArrayList<>();
                                    conflictCourse.add(genCourses.get(i));
                                    Courses lunchCourse = new Courses(0, "lunch", "", "", 0);
                                    conflictCourse.add(lunchCourse);
                                    conflictLunches.add(conflictCourse);
                                    conlunchcount++;
                                    break conflictLunch;
                                }
                            }
                        }
                    }
                }
            }
            for (int i2 = i + 1; i2 < genscheduleblocks.size(); i2++) {
                Scheduleblocks secondBlock = genscheduleblocks.get(i2);
                checkConflict: {
                    if (firstBlock.getPeriod().equals(secondBlock.getPeriod())) {
                        String[] days = firstBlock.getDays().split(",");
                        String[] days2 = secondBlock.getDays().split(",");
                        for (String d : days) {
                            for (String d2 : days2) {
                                if (d.equals(d2)) {
                                    Courses genCourse = genCourses.get(i);
                                    Courses genCourse2 = genCourses.get(i2);
                                    String[] semesters = genCourse.getSemester().split(",");
                                    String[] semesters2 = genCourse2.getSemester().split(",");
                                    for (String s : semesters) {
                                        for (String s2 : semesters2) {
                                            if (s.equals(s2)) {
                                                List<Courses> conflictCourse = new ArrayList<>();
                                                conflictCourse.add(genCourse);
                                                conflictCourse.add(genCourse2);
                                                conflictCourses.add(conflictCourse);
                                                break checkConflict;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.print(conlunchcount + " " + lunchperiod.length);
        if (conlunchcount >= lunchperiod.length) {

            for (List<Courses> conlunch : conflictLunches) {
                System.out.print(conlunch);
                conflictCourses.add(conlunch);
            }
        }
        if (conflictCourses.isEmpty()) {
            String[] instructor = instructors.split(",");
            for (int i = 0; i < genCourses.size(); i++) {
                Courses course1 = genCourses.get(i);
                String courseiden = course1.getCourseidentifier();
                for (int i2 = i + 1; i2 < genCourses.size(); i2++) {
                    Courses course2 = genCourses.get(i2);
                    checkCourse: {
                        if (courseiden.equals(course2.getCourseidentifier())) {
                            if (instructors.isEmpty()) {
                                List<Students> friends = StudentDAO.getFriends(currentStudent.getStudentid());
                                int friendcourse1 = 0;
                                int friendcourse2 = 0;
                                for (Students friend : friends) {
                                    if (RegistrationDAO.isRegistered(course1, friend)) {
                                        friendcourse1++;
                                    } else if (RegistrationDAO.isRegistered(course2, friend)) {
                                        friendcourse2++;
                                    }
                                }
                                if (friendcourse1 >= friendcourse2) {
                                    genCourses.remove(i2);
                                    break checkCourse;
                                } else if (friendcourse1 < friendcourse2) {
                                    genCourses.remove(i);
                                    break checkCourse;
                                } else {
                                    genCourses.remove(i);
                                    break checkCourse;
                                }
                            } else {
                                for (String inst : instructor) {
                                    if (course1.getInstructor().equals(inst)) {
                                        genCourses.remove(i2);
                                        break checkCourse;
                                    } else if (course2.getInstructor().equals(inst)) {
                                        genCourses.remove(i);
                                        break checkCourse;
                                    } else {
                                        List<Students> friends = StudentDAO
                                                .getFriends(currentStudent.getStudentid());
                                        int friendcourse1 = 0;
                                        int friendcourse2 = 0;
                                        for (Students friend : friends) {
                                            if (RegistrationDAO.isRegistered(course1, friend)) {
                                                friendcourse1++;
                                            } else if (RegistrationDAO.isRegistered(course2, friend)) {
                                                friendcourse2++;
                                            }
                                        }
                                        if (friendcourse1 >= friendcourse2) {
                                            genCourses.remove(i2);
                                            break checkCourse;
                                        } else if (friendcourse1 < friendcourse2) {
                                            genCourses.remove(i);
                                            break checkCourse;
                                        } else {
                                            genCourses.remove(i);
                                            break checkCourse;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            List<List<Courses[]>> semesters = new ArrayList<>();
            List<Students> friends = StudentDAO.getFriends(currentStudent.getStudentid());
            for (Courses course : genCourses) {
                for (Students friend : friends) {
                    List<Courses> friendCourses = CourseDAO.getCoursesForStudent(friend.getStudentid());
                    for (Courses friendCourse : friendCourses) {
                        if (friendCourse.getCourseid() == (course.getCourseid())) {
                            if (course.getFriends() != null) {
                                course.setFriends(course.getFriends() + " " + friend.getFirstname() + " "
                                        + friend.getLastname());
                            } else {
                                course.setFriends(friend.getFirstname() + " " + friend.getLastname());
                            }
                        }
                    }
                }
            }
            for (int s = 0; s < currentSchool.getNumsemesters(); s++) {
                List<Courses[]> schedule = new ArrayList<>();
                for (int i = 0; i < currentSchool.getNumperiods(); i++) {
                    Courses[] period = new Courses[7];
                    for (Courses course : genCourses) {
                        Scheduleblocks sb = ScheduleBlockDAO.getScheduleBlock(course.getScheduleblockid());
                        if (sb.getPeriod() == i + 1) {
                            String[] days = sb.getDays().split(",");
                            String[] semester = course.getSemester().split(",");
                            for (String sem : semester) {
                                if (Integer.parseInt(sem) == s + 1) {
                                    for (String day : days) {
                                        period[Integer.parseInt(day) - 1] = course;
                                    }
                                }
                            }
                        }
                    }
                    schedule.add(period);
                }
                semesters.add(schedule);
            }
            model.addAttribute("semester", semesters);
            logger.info("Generated schedule complete.");
        } else {
            model.addAttribute("conflictCourses", conflictCourses);
            logger.info("Conflicting courses displayed.");
        }

        List<Schools> schoolyears = SchoolDAO.getSchoolSameName(currentSchool.getSchoolname());
        model.addAttribute("schoolyears", schoolyears);
        handler.close();
        logger.removeHandler(handler);
    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
    } catch (SecurityException ex) {
        logger.log(Level.SEVERE, null, ex);
    }
    return "studentviewgenerated";
}

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandler.java

/**
 * Closes all handlers for the given logger
 * @param logger/*from   w  ww  .  j  a  v  a2  s .  c o m*/
 */
private void close(final Logger logger) {
    if (logger == null) {
        return;
    }
    for (final Handler handler : logger.getHandlers()) {
        try {
            handler.close();
        } catch (final Exception e) {
            LOG.warn("Error while closing log handler - Ignoring", e);
        }
    }
}

From source file:org.apache.directory.studio.connection.core.io.jndi.LdifSearchLogger.java

/**
 * Creates a new instance of LdifSearchLogger.
 *//*  w ww.j av  a 2 s  .  c  o m*/
public LdifSearchLogger() {
    ConnectionCorePlugin.getDefault().getPluginPreferences()
            .addPropertyChangeListener(new IPropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent event) {
                    if (ConnectionCoreConstants.PREFERENCE_SEARCHLOGS_FILE_COUNT.equals(event.getProperty())
                            || ConnectionCoreConstants.PREFERENCE_SEARCHLOGS_FILE_SIZE
                                    .equals(event.getProperty())) {
                        // dispose all loggers/handlers
                        for (Logger logger : loggers.values()) {
                            for (Handler handler : logger.getHandlers()) {
                                handler.close();
                            }
                        }

                        // delete files with index greater than new file count
                        for (FileHandler fh : fileHandlers.values()) {
                            try {
                                File[] logFiles = getLogFiles(fh);
                                for (int i = getFileCount(); i < logFiles.length; i++) {
                                    if (logFiles[i] != null && logFiles[i].exists()) {
                                        logFiles[i].delete();
                                    }
                                }
                            } catch (Exception e) {
                            }
                        }

                        loggers.clear();
                    }
                }
            });
}

From source file:org.apache.directory.studio.connection.core.io.jndi.LdifSearchLogger.java

/**
 * Disposes the search logger of the given connection.
 * /*ww  w .  j  av a 2  s. co  m*/
 * @param connection the connection
 */
public void dispose(Connection connection) {
    String id = connection.getId();
    if (loggers.containsKey(id)) {
        Handler[] handlers = loggers.get(id).getHandlers();
        for (Handler handler : handlers) {
            handler.close();
        }

        loggers.remove(id);
    }
}

From source file:org.hillview.utils.HillviewLogger.java

@SuppressWarnings("unused")
public void shutdown() {
    Handler[] hs = this.logger.getHandlers();
    for (Handler h : hs)
        h.close();
}

From source file:org.kalypso.model.hydrology.internal.test.OptimizeTest.java

private void disconnectLogger(final Logger logger) {
    final Handler[] handlers = logger.getHandlers();
    for (final Handler handler : handlers) {
        handler.close();
        logger.removeHandler(handler);/*from   www. ja  v a 2s.co m*/
    }
}

From source file:org.tros.utils.logging.Logging.java

public static void initLogging(BuildInfo binfo, Class init) {
    try {//from www.jav a2  s  .  co  m
        //hack to get this logger to shut up
        Class<?> forName = Class.forName("org.reflections.Reflections");
        Field f = forName.getField("log");
        f.set(null, null);
    } catch (ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException
            | IllegalAccessException ex) {
        Logger.getLogger(Logging.class.getName()).log(Level.FINEST,
                "org.reflections.Reflections not in CLASSPATH...");
    }

    //make logs directory
    getLogDirectory(binfo);

    //init logger
    String dir = getApplicationEtcDirectory(binfo) + "/logging.properties";
    File logProp = new File(dir);
    if (!logProp.exists()) {
        copyFile(binfo, init, logProp);
    }

    if (logProp.exists()) {
        loadFile(logProp);
    }

    LogManager lm = LogManager.getLogManager();
    String property = lm.getProperty("version");
    if (property == null || !property.equals(binfo.getVersion().replace("-SNAPSHOT", ""))) {
        //backup old file
        File backup = new File(logProp.getAbsolutePath() + "." + (property == null ? "old" : property));
        logProp.renameTo(backup);
        //copy new file
        copyFile(binfo, init, logProp);
        //re-load new file
        loadFile(logProp);
    }

    //Small hack to close SwingComponentHandler which should only be used by a GUI
    //however, if the logging.properties file is already set with this handler, remove
    //it and then the GUI will manually re-add it in the LogConsole constructor.
    Logger logger = Logger.getLogger("");
    try {
        Class<?> swingLogger = Class.forName("org.tros.utils.logging.SwingComponentHandler");
        for (Handler h : logger.getHandlers()) {
            if (swingLogger.isAssignableFrom(h.getClass())) {
                logger.removeHandler(h);
                h.close();
            }
        }
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Logging.class.getName()).log(Level.SEVERE, null, ex);
    }
}