]> git.argeo.org Git - lgpl/argeo-commons.git/blob - Activator.java
73c530c82d63d1e0ec31e32cb88da7b47a80157a
[lgpl/argeo-commons.git] / Activator.java
1 package org.argeo.cms.internal.kernel;
2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.util.Dictionary;
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.NodeDeployment;
16 import org.argeo.node.NodeInstance;
17 import org.argeo.node.NodeState;
18 import org.argeo.util.LangUtils;
19 import org.ietf.jgss.GSSCredential;
20 import org.osgi.framework.BundleActivator;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.Constants;
23 import org.osgi.framework.ServiceReference;
24 import org.osgi.service.log.LogReaderService;
25
26 /**
27 * Activates the {@link Kernel} from the provided {@link BundleContext}. Gives
28 * access to kernel information for the rest of the bundle (and only it)
29 */
30 public class Activator implements BundleActivator {
31 private final static Log log = LogFactory.getLog(Activator.class);
32
33 private static Activator instance;
34
35 private BundleContext bc;
36 private CmsSecurity nodeSecurity;
37 private LogReaderService logReaderService;
38 // private ConfigurationAdmin configurationAdmin;
39
40 private NodeLogger logger;
41 private CmsState nodeState;
42 private CmsDeployment nodeDeployment;
43 private CmsInstance nodeInstance;
44
45 @Override
46 public void start(BundleContext bundleContext) throws Exception {
47 instance = this;
48 this.bc = bundleContext;
49 this.logReaderService = getService(LogReaderService.class);
50 // this.configurationAdmin = getService(ConfigurationAdmin.class);
51
52 try {
53 nodeSecurity = new CmsSecurity();
54 initArgeoLogger();
55 initNode();
56 } catch (Exception e) {
57 log.error("## FATAL: CMS activator failed", e);
58 // throw new CmsException("Cannot initialize node", e);
59 }
60 }
61
62 private void initArgeoLogger() {
63 logger = new NodeLogger(logReaderService);
64 bc.registerService(ArgeoLogger.class, logger, null);
65 }
66
67 private void initNode() throws IOException {
68 // Node state
69 Path stateUuidPath = bc.getDataFile("stateUuid").toPath();
70 String stateUuid;
71 if (Files.exists(stateUuidPath)) {
72 stateUuid = Files.readAllLines(stateUuidPath).get(0);
73 } else {
74 stateUuid = bc.getProperty(Constants.FRAMEWORK_UUID);
75 Files.write(stateUuidPath, stateUuid.getBytes());
76 }
77 nodeState = new CmsState(stateUuid);
78 Dictionary<String, Object> regProps = LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_STATE_PID);
79 regProps.put(NodeConstants.CN, stateUuid);
80 bc.registerService(NodeState.class, nodeState, regProps);
81
82 // Node deployment
83 nodeDeployment = new CmsDeployment();
84 bc.registerService(NodeDeployment.class, nodeDeployment, null);
85
86 // Node instance
87 nodeInstance = new CmsInstance();
88 bc.registerService(NodeInstance.class, nodeInstance, null);
89 }
90
91 @Override
92 public void stop(BundleContext bundleContext) throws Exception {
93 nodeInstance.shutdown();
94 nodeDeployment.shutdown();
95 nodeState.shutdown();
96
97 instance = null;
98 this.bc = null;
99 this.logReaderService = null;
100 // this.configurationAdmin = null;
101 }
102
103 private <T> T getService(Class<T> clazz) {
104 ServiceReference<T> sr = bc.getServiceReference(clazz);
105 if (sr == null)
106 throw new CmsException("No service available for " + clazz);
107 return bc.getService(sr);
108 }
109
110 public static NodeState getNodeState() {
111 return instance.nodeState;
112 }
113
114 public static GSSCredential getAcceptorCredentials() {
115 return getCmsSecurity().getServerCredentials();
116 }
117
118 static CmsSecurity getCmsSecurity() {
119 return instance.nodeSecurity;
120 }
121
122 public String[] getLocales() {
123 // TODO optimize?
124 List<Locale> locales = getNodeState().getLocales();
125 String[] res = new String[locales.size()];
126 for (int i = 0; i < locales.size(); i++)
127 res[i] = locales.get(i).toString();
128 return res;
129 }
130
131 }