Skip to contentPackage: SpringContextHelper
SpringContextHelper
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.headlessservice.impl;
28:
29: import java.util.ArrayList;
30: import java.util.List;
31: import java.util.Map;
32: import java.util.SortedMap;
33: import java.util.TreeMap;
34: import org.springframework.context.support.ClassPathXmlApplicationContext;
35: import org.slf4j.Logger;
36: import org.slf4j.LoggerFactory;
37: import lombok.Getter;
38:
39: /***********************************************************************************************************************
40: *
41: * FIXME: partially copied from JavaFXSpringApplication.
42: *
43: * @author Fabrizio Giudici
44: *
45: **********************************************************************************************************************/
46: public class SpringContextHelper
47: {
48: // Don't use Slf4j and its static logger - give Main a chance to initialize things
49: private final Logger log = LoggerFactory.getLogger(SpringContextHelper.class);
50:
51: @Getter
52: private ClassPathXmlApplicationContext applicationContext;
53:
54: private final List<String> springConfigLocations = new ArrayList<>();
55:
56: /*******************************************************************************************************************
57: *
58: * FIXME: this is duplicated in JavaFXSpringApplication
59: *
60: ******************************************************************************************************************/
61: protected void initialize()
62: {
63: try
64: {
65: logProperties();
66: // TODO: workaround for NWRCA-41
67: System.setProperty("it.tidalwave.util.spring.ClassScanner.basePackages", "it");
68:
69: springConfigLocations.add("classpath*:/META-INF/*AutoBeans.xml");
70: final String osName = System.getProperty("os.name", "").toLowerCase();
71:
72:• if (osName.contains("os x"))
73: {
74: springConfigLocations.add("classpath*:/META-INF/*AutoMacOSXBeans.xml");
75: }
76:
77:• if (osName.contains("linux"))
78: {
79: springConfigLocations.add("classpath*:/META-INF/*AutoLinuxBeans.xml");
80: }
81:
82:• if (osName.contains("windows"))
83: {
84: springConfigLocations.add("classpath*:/META-INF/*AutoWindowsBeans.xml");
85: }
86:
87: log.info("Loading Spring configuration from {} ...", springConfigLocations);
88: applicationContext = new ClassPathXmlApplicationContext(springConfigLocations.toArray(new String[0]));
89: applicationContext.registerShutdownHook(); // this actually seems not working, onClosing() does
90: }
91: catch (Throwable t)
92: {
93: log.error("", t);
94: }
95: }
96:
97: /*******************************************************************************************************************
98: *
99: *
100: *
101: ******************************************************************************************************************/
102: private void logProperties()
103: {
104: final SortedMap<Object, Object> map = new TreeMap<>(System.getProperties());
105:
106:• for (final Map.Entry<Object, Object> e : map.entrySet())
107: {
108: log.debug("{}: {}", e.getKey(), e.getValue());
109: }
110: }
111: }