1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.io
.IOException
;
5 import java
.nio
.file
.Files
;
6 import java
.nio
.file
.Path
;
7 import java
.util
.Dictionary
;
9 import java
.util
.Locale
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.cms
.CmsException
;
14 import org
.argeo
.node
.ArgeoLogger
;
15 import org
.argeo
.node
.NodeConstants
;
16 import org
.argeo
.node
.NodeDeployment
;
17 import org
.argeo
.node
.NodeInstance
;
18 import org
.argeo
.node
.NodeState
;
19 import org
.argeo
.util
.LangUtils
;
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
.condpermadmin
.ConditionalPermissionAdmin
;
25 import org
.osgi
.service
.log
.LogReaderService
;
28 * Activates the {@link Kernel} from the provided {@link BundleContext}. Gives
29 * access to kernel information for the rest of the bundle (and only it)
31 public class Activator
implements BundleActivator
{
32 private final Log log
= LogFactory
.getLog(Activator
.class);
34 private static Activator instance
;
36 private BundleContext bc
;
37 private ConditionalPermissionAdmin permissionAdmin
;
38 private LogReaderService logReaderService
;
39 // private ConfigurationAdmin configurationAdmin;
41 private NodeLogger logger
;
42 private CmsState nodeState
;
43 private CmsDeployment nodeDeployment
;
44 private CmsInstance nodeInstance
;
47 public void start(BundleContext bundleContext
) throws Exception
{
49 this.bc
= bundleContext
;
50 this.permissionAdmin
= getService(ConditionalPermissionAdmin
.class);
51 this.logReaderService
= getService(LogReaderService
.class);
52 // this.configurationAdmin = getService(ConfigurationAdmin.class);
54 initSecurity();// must be first
58 } catch (Exception e
) {
60 throw new CmsException("Cannot initialize node", e
);
64 private void initSecurity() {
65 URL url
= getClass().getClassLoader().getResource(KernelConstants
.JAAS_CONFIG
);
66 System
.setProperty("java.security.auth.login.config", url
.toExternalForm());
69 private void initArgeoLogger() {
70 logger
= new NodeLogger(logReaderService
);
71 bc
.registerService(ArgeoLogger
.class, logger
, null);
74 private void initNode() throws IOException
{
76 Path stateUuidPath
= bc
.getDataFile("stateUuid").toPath();
78 if (Files
.exists(stateUuidPath
)) {
79 stateUuid
= Files
.readAllLines(stateUuidPath
).get(0);
81 stateUuid
= bc
.getProperty(Constants
.FRAMEWORK_UUID
);
82 Files
.write(stateUuidPath
, stateUuid
.getBytes());
84 nodeState
= new CmsState(stateUuid
);
86 // Configuration nodeConf =
87 // configurationAdmin.getConfiguration(NodeConstants.NODE_STATE_PID);
88 // Dictionary<String, Object> props = nodeConf.getProperties();
89 // if (props == null) {
90 // if (log.isDebugEnabled())
91 // log.debug("Clean node state");
92 // Dictionary<String, Object> envProps = new Hashtable<>();
93 // // Use the UUID of the first framework run as state UUID
94 // cn = bc.getProperty(Constants.FRAMEWORK_UUID);
95 // envProps.put(NodeConstants.CN, cn);
96 // nodeConf.update(envProps);
98 // cn = props.get(NodeConstants.CN);
100 // throw new CmsException("No state UUID available");
102 Dictionary
<String
, Object
> regProps
= LangUtils
.init(Constants
.SERVICE_PID
, NodeConstants
.NODE_STATE_PID
);
103 regProps
.put(NodeConstants
.CN
, stateUuid
);
104 bc
.registerService(NodeState
.class, nodeState
, regProps
);
107 nodeDeployment
= new CmsDeployment();
108 bc
.registerService(NodeDeployment
.class, nodeDeployment
, null);
111 nodeInstance
= new CmsInstance();
112 bc
.registerService(NodeInstance
.class, nodeInstance
, null);
116 public void stop(BundleContext bundleContext
) throws Exception
{
117 nodeInstance
.shutdown();
118 nodeDeployment
.shutdown();
119 nodeState
.shutdown();
123 this.permissionAdmin
= null;
124 this.logReaderService
= null;
125 // this.configurationAdmin = null;
128 private <T
> T
getService(Class
<T
> clazz
) {
129 ServiceReference
<T
> sr
= bc
.getServiceReference(clazz
);
131 throw new CmsException("No service available for " + clazz
);
132 return bc
.getService(sr
);
135 public static NodeState
getNodeState() {
136 return instance
.nodeState
;
139 public String
[] getLocales() {
141 List
<Locale
> locales
= getNodeState().getLocales();
142 String
[] res
= new String
[locales
.size()];
143 for (int i
= 0; i
< locales
.size(); i
++)
144 res
[i
] = locales
.get(i
).toString();