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
{
52 Runtime
.getRuntime().addShutdownHook(new CmsShutdown());
54 this.bc
= bundleContext
;
55 this.logReaderService
= getService(LogReaderService
.class);
56 // this.configurationAdmin = getService(ConfigurationAdmin.class);
59 // nodeSecurity = new CmsSecurity();
63 } catch (Exception e
) {
64 log
.error("## FATAL: CMS activator failed", 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,
73 // url.toExternalForm());
74 KernelUtils
.setJaasConfiguration(url
);
76 // explicitly load JAAS configuration
77 Configuration
.getConfiguration();
79 // ConditionalPermissionAdmin permissionAdmin = bc
80 // .getService(bc.getServiceReference(ConditionalPermissionAdmin.class));
81 // ConditionalPermissionUpdate update =
82 // permissionAdmin.newConditionalPermissionUpdate();
84 // update.getConditionalPermissionInfos()
85 // .add(permissionAdmin.newConditionalPermissionInfo(null,
86 // new ConditionInfo[] {
87 // new ConditionInfo(BundleLocationCondition.class.getName(), new
88 // String[] { "*" }) },
89 // new PermissionInfo[] { new
90 // PermissionInfo(AllPermission.class.getName(), null, null) },
91 // ConditionalPermissionInfo.ALLOW));
95 private void initArgeoLogger() {
97 // disable integration of Jetty logging with SLF4J
98 // in order to avoid chicken and egg problems
99 // org.eclipse.jetty.util.log.Log.setLog(new StdErrLog());
100 // org.eclipse.jetty.util.log.Logger jettyLog =
101 // org.eclipse.jetty.util.log.Log.getLog();
102 // if (jettyLog != null) {
103 // jettyLog.warn("TEST JETTY LOG", new Object[0]);
106 logger
= new NodeLogger(logReaderService
);
107 bc
.registerService(ArgeoLogger
.class, logger
, null);
110 private void initNode() throws IOException
{
112 Path stateUuidPath
= bc
.getDataFile("stateUuid").toPath();
114 if (Files
.exists(stateUuidPath
)) {
115 stateUuid
= Files
.readAllLines(stateUuidPath
).get(0);
117 stateUuid
= bc
.getProperty(Constants
.FRAMEWORK_UUID
);
118 Files
.write(stateUuidPath
, stateUuid
.getBytes());
120 nodeState
= new CmsState(stateUuid
);
121 Dictionary
<String
, Object
> regProps
= LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_STATE_PID
);
122 regProps
.put(NodeConstants
.CN
, stateUuid
);
123 bc
.registerService(NodeState
.class, nodeState
, regProps
);
126 nodeDeployment
= new CmsDeployment();
127 bc
.registerService(NodeDeployment
.class, nodeDeployment
, null);
130 nodeInstance
= new CmsInstance();
131 bc
.registerService(NodeInstance
.class, nodeInstance
, null);
135 public void stop(BundleContext bundleContext
) throws Exception
{
137 if (nodeInstance
!= null)
138 nodeInstance
.shutdown();
139 if (nodeDeployment
!= null)
140 nodeDeployment
.shutdown();
141 if (nodeState
!= null)
142 nodeState
.shutdown();
146 this.logReaderService
= null;
147 // this.configurationAdmin = null;
148 } catch (Exception e
) {
149 log
.error("CMS activator shutdown failed", e
);
153 private <T
> T
getService(Class
<T
> clazz
) {
154 ServiceReference
<T
> sr
= bc
.getServiceReference(clazz
);
156 throw new CmsException("No service available for " + clazz
);
157 return bc
.getService(sr
);
160 public static NodeState
getNodeState() {
161 return instance
.nodeState
;
164 public static GSSCredential
getAcceptorCredentials() {
165 ServiceReference
<UserAdmin
> sr
= instance
.bc
.getServiceReference(UserAdmin
.class);
166 NodeUserAdmin userAdmin
= (NodeUserAdmin
) instance
.bc
.getService(sr
);
167 return userAdmin
.getAcceptorCredentials();
170 // static CmsSecurity getCmsSecurity() {
171 // return instance.nodeSecurity;
174 public String
[] getLocales() {
176 List
<Locale
> locales
= getNodeState().getLocales();
177 String
[] res
= new String
[locales
.size()];
178 for (int i
= 0; i
< locales
.size(); i
++)
179 res
[i
] = locales
.get(i
).toString();