77
88import org .eclipse .microprofile .config .inject .ConfigProperty ;
99
10+ import jakarta .annotation .PreDestroy ;
1011import jakarta .enterprise .context .ApplicationScoped ;
1112import jakarta .inject .Inject ;
1213import jakarta .xml .soap .MessageFactory ;
3031@ ApplicationScoped
3132public class MyFactorySessionManager {
3233
33- private static final Logger LOGGER = Logger .getLogger (MyFactorySessionManager .class .getName ());
34+ private static final Logger logger = Logger .getLogger (MyFactorySessionManager .class .getName ());
3435
3536 // Correct namespace from WSDL
3637 private static final String SOAP_NAMESPACE = "http://www.myfactory.de/" ;
@@ -55,6 +56,12 @@ public class MyFactorySessionManager {
5556 @ ConfigProperty (name = "myfactory.division" , defaultValue = "1" )
5657 private int division ;
5758
59+ @ Inject
60+ @ ConfigProperty (name = "myfactory.session.timeout.minutes" , defaultValue = "5" )
61+ private int sessionTimeoutMinutes ;
62+
63+ private volatile long lastAccessTime ;
64+
5865 // Thread-safe session storage
5966 private volatile String currentClientId ;
6067 private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock ();
@@ -68,15 +75,33 @@ public MyFactorySessionManager() {
6875 /**
6976 * Optionaler Konstruktor für JUnit / Standalone
7077 */
71- public MyFactorySessionManager (String endpointUrl , String username , String password , String database ,
78+ public MyFactorySessionManager (String endpointUrl , int sessionTimeoutMinutes , String username , String password ,
79+ String database ,
7280 int division ) {
7381 this .endpointUrl = endpointUrl ;
82+ this .sessionTimeoutMinutes = sessionTimeoutMinutes ;
7483 this .username = username ;
7584 this .password = password ;
7685 this .database = Optional .ofNullable (database );
7786 this .division = division ;
7887 }
7988
89+ /**
90+ * Cleanup on application shutdown.
91+ * Ensures the MyFactory session is properly closed when the server stops.
92+ */
93+ @ PreDestroy
94+ void destroy () {
95+ if (currentClientId != null ) {
96+ logger .info ("├── Application shutting down, logging out from MyFactory..." );
97+ try {
98+ logout ();
99+ } catch (MyFactorySessionException e ) {
100+ logger .warning ("├── Logout during shutdown failed: " + e .getMessage ());
101+ }
102+ }
103+ }
104+
80105 public String getEndpointUrl () {
81106 return endpointUrl ;
82107 }
@@ -88,17 +113,33 @@ public String getEndpointUrl() {
88113 * @throws MyFactorySessionException if login fails
89114 */
90115 public String getClientId () throws MyFactorySessionException {
91- lock .readLock ().lock ();
116+ lock .writeLock ().lock ();
92117 try {
118+ // Session expired?
119+ if (currentClientId != null && isSessionExpired ()) {
120+ logger .info ("├── Session expired, logging out..." );
121+ logout ();
122+ currentClientId = null ;
123+ }
124+
125+ // do we have a login?
93126 if (currentClientId == null ) {
94- throw new MyFactorySessionException ("Not logged in. Call login() first." );
127+ currentClientId = login ();
128+ logger .info ("Login successful. ClientID: " + currentClientId );
95129 }
130+
131+ // update Timestamp
132+ lastAccessTime = System .currentTimeMillis ();
96133 return currentClientId ;
97134 } finally {
98- lock .readLock ().unlock ();
135+ lock .writeLock ().unlock ();
99136 }
100137 }
101138
139+ private boolean isSessionExpired () {
140+ return System .currentTimeMillis () - lastAccessTime > (sessionTimeoutMinutes * 60 * 1000L );
141+ }
142+
102143 /**
103144 * Login and get a client ID for subsequent API calls.
104145 *
@@ -107,14 +148,18 @@ public String getClientId() throws MyFactorySessionException {
107148 */
108149 public String login () throws MyFactorySessionException {
109150 lock .writeLock ().lock ();
151+ logger .info ("├── MyFactory API Login..." );
110152 try {
111153 // Check if already logged in
112154 if (currentClientId != null ) {
113- LOGGER .warning ("Already logged in with clientID: " + currentClientId );
155+ logger .warning ("├── Already logged in with clientID: " + currentClientId );
114156 return currentClientId ;
115157 }
116158
117- LOGGER .info ("Logging in to MyFactory API as user: " + username );
159+ logger .info ("│ ├── user: " + username );
160+ logger .info ("│ ├── endpointUrl: " + endpointUrl );
161+ logger .info ("│ ├── database: " + database .orElse ("" ));
162+ logger .info ("│ ├── division: " + division );
118163
119164 SOAPConnection soapConnection = null ;
120165 try {
@@ -124,14 +169,14 @@ public String login() throws MyFactorySessionException {
124169 SOAPMessage loginRequest = createLoginRequest ();
125170
126171 // Debug output
127- if (LOGGER .isLoggable (java .util .logging .Level .FINE )) {
172+ if (logger .isLoggable (java .util .logging .Level .FINE )) {
128173 logSoapMessage ("Login Request" , loginRequest );
129174 }
130175
131176 SOAPMessage loginResponse = soapConnection .call (loginRequest , endpointUrl );
132177
133178 // Debug output
134- if (LOGGER .isLoggable (java .util .logging .Level .FINE )) {
179+ if (logger .isLoggable (java .util .logging .Level .FINE )) {
135180 logSoapMessage ("Login Response" , loginResponse );
136181 }
137182
@@ -142,19 +187,19 @@ public String login() throws MyFactorySessionException {
142187 }
143188
144189 this .currentClientId = clientId ;
145- LOGGER .info ("Login successful. ClientID: " + clientId );
190+ logger .info ("├── Login successful. ClientID: " + clientId );
146191
147192 return clientId ;
148193
149194 } catch (SOAPException e ) {
150- LOGGER .severe ("SOAP error during login: " + e .getMessage ());
195+ logger .severe ("├── SOAP error during login: " + e .getMessage ());
151196 throw new MyFactorySessionException ("Login failed" , e );
152197 } finally {
153198 if (soapConnection != null ) {
154199 try {
155200 soapConnection .close ();
156201 } catch (SOAPException e ) {
157- LOGGER .warning ("Failed to close SOAP connection: " + e .getMessage ());
202+ logger .warning ("├── Failed to close SOAP connection: " + e .getMessage ());
158203 }
159204 }
160205 }
@@ -174,12 +219,12 @@ public void logout() throws MyFactorySessionException {
174219 lock .writeLock ().lock ();
175220 try {
176221 if (currentClientId == null ) {
177- LOGGER .warning ("No active session to logout" );
222+ logger .warning ("└── No active session to logout" );
178223 return ;
179224 }
180225
181226 String clientIdToLogout = currentClientId ;
182- LOGGER .info ("Logging out from MyFactory API. ClientID: " + clientIdToLogout );
227+ logger .info ("├── Logging out from MyFactory API. ClientID: " + clientIdToLogout );
183228
184229 SOAPConnection soapConnection = null ;
185230 try {
@@ -189,25 +234,25 @@ public void logout() throws MyFactorySessionException {
189234 SOAPMessage logoutRequest = createLogoutRequest (clientIdToLogout );
190235
191236 // Debug output
192- if (LOGGER .isLoggable (java .util .logging .Level .FINE )) {
237+ if (logger .isLoggable (java .util .logging .Level .FINE )) {
193238 logSoapMessage ("Logout Request" , logoutRequest );
194239 }
195240
196241 SOAPMessage logoutResponse = soapConnection .call (logoutRequest , endpointUrl );
197242
198243 // Debug output
199- if (LOGGER .isLoggable (java .util .logging .Level .FINE )) {
244+ if (logger .isLoggable (java .util .logging .Level .FINE )) {
200245 logSoapMessage ("Logout Response" , logoutResponse );
201246 }
202247
203248 // Check if logout was successful
204249 checkLogoutResponse (logoutResponse );
205250
206251 this .currentClientId = null ;
207- LOGGER .info ("Logout successful" );
252+ logger .info ("└── Logout successful" );
208253
209254 } catch (SOAPException e ) {
210- LOGGER .severe ("SOAP error during logout: " + e .getMessage ());
255+ logger .severe ("SOAP error during logout: " + e .getMessage ());
211256 // Still clear the client ID to avoid stuck sessions
212257 this .currentClientId = null ;
213258 throw new MyFactorySessionException ("Logout failed" , e );
@@ -216,7 +261,7 @@ public void logout() throws MyFactorySessionException {
216261 try {
217262 soapConnection .close ();
218263 } catch (SOAPException e ) {
219- LOGGER .warning ("Failed to close SOAP connection: " + e .getMessage ());
264+ logger .warning ("Failed to close SOAP connection: " + e .getMessage ());
220265 }
221266 }
222267 }
@@ -244,7 +289,7 @@ public boolean isLoggedIn() {
244289 public void clearSession () {
245290 lock .writeLock ().lock ();
246291 try {
247- LOGGER .warning ("Forcefully clearing session. ClientID was: " + currentClientId );
292+ logger .warning ("Forcefully clearing session. ClientID was: " + currentClientId );
248293 this .currentClientId = null ;
249294 } finally {
250295 lock .writeLock ().unlock ();
@@ -341,7 +386,7 @@ private String extractClientId(SOAPMessage response) throws SOAPException, MyFac
341386 if (responseBody .hasFault ()) {
342387 SOAPFault fault = responseBody .getFault ();
343388 String faultString = fault .getFaultString ();
344- LOGGER .severe ("Login SOAP Fault: " + faultString );
389+ logger .severe ("Login SOAP Fault: " + faultString );
345390 throw new MyFactorySessionException ("Login failed: " + faultString );
346391 }
347392
@@ -356,7 +401,7 @@ private String extractClientId(SOAPMessage response) throws SOAPException, MyFac
356401 if (errorNodes .getLength () > 0 ) {
357402 String error = errorNodes .item (0 ).getTextContent ();
358403 if (error != null && !error .trim ().isEmpty ()) {
359- LOGGER .severe ("Login error from API: " + error );
404+ logger .severe ("Login error from API: " + error );
360405 throw new MyFactorySessionException ("Login failed: " + error );
361406 }
362407 }
@@ -376,7 +421,7 @@ private void checkLogoutResponse(SOAPMessage response) throws SOAPException, MyF
376421 if (responseBody .hasFault ()) {
377422 SOAPFault fault = responseBody .getFault ();
378423 String faultString = fault .getFaultString ();
379- LOGGER .severe ("Logout SOAP Fault: " + faultString );
424+ logger .severe ("Logout SOAP Fault: " + faultString );
380425 throw new MyFactorySessionException ("Logout failed: " + faultString );
381426 }
382427
@@ -386,7 +431,7 @@ private void checkLogoutResponse(SOAPMessage response) throws SOAPException, MyF
386431 if (nodes .getLength () > 0 ) {
387432 String result = nodes .item (0 ).getTextContent ();
388433 if (!"true" .equalsIgnoreCase (result )) {
389- LOGGER .warning ("Logout returned false" );
434+ logger .warning ("Logout returned false" );
390435 }
391436 }
392437 }
@@ -397,9 +442,9 @@ private void checkLogoutResponse(SOAPMessage response) throws SOAPException, MyF
397442 private void logSoapMessage (String label , SOAPMessage message ) {
398443 try (ByteArrayOutputStream out = new ByteArrayOutputStream ()) {
399444 message .writeTo (out );
400- LOGGER .fine (label + ":\n " + out .toString ("UTF-8" ));
445+ logger .fine (label + ":\n " + out .toString ("UTF-8" ));
401446 } catch (Exception e ) {
402- LOGGER .warning ("Failed to log SOAP message: " + e .getMessage ());
447+ logger .warning ("Failed to log SOAP message: " + e .getMessage ());
403448 }
404449 }
405450
0 commit comments