]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/Activator.java
Implement lout at repository level in Data Explorer
[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.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.ietf.jgss.GSSCredential;
23 import org.osgi.framework.BundleActivator;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.Constants;
26 import org.osgi.framework.ServiceReference;
27 import org.osgi.service.log.LogReaderService;
28 import org.osgi.service.useradmin.UserAdmin;
29
30 /**
31 * Activates the kernel. Gives access to kernel information for the rest of the
32 * bundle (and only it)
33 */
34 public class Activator implements BundleActivator {
35 private final static Log log = LogFactory.getLog(Activator.class);
36
37 private static Activator instance;
38
39 private BundleContext bc;
40 // private CmsSecurity nodeSecurity;
41 private LogReaderService logReaderService;
42 // private ConfigurationAdmin configurationAdmin;
43
44 private NodeLogger logger;
45 private CmsState nodeState;
46 private CmsDeployment nodeDeployment;
47 private CmsInstance nodeInstance;
48
49 @Override
50 public void start(BundleContext bundleContext) throws Exception {
51 Runtime.getRuntime().addShutdownHook(new CmsShutdown());
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 log.debug("Kernel bundle started");
63 } catch (Exception e) {
64 log.error("## FATAL: CMS activator failed", 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 // ConditionalPermissionAdmin permissionAdmin = bc
80 // .getService(bc.getServiceReference(ConditionalPermissionAdmin.class));
81 // ConditionalPermissionUpdate update =
82 // permissionAdmin.newConditionalPermissionUpdate();
83 // // Self
84 // update.getConditionalPermissionInfos()
85 // .add(permissionAdmin.newConditionalPermissionInfo(null,
86 // new ConditionInfo[] {
87 // new ConditionInfo(BundleLocationCondition.class.getName(), new
88 // String[] { "*" }) },
89 // new PermissionInfo[] { new
90 // PermissionInfo(AllPermission.class.getName(), null, null) },
91 // ConditionalPermissionInfo.ALLOW));
92 //
93 }
94
95 private void initArgeoLogger() {
96 // Jetty
97 // disable integration of Jetty logging with SLF4J
98 // in order to avoid chicken and egg problems
99 // org.eclipse.jetty.util.log.Log.setLog(new StdErrLog());
100 // org.eclipse.jetty.util.log.Logger jettyLog =
101 // org.eclipse.jetty.util.log.Log.getLog();
102 // if (jettyLog != null) {
103 // jettyLog.warn("TEST JETTY LOG", new Object[0]);
104 // }
105
106 logger = new NodeLogger(logReaderService);
107 bc.registerService(ArgeoLogger.class, logger, null);
108 }
109
110 private void initNode() throws IOException {
111 // Node state
112 Path stateUuidPath = bc.getDataFile("stateUuid").toPath();
113 String stateUuid;
114 if (Files.exists(stateUuidPath)) {
115 stateUuid = Files.readAllLines(stateUuidPath).get(0);
116 } else {
117 stateUuid = bc.getProperty(Constants.FRAMEWORK_UUID);
118 Files.write(stateUuidPath, stateUuid.getBytes());
119 }
120 nodeState = new CmsState(stateUuid);
121 Dictionary<String, Object> regProps = LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_STATE_PID);
122 regProps.put(NodeConstants.CN, stateUuid);
123 bc.registerService(NodeState.class, nodeState, regProps);
124
125 // Node deployment
126 nodeDeployment = new CmsDeployment();
127 bc.registerService(NodeDeployment.class, nodeDeployment, null);
128
129 // Node instance
130 nodeInstance = new CmsInstance();
131 bc.registerService(NodeInstance.class, nodeInstance, null);
132 }
133
134 @Override
135 public void stop(BundleContext bundleContext) throws Exception {
136 try {
137 if (nodeInstance != null)
138 nodeInstance.shutdown();
139 if (nodeDeployment != null)
140 nodeDeployment.shutdown();
141 if (nodeState != null)
142 nodeState.shutdown();
143
144 instance = null;
145 this.bc = null;
146 this.logReaderService = null;
147 // this.configurationAdmin = null;
148 } catch (Exception e) {
149 log.error("CMS activator shutdown failed", e);
150 }
151 }
152
153 private <T> T getService(Class<T> clazz) {
154 ServiceReference<T> sr = bc.getServiceReference(clazz);
155 if (sr == null)
156 throw new CmsException("No service available for " + clazz);
157 return bc.getService(sr);
158 }
159
160 public static NodeState getNodeState() {
161 return instance.nodeState;
162 }
163
164 public static GSSCredential getAcceptorCredentials() {
165 return getNodeUserAdmin().getAcceptorCredentials();
166 }
167
168 public static boolean isSingleUser() {
169 return getNodeUserAdmin().isSingleUser();
170 }
171
172 private static NodeUserAdmin getNodeUserAdmin() {
173 ServiceReference<UserAdmin> sr = instance.bc.getServiceReference(UserAdmin.class);
174 NodeUserAdmin userAdmin = (NodeUserAdmin) instance.bc.getService(sr);
175 return userAdmin;
176
177 }
178
179 // static CmsSecurity getCmsSecurity() {
180 // return instance.nodeSecurity;
181 // }
182
183 public String[] getLocales() {
184 // TODO optimize?
185 List<Locale> locales = getNodeState().getLocales();
186 String[] res = new String[locales.size()];
187 for (int i = 0; i < locales.size(); i++)
188 res[i] = locales.get(i).toString();
189 return res;
190 }
191
192 }