]> git.argeo.org Git - lgpl/argeo-commons.git/blob - Activator.java
5ef545e6fa49e8cc59880ae9f3dc9b6fb5db14c0
[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 import org.osgi.util.tracker.ServiceTracker;
30
31 /**
32 * Activates the kernel. Gives access to kernel information for the rest of the
33 * 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
42 private LogReaderService logReaderService;
43
44 private NodeLogger logger;
45 private CmsState nodeState;
46 private CmsDeployment nodeDeployment;
47 private CmsInstance nodeInstance;
48
49 private ServiceTracker<UserAdmin, NodeUserAdmin> userAdminSt;
50
51 @Override
52 public void start(BundleContext bundleContext) throws Exception {
53 Runtime.getRuntime().addShutdownHook(new CmsShutdown());
54 instance = this;
55 this.bc = bundleContext;
56 this.logReaderService = getService(LogReaderService.class);
57
58 try {
59 initSecurity();
60 initArgeoLogger();
61 initNode();
62
63 userAdminSt = new ServiceTracker<>(instance.bc, UserAdmin.class, null);
64 userAdminSt.open();
65 log.debug("Kernel bundle started");
66 } catch (Throwable e) {
67 log.error("## FATAL: CMS activator failed", e);
68 }
69 }
70
71 private void initSecurity() {
72 if (System.getProperty(KernelConstants.JAAS_CONFIG_PROP) == null) {
73 String jaasConfig = KernelConstants.JAAS_CONFIG;
74 URL url = getClass().getClassLoader().getResource(jaasConfig);
75 // System.setProperty(KernelConstants.JAAS_CONFIG_PROP,
76 // url.toExternalForm());
77 KernelUtils.setJaasConfiguration(url);
78 }
79 // explicitly load JAAS configuration
80 Configuration.getConfiguration();
81
82 // ConditionalPermissionAdmin permissionAdmin = bc
83 // .getService(bc.getServiceReference(ConditionalPermissionAdmin.class));
84 // ConditionalPermissionUpdate update =
85 // permissionAdmin.newConditionalPermissionUpdate();
86 // // Self
87 // update.getConditionalPermissionInfos()
88 // .add(permissionAdmin.newConditionalPermissionInfo(null,
89 // new ConditionInfo[] {
90 // new ConditionInfo(BundleLocationCondition.class.getName(), new
91 // String[] { "*" }) },
92 // new PermissionInfo[] { new
93 // PermissionInfo(AllPermission.class.getName(), null, null) },
94 // ConditionalPermissionInfo.ALLOW));
95 //
96 }
97
98 private void initArgeoLogger() {
99 logger = new NodeLogger(logReaderService);
100 bc.registerService(ArgeoLogger.class, logger, null);
101 }
102
103 private void initNode() throws IOException {
104 // Node state
105 Path stateUuidPath = bc.getDataFile("stateUuid").toPath();
106 String stateUuid;
107 if (Files.exists(stateUuidPath)) {
108 stateUuid = Files.readAllLines(stateUuidPath).get(0);
109 } else {
110 stateUuid = bc.getProperty(Constants.FRAMEWORK_UUID);
111 Files.write(stateUuidPath, stateUuid.getBytes());
112 }
113 nodeState = new CmsState(stateUuid);
114 Dictionary<String, Object> regProps = LangUtils.dico(Constants.SERVICE_PID, NodeConstants.NODE_STATE_PID);
115 regProps.put(NodeConstants.CN, stateUuid);
116 bc.registerService(NodeState.class, nodeState, regProps);
117
118 // Node deployment
119 nodeDeployment = new CmsDeployment();
120 bc.registerService(NodeDeployment.class, nodeDeployment, null);
121
122 // Node instance
123 nodeInstance = new CmsInstance();
124 bc.registerService(NodeInstance.class, nodeInstance, null);
125 }
126
127 @Override
128 public void stop(BundleContext bundleContext) throws Exception {
129 try {
130 if (nodeInstance != null)
131 nodeInstance.shutdown();
132 if (nodeDeployment != null)
133 nodeDeployment.shutdown();
134 if (nodeState != null)
135 nodeState.shutdown();
136
137 if (userAdminSt != null)
138 userAdminSt.close();
139
140 instance = null;
141 this.bc = null;
142 this.logReaderService = null;
143 // this.configurationAdmin = null;
144 } catch (Exception e) {
145 log.error("CMS activator shutdown failed", e);
146 }
147 }
148
149 private <T> T getService(Class<T> clazz) {
150 ServiceReference<T> sr = bc.getServiceReference(clazz);
151 if (sr == null)
152 throw new CmsException("No service available for " + clazz);
153 return bc.getService(sr);
154 }
155
156 public static NodeState getNodeState() {
157 return instance.nodeState;
158 }
159
160 public static GSSCredential getAcceptorCredentials() {
161 return getNodeUserAdmin().getAcceptorCredentials();
162 }
163
164 public static boolean isSingleUser() {
165 return getNodeUserAdmin().isSingleUser();
166 }
167
168 public static UserAdmin getUserAdmin() {
169 return (UserAdmin) getNodeUserAdmin();
170 }
171
172 public static String getHttpProxySslHeader() {
173 return KernelUtils.getFrameworkProp(NodeConstants.HTTP_PROXY_SSL_DN);
174 }
175
176 private static NodeUserAdmin getNodeUserAdmin() {
177 NodeUserAdmin res;
178 try {
179 res = instance.userAdminSt.waitForService(60000);
180 } catch (InterruptedException e) {
181 throw new CmsException("Cannot retrieve Node user admin", e);
182 }
183 if (res == null)
184 throw new CmsException("No Node user admin found");
185
186 return res;
187 // ServiceReference<UserAdmin> sr =
188 // instance.bc.getServiceReference(UserAdmin.class);
189 // NodeUserAdmin userAdmin = (NodeUserAdmin) instance.bc.getService(sr);
190 // return userAdmin;
191
192 }
193
194 // static CmsSecurity getCmsSecurity() {
195 // return instance.nodeSecurity;
196 // }
197
198 public String[] getLocales() {
199 // TODO optimize?
200 List<Locale> locales = getNodeState().getLocales();
201 String[] res = new String[locales.size()];
202 for (int i = 0; i < locales.size(); i++)
203 res[i] = locales.get(i).toString();
204 return res;
205 }
206
207 }