1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.awt
.image
.Kernel
;
4 import java
.io
.IOException
;
6 import java
.nio
.file
.Files
;
7 import java
.nio
.file
.Path
;
8 import java
.util
.Dictionary
;
10 import java
.util
.Locale
;
12 import javax
.security
.auth
.login
.Configuration
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.cms
.CmsException
;
17 import org
.argeo
.node
.ArgeoLogger
;
18 import org
.argeo
.node
.NodeConstants
;
19 import org
.argeo
.node
.NodeDeployment
;
20 import org
.argeo
.node
.NodeInstance
;
21 import org
.argeo
.node
.NodeState
;
22 import org
.argeo
.util
.LangUtils
;
23 import org
.ietf
.jgss
.GSSCredential
;
24 import org
.osgi
.framework
.BundleActivator
;
25 import org
.osgi
.framework
.BundleContext
;
26 import org
.osgi
.framework
.Constants
;
27 import org
.osgi
.framework
.ServiceReference
;
28 import org
.osgi
.service
.log
.LogReaderService
;
29 import org
.osgi
.service
.useradmin
.UserAdmin
;
32 * Activates the {@link Kernel} from the provided {@link BundleContext}. Gives
33 * access to kernel information for the rest of the bundle (and only it)
35 public class Activator
implements BundleActivator
{
36 private final static Log log
= LogFactory
.getLog(Activator
.class);
38 private static Activator instance
;
40 private BundleContext bc
;
41 // private CmsSecurity nodeSecurity;
42 private LogReaderService logReaderService
;
43 // private ConfigurationAdmin configurationAdmin;
45 private NodeLogger logger
;
46 private CmsState nodeState
;
47 private CmsDeployment nodeDeployment
;
48 private CmsInstance nodeInstance
;
51 public void start(BundleContext bundleContext
) throws Exception
{
53 this.bc
= bundleContext
;
54 this.logReaderService
= getService(LogReaderService
.class);
55 // this.configurationAdmin = getService(ConfigurationAdmin.class);
58 // nodeSecurity = new CmsSecurity();
62 } catch (Exception e
) {
63 log
.error("## FATAL: CMS activator failed", e
);
64 // throw new CmsException("Cannot initialize node", e);
68 private void initSecurity() {
69 if (System
.getProperty(KernelConstants
.JAAS_CONFIG_PROP
) == null) {
70 String jaasConfig
= KernelConstants
.JAAS_CONFIG
;
71 URL url
= getClass().getClassLoader().getResource(jaasConfig
);
72 System
.setProperty(KernelConstants
.JAAS_CONFIG_PROP
, url
.toExternalForm());
74 // explicitly load JAAS configuration
75 Configuration
.getConfiguration();
78 private void initArgeoLogger() {
79 logger
= new NodeLogger(logReaderService
);
80 bc
.registerService(ArgeoLogger
.class, logger
, null);
83 private void initNode() throws IOException
{
85 Path stateUuidPath
= bc
.getDataFile("stateUuid").toPath();
87 if (Files
.exists(stateUuidPath
)) {
88 stateUuid
= Files
.readAllLines(stateUuidPath
).get(0);
90 stateUuid
= bc
.getProperty(Constants
.FRAMEWORK_UUID
);
91 Files
.write(stateUuidPath
, stateUuid
.getBytes());
93 nodeState
= new CmsState(stateUuid
);
94 Dictionary
<String
, Object
> regProps
= LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_STATE_PID
);
95 regProps
.put(NodeConstants
.CN
, stateUuid
);
96 bc
.registerService(NodeState
.class, nodeState
, regProps
);
99 nodeDeployment
= new CmsDeployment();
100 bc
.registerService(NodeDeployment
.class, nodeDeployment
, null);
103 nodeInstance
= new CmsInstance();
104 bc
.registerService(NodeInstance
.class, nodeInstance
, null);
108 public void stop(BundleContext bundleContext
) throws Exception
{
109 nodeInstance
.shutdown();
110 nodeDeployment
.shutdown();
111 nodeState
.shutdown();
115 this.logReaderService
= null;
116 // this.configurationAdmin = null;
119 private <T
> T
getService(Class
<T
> clazz
) {
120 ServiceReference
<T
> sr
= bc
.getServiceReference(clazz
);
122 throw new CmsException("No service available for " + clazz
);
123 return bc
.getService(sr
);
126 public static NodeState
getNodeState() {
127 return instance
.nodeState
;
130 public static GSSCredential
getAcceptorCredentials() {
131 ServiceReference
<UserAdmin
> sr
= instance
.bc
.getServiceReference(UserAdmin
.class);
132 NodeUserAdmin userAdmin
= (NodeUserAdmin
) instance
.bc
.getService(sr
);
133 return userAdmin
.getAcceptorCredentials();
136 // static CmsSecurity getCmsSecurity() {
137 // return instance.nodeSecurity;
140 public String
[] getLocales() {
142 List
<Locale
> locales
= getNodeState().getLocales();
143 String
[] res
= new String
[locales
.size()];
144 for (int i
= 0; i
< locales
.size(); i
++)
145 res
[i
] = locales
.get(i
).toString();