]> git.argeo.org Git - lgpl/argeo-commons.git/blob - Activator.java
24c2f6bccc7d1c7e9d8e004f94d56623e92513dc
[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.nio.file.Files;
6 import java.nio.file.Path;
7 import java.util.Dictionary;
8 import java.util.List;
9 import java.util.Locale;
10
11 import javax.security.auth.login.Configuration;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.argeo.cms.CmsException;
16 import org.argeo.node.ArgeoLogger;
17 import org.argeo.node.NodeConstants;
18 import org.argeo.node.NodeDeployment;
19 import org.argeo.node.NodeInstance;
20 import org.argeo.node.NodeState;
21 import org.argeo.util.LangUtils;
22 import org.osgi.framework.BundleActivator;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.Constants;
25 import org.osgi.framework.ServiceReference;
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 private final static Log log = LogFactory.getLog(Activator.class);
34
35 private static Activator instance;
36
37 private BundleContext bc;
38 private LogReaderService logReaderService;
39 // private ConfigurationAdmin configurationAdmin;
40
41 private NodeLogger logger;
42 private CmsState nodeState;
43 private CmsDeployment nodeDeployment;
44 private CmsInstance nodeInstance;
45
46 @Override
47 public void start(BundleContext bundleContext) throws Exception {
48 instance = this;
49 this.bc = bundleContext;
50 this.logReaderService = getService(LogReaderService.class);
51 // this.configurationAdmin = getService(ConfigurationAdmin.class);
52
53 try {
54 initSecurity();// must be first
55 initArgeoLogger();
56 initNode();
57 } catch (Exception e) {
58 log.error("## FATAL: CMS activator failed", e);
59 // throw new CmsException("Cannot initialize node", e);
60 }
61 }
62
63 private void initSecurity() {
64 if (System.getProperty(KernelConstants.JAAS_CONFIG_PROP) == null) {
65 URL url = getClass().getClassLoader().getResource(KernelConstants.JAAS_CONFIG);
66 // URL url =
67 // getClass().getClassLoader().getResource(KernelConstants.JAAS_CONFIG_IPA);
68 System.setProperty(KernelConstants.JAAS_CONFIG_PROP, url.toExternalForm());
69 }
70 Configuration.getConfiguration();
71 }
72
73 private void initArgeoLogger() {
74 logger = new NodeLogger(logReaderService);
75 bc.registerService(ArgeoLogger.class, logger, null);
76 }
77
78 private void initNode() throws IOException {
79 // Node state
80 Path stateUuidPath = bc.getDataFile("stateUuid").toPath();
81 String stateUuid;
82 if (Files.exists(stateUuidPath)) {
83 stateUuid = Files.readAllLines(stateUuidPath).get(0);
84 } else {
85 stateUuid = bc.getProperty(Constants.FRAMEWORK_UUID);
86 Files.write(stateUuidPath, stateUuid.getBytes());
87 }
88 nodeState = new CmsState(stateUuid);
89 Dictionary<String, Object> regProps = LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_STATE_PID);
90 regProps.put(NodeConstants.CN, stateUuid);
91 bc.registerService(NodeState.class, nodeState, regProps);
92
93 // Node deployment
94 nodeDeployment = new CmsDeployment();
95 bc.registerService(NodeDeployment.class, nodeDeployment, null);
96
97 // Node instance
98 nodeInstance = new CmsInstance();
99 bc.registerService(NodeInstance.class, nodeInstance, null);
100 }
101
102 @Override
103 public void stop(BundleContext bundleContext) throws Exception {
104 nodeInstance.shutdown();
105 nodeDeployment.shutdown();
106 nodeState.shutdown();
107
108 instance = null;
109 this.bc = null;
110 this.logReaderService = null;
111 // this.configurationAdmin = null;
112 }
113
114 private <T> T getService(Class<T> clazz) {
115 ServiceReference<T> sr = bc.getServiceReference(clazz);
116 if (sr == null)
117 throw new CmsException("No service available for " + clazz);
118 return bc.getService(sr);
119 }
120
121 public static NodeState getNodeState() {
122 return instance.nodeState;
123 }
124
125 public String[] getLocales() {
126 // TODO optimize?
127 List<Locale> locales = getNodeState().getLocales();
128 String[] res = new String[locales.size()];
129 for (int i = 0; i < locales.size(); i++)
130 res[i] = locales.get(i).toString();
131 return res;
132 }
133
134 }