]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsActivator.java
d4b2f4595a8b400b0cadf5c8383c80614b0a1dcb
[lgpl/argeo-commons.git] / CmsActivator.java
1 package org.argeo.cms.internal.osgi;
2
3 import java.security.AllPermission;
4 import java.util.Dictionary;
5
6 import org.argeo.api.cms.CmsLog;
7 import org.argeo.cms.ArgeoLogger;
8 import org.osgi.framework.BundleActivator;
9 import org.osgi.framework.BundleContext;
10 import org.osgi.framework.Constants;
11 import org.osgi.service.condpermadmin.BundleLocationCondition;
12 import org.osgi.service.condpermadmin.ConditionInfo;
13 import org.osgi.service.condpermadmin.ConditionalPermissionAdmin;
14 import org.osgi.service.condpermadmin.ConditionalPermissionInfo;
15 import org.osgi.service.condpermadmin.ConditionalPermissionUpdate;
16 import org.osgi.service.log.LogReaderService;
17 import org.osgi.service.permissionadmin.PermissionInfo;
18
19 /**
20 * Activates the kernel. Gives access to kernel information for the rest of the
21 * bundle (and only it)
22 */
23 public class CmsActivator implements BundleActivator {
24 private final static CmsLog log = CmsLog.getLog(CmsActivator.class);
25
26 // private static Activator instance;
27
28 // TODO make it configurable
29 private boolean hardened = false;
30
31 private static BundleContext bundleContext;
32
33 private LogReaderService logReaderService;
34
35 private CmsOsgiLogger logger;
36 // private CmsStateImpl nodeState;
37 // private CmsDeploymentImpl nodeDeployment;
38 // private CmsContextImpl nodeInstance;
39
40 // private ServiceTracker<UserAdmin, NodeUserAdmin> userAdminSt;
41
42 // static {
43 // Bundle bundle = FrameworkUtil.getBundle(Activator.class);
44 // if (bundle != null) {
45 // bundleContext = bundle.getBundleContext();
46 // }
47 // }
48
49 void init() {
50 // Runtime.getRuntime().addShutdownHook(new CmsShutdown());
51 // instance = this;
52 // this.bc = bundleContext;
53 if (bundleContext != null)
54 this.logReaderService = getService(LogReaderService.class);
55 initArgeoLogger();
56 // this.internalExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
57 //
58 // try {
59 // initSecurity();
60 //// initArgeoLogger();
61 // initNode();
62 //
63 // if (log.isTraceEnabled())
64 // log.trace("Kernel bundle started");
65 // } catch (Throwable e) {
66 // log.error("## FATAL: CMS activator failed", e);
67 // }
68 }
69
70 void destroy() {
71 try {
72 // if (nodeInstance != null)
73 // nodeInstance.shutdown();
74 // if (nodeDeployment != null)
75 // nodeDeployment.shutdown();
76 // if (nodeState != null)
77 // nodeState.shutdown();
78 //
79 // if (userAdminSt != null)
80 // userAdminSt.close();
81
82 // internalExecutorService.shutdown();
83 // instance = null;
84 bundleContext = null;
85 this.logReaderService = null;
86 // this.configurationAdmin = null;
87 } catch (Exception e) {
88 log.error("CMS activator shutdown failed", e);
89 }
90
91 new GogoShellKiller().start();
92 }
93
94 private void initSecurity() {
95 // code-level permissions
96 String osgiSecurity = bundleContext.getProperty(Constants.FRAMEWORK_SECURITY);
97 if (osgiSecurity != null && Constants.FRAMEWORK_SECURITY_OSGI.equals(osgiSecurity)) {
98 // TODO rather use a tracker?
99 ConditionalPermissionAdmin permissionAdmin = bundleContext
100 .getService(bundleContext.getServiceReference(ConditionalPermissionAdmin.class));
101 if (!hardened) {
102 // All permissions to all bundles
103 ConditionalPermissionUpdate update = permissionAdmin.newConditionalPermissionUpdate();
104 update.getConditionalPermissionInfos().add(permissionAdmin.newConditionalPermissionInfo(null,
105 new ConditionInfo[] {
106 new ConditionInfo(BundleLocationCondition.class.getName(), new String[] { "*" }) },
107 new PermissionInfo[] { new PermissionInfo(AllPermission.class.getName(), null, null) },
108 ConditionalPermissionInfo.ALLOW));
109 // TODO data admin permission
110 // PermissionInfo dataAdminPerm = new PermissionInfo(AuthPermission.class.getName(),
111 // "createLoginContext." + NodeConstants.LOGIN_CONTEXT_DATA_ADMIN, null);
112 // update.getConditionalPermissionInfos().add(permissionAdmin.newConditionalPermissionInfo(null,
113 // new ConditionInfo[] {
114 // new ConditionInfo(BundleLocationCondition.class.getName(), new String[] { "*" }) },
115 // new PermissionInfo[] { dataAdminPerm }, ConditionalPermissionInfo.DENY));
116 // update.getConditionalPermissionInfos().add(permissionAdmin.newConditionalPermissionInfo(null,
117 // new ConditionInfo[] {
118 // new ConditionInfo(BundleSignerCondition.class.getName(), new String[] { "CN=\"Eclipse.org Foundation, Inc.\", OU=IT, O=\"Eclipse.org Foundation, Inc.\", L=Nepean, ST=Ontario, C=CA" }) },
119 // new PermissionInfo[] { dataAdminPerm }, ConditionalPermissionInfo.ALLOW));
120 update.commit();
121 } else {
122 SecurityProfile securityProfile = new SecurityProfile() {
123 };
124 securityProfile.applySystemPermissions(permissionAdmin);
125 }
126 }
127
128 }
129
130 private void initArgeoLogger() {
131 logger = new CmsOsgiLogger(logReaderService);
132 if (bundleContext != null)
133 bundleContext.registerService(ArgeoLogger.class, logger, null);
134 }
135
136 // private void initNode() throws IOException {
137 // // Node state
138 // nodeState = new CmsStateImpl();
139 // registerService(CmsState.class, nodeState, null);
140 //
141 // // Node deployment
142 // nodeDeployment = new CmsDeploymentImpl();
143 //// registerService(NodeDeployment.class, nodeDeployment, null);
144 //
145 // // Node instance
146 // nodeInstance = new CmsContextImpl();
147 // registerService(CmsContext.class, nodeInstance, null);
148 // }
149
150 public static <T> void registerService(Class<T> clss, T service, Dictionary<String, ?> properties) {
151 if (bundleContext != null) {
152 bundleContext.registerService(clss, service, properties);
153 }
154
155 }
156
157 public static <T> T getService(Class<T> clss) {
158 if (bundleContext != null) {
159 return bundleContext.getService(bundleContext.getServiceReference(clss));
160 } else {
161 return null;
162 }
163 }
164
165 /*
166 * OSGi
167 */
168
169 @Override
170 public void start(BundleContext bc) throws Exception {
171 bundleContext = bc;
172 // if (!bc.getBundle().equals(bundleContext.getBundle()))
173 // throw new IllegalStateException(
174 // "Bundle " + bc.getBundle() + " is not consistent with " + bundleContext.getBundle());
175 init();
176 // userAdminSt = new ServiceTracker<>(bundleContext, UserAdmin.class, null);
177 // userAdminSt.open();
178
179 // ServiceTracker<?, ?> httpSt = new ServiceTracker<HttpService, HttpService>(bc, HttpService.class, null) {
180 //
181 // @Override
182 // public HttpService addingService(ServiceReference<HttpService> sr) {
183 // Object httpPort = sr.getProperty("http.port");
184 // Object httpsPort = sr.getProperty("https.port");
185 // log.info(httpPortsMsg(httpPort, httpsPort));
186 // close();
187 // return super.addingService(sr);
188 // }
189 // };
190 // httpSt.open();
191 }
192
193 private String httpPortsMsg(Object httpPort, Object httpsPort) {
194 return (httpPort != null ? "HTTP " + httpPort + " " : " ") + (httpsPort != null ? "HTTPS " + httpsPort : "");
195 }
196
197 @Override
198 public void stop(BundleContext bc) throws Exception {
199 // if (!bc.getBundle().equals(bundleContext.getBundle()))
200 // throw new IllegalStateException(
201 // "Bundle " + bc.getBundle() + " is not consistent with " + bundleContext.getBundle());
202 destroy();
203 bundleContext = null;
204 }
205
206 // private <T> T getService(Class<T> clazz) {
207 // ServiceReference<T> sr = bundleContext.getServiceReference(clazz);
208 // if (sr == null)
209 // throw new IllegalStateException("No service available for " + clazz);
210 // return bundleContext.getService(sr);
211 // }
212
213 // public static GSSCredential getAcceptorCredentials() {
214 // return getNodeUserAdmin().getAcceptorCredentials();
215 // }
216 //
217 // @Deprecated
218 // public static boolean isSingleUser() {
219 // return getNodeUserAdmin().isSingleUser();
220 // }
221 //
222 // public static UserAdmin getUserAdmin() {
223 // return (UserAdmin) getNodeUserAdmin();
224 // }
225 //
226 // public static String getHttpProxySslHeader() {
227 // return KernelUtils.getFrameworkProp(CmsConstants.HTTP_PROXY_SSL_DN);
228 // }
229 //
230 // private static NodeUserAdmin getNodeUserAdmin() {
231 // NodeUserAdmin res;
232 // try {
233 // res = instance.userAdminSt.waitForService(60000);
234 // } catch (InterruptedException e) {
235 // throw new IllegalStateException("Cannot retrieve Node user admin", e);
236 // }
237 // if (res == null)
238 // throw new IllegalStateException("No Node user admin found");
239 //
240 // return res;
241 // // ServiceReference<UserAdmin> sr =
242 // // instance.bc.getServiceReference(UserAdmin.class);
243 // // NodeUserAdmin userAdmin = (NodeUserAdmin) instance.bc.getService(sr);
244 // // return userAdmin;
245 //
246 // }
247
248 // public static ExecutorService getInternalExecutorService() {
249 // return instance.internalExecutorService;
250 // }
251
252 // static CmsSecurity getCmsSecurity() {
253 // return instance.nodeSecurity;
254 // }
255
256 // public String[] getLocales() {
257 // // TODO optimize?
258 // List<Locale> locales = CmsStateImpl.getNodeState().getLocales();
259 // String[] res = new String[locales.size()];
260 // for (int i = 0; i < locales.size(); i++)
261 // res[i] = locales.get(i).toString();
262 // return res;
263 // }
264
265 public static BundleContext getBundleContext() {
266 return bundleContext;
267 }
268
269 }