]> git.argeo.org Git - lgpl/argeo-commons.git/blob - Activator.java
25746a48116b29f84930e93ea7a18272d1199412
[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.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 } catch (Exception e) {
63 log.error("## FATAL: CMS activator failed", e);
64 }
65 }
66
67 private void initSecurity() {
68 if (System.getProperty(KernelConstants.JAAS_CONFIG_PROP) == null) {
69 String jaasConfig = KernelConstants.JAAS_CONFIG;
70 URL url = getClass().getClassLoader().getResource(jaasConfig);
71 // System.setProperty(KernelConstants.JAAS_CONFIG_PROP,
72 // url.toExternalForm());
73 KernelUtils.setJaasConfiguration(url);
74 }
75 // explicitly load JAAS configuration
76 Configuration.getConfiguration();
77
78 // ConditionalPermissionAdmin permissionAdmin = bc
79 // .getService(bc.getServiceReference(ConditionalPermissionAdmin.class));
80 // ConditionalPermissionUpdate update =
81 // permissionAdmin.newConditionalPermissionUpdate();
82 // // Self
83 // update.getConditionalPermissionInfos()
84 // .add(permissionAdmin.newConditionalPermissionInfo(null,
85 // new ConditionInfo[] {
86 // new ConditionInfo(BundleLocationCondition.class.getName(), new
87 // String[] { "*" }) },
88 // new PermissionInfo[] { new
89 // PermissionInfo(AllPermission.class.getName(), null, null) },
90 // ConditionalPermissionInfo.ALLOW));
91 //
92 }
93
94 private void initArgeoLogger() {
95 // Jetty
96 // disable integration of Jetty logging with SLF4J
97 // in order to avoid chicken and egg problems
98 // org.eclipse.jetty.util.log.Log.setLog(new StdErrLog());
99 // org.eclipse.jetty.util.log.Logger jettyLog =
100 // org.eclipse.jetty.util.log.Log.getLog();
101 // if (jettyLog != null) {
102 // jettyLog.warn("TEST JETTY LOG", new Object[0]);
103 // }
104
105 logger = new NodeLogger(logReaderService);
106 bc.registerService(ArgeoLogger.class, logger, null);
107 }
108
109 private void initNode() throws IOException {
110 // Node state
111 Path stateUuidPath = bc.getDataFile("stateUuid").toPath();
112 String stateUuid;
113 if (Files.exists(stateUuidPath)) {
114 stateUuid = Files.readAllLines(stateUuidPath).get(0);
115 } else {
116 stateUuid = bc.getProperty(Constants.FRAMEWORK_UUID);
117 Files.write(stateUuidPath, stateUuid.getBytes());
118 }
119 nodeState = new CmsState(stateUuid);
120 Dictionary<String, Object> regProps = LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_STATE_PID);
121 regProps.put(NodeConstants.CN, stateUuid);
122 bc.registerService(NodeState.class, nodeState, regProps);
123
124 // Node deployment
125 nodeDeployment = new CmsDeployment();
126 bc.registerService(NodeDeployment.class, nodeDeployment, null);
127
128 // Node instance
129 nodeInstance = new CmsInstance();
130 bc.registerService(NodeInstance.class, nodeInstance, null);
131 }
132
133 @Override
134 public void stop(BundleContext bundleContext) throws Exception {
135 try {
136 if (nodeInstance != null)
137 nodeInstance.shutdown();
138 if (nodeDeployment != null)
139 nodeDeployment.shutdown();
140 if (nodeState != null)
141 nodeState.shutdown();
142
143 instance = null;
144 this.bc = null;
145 this.logReaderService = null;
146 // this.configurationAdmin = null;
147 } catch (Exception e) {
148 log.error("CMS activator shutdown failed", e);
149 }
150 }
151
152 private <T> T getService(Class<T> clazz) {
153 ServiceReference<T> sr = bc.getServiceReference(clazz);
154 if (sr == null)
155 throw new CmsException("No service available for " + clazz);
156 return bc.getService(sr);
157 }
158
159 public static NodeState getNodeState() {
160 return instance.nodeState;
161 }
162
163 public static GSSCredential getAcceptorCredentials() {
164 return getNodeUserAdmin().getAcceptorCredentials();
165 }
166
167 public static boolean isSingleUser() {
168 return getNodeUserAdmin().isSingleUser();
169 }
170
171 private static NodeUserAdmin getNodeUserAdmin() {
172 ServiceReference<UserAdmin> sr = instance.bc.getServiceReference(UserAdmin.class);
173 NodeUserAdmin userAdmin = (NodeUserAdmin) instance.bc.getService(sr);
174 return userAdmin;
175
176 }
177
178 // static CmsSecurity getCmsSecurity() {
179 // return instance.nodeSecurity;
180 // }
181
182 public String[] getLocales() {
183 // TODO optimize?
184 List<Locale> locales = getNodeState().getLocales();
185 String[] res = new String[locales.size()];
186 for (int i = 0; i < locales.size(); i++)
187 res[i] = locales.get(i).toString();
188 return res;
189 }
190
191 }