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 javax
.security
.auth
.login
.Configuration
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.cms
.CmsException
;
16 import org
.argeo
.node
.ArgeoLogger
;
17 import org
.argeo
.node
.NodeConstants
;
18 import org
.argeo
.node
.NodeDeployment
;
19 import org
.argeo
.node
.NodeInstance
;
20 import org
.argeo
.node
.NodeState
;
21 import org
.argeo
.util
.LangUtils
;
22 import org
.ietf
.jgss
.GSSCredential
;
23 import org
.osgi
.framework
.BundleActivator
;
24 import org
.osgi
.framework
.BundleContext
;
25 import org
.osgi
.framework
.Constants
;
26 import org
.osgi
.framework
.ServiceReference
;
27 import org
.osgi
.service
.log
.LogReaderService
;
28 import org
.osgi
.service
.useradmin
.UserAdmin
;
31 * Activates the kernel. Gives access to kernel information for the rest of the
32 * bundle (and only it)
34 public class Activator
implements BundleActivator
{
35 private final static Log log
= LogFactory
.getLog(Activator
.class);
37 private static Activator instance
;
39 private BundleContext bc
;
40 private LogReaderService logReaderService
;
42 private NodeLogger logger
;
43 private CmsState nodeState
;
44 private CmsDeployment nodeDeployment
;
45 private CmsInstance nodeInstance
;
48 public void start(BundleContext bundleContext
) throws Exception
{
49 Runtime
.getRuntime().addShutdownHook(new CmsShutdown());
51 this.bc
= bundleContext
;
52 this.logReaderService
= getService(LogReaderService
.class);
58 log
.debug("Kernel bundle started");
59 } catch (Throwable e
) {
60 log
.error("## FATAL: CMS activator failed", e
);
64 private void initSecurity() {
65 if (System
.getProperty(KernelConstants
.JAAS_CONFIG_PROP
) == null) {
66 String jaasConfig
= KernelConstants
.JAAS_CONFIG
;
67 URL url
= getClass().getClassLoader().getResource(jaasConfig
);
68 // System.setProperty(KernelConstants.JAAS_CONFIG_PROP,
69 // url.toExternalForm());
70 KernelUtils
.setJaasConfiguration(url
);
72 // explicitly load JAAS configuration
73 Configuration
.getConfiguration();
75 // ConditionalPermissionAdmin permissionAdmin = bc
76 // .getService(bc.getServiceReference(ConditionalPermissionAdmin.class));
77 // ConditionalPermissionUpdate update =
78 // permissionAdmin.newConditionalPermissionUpdate();
80 // update.getConditionalPermissionInfos()
81 // .add(permissionAdmin.newConditionalPermissionInfo(null,
82 // new ConditionInfo[] {
83 // new ConditionInfo(BundleLocationCondition.class.getName(), new
84 // String[] { "*" }) },
85 // new PermissionInfo[] { new
86 // PermissionInfo(AllPermission.class.getName(), null, null) },
87 // ConditionalPermissionInfo.ALLOW));
91 private void initArgeoLogger() {
92 logger
= new NodeLogger(logReaderService
);
93 bc
.registerService(ArgeoLogger
.class, logger
, null);
96 private void initNode() throws IOException
{
98 Path stateUuidPath
= bc
.getDataFile("stateUuid").toPath();
100 if (Files
.exists(stateUuidPath
)) {
101 stateUuid
= Files
.readAllLines(stateUuidPath
).get(0);
103 stateUuid
= bc
.getProperty(Constants
.FRAMEWORK_UUID
);
104 Files
.write(stateUuidPath
, stateUuid
.getBytes());
106 nodeState
= new CmsState(stateUuid
);
107 Dictionary
<String
, Object
> regProps
= LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_STATE_PID
);
108 regProps
.put(NodeConstants
.CN
, stateUuid
);
109 bc
.registerService(NodeState
.class, nodeState
, regProps
);
112 nodeDeployment
= new CmsDeployment();
113 bc
.registerService(NodeDeployment
.class, nodeDeployment
, null);
116 nodeInstance
= new CmsInstance();
117 bc
.registerService(NodeInstance
.class, nodeInstance
, null);
121 public void stop(BundleContext bundleContext
) throws Exception
{
123 if (nodeInstance
!= null)
124 nodeInstance
.shutdown();
125 if (nodeDeployment
!= null)
126 nodeDeployment
.shutdown();
127 if (nodeState
!= null)
128 nodeState
.shutdown();
132 this.logReaderService
= null;
133 // this.configurationAdmin = null;
134 } catch (Exception e
) {
135 log
.error("CMS activator shutdown failed", e
);
139 private <T
> T
getService(Class
<T
> clazz
) {
140 ServiceReference
<T
> sr
= bc
.getServiceReference(clazz
);
142 throw new CmsException("No service available for " + clazz
);
143 return bc
.getService(sr
);
146 public static NodeState
getNodeState() {
147 return instance
.nodeState
;
150 public static GSSCredential
getAcceptorCredentials() {
151 return getNodeUserAdmin().getAcceptorCredentials();
154 public static boolean isSingleUser() {
155 return getNodeUserAdmin().isSingleUser();
158 private static NodeUserAdmin
getNodeUserAdmin() {
159 ServiceReference
<UserAdmin
> sr
= instance
.bc
.getServiceReference(UserAdmin
.class);
160 NodeUserAdmin userAdmin
= (NodeUserAdmin
) instance
.bc
.getService(sr
);
165 // static CmsSecurity getCmsSecurity() {
166 // return instance.nodeSecurity;
169 public String
[] getLocales() {
171 List
<Locale
> locales
= getNodeState().getLocales();
172 String
[] res
= new String
[locales
.size()];
173 for (int i
= 0; i
< locales
.size(); i
++)
174 res
[i
] = locales
.get(i
).toString();