]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/Activator.java
[maven-release-plugin] prepare for next development iteration
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / Activator.java
1 package org.argeo.cms.internal.kernel;
2
3 import java.awt.image.Kernel;
4 import java.io.IOException;
5 import java.net.URL;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.util.Dictionary;
9 import java.util.List;
10 import java.util.Locale;
11
12 import javax.security.auth.login.Configuration;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.argeo.cms.CmsException;
17 import org.argeo.node.ArgeoLogger;
18 import org.argeo.node.NodeConstants;
19 import org.argeo.node.NodeDeployment;
20 import org.argeo.node.NodeInstance;
21 import org.argeo.node.NodeState;
22 import org.argeo.util.LangUtils;
23 import org.ietf.jgss.GSSCredential;
24 import org.osgi.framework.BundleActivator;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.Constants;
27 import org.osgi.framework.ServiceReference;
28 import org.osgi.service.log.LogReaderService;
29 import org.osgi.service.useradmin.UserAdmin;
30
31 /**
32 * Activates the {@link Kernel} from the provided {@link BundleContext}. Gives
33 * access to kernel information for the rest of the bundle (and only it)
34 */
35 public class Activator implements BundleActivator {
36 private final static Log log = LogFactory.getLog(Activator.class);
37
38 private static Activator instance;
39
40 private BundleContext bc;
41 // private CmsSecurity nodeSecurity;
42 private LogReaderService logReaderService;
43 // private ConfigurationAdmin configurationAdmin;
44
45 // private NodeLogger logger;
46 private CmsState nodeState;
47 private CmsDeployment nodeDeployment;
48 private CmsInstance nodeInstance;
49
50 @Override
51 public void start(BundleContext bundleContext) throws Exception {
52 instance = this;
53 this.bc = bundleContext;
54 this.logReaderService = getService(LogReaderService.class);
55 // this.configurationAdmin = getService(ConfigurationAdmin.class);
56
57 try {
58 // nodeSecurity = new CmsSecurity();
59 initSecurity();
60 initArgeoLogger();
61 initNode();
62 } catch (Exception e) {
63 log.error("## FATAL: CMS activator failed", e);
64 // throw new CmsException("Cannot initialize node", e);
65 }
66 }
67
68 private void initSecurity() {
69 if (System.getProperty(KernelConstants.JAAS_CONFIG_PROP) == null) {
70 String jaasConfig = KernelConstants.JAAS_CONFIG;
71 URL url = getClass().getClassLoader().getResource(jaasConfig);
72 // System.setProperty(KernelConstants.JAAS_CONFIG_PROP,
73 // url.toExternalForm());
74 KernelUtils.setJaasConfiguration(url);
75 }
76 // explicitly load JAAS configuration
77 Configuration.getConfiguration();
78 }
79
80 private void initArgeoLogger() {
81 // logger = new NodeLogger(logReaderService);
82 // bc.registerService(ArgeoLogger.class, logger, null);
83 }
84
85 private void initNode() throws IOException {
86 // Node state
87 Path stateUuidPath = bc.getDataFile("stateUuid").toPath();
88 String stateUuid;
89 if (Files.exists(stateUuidPath)) {
90 stateUuid = Files.readAllLines(stateUuidPath).get(0);
91 } else {
92 stateUuid = bc.getProperty(Constants.FRAMEWORK_UUID);
93 Files.write(stateUuidPath, stateUuid.getBytes());
94 }
95 nodeState = new CmsState(stateUuid);
96 Dictionary<String, Object> regProps = LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_STATE_PID);
97 regProps.put(NodeConstants.CN, stateUuid);
98 bc.registerService(NodeState.class, nodeState, regProps);
99
100 // Node deployment
101 nodeDeployment = new CmsDeployment();
102 bc.registerService(NodeDeployment.class, nodeDeployment, null);
103
104 // Node instance
105 nodeInstance = new CmsInstance();
106 bc.registerService(NodeInstance.class, nodeInstance, null);
107 }
108
109 @Override
110 public void stop(BundleContext bundleContext) throws Exception {
111 nodeInstance.shutdown();
112 nodeDeployment.shutdown();
113 nodeState.shutdown();
114
115 instance = null;
116 this.bc = null;
117 this.logReaderService = null;
118 // this.configurationAdmin = null;
119 }
120
121 private <T> T getService(Class<T> clazz) {
122 ServiceReference<T> sr = bc.getServiceReference(clazz);
123 if (sr == null)
124 throw new CmsException("No service available for " + clazz);
125 return bc.getService(sr);
126 }
127
128 public static NodeState getNodeState() {
129 return instance.nodeState;
130 }
131
132 public static GSSCredential getAcceptorCredentials() {
133 ServiceReference<UserAdmin> sr = instance.bc.getServiceReference(UserAdmin.class);
134 NodeUserAdmin userAdmin = (NodeUserAdmin) instance.bc.getService(sr);
135 return userAdmin.getAcceptorCredentials();
136 }
137
138 // static CmsSecurity getCmsSecurity() {
139 // return instance.nodeSecurity;
140 // }
141
142 public String[] getLocales() {
143 // TODO optimize?
144 List<Locale> locales = getNodeState().getLocales();
145 String[] res = new String[locales.size()];
146 for (int i = 0; i < locales.size(); i++)
147 res[i] = locales.get(i).toString();
148 return res;
149 }
150
151 }