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
.NodeState
;
16 import org
.argeo
.node
.RepoConf
;
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
;
54 public void start(BundleContext bundleContext
) throws Exception
{
56 // kernel = new Kernel();
58 // } catch (Exception e) {
59 // log.error("Cannot boot kernel", e);
63 this.bc
= bundleContext
;
64 this.permissionAdmin
= getService(ConditionalPermissionAdmin
.class);
65 this.logReaderService
= getService(LogReaderService
.class);
66 this.configurationAdmin
= getService(ConfigurationAdmin
.class);
68 initSecurity();// must be first
73 private void initSecurity() {
74 URL url
= getClass().getClassLoader().getResource(KernelConstants
.JAAS_CONFIG
);
75 System
.setProperty("java.security.auth.login.config", url
.toExternalForm());
78 private void initArgeoLogger() {
79 logger
= new NodeLogger(logReaderService
);
82 bc
.registerService(ArgeoLogger
.class, logger
, null);
85 private void initNodeState() throws IOException
{
86 nodeState
= new CmsState();
89 Configuration nodeConf
= configurationAdmin
.getConfiguration(NodeConstants
.NODE_STATE_PID
);
90 Dictionary
<String
, Object
> props
= nodeConf
.getProperties();
92 if (log
.isDebugEnabled())
93 log
.debug("Clean node state");
94 Dictionary
<String
, Object
> envProps
= getStatePropertiesFromEnvironment();
95 // Use the UUID of the first framework run as state UUID
96 cn
= bc
.getProperty(Constants
.FRAMEWORK_UUID
);
97 envProps
.put(NodeConstants
.CN
, cn
);
98 nodeConf
.update(envProps
);
100 // Check if state is in line with environment
101 Dictionary
<String
, Object
> envProps
= getStatePropertiesFromEnvironment();
102 for (String key
: LangUtils
.keys(envProps
)) {
103 Object envValue
= envProps
.get(key
);
104 Object storedValue
= props
.get(key
);
105 if (storedValue
== null)
106 throw new CmsException("No state value for env " + key
+ "=" + envValue
107 + ", please clean the OSGi configuration.");
108 if (!storedValue
.equals(envValue
))
109 throw new CmsException("State value for " + key
+ "=" + storedValue
110 + " is different from env value =" + envValue
+ ", please clean the OSGi configuration.");
112 cn
= props
.get(NodeConstants
.CN
);
114 throw new CmsException("No state UUID available");
117 Dictionary
<String
, Object
> regProps
= LangUtils
.init(Constants
.SERVICE_PID
, NodeConstants
.NODE_STATE_PID
);
118 regProps
.put(NodeConstants
.CN
, cn
);
119 bc
.registerService(LangUtils
.names(NodeState
.class, ManagedService
.class), nodeState
, regProps
);
124 public void stop(BundleContext bundleContext
) throws Exception
{
125 nodeState
.shutdown();
129 this.permissionAdmin
= null;
130 this.logReaderService
= null;
131 this.configurationAdmin
= null;
133 // if (kernel != null) {
140 private <T
> T
getService(Class
<T
> clazz
) {
141 ServiceReference
<T
> sr
= bc
.getServiceReference(clazz
);
143 throw new CmsException("No service available for " + clazz
);
144 return bc
.getService(sr
);
147 protected Dictionary
<String
, Object
> getStatePropertiesFromEnvironment() {
148 Hashtable
<String
, Object
> props
= new Hashtable
<>();
150 copyFrameworkProp(NodeConstants
.I18N_DEFAULT_LOCALE
, props
);
151 copyFrameworkProp(NodeConstants
.I18N_LOCALES
, props
);
153 copyFrameworkProp(NodeConstants
.ROLES_URI
, props
);
154 copyFrameworkProp(NodeConstants
.USERADMIN_URIS
, props
);
156 for (RepoConf repoConf
: RepoConf
.values())
157 copyFrameworkProp(NodeConstants
.NODE_REPO_PROP_PREFIX
+ repoConf
.name(), props
);
158 // TODO add other environment sources
162 private void copyFrameworkProp(String key
, Dictionary
<String
, Object
> props
) {
163 String value
= bc
.getProperty(key
);
165 props
.put(key
, value
);
168 public static NodeState
getNodeState() {
169 return instance
.nodeState
;
172 public String
[] getLocales() {
174 List
<Locale
> locales
= getNodeState().getLocales();
175 String
[] res
= new String
[locales
.size()];
176 for (int i
= 0; i
< locales
.size(); i
++)
177 res
[i
] = locales
.get(i
).toString();