1 package org
.argeo
.cms
.internal
.kernel
;
3 import static bitronix
.tm
.TransactionManagerServices
.getTransactionManager
;
4 import static bitronix
.tm
.TransactionManagerServices
.getTransactionSynchronizationRegistry
;
5 import static java
.util
.Locale
.ENGLISH
;
8 import java
.lang
.management
.ManagementFactory
;
9 import java
.net
.InetAddress
;
10 import java
.net
.UnknownHostException
;
11 import java
.nio
.file
.spi
.FileSystemProvider
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
14 import java
.util
.Locale
;
15 import java
.util
.UUID
;
17 import javax
.jcr
.RepositoryFactory
;
18 import javax
.transaction
.TransactionManager
;
19 import javax
.transaction
.TransactionSynchronizationRegistry
;
20 import javax
.transaction
.UserTransaction
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
24 import org
.argeo
.cms
.i18n
.LocaleUtils
;
25 import org
.argeo
.node
.NodeConstants
;
26 import org
.argeo
.node
.NodeState
;
27 import org
.argeo
.util
.LangUtils
;
28 import org
.osgi
.framework
.Bundle
;
29 import org
.osgi
.framework
.BundleContext
;
30 import org
.osgi
.framework
.Constants
;
31 import org
.osgi
.framework
.FrameworkUtil
;
32 import org
.osgi
.framework
.ServiceReference
;
33 import org
.osgi
.service
.cm
.ManagedServiceFactory
;
35 import bitronix
.tm
.BitronixTransactionManager
;
36 import bitronix
.tm
.BitronixTransactionSynchronizationRegistry
;
37 import bitronix
.tm
.TransactionManagerServices
;
39 public class CmsState
implements NodeState
{
40 private final Log log
= LogFactory
.getLog(CmsState
.class);
41 private final BundleContext bc
= FrameworkUtil
.getBundle(CmsState
.class).getBundleContext();
44 private Long availableSince
;
47 private Locale defaultLocale
;
48 private List
<Locale
> locales
= null;
50 private ThreadGroup threadGroup
= new ThreadGroup("CMS");
51 private KernelThread kernelThread
;
52 private List
<Runnable
> stopHooks
= new ArrayList
<>();
54 private final String stateUuid
;
55 private final boolean cleanState
;
56 private String hostname
;
58 public CmsState(String stateUuid
) {
59 this.stateUuid
= stateUuid
;
60 String frameworkUuid
= KernelUtils
.getFrameworkProp(Constants
.FRAMEWORK_UUID
);
61 this.cleanState
= stateUuid
.equals(frameworkUuid
);
63 this.hostname
= InetAddress
.getLocalHost().getHostName();
64 } catch (UnknownHostException e
) {
65 log
.error("Cannot set hostname: " + e
);
68 availableSince
= System
.currentTimeMillis();
69 if (log
.isDebugEnabled())
70 log
.debug("## CMS starting... stateUuid=" + this.stateUuid
+ (cleanState ?
" (clean state) " : " "));
76 kernelThread
= new KernelThread(threadGroup
, "Kernel Thread");
77 kernelThread
.setContextClassLoader(getClass().getClassLoader());
81 private void initI18n() {
82 Object defaultLocaleValue
= KernelUtils
.getFrameworkProp(NodeConstants
.I18N_DEFAULT_LOCALE
);
83 defaultLocale
= defaultLocaleValue
!= null ?
new Locale(defaultLocaleValue
.toString())
84 : new Locale(ENGLISH
.getLanguage());
85 locales
= LocaleUtils
.asLocaleList(KernelUtils
.getFrameworkProp(NodeConstants
.I18N_LOCALES
));
88 private void initServices() {
90 initTransactionManager();
93 RepositoryServiceFactory repositoryServiceFactory
= new RepositoryServiceFactory();
94 stopHooks
.add(() -> repositoryServiceFactory
.shutdown());
95 bc
.registerService(ManagedServiceFactory
.class, repositoryServiceFactory
,
96 LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_REPOS_FACTORY_PID
));
98 NodeRepositoryFactory repositoryFactory
= new NodeRepositoryFactory();
99 bc
.registerService(RepositoryFactory
.class, repositoryFactory
, null);
102 NodeUserAdmin userAdmin
= new NodeUserAdmin(NodeConstants
.ROLES_BASEDN
);
103 stopHooks
.add(() -> userAdmin
.destroy());
104 bc
.registerService(ManagedServiceFactory
.class, userAdmin
,
105 LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_USER_ADMIN_PID
));
108 CmsFsProvider cmsFsProvider
= new CmsFsProvider();
109 bc
.registerService(FileSystemProvider
.class, cmsFsProvider
,
110 LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_FS_PROVIDER_PID
));
113 private void initTransactionManager() {
114 // TODO manage it in a managed service, as startup could be long
115 ServiceReference
<TransactionManager
> existingTm
= bc
.getServiceReference(TransactionManager
.class);
116 if (existingTm
!= null) {
117 if (log
.isDebugEnabled())
118 log
.debug("Using provided transaction manager " + existingTm
);
122 if (!TransactionManagerServices
.isTransactionManagerRunning()) {
123 bitronix
.tm
.Configuration tmConf
= TransactionManagerServices
.getConfiguration();
124 tmConf
.setServerId(UUID
.randomUUID().toString());
126 Bundle bitronixBundle
= FrameworkUtil
.getBundle(bitronix
.tm
.Configuration
.class);
127 File tmBaseDir
= bitronixBundle
.getDataFile(KernelConstants
.DIR_TRANSACTIONS
);
128 File tmDir1
= new File(tmBaseDir
, "btm1");
130 tmConf
.setLogPart1Filename(new File(tmDir1
, tmDir1
.getName() + ".tlog").getAbsolutePath());
131 File tmDir2
= new File(tmBaseDir
, "btm2");
133 tmConf
.setLogPart2Filename(new File(tmDir2
, tmDir2
.getName() + ".tlog").getAbsolutePath());
135 BitronixTransactionManager transactionManager
= getTransactionManager();
136 stopHooks
.add(() -> transactionManager
.shutdown());
137 BitronixTransactionSynchronizationRegistry transactionSynchronizationRegistry
= getTransactionSynchronizationRegistry();
139 bc
.registerService(TransactionManager
.class, transactionManager
, null);
140 bc
.registerService(UserTransaction
.class, transactionManager
, null);
141 bc
.registerService(TransactionSynchronizationRegistry
.class, transactionSynchronizationRegistry
, null);
142 if (log
.isDebugEnabled())
143 log
.debug("Initialised default Bitronix transaction manager");
147 if (log
.isDebugEnabled())
148 log
.debug("CMS stopping... stateUuid=" + this.stateUuid
+ (cleanState ?
" (clean state) " : " "));
150 if (kernelThread
!= null)
151 kernelThread
.destroyAndJoin();
154 long duration
= ((System
.currentTimeMillis() - availableSince
) / 1000) / 60;
155 log
.info("## ARGEO CMS STOPPED after " + (duration
/ 60) + "h " + (duration
% 60) + "min uptime ##");
158 /** Apply shutdown hoos in reverse order. */
159 private void applyStopHooks() {
160 for (int i
= stopHooks
.size() - 1; i
>= 0; i
--) {
162 stopHooks
.get(i
).run();
163 } catch (Exception e
) {
164 log
.error("Could not run shutdown hook #" + i
);
167 // Clean hanging Gogo shell thread
168 new GogoShellKiller().start();
172 public boolean isClean() {
177 public Long
getAvailableSince() {
178 return availableSince
;
184 public Locale
getDefaultLocale() {
185 return defaultLocale
;
188 public List
<Locale
> getLocales() {
192 public String
getHostname() {