package com.its.vds; import com.its.app.AppUtils; import com.its.app.utils.OS; import com.its.app.utils.SysUtils; import com.its.vds.config.ApplicationConfig; import com.its.vds.entity.TbUnitSyst; import com.its.vds.process.DbmsDataProcess; import com.its.vds.service.UnitSystService; import com.its.vds.service.VdsCtlrService; import com.its.vds.ui.JTextAreaOutputStream; import com.its.vds.ui.MainUI; import com.its.vds.xnettcp.center.CenterTcpServerService; import com.its.vds.xnettcp.vds.VdsTcpClientCommService; import com.its.vds.xnettcp.vds.process.VdsDataProcess; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.Banner; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.ApplicationPidFileWriter; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextClosedEvent; import org.springframework.core.io.ClassPathResource; import org.springframework.scheduling.annotation.EnableAsync; import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.lang.reflect.Field; import java.net.URL; import java.nio.charset.Charset; @Slf4j @EnableAsync @Configuration @SpringBootApplication @ComponentScan(basePackages = {"com.its.vds.config", "com.its.vds.dao.mapper", "com.its"}) public class VdsCommServerApplication implements CommandLineRunner, ApplicationListener, InitializingBean, DisposableBean { private static String applicationName = "vds-comm-server"; public static void main(String[] args) { System.setProperty("file.encoding","UTF-8"); try { Field charset = Charset.class.getDeclaredField("defaultCharset"); charset.setAccessible(true); charset.set(null,null); } catch(Exception e){ } File file1 = new File("./conf/" + applicationName + ".pid"); if (file1.exists()) { System.out.println(System.getProperty("Program Already Running.....")); log.error("Program Already Running....."); } if (OS.isWindows()) { ApplicationContext context = new SpringApplicationBuilder(VdsCommServerApplication.class) //.web(WebApplicationType.NONE) .listeners(new ApplicationPidFileWriter("./conf/" + applicationName + ".pid")) .headless(false) .bannerMode(Banner.Mode.OFF) .run(args); } else { SpringApplication application = new SpringApplicationBuilder() .sources(VdsCommServerApplication.class) .listeners(new ApplicationPidFileWriter("./conf/" + applicationName + ".pid")) .build(); application.setBannerMode(Banner.Mode.OFF); application.run(args); } } @Override public void run(String... args) throws Exception { if (OS.isWindows()) { SwingUtilities.invokeLater(() -> { String sysTime = SysUtils.getSysTimeStr(); //JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("VDS 통신 서버 - [" + sysTime + "]"); MainUI UI = new MainUI(frame); try { ClassPathResource file = new ClassPathResource("static/image/application.png"); URL imgURL = file.getURL(); frame.setIconImage(new ImageIcon(imgURL).getImage()); } catch (IOException e) { log.error("Not found application icon image"); } frame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE); frame.setContentPane(UI.getRootPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { if (JOptionPane.showConfirmDialog(UI.getRootPanel(), "시스템을 종료 하시겠습니까?", "시스템 종료", 0) == 0) { System.exit(0); } else { frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); } } }); frame.pack(); frame.setBounds(100, 100, 1000, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); JTextArea logArea = UI.getTaLog(); logArea.setText(null); JTextAreaOutputStream out = new JTextAreaOutputStream(logArea); System.setOut(new PrintStream(out)); }); } ApplicationConfig applicationConfig = (ApplicationConfig) AppUtils.getBean(ApplicationConfig.class); log.info(""); log.info(""); log.info("************************************************************************************"); log.info("** **"); log.info("** Intelligent Traffic System **"); log.info("** VDS Communication Server Program. **"); log.info("** **"); log.info("** [ver.1.0] **"); log.info("** {}", applicationConfig.getId()); log.info("** startup: {}", applicationConfig.getBootingDateTime()); log.info("************************************************************************************"); // init application DbmsDataProcess dbmsDataProcess = (DbmsDataProcess)AppUtils.getBean(DbmsDataProcess.class); dbmsDataProcess.run(); VdsDataProcess tcpServerDataProcess = (VdsDataProcess)AppUtils.getBean(VdsDataProcess.class); tcpServerDataProcess.run(); UnitSystService unitSystService = (UnitSystService)AppUtils.getBean(UnitSystService.class); unitSystService.loadMaster(); unitSystService.updateUnitSystStts(true); TbUnitSyst unit = unitSystService.getUnitSystMap().get(applicationConfig.getId()); if (unit != null) { applicationConfig.setListenPort(unit.getPRGM_PORT()); } VdsCtlrService ctlrService = (VdsCtlrService)AppUtils.getBean(VdsCtlrService.class); ctlrService.loadDb(); ctlrService.updateCtlrStts(true); VdsTcpClientCommService vdsCommClientService = (VdsTcpClientCommService)AppUtils.getBean(VdsTcpClientCommService.class); vdsCommClientService.run(); CenterTcpServerService centerService = (CenterTcpServerService)AppUtils.getBean(CenterTcpServerService.class); centerService.run(); // schedule enable applicationConfig.setStartSchedule(true); if (OS.isWindows()) { MainUI UI = MainUI.getInstance(); if (UI != null) { UI.LoadControllerInfo(); } } } public void terminateApplication() { UnitSystService unitSystService = (UnitSystService) AppUtils.getBean(UnitSystService.class); unitSystService.updateUnitSystStts(false); VdsCtlrService ctlrService = (VdsCtlrService) AppUtils.getBean(VdsCtlrService.class); ctlrService.updateCtlrStts(false); CenterTcpServerService centerService = (CenterTcpServerService)AppUtils.getBean(CenterTcpServerService.class); centerService.stop(); VdsTcpClientCommService vdsCommClientService = (VdsTcpClientCommService)AppUtils.getBean(VdsTcpClientCommService.class); vdsCommClientService.shutdown(); } @Override public void onApplicationEvent(ContextClosedEvent event) { log.error("Application Terminated: {}", event.getTimestamp()); terminateApplication(); } @Override public void afterPropertiesSet() throws Exception { //System.err.println("InitializingBean 인터페이스 구현 메서드입니다. 'Bean'이 생성될 때 마다 호출되는 메서드 입니다."); } @Override public void destroy() throws Exception { //System.err.println("DisposableBean 인터페이스 구현 메서드입니다. 'Bean'이 소멸될 때 마다 호출되는 메서드입니다"); } /*@Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { System.out.println("Let's inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } }; }*/ }