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
);
67 private void initSecurity() {
68 if (System
.getProperty(KernelConstants
.JAAS_CONFIG_PROP
) == null) {
69 String jaasConfig
= KernelConstants
.JAAS_CONFIG
;
70 URL url
= getClass().getClassLoader().getResource(jaasConfig
);
71 // System.setProperty(KernelConstants.JAAS_CONFIG_PROP,
72 // url.toExternalForm());
73 KernelUtils
.setJaasConfiguration(url
);
75 // explicitly load JAAS configuration
76 Configuration
.getConfiguration();
78 // ConditionalPermissionAdmin permissionAdmin = bc
79 // .getService(bc.getServiceReference(ConditionalPermissionAdmin.class));
80 // ConditionalPermissionUpdate update =
81 // permissionAdmin.newConditionalPermissionUpdate();
83 // update.getConditionalPermissionInfos()
84 // .add(permissionAdmin.newConditionalPermissionInfo(null,
85 // new ConditionInfo[] {
86 // new ConditionInfo(BundleLocationCondition.class.getName(), new
87 // String[] { "*" }) },
88 // new PermissionInfo[] { new
89 // PermissionInfo(AllPermission.class.getName(), null, null) },
90 // ConditionalPermissionInfo.ALLOW));
94 private void initArgeoLogger() {
96 // disable integration of Jetty logging with SLF4J
97 // in order to avoid chicken and egg problems
98 // org.eclipse.jetty.util.log.Log.setLog(new StdErrLog());
99 // org.eclipse.jetty.util.log.Logger jettyLog =
100 // org.eclipse.jetty.util.log.Log.getLog();
101 // if (jettyLog != null) {
102 // jettyLog.warn("TEST JETTY LOG", new Object[0]);
105 logger
= new NodeLogger(logReaderService
);
106 bc
.registerService(ArgeoLogger
.class, logger
, null);
109 private void initNode() throws IOException
{
111 Path stateUuidPath
= bc
.getDataFile("stateUuid").toPath();
113 if (Files
.exists(stateUuidPath
)) {
114 stateUuid
= Files
.readAllLines(stateUuidPath
).get(0);
116 stateUuid
= bc
.getProperty(Constants
.FRAMEWORK_UUID
);
117 Files
.write(stateUuidPath
, stateUuid
.getBytes());
119 nodeState
= new CmsState(stateUuid
);
120 Dictionary
<String
, Object
> regProps
= LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_STATE_PID
);
121 regProps
.put(NodeConstants
.CN
, stateUuid
);
122 bc
.registerService(NodeState
.class, nodeState
, regProps
);
125 nodeDeployment
= new CmsDeployment();
126 bc
.registerService(NodeDeployment
.class, nodeDeployment
, null);
129 nodeInstance
= new CmsInstance();
130 bc
.registerService(NodeInstance
.class, nodeInstance
, null);
134 public void stop(BundleContext bundleContext
) throws Exception
{
136 if (nodeInstance
!= null)
137 nodeInstance
.shutdown();
138 if (nodeDeployment
!= null)
139 nodeDeployment
.shutdown();
140 if (nodeState
!= null)
141 nodeState
.shutdown();
145 this.logReaderService
= null;
146 // this.configurationAdmin = null;
147 } catch (Exception e
) {
148 log
.error("CMS activator shutdown failed", e
);
152 private <T
> T
getService(Class
<T
> clazz
) {
153 ServiceReference
<T
> sr
= bc
.getServiceReference(clazz
);
155 throw new CmsException("No service available for " + clazz
);
156 return bc
.getService(sr
);
159 public static NodeState
getNodeState() {
160 return instance
.nodeState
;
163 public static GSSCredential
getAcceptorCredentials() {
164 ServiceReference
<UserAdmin
> sr
= instance
.bc
.getServiceReference(UserAdmin
.class);
165 NodeUserAdmin userAdmin
= (NodeUserAdmin
) instance
.bc
.getService(sr
);
166 return userAdmin
.getAcceptorCredentials();
169 // static CmsSecurity getCmsSecurity() {
170 // return instance.nodeSecurity;
173 public String
[] getLocales() {
175 List
<Locale
> locales
= getNodeState().getLocales();
176 String
[] res
= new String
[locales
.size()];
177 for (int i
= 0; i
< locales
.size(); i
++)
178 res
[i
] = locales
.get(i
).toString();