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
.net
.InetAddress
;
9 import java
.net
.UnknownHostException
;
10 import java
.nio
.file
.spi
.FileSystemProvider
;
11 import java
.util
.ArrayList
;
12 import java
.util
.List
;
13 import java
.util
.Locale
;
14 import java
.util
.UUID
;
16 import javax
.jcr
.RepositoryFactory
;
17 import javax
.transaction
.TransactionManager
;
18 import javax
.transaction
.TransactionSynchronizationRegistry
;
19 import javax
.transaction
.UserTransaction
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.cms
.i18n
.LocaleUtils
;
24 import org
.argeo
.node
.NodeConstants
;
25 import org
.argeo
.node
.NodeState
;
26 import org
.argeo
.util
.LangUtils
;
27 import org
.osgi
.framework
.Bundle
;
28 import org
.osgi
.framework
.BundleContext
;
29 import org
.osgi
.framework
.Constants
;
30 import org
.osgi
.framework
.FrameworkUtil
;
31 import org
.osgi
.framework
.ServiceReference
;
32 import org
.osgi
.service
.cm
.ManagedServiceFactory
;
34 import bitronix
.tm
.BitronixTransactionManager
;
35 import bitronix
.tm
.BitronixTransactionSynchronizationRegistry
;
36 import bitronix
.tm
.TransactionManagerServices
;
38 public class CmsState
implements NodeState
{
39 private final Log log
= LogFactory
.getLog(CmsState
.class);
40 private final BundleContext bc
= FrameworkUtil
.getBundle(CmsState
.class).getBundleContext();
43 private Long availableSince
;
46 private Locale defaultLocale
;
47 private List
<Locale
> locales
= null;
49 private ThreadGroup threadGroup
= new ThreadGroup("CMS");
50 private KernelThread kernelThread
;
51 private List
<Runnable
> shutdownHooks
= new ArrayList
<>();
53 private final String stateUuid
;
54 private final boolean cleanState
;
55 private String hostname
;
57 public CmsState(String stateUuid
) {
58 this.stateUuid
= stateUuid
;
59 String frameworkUuid
= KernelUtils
.getFrameworkProp(Constants
.FRAMEWORK_UUID
);
60 this.cleanState
= stateUuid
.equals(frameworkUuid
);
62 this.hostname
= InetAddress
.getLocalHost().getHostName();
63 } catch (UnknownHostException e
) {
64 log
.error("Cannot set hostname: " + e
);
67 availableSince
= System
.currentTimeMillis();
68 if (log
.isDebugEnabled())
69 log
.debug("## CMS STARTED " + this.stateUuid
+ (cleanState ?
" (clean state) " : " "));
75 kernelThread
= new KernelThread(threadGroup
, "Kernel Thread");
76 kernelThread
.setContextClassLoader(getClass().getClassLoader());
80 private void initI18n() {
81 Object defaultLocaleValue
= KernelUtils
.getFrameworkProp(NodeConstants
.I18N_DEFAULT_LOCALE
);
82 defaultLocale
= defaultLocaleValue
!= null ?
new Locale(defaultLocaleValue
.toString())
83 : new Locale(ENGLISH
.getLanguage());
84 locales
= LocaleUtils
.asLocaleList(KernelUtils
.getFrameworkProp(NodeConstants
.I18N_LOCALES
));
87 private void initServices() {
89 initTransactionManager();
92 RepositoryServiceFactory repositoryServiceFactory
= new RepositoryServiceFactory();
93 shutdownHooks
.add(() -> repositoryServiceFactory
.shutdown());
94 bc
.registerService(ManagedServiceFactory
.class, repositoryServiceFactory
,
95 LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_REPOS_FACTORY_PID
));
97 NodeRepositoryFactory repositoryFactory
= new NodeRepositoryFactory();
98 bc
.registerService(RepositoryFactory
.class, repositoryFactory
, null);
101 NodeUserAdmin userAdmin
= new NodeUserAdmin(NodeConstants
.ROLES_BASEDN
);
102 shutdownHooks
.add(() -> userAdmin
.destroy());
103 bc
.registerService(ManagedServiceFactory
.class, userAdmin
,
104 LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_USER_ADMIN_PID
));
107 CmsFsProvider cmsFsProvider
= new CmsFsProvider();
108 bc
.registerService(FileSystemProvider
.class, cmsFsProvider
,
109 LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_FS_PROVIDER_PID
));
112 private void initTransactionManager() {
113 // TODO manage it in a managed service, as startup could be long
114 ServiceReference
<TransactionManager
> existingTm
= bc
.getServiceReference(TransactionManager
.class);
115 if (existingTm
!= null) {
116 if (log
.isDebugEnabled())
117 log
.debug("Using provided transaction manager " + existingTm
);
119 bitronix
.tm
.Configuration tmConf
= TransactionManagerServices
.getConfiguration();
120 tmConf
.setServerId(UUID
.randomUUID().toString());
122 Bundle bitronixBundle
= FrameworkUtil
.getBundle(bitronix
.tm
.Configuration
.class);
123 File tmBaseDir
= bitronixBundle
.getDataFile(KernelConstants
.DIR_TRANSACTIONS
);
124 File tmDir1
= new File(tmBaseDir
, "btm1");
126 tmConf
.setLogPart1Filename(new File(tmDir1
, tmDir1
.getName() + ".tlog").getAbsolutePath());
127 File tmDir2
= new File(tmBaseDir
, "btm2");
129 tmConf
.setLogPart2Filename(new File(tmDir2
, tmDir2
.getName() + ".tlog").getAbsolutePath());
131 BitronixTransactionManager transactionManager
= getTransactionManager();
132 shutdownHooks
.add(() -> transactionManager
.shutdown());
133 BitronixTransactionSynchronizationRegistry transactionSynchronizationRegistry
= getTransactionSynchronizationRegistry();
135 bc
.registerService(TransactionManager
.class, transactionManager
, null);
136 bc
.registerService(UserTransaction
.class, transactionManager
, null);
137 bc
.registerService(TransactionSynchronizationRegistry
.class, transactionSynchronizationRegistry
, null);
138 if (log
.isDebugEnabled())
139 log
.debug("Initialised default Bitronix transaction manager");
143 applyShutdownHooks();
145 if (kernelThread
!= null)
146 kernelThread
.destroyAndJoin();
148 if (log
.isDebugEnabled())
149 log
.debug("## CMS STOPPED");
152 /** Apply shutdown hoos in reverse order. */
153 private void applyShutdownHooks() {
154 for (int i
= shutdownHooks
.size() - 1; i
>= 0; i
--) {
156 shutdownHooks
.get(i
).run();
157 } catch (Exception e
) {
158 log
.error("Could not run shutdown hook #" + i
);
161 // Clean hanging Gogo shell thread
162 new GogoShellKiller().start();
166 public boolean isClean() {
171 public Long
getAvailableSince() {
172 return availableSince
;
178 public Locale
getDefaultLocale() {
179 return defaultLocale
;
182 public List
<Locale
> getLocales() {
186 public String
getHostname() {
190 /** Workaround for blocking Gogo shell by system shutdown. */
191 private class GogoShellKiller
extends Thread
{
193 public GogoShellKiller() {
194 super("Gogo Shell Killer");
200 ThreadGroup rootTg
= getRootThreadGroup(null);
201 Thread gogoShellThread
= findGogoShellThread(rootTg
);
202 if (gogoShellThread
== null)
204 while (getNonDaemonCount(rootTg
) > 2) {
207 } catch (InterruptedException e
) {
211 gogoShellThread
= findGogoShellThread(rootTg
);
212 if (gogoShellThread
== null)
218 private static ThreadGroup
getRootThreadGroup(ThreadGroup tg
) {
220 tg
= Thread
.currentThread().getThreadGroup();
221 if (tg
.getParent() == null)
224 return getRootThreadGroup(tg
.getParent());
227 private static int getNonDaemonCount(ThreadGroup rootThreadGroup
) {
228 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
229 rootThreadGroup
.enumerate(threads
);
230 int nonDameonCount
= 0;
231 for (Thread t
: threads
)
232 if (t
!= null && !t
.isDaemon())
234 return nonDameonCount
;
237 private static Thread
findGogoShellThread(ThreadGroup rootThreadGroup
) {
238 Thread
[] threads
= new Thread
[rootThreadGroup
.activeCount()];
239 rootThreadGroup
.enumerate(threads
, true);
240 for (Thread thread
: threads
) {
241 if (thread
.getName().equals("Gogo shell"))