18
18
19
19
package org .mycore .common .events ;
20
20
21
+ import java .io .IOException ;
22
+ import java .time .ZoneId ;
23
+ import java .time .format .DateTimeFormatter ;
24
+ import java .util .List ;
25
+ import java .util .Locale ;
26
+ import java .util .jar .JarFile ;
21
27
import java .util .stream .Stream ;
22
28
23
29
import org .apache .logging .log4j .LogManager ;
24
30
import org .apache .logging .log4j .Logger ;
25
31
import org .mycore .common .MCRClassTools ;
32
+ import org .mycore .common .config .MCRComponent ;
26
33
import org .mycore .common .config .MCRConfiguration2 ;
27
34
import org .mycore .common .config .MCRConfigurationDirSetup ;
28
35
import org .mycore .common .config .MCRConfigurationException ;
29
36
import org .mycore .common .config .MCRRuntimeComponentDetector ;
37
+ import org .mycore .common .log .MCRTableMessage ;
30
38
import org .mycore .common .xml .MCRURIResolver ;
31
39
32
40
import jakarta .servlet .ServletContext ;
33
41
42
+ import static jakarta .servlet .ServletContext .ORDERED_LIBS ;
43
+ import static org .mycore .common .config .MCRRuntimeComponentDetector .ComponentOrder .LOWEST_PRIORITY_FIRST ;
44
+
34
45
/**
35
46
* Initializes classes that implement {@link AutoExecutable} interface that are defined via
36
47
* <code>MCR.Startup.Class</code> property.
@@ -56,26 +67,71 @@ public static void startUp(ServletContext servletContext) {
56
67
isWebApp = servletContext != null ;
57
68
//initialize ClassLoader here, so it can be used later reliably.
58
69
MCRClassTools .updateClassLoader ();
70
+
59
71
ClassLoader resourceClassLoader = MCRClassTools .getClassLoader ();
60
72
LOGGER .info ("The following ClassLoader is used: {}" , resourceClassLoader );
61
- LOGGER .info ("I have these components for you: {}" , MCRRuntimeComponentDetector .getAllComponents ());
62
- LOGGER .info ("I have these mycore components for you: {}" , MCRRuntimeComponentDetector .getMyCoReComponents ());
63
- LOGGER .info ("I have these app modules for you: {}" , MCRRuntimeComponentDetector .getApplicationModules ());
73
+
74
+ MCRTableMessage <MCRComponent > componentTable = new MCRTableMessage <>(
75
+ new MCRTableMessage .Column <>("Type" , MCRStartupHandler ::toType ),
76
+ new MCRTableMessage .Column <>("Name" , MCRComponent ::getFullName ),
77
+ new MCRTableMessage .Column <>("Priority" , MCRComponent ::getPriority ),
78
+ new MCRTableMessage .Column <>("Version" , MCRStartupHandler ::toVersion ),
79
+ new MCRTableMessage .Column <>("Build time" , MCRStartupHandler ::toManifestModificationDate ),
80
+ new MCRTableMessage .Column <>("Location" , MCRStartupHandler ::toJarFile ));
81
+ MCRRuntimeComponentDetector .getAllComponents (LOWEST_PRIORITY_FIRST ).forEach (componentTable ::add );
82
+ LOGGER .info (componentTable .logMessage ("Detected components:" ));
83
+
64
84
if (servletContext != null ) {
65
- LOGGER .info ("Library order: {}" , servletContext .getAttribute (ServletContext . ORDERED_LIBS ));
85
+ LOGGER .info ("Library order: {}" , servletContext .getAttribute (ORDERED_LIBS ));
66
86
}
67
87
68
- MCRConfiguration2 .getString ("MCR.Startup.Class" )
88
+ MCRTableMessage <AutoExecutable > executableTable = new MCRTableMessage <>(
89
+ new MCRTableMessage .Column <>("Name" , AutoExecutable ::getName ),
90
+ new MCRTableMessage .Column <>("Priority" , AutoExecutable ::getPriority ),
91
+ new MCRTableMessage .Column <>("Class" , executable -> executable .getClass ().getName ()));
92
+ List <AutoExecutable > executables = MCRConfiguration2 .getString ("MCR.Startup.Class" )
69
93
.map (MCRConfiguration2 ::splitValue )
70
94
.orElseGet (Stream ::empty )
71
95
.map (MCRStartupHandler ::getAutoExecutable )
72
- //reverse ordering: highest priority first
73
- .sorted ((o1 , o2 ) -> Integer .compare (o2 .getPriority (), o1 .getPriority ()))
74
- .forEachOrdered (autoExecutable -> startExecutable (servletContext , autoExecutable ));
96
+ .sorted ()
97
+ .peek (executableTable ::add )
98
+ .toList ();
99
+ LOGGER .info (executableTable .logMessage ("Detected auto executables:" ));
100
+ executables .forEach (autoExecutable -> startExecutable (servletContext , autoExecutable ));
101
+
75
102
//initialize MCRURIResolver
76
103
MCRURIResolver .init (servletContext );
77
104
}
78
105
106
+ private static String toType (MCRComponent component ) {
107
+ if (component .isMyCoReBaseComponent ()) {
108
+ return "MyCoRe base component" ;
109
+ } else if (component .isMyCoReComponent ()) {
110
+ return "MyCoRe component" ;
111
+ } else {
112
+ return "Application module" ;
113
+ }
114
+ }
115
+
116
+ private static Object toVersion (MCRComponent component ) {
117
+ String version = component .getManifestMainAttribute ("Implementation-Version" );
118
+ return version != null ? version : "n/a" ;
119
+ }
120
+
121
+ private static Object toJarFile (MCRComponent component ) {
122
+ return component .getJarFile ().getAbsolutePath ();
123
+ }
124
+
125
+ private static Object toManifestModificationDate (MCRComponent component ) {
126
+ DateTimeFormatter formatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss" , Locale .getDefault ());
127
+ try (JarFile jarFile = new JarFile (component .getJarFile ())) {
128
+ return formatter .format (jarFile .getEntry ("META-INF/MANIFEST.MF" ).getLastModifiedTime ()
129
+ .toInstant ().atZone (ZoneId .systemDefault ()).toLocalDateTime ());
130
+ } catch (IOException e ) {
131
+ return "n/a" ;
132
+ }
133
+ }
134
+
79
135
public static boolean isWebApp () {
80
136
return isWebApp ;
81
137
}
@@ -103,7 +159,7 @@ private static AutoExecutable getAutoExecutable(String className) {
103
159
}
104
160
}
105
161
106
- public interface AutoExecutable {
162
+ public interface AutoExecutable extends Comparable < AutoExecutable > {
107
163
/**
108
164
* returns a name to display on start-up.
109
165
*/
@@ -118,5 +174,11 @@ public interface AutoExecutable {
118
174
* This method get executed by {@link MCRStartupHandler#startUp(ServletContext)}
119
175
*/
120
176
void startUp (ServletContext servletContext );
177
+
178
+ @ Override
179
+ default int compareTo (AutoExecutable other ) {
180
+ return Integer .compare (other .getPriority (), getPriority ());
181
+ }
182
+
121
183
}
122
184
}
0 commit comments