]> git.argeo.org Git - lgpl/argeo-commons.git/blob - Activator.java
03cbbd90df2473c0bbc74848364a2c9d7fb1bec9
[lgpl/argeo-commons.git] / Activator.java
1 package org.argeo.cms.internal.kernel;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.Dictionary;
6 import java.util.Hashtable;
7 import java.util.List;
8 import java.util.Locale;
9
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;
27
28 /**
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)
31 */
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);
36
37 // private final static String systemKey;
38 // static {
39 // System.setProperty(SYSTEM_KEY_PROPERTY, systemKey);
40 // }
41
42 // private static Kernel kernel;
43 private static Activator instance;
44
45 private BundleContext bc;
46 private ConditionalPermissionAdmin permissionAdmin;
47 private LogReaderService logReaderService;
48 private ConfigurationAdmin configurationAdmin;
49
50 private NodeLogger logger;
51 private CmsState nodeState;
52
53 @Override
54 public void start(BundleContext bundleContext) throws Exception {
55 // try {
56 // kernel = new Kernel();
57 // kernel.init();
58 // } catch (Exception e) {
59 // log.error("Cannot boot kernel", e);
60 // }
61
62 instance = this;
63 this.bc = bundleContext;
64 this.permissionAdmin = getService(ConditionalPermissionAdmin.class);
65 this.logReaderService = getService(LogReaderService.class);
66 this.configurationAdmin = getService(ConfigurationAdmin.class);
67
68 initSecurity();// must be first
69 initArgeoLogger();
70 initNodeState();
71 }
72
73 private void initSecurity() {
74 URL url = getClass().getClassLoader().getResource(KernelConstants.JAAS_CONFIG);
75 System.setProperty("java.security.auth.login.config", url.toExternalForm());
76 }
77
78 private void initArgeoLogger() {
79 logger = new NodeLogger(logReaderService);
80
81 // register
82 bc.registerService(ArgeoLogger.class, logger, null);
83 }
84
85 private void initNodeState() throws IOException {
86 nodeState = new CmsState();
87
88 Object cn;
89 Configuration nodeConf = configurationAdmin.getConfiguration(NodeConstants.NODE_STATE_PID);
90 Dictionary<String, Object> props = nodeConf.getProperties();
91 if (props == null) {
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);
99 } else {
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.");
111 }
112 cn = props.get(NodeConstants.CN);
113 if (cn == null)
114 throw new CmsException("No state UUID available");
115 }
116
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);
120
121 }
122
123 @Override
124 public void stop(BundleContext bundleContext) throws Exception {
125 nodeState.shutdown();
126
127 instance = null;
128 this.bc = null;
129 this.permissionAdmin = null;
130 this.logReaderService = null;
131 this.configurationAdmin = null;
132
133 // if (kernel != null) {
134 // kernel.destroy();
135 // kernel = null;
136 // }
137
138 }
139
140 private <T> T getService(Class<T> clazz) {
141 ServiceReference<T> sr = bc.getServiceReference(clazz);
142 if (sr == null)
143 throw new CmsException("No service available for " + clazz);
144 return bc.getService(sr);
145 }
146
147 protected Dictionary<String, Object> getStatePropertiesFromEnvironment() {
148 Hashtable<String, Object> props = new Hashtable<>();
149 // i18n
150 copyFrameworkProp(NodeConstants.I18N_DEFAULT_LOCALE, props);
151 copyFrameworkProp(NodeConstants.I18N_LOCALES, props);
152 // user admin
153 copyFrameworkProp(NodeConstants.ROLES_URI, props);
154 copyFrameworkProp(NodeConstants.USERADMIN_URIS, props);
155 // data
156 for (RepoConf repoConf : RepoConf.values())
157 copyFrameworkProp(NodeConstants.NODE_REPO_PROP_PREFIX + repoConf.name(), props);
158 // TODO add other environment sources
159 return props;
160 }
161
162 private void copyFrameworkProp(String key, Dictionary<String, Object> props) {
163 String value = bc.getProperty(key);
164 if (value != null)
165 props.put(key, value);
166 }
167
168 public static NodeState getNodeState() {
169 return instance.nodeState;
170 }
171
172 public String[] getLocales() {
173 // TODO optimize?
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();
178 return res;
179 }
180
181 }