]> git.argeo.org Git - lgpl/argeo-commons.git/blob - kernel/Activator.java
Prepare next development cycle
[lgpl/argeo-commons.git] / kernel / 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 org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.cms.CmsException;
14 import org.argeo.node.ArgeoLogger;
15 import org.argeo.node.NodeConstants;
16 import org.argeo.node.NodeDeployment;
17 import org.argeo.node.NodeInstance;
18 import org.argeo.node.NodeState;
19 import org.argeo.util.LangUtils;
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.condpermadmin.ConditionalPermissionAdmin;
25 import org.osgi.service.log.LogReaderService;
26
27 /**
28 * Activates the {@link Kernel} from the provided {@link BundleContext}. Gives
29 * access to kernel information for the rest of the bundle (and only it)
30 */
31 public class Activator implements BundleActivator {
32 private final Log log = LogFactory.getLog(Activator.class);
33
34 private static Activator instance;
35
36 private BundleContext bc;
37 private ConditionalPermissionAdmin permissionAdmin;
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.permissionAdmin = getService(ConditionalPermissionAdmin.class);
51 this.logReaderService = getService(LogReaderService.class);
52 // this.configurationAdmin = getService(ConfigurationAdmin.class);
53
54 initSecurity();// must be first
55 initArgeoLogger();
56 try {
57 initNode();
58 } catch (Exception e) {
59 e.printStackTrace();
60 throw new CmsException("Cannot initialize node", e);
61 }
62 }
63
64 private void initSecurity() {
65 URL url = getClass().getClassLoader().getResource(KernelConstants.JAAS_CONFIG);
66 System.setProperty("java.security.auth.login.config", url.toExternalForm());
67 }
68
69 private void initArgeoLogger() {
70 logger = new NodeLogger(logReaderService);
71 bc.registerService(ArgeoLogger.class, logger, null);
72 }
73
74 private void initNode() throws IOException {
75 // Node state
76 Path stateUuidPath = bc.getDataFile("stateUuid").toPath();
77 String stateUuid;
78 if (Files.exists(stateUuidPath)) {
79 stateUuid = Files.readAllLines(stateUuidPath).get(0);
80 } else {
81 stateUuid = bc.getProperty(Constants.FRAMEWORK_UUID);
82 Files.write(stateUuidPath, stateUuid.getBytes());
83 }
84 nodeState = new CmsState(stateUuid);
85 // Object cn;
86 // Configuration nodeConf =
87 // configurationAdmin.getConfiguration(NodeConstants.NODE_STATE_PID);
88 // Dictionary<String, Object> props = nodeConf.getProperties();
89 // if (props == null) {
90 // if (log.isDebugEnabled())
91 // log.debug("Clean node state");
92 // Dictionary<String, Object> envProps = new Hashtable<>();
93 // // Use the UUID of the first framework run as state UUID
94 // cn = bc.getProperty(Constants.FRAMEWORK_UUID);
95 // envProps.put(NodeConstants.CN, cn);
96 // nodeConf.update(envProps);
97 // } else {
98 // cn = props.get(NodeConstants.CN);
99 // if (cn == null)
100 // throw new CmsException("No state UUID available");
101 // }
102 Dictionary<String, Object> regProps = LangUtils.init(Constants.SERVICE_PID, NodeConstants.NODE_STATE_PID);
103 regProps.put(NodeConstants.CN, stateUuid);
104 bc.registerService(NodeState.class, nodeState, regProps);
105
106 // Node deployment
107 nodeDeployment = new CmsDeployment();
108 bc.registerService(NodeDeployment.class, nodeDeployment, null);
109
110 // Node instance
111 nodeInstance = new CmsInstance();
112 bc.registerService(NodeInstance.class, nodeInstance, null);
113 }
114
115 @Override
116 public void stop(BundleContext bundleContext) throws Exception {
117 nodeInstance.shutdown();
118 nodeDeployment.shutdown();
119 nodeState.shutdown();
120
121 instance = null;
122 this.bc = null;
123 this.permissionAdmin = null;
124 this.logReaderService = null;
125 // this.configurationAdmin = null;
126 }
127
128 private <T> T getService(Class<T> clazz) {
129 ServiceReference<T> sr = bc.getServiceReference(clazz);
130 if (sr == null)
131 throw new CmsException("No service available for " + clazz);
132 return bc.getService(sr);
133 }
134
135 public static NodeState getNodeState() {
136 return instance.nodeState;
137 }
138
139 public String[] getLocales() {
140 // TODO optimize?
141 List<Locale> locales = getNodeState().getLocales();
142 String[] res = new String[locales.size()];
143 for (int i = 0; i < locales.size(); i++)
144 res[i] = locales.get(i).toString();
145 return res;
146 }
147
148 }