1 package com.acumenvelocity.ath.common;
2
3 import javax.servlet.ServletContextEvent;
4 import javax.servlet.ServletContextListener;
5
6 import com.acumenvelocity.ath.controller.DocumentController;
7 import com.acumenvelocity.ath.gcs.AthStorage;
8 import com.acumenvelocity.ath.gct.v3.AthTranslation;
9 import com.acumenvelocity.ath.gemini.GenAi;
10 import com.acumenvelocity.ath.solr.AthIndex;
11
12 /**
13 * ServletContextListener implementation for the ATH (Acumen Velocity Translation Hub) application.
14 * This listener handles servlet context lifecycle events, allowing for initialization and cleanup
15 * operations when the web application starts up or shuts down.
16 *
17 * <p>
18 * This class provides hooks for:
19 * <ul>
20 * <li>Application startup initialization (contextInitialized)</li>
21 * <li>Application shutdown cleanup (contextDestroyed)</li>
22 * </ul>
23 *
24 * @author Acumen Velocity
25 * @version 1.0
26 * @since 1.0
27 */
28 public class AthServletContextListener implements ServletContextListener {
29
30 /**
31 * Called when the servlet context is initialized (i.e., when the web application starts).
32 * This method can be used to perform startup tasks such as:
33 * <ul>
34 * <li>Initializing application-wide resources</li>
35 * <li>Setting up database connections</li>
36 * <li>Loading configuration data</li>
37 * <li>Starting background services</li>
38 * </ul>
39 *
40 * @param sce the ServletContextEvent containing the ServletContext that was initialized
41 */
42 @Override
43 public void contextInitialized(ServletContextEvent sce) {
44 try {
45 EnvVarsChecker.init();
46
47 } catch (Exception e) {
48 Log.error(getClass(), e, "Env vars initialization error");
49 }
50
51 try {
52 AthIndex.init();
53
54 } catch (Exception e) {
55 Log.error(getClass(), e, "Solr initialization error");
56 }
57
58 try {
59 AthStorage.init();
60
61 } catch (Exception e) {
62 Log.error(getClass(), e, "GCS initialization error");
63 }
64
65 try {
66 PdfUtil.init();
67
68 } catch (Exception e) {
69 Log.error(getClass(), e, "Adobe PDF services initialization error");
70 }
71
72 // try {
73 // OpenAi.init();
74 //
75 // } catch (Exception e) {
76 // Log.error(getClass(), e, "OpenAI API client initialization error");
77 // }
78
79 try {
80 GenAi.init();
81
82 } catch (Exception e) {
83 Log.error(getClass(), e, "Gemini API client initialization error");
84 }
85
86 try {
87 AthTranslation.init();
88
89 } catch (Exception e) {
90 Log.error(getClass(), e, "Google Cloud Translate v3 API client initialization error");
91 }
92 }
93
94 /**
95 * Called when the servlet context is destroyed (i.e., when the web application shuts down).
96 * This method can be used to perform cleanup tasks such as:
97 * <ul>
98 * <li>Closing database connections</li>
99 * <li>Shutting down thread pools</li>
100 * <li>Releasing system resources</li>
101 * <li>Persisting application state</li>
102 * </ul>
103 *
104 * @param sce the ServletContextEvent containing the ServletContext that is being destroyed
105 */
106 @Override
107 public void contextDestroyed(ServletContextEvent sce) {
108 // try {
109 // OpenAi.done();
110 //
111 // } catch (Exception e) {
112 // Log.error(getClass(), e, "OpenAI API client shutdown error");
113 // }
114
115 try {
116 AthTranslation.done();
117
118 } catch (Exception e) {
119 Log.error(getClass(), e, "Google Cloud Translate v3 API client shutdown error");
120 }
121
122 try {
123 GenAi.done();
124
125 } catch (Exception e) {
126 Log.error(getClass(), e, "Gemini API client shutdown error");
127 }
128
129 try {
130 DocumentController.shutdownExecutor();
131 AthIndex.done();
132
133 } catch (Exception e) {
134 Log.error(getClass(), e, "Solr shutdown error");
135 }
136 }
137
138 }