1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.io
.IOException
;
4 import java
.nio
.file
.Files
;
5 import java
.nio
.file
.Path
;
6 import java
.util
.Dictionary
;
8 import java
.util
.Locale
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.cms
.CmsException
;
13 import org
.argeo
.node
.ArgeoLogger
;
14 import org
.argeo
.node
.NodeConstants
;
15 import org
.argeo
.node
.NodeDeployment
;
16 import org
.argeo
.node
.NodeInstance
;
17 import org
.argeo
.node
.NodeState
;
18 import org
.argeo
.util
.LangUtils
;
19 import org
.ietf
.jgss
.GSSCredential
;
20 import org
.osgi
.framework
.BundleActivator
;
21 import org
.osgi
.framework
.BundleContext
;
22 import org
.osgi
.framework
.Constants
;
23 import org
.osgi
.framework
.ServiceReference
;
24 import org
.osgi
.service
.log
.LogReaderService
;
27 * Activates the {@link Kernel} from the provided {@link BundleContext}. Gives
28 * access to kernel information for the rest of the bundle (and only it)
30 public class Activator
implements BundleActivator
{
31 private final static Log log
= LogFactory
.getLog(Activator
.class);
33 private static Activator instance
;
35 private BundleContext bc
;
36 private CmsSecurity nodeSecurity
;
37 private LogReaderService logReaderService
;
38 // private ConfigurationAdmin configurationAdmin;
40 private NodeLogger logger
;
41 private CmsState nodeState
;
42 private CmsDeployment nodeDeployment
;
43 private CmsInstance nodeInstance
;
46 public void start(BundleContext bundleContext
) throws Exception
{
48 this.bc
= bundleContext
;
49 this.logReaderService
= getService(LogReaderService
.class);
50 // this.configurationAdmin = getService(ConfigurationAdmin.class);
53 nodeSecurity
= new CmsSecurity();
56 } catch (Exception e
) {
57 log
.error("## FATAL: CMS activator failed", e
);
58 // throw new CmsException("Cannot initialize node", e);
62 private void initArgeoLogger() {
63 logger
= new NodeLogger(logReaderService
);
64 bc
.registerService(ArgeoLogger
.class, logger
, null);
67 private void initNode() throws IOException
{
69 Path stateUuidPath
= bc
.getDataFile("stateUuid").toPath();
71 if (Files
.exists(stateUuidPath
)) {
72 stateUuid
= Files
.readAllLines(stateUuidPath
).get(0);
74 stateUuid
= bc
.getProperty(Constants
.FRAMEWORK_UUID
);
75 Files
.write(stateUuidPath
, stateUuid
.getBytes());
77 nodeState
= new CmsState(stateUuid
);
78 Dictionary
<String
, Object
> regProps
= LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_STATE_PID
);
79 regProps
.put(NodeConstants
.CN
, stateUuid
);
80 bc
.registerService(NodeState
.class, nodeState
, regProps
);
83 nodeDeployment
= new CmsDeployment();
84 bc
.registerService(NodeDeployment
.class, nodeDeployment
, null);
87 nodeInstance
= new CmsInstance();
88 bc
.registerService(NodeInstance
.class, nodeInstance
, null);
92 public void stop(BundleContext bundleContext
) throws Exception
{
93 nodeInstance
.shutdown();
94 nodeDeployment
.shutdown();
99 this.logReaderService
= null;
100 // this.configurationAdmin = null;
103 private <T
> T
getService(Class
<T
> clazz
) {
104 ServiceReference
<T
> sr
= bc
.getServiceReference(clazz
);
106 throw new CmsException("No service available for " + clazz
);
107 return bc
.getService(sr
);
110 public static NodeState
getNodeState() {
111 return instance
.nodeState
;
114 public static GSSCredential
getAcceptorCredentials() {
115 return getCmsSecurity().getServerCredentials();
118 static CmsSecurity
getCmsSecurity() {
119 return instance
.nodeSecurity
;
122 public String
[] getLocales() {
124 List
<Locale
> locales
= getNodeState().getLocales();
125 String
[] res
= new String
[locales
.size()];
126 for (int i
= 0; i
< locales
.size(); i
++)
127 res
[i
] = locales
.get(i
).toString();