|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package org.semanticwb.swbforms.servlet; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.FileSystems; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.nio.file.StandardWatchEventKinds; |
| 13 | +import java.nio.file.WatchKey; |
| 14 | +import java.nio.file.WatchService; |
| 15 | +import java.util.concurrent.Executors; |
| 16 | +import java.util.concurrent.ScheduledExecutorService; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | +import java.util.logging.Level; |
| 19 | +import java.util.logging.Logger; |
| 20 | +import javax.servlet.ServletContextEvent; |
| 21 | +import javax.servlet.ServletContextListener; |
| 22 | +import javax.servlet.annotation.WebListener; |
| 23 | +import org.semanticwb.datamanager.DataMgr; |
| 24 | +import org.semanticwb.datamanager.SWBScriptEngine; |
| 25 | +import org.semanticwb.datamanager.script.ScriptObject; |
| 26 | +import org.semanticwb.swbforms.servlet.router.Router; |
| 27 | + |
| 28 | +@WebListener |
| 29 | +public class FormsServletContextListener implements ServletContextListener { |
| 30 | + |
| 31 | + static Logger log = Logger.getLogger(FormsServletContextListener.class.getName()); |
| 32 | + ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); |
| 33 | + |
| 34 | + @Override |
| 35 | + public void contextInitialized(ServletContextEvent sce) { |
| 36 | + log.info("Starting SWBForms"); |
| 37 | + DataMgr.createInstance(sce.getServletContext().getRealPath("/")); |
| 38 | + log.info("SWBForms DataMgr Started"); |
| 39 | + |
| 40 | + SWBScriptEngine engine = DataMgr.getUserScriptEngine("/WEB-INF/global.js", null, false); |
| 41 | + log.info("SWBForms SWBScriptEngine Started"); |
| 42 | + |
| 43 | + ScriptObject config = engine.getScriptObject().get("config"); |
| 44 | + if (config != null) { |
| 45 | + String base = config.getString("baseDatasource"); |
| 46 | + if (base != null) { |
| 47 | + DataMgr.getBaseInstance().setBaseDatasourse(base); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + log.info("Configuring Router"); |
| 52 | + ScriptObject ros = engine.getScriptObject().get("routes"); |
| 53 | + Router.initRouter(ros); |
| 54 | + log.info("Router configured"); |
| 55 | + try { |
| 56 | + WatchService wservice = FileSystems.getDefault().newWatchService(); |
| 57 | + Path file = Paths.get(DataMgr.getApplicationPath(), "/WEB-INF/global.js").getParent().toAbsolutePath(); |
| 58 | + WatchKey key = file.register(wservice, StandardWatchEventKinds.ENTRY_MODIFY); |
| 59 | + service.scheduleWithFixedDelay(() -> { |
| 60 | + key.pollEvents().stream().forEach((f) -> { |
| 61 | + if ("global.js".equals(f.context().toString())) { |
| 62 | + SWBScriptEngine engine1 = DataMgr.getUserScriptEngine("/WEB-INF/global.js", null, false); |
| 63 | + ScriptObject ros1 = engine1.getScriptObject().get("routes"); |
| 64 | + Router.initRouter(ros1); |
| 65 | + } |
| 66 | + }); |
| 67 | + }, 10, 10, TimeUnit.SECONDS); |
| 68 | + } catch (IOException | UnsupportedOperationException pex) { |
| 69 | + log.log(Level.SEVERE, "Error Reloading routes from global.js file ", pex); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void contextDestroyed(ServletContextEvent sce) { |
| 75 | + System.out.println("aplicacion web parada"); |
| 76 | + try { |
| 77 | + service.shutdownNow(); |
| 78 | + service.awaitTermination(10, TimeUnit.SECONDS); |
| 79 | + } catch (InterruptedException iex) { |
| 80 | + log.log(Level.SEVERE, "Shootingdown", iex); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments