]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsStateImpl.java
7b47b520d580fa602dda48c663f94ec493a9bb1c
1 package org
.argeo
.cms
.internal
.runtime
;
3 import java
.net
.InetAddress
;
5 import java
.net
.UnknownHostException
;
6 import java
.util
.ArrayList
;
9 import javax
.security
.auth
.login
.Configuration
;
11 import org
.argeo
.api
.cms
.CmsLog
;
12 import org
.argeo
.api
.cms
.CmsState
;
13 import org
.argeo
.cms
.auth
.ident
.IdentClient
;
14 import org
.argeo
.cms
.internal
.osgi
.CmsShutdown
;
15 import org
.osgi
.framework
.Constants
;
18 * Implementation of a {@link CmsState}, initialising the required services.
20 public class CmsStateImpl
implements CmsState
{
21 private final static CmsLog log
= CmsLog
.getLog(CmsStateImpl
.class);
22 // private final BundleContext bc = FrameworkUtil.getBundle(CmsState.class).getBundleContext();
24 // private static CmsStateImpl instance;
26 // private ExecutorService internalExecutorService;
29 private Long availableSince
;
32 // private ThreadGroup threadGroup = new ThreadGroup("CMS");
33 private List
<Runnable
> stopHooks
= new ArrayList
<>();
35 private String stateUuid
;
36 // private final boolean cleanState;
37 private String hostname
;
42 Runtime
.getRuntime().addShutdownHook(new CmsShutdown());
43 // this.internalExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
50 if (log
.isTraceEnabled())
51 log
.trace("CMS State started");
52 } catch (Throwable e
) {
53 log
.error("## FATAL: CMS activator failed", e
);
56 this.stateUuid
= KernelUtils
.getFrameworkProp(Constants
.FRAMEWORK_UUID
);
57 // this.cleanState = stateUuid.equals(frameworkUuid);
59 this.hostname
= InetAddress
.getLocalHost().getHostName();
60 } catch (UnknownHostException e
) {
61 log
.error("Cannot set hostname: " + e
);
64 availableSince
= System
.currentTimeMillis();
65 if (log
.isDebugEnabled())
66 // log.debug("## CMS starting... stateUuid=" + this.stateUuid + (cleanState ? "
67 // (clean state) " : " "));
68 log
.debug("## CMS starting... (" + stateUuid
+ ")");
75 private void initSecurity() {
76 if (System
.getProperty(KernelConstants
.JAAS_CONFIG_PROP
) == null) {
77 String jaasConfig
= KernelConstants
.JAAS_CONFIG
;
78 URL url
= getClass().getResource(jaasConfig
);
79 // System.setProperty(KernelConstants.JAAS_CONFIG_PROP,
80 // url.toExternalForm());
81 KernelUtils
.setJaasConfiguration(url
);
83 // explicitly load JAAS configuration
84 Configuration
.getConfiguration();
87 // private void initI18n() {
88 // Object defaultLocaleValue = KernelUtils.getFrameworkProp(CmsConstants.I18N_DEFAULT_LOCALE);
89 // defaultLocale = defaultLocaleValue != null ? new Locale(defaultLocaleValue.toString())
90 // : new Locale(ENGLISH.getLanguage());
91 // locales = LocaleUtils.asLocaleList(KernelUtils.getFrameworkProp(CmsConstants.I18N_LOCALES));
94 private void initServices() {
96 // String tmType = KernelUtils.getFrameworkProp(CmsConstants.TRANSACTION_MANAGER,
97 // CmsConstants.TRANSACTION_MANAGER_SIMPLE);
98 // if (CmsConstants.TRANSACTION_MANAGER_SIMPLE.equals(tmType)) {
99 // initSimpleTransactionManager();
100 // } else if (CmsConstants.TRANSACTION_MANAGER_BITRONIX.equals(tmType)) {
101 //// initBitronixTransactionManager();
102 // throw new UnsupportedOperationException(
103 // "Bitronix is not supported anymore, but could be again if there is enough interest.");
105 // throw new IllegalArgumentException("Usupported transaction manager type " + tmType);
109 // POIXMLTypeLoader.setClassLoader(CTConnection.class.getClassLoader());
112 // OpenDocumentParser odfParser = new OpenDocumentParser();
113 // bc.registerService(Parser.class, odfParser, new Hashtable());
114 // PDFParser pdfParser = new PDFParser();
115 // bc.registerService(Parser.class, pdfParser, new Hashtable());
116 // OOXMLParser ooxmlParser = new OOXMLParser();
117 // bc.registerService(Parser.class, ooxmlParser, new Hashtable());
118 // TesseractOCRParser ocrParser = new TesseractOCRParser();
119 // ocrParser.setLanguage("ara");
120 // bc.registerService(Parser.class, ocrParser, new Hashtable());
123 // RepositoryServiceFactory repositoryServiceFactory = new RepositoryServiceFactory();
124 // stopHooks.add(() -> repositoryServiceFactory.shutdown());
125 // Activator.registerService(ManagedServiceFactory.class, repositoryServiceFactory,
126 // LangUtils.dict(Constants.SERVICE_PID, NodeConstants.NODE_REPOS_FACTORY_PID));
128 // NodeRepositoryFactory repositoryFactory = new NodeRepositoryFactory();
129 // Activator.registerService(RepositoryFactory.class, repositoryFactory, null);
132 // NodeUserAdmin userAdmin = new NodeUserAdmin(CmsConstants.ROLES_BASEDN, CmsConstants.TOKENS_BASEDN);
133 // stopHooks.add(() -> userAdmin.destroy());
134 // Activator.registerService(ManagedServiceFactory.class, userAdmin,
135 // LangUtils.dict(Constants.SERVICE_PID, CmsConstants.NODE_USER_ADMIN_PID));
139 // private void initSimpleTransactionManager() {
140 // SimpleTransactionManager transactionManager = new SimpleTransactionManager();
141 // Activator.registerService(WorkControl.class, transactionManager, null);
142 // Activator.registerService(WorkTransaction.class, transactionManager, null);
143 //// Activator.registerService(TransactionManager.class, transactionManager, null);
144 //// Activator.registerService(UserTransaction.class, transactionManager, null);
145 // // TODO TransactionSynchronizationRegistry
148 // private void initBitronixTransactionManager() {
149 // // TODO manage it in a managed service, as startup could be long
150 // ServiceReference<TransactionManager> existingTm = bc.getServiceReference(TransactionManager.class);
151 // if (existingTm != null) {
152 // if (log.isDebugEnabled())
153 // log.debug("Using provided transaction manager " + existingTm);
157 // if (!TransactionManagerServices.isTransactionManagerRunning()) {
158 // bitronix.tm.Configuration tmConf = TransactionManagerServices.getConfiguration();
159 // tmConf.setServerId(UUID.randomUUID().toString());
161 // Bundle bitronixBundle = FrameworkUtil.getBundle(bitronix.tm.Configuration.class);
162 // File tmBaseDir = bitronixBundle.getDataFile(KernelConstants.DIR_TRANSACTIONS);
163 // File tmDir1 = new File(tmBaseDir, "btm1");
165 // tmConf.setLogPart1Filename(new File(tmDir1, tmDir1.getName() + ".tlog").getAbsolutePath());
166 // File tmDir2 = new File(tmBaseDir, "btm2");
168 // tmConf.setLogPart2Filename(new File(tmDir2, tmDir2.getName() + ".tlog").getAbsolutePath());
170 // BitronixTransactionManager transactionManager = getTransactionManager();
171 // stopHooks.add(() -> transactionManager.shutdown());
172 // BitronixTransactionSynchronizationRegistry transactionSynchronizationRegistry = getTransactionSynchronizationRegistry();
174 // bc.registerService(TransactionManager.class, transactionManager, null);
175 // bc.registerService(UserTransaction.class, transactionManager, null);
176 // bc.registerService(TransactionSynchronizationRegistry.class, transactionSynchronizationRegistry, null);
177 // if (log.isDebugEnabled())
178 // log.debug("Initialised default Bitronix transaction manager");
181 public void destroy() {
182 if (log
.isDebugEnabled())
183 log
.debug("CMS stopping... (" + this.stateUuid
+ ")");
185 // In a different thread in order to avoid interruptions
186 Thread stopHookThread
= new Thread(() -> applyStopHooks(), "Apply Argeo Stop Hooks");
187 stopHookThread
.start();
189 stopHookThread
.join(10 * 60 * 1000);
190 } catch (InterruptedException e
) {
194 // internalExecutorService.shutdown();
196 long duration
= ((System
.currentTimeMillis() - availableSince
) / 1000) / 60;
197 log
.info("## ARGEO CMS STOPPED after " + (duration
/ 60) + "h " + (duration
% 60) + "min uptime ##");
200 /** Apply shutdown hoos in reverse order. */
201 private void applyStopHooks() {
202 for (int i
= stopHooks
.size() - 1; i
>= 0; i
--) {
204 stopHooks
.get(i
).run();
205 } catch (Exception e
) {
206 log
.error("Could not run shutdown hook #" + i
);
209 // Clean hanging Gogo shell thread
210 new GogoShellKiller().start();
216 // public boolean isClean() {
217 // return cleanState;
221 public Long
getAvailableSince() {
222 return availableSince
;
228 public String
getHostname() {
235 public static IdentClient
getIdentClient(String remoteAddr
) {
236 if (!IdentClient
.isDefaultAuthdPassphraseFileAvailable())
238 // TODO make passphrase more configurable
239 return new IdentClient(remoteAddr
);