1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.io
.IOException
;
5 import java
.util
.Dictionary
;
6 import java
.util
.Hashtable
;
8 import java
.util
.Locale
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.cms
.CmsException
;
13 import org
.argeo
.node
.ArgeoLogger
;
14 import org
.argeo
.node
.NodeConstants
;
15 import org
.argeo
.node
.NodeDeployment
;
16 import org
.argeo
.node
.NodeState
;
17 import org
.argeo
.util
.LangUtils
;
18 import org
.osgi
.framework
.BundleActivator
;
19 import org
.osgi
.framework
.BundleContext
;
20 import org
.osgi
.framework
.Constants
;
21 import org
.osgi
.framework
.ServiceReference
;
22 import org
.osgi
.service
.cm
.Configuration
;
23 import org
.osgi
.service
.cm
.ConfigurationAdmin
;
24 import org
.osgi
.service
.cm
.ManagedService
;
25 import org
.osgi
.service
.condpermadmin
.ConditionalPermissionAdmin
;
26 import org
.osgi
.service
.log
.LogReaderService
;
29 * Activates the {@link Kernel} from the provided {@link BundleContext}. Gives
30 * access to kernel information for the rest of the bundle (and only it)
32 public class Activator
implements BundleActivator
{
33 // public final static String SYSTEM_KEY_PROPERTY =
34 // "argeo.security.systemKey";
35 private final Log log
= LogFactory
.getLog(Activator
.class);
37 // private final static String systemKey;
39 // System.setProperty(SYSTEM_KEY_PROPERTY, systemKey);
42 // private static Kernel kernel;
43 private static Activator instance
;
45 private BundleContext bc
;
46 private ConditionalPermissionAdmin permissionAdmin
;
47 private LogReaderService logReaderService
;
48 private ConfigurationAdmin configurationAdmin
;
50 private NodeLogger logger
;
51 private CmsState nodeState
;
52 private CmsDeployment nodeDeployment
;
55 public void start(BundleContext bundleContext
) throws Exception
{
57 // kernel = new Kernel();
59 // } catch (Exception e) {
60 // log.error("Cannot boot kernel", e);
64 this.bc
= bundleContext
;
65 this.permissionAdmin
= getService(ConditionalPermissionAdmin
.class);
66 this.logReaderService
= getService(LogReaderService
.class);
67 this.configurationAdmin
= getService(ConfigurationAdmin
.class);
69 initSecurity();// must be first
74 private void initSecurity() {
75 URL url
= getClass().getClassLoader().getResource(KernelConstants
.JAAS_CONFIG
);
76 System
.setProperty("java.security.auth.login.config", url
.toExternalForm());
79 private void initArgeoLogger() {
80 logger
= new NodeLogger(logReaderService
);
83 bc
.registerService(ArgeoLogger
.class, logger
, null);
86 private void initNodeState() throws IOException
{
87 nodeState
= new CmsState();
90 Configuration nodeConf
= configurationAdmin
.getConfiguration(NodeConstants
.NODE_STATE_PID
);
91 Dictionary
<String
, Object
> props
= nodeConf
.getProperties();
93 if (log
.isDebugEnabled())
94 log
.debug("Clean node state");
95 Dictionary
<String
, Object
> envProps
= new Hashtable
<>();
96 // Use the UUID of the first framework run as state UUID
97 cn
= bc
.getProperty(Constants
.FRAMEWORK_UUID
);
98 envProps
.put(NodeConstants
.CN
, cn
);
99 nodeConf
.update(envProps
);
101 // Check if state is in line with environment
102 // Dictionary<String, Object> envProps = new Hashtable<>();
103 // for (String key : LangUtils.keys(envProps)) {
104 // Object envValue = envProps.get(key);
105 // Object storedValue = props.get(key);
106 // if (storedValue == null)
107 // throw new CmsException("No state value for env " + key + "=" +
109 // + ", please clean the OSGi configuration.");
110 // if (!storedValue.equals(envValue))
111 // throw new CmsException("State value for " + key + "=" +
113 // + " is different from env value =" + envValue + ", please clean
114 // the OSGi configuration.");
116 cn
= props
.get(NodeConstants
.CN
);
118 throw new CmsException("No state UUID available");
121 Dictionary
<String
, Object
> regProps
= LangUtils
.init(Constants
.SERVICE_PID
, NodeConstants
.NODE_STATE_PID
);
122 regProps
.put(NodeConstants
.CN
, cn
);
123 bc
.registerService(LangUtils
.names(NodeState
.class, ManagedService
.class), nodeState
, regProps
);
126 nodeDeployment
= new CmsDeployment();
127 bc
.registerService(LangUtils
.names(NodeDeployment
.class), nodeDeployment
, null);
128 } catch (RuntimeException e
) {
135 public void stop(BundleContext bundleContext
) throws Exception
{
136 nodeState
.shutdown();
140 this.permissionAdmin
= null;
141 this.logReaderService
= null;
142 this.configurationAdmin
= null;
144 // if (kernel != null) {
151 private <T
> T
getService(Class
<T
> clazz
) {
152 ServiceReference
<T
> sr
= bc
.getServiceReference(clazz
);
154 throw new CmsException("No service available for " + clazz
);
155 return bc
.getService(sr
);
158 public static NodeState
getNodeState() {
159 return instance
.nodeState
;
162 public String
[] getLocales() {
164 List
<Locale
> locales
= getNodeState().getLocales();
165 String
[] res
= new String
[locales
.size()];
166 for (int i
= 0; i
< locales
.size(); i
++)
167 res
[i
] = locales
.get(i
).toString();