]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/osgi/CmsActivator.java
Move CMS file utilities to a dedicated package
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / osgi / 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.osgi.framework.BundleActivator;
8 import org.osgi.framework.BundleContext;
9 import org.osgi.framework.Constants;
10 import org.osgi.service.condpermadmin.BundleLocationCondition;
11 import org.osgi.service.condpermadmin.ConditionInfo;
12 import org.osgi.service.condpermadmin.ConditionalPermissionAdmin;
13 import org.osgi.service.condpermadmin.ConditionalPermissionInfo;
14 import org.osgi.service.condpermadmin.ConditionalPermissionUpdate;
15 import org.osgi.service.permissionadmin.PermissionInfo;
16
17 /**
18 * Activates the kernel. Gives access to kernel information for the rest of the
19 * bundle (and only it)
20 */
21 public class CmsActivator implements BundleActivator {
22 private final static CmsLog log = CmsLog.getLog(CmsActivator.class);
23
24 // TODO make it configurable
25 private boolean hardened = false;
26
27 private static BundleContext bundleContext;
28
29 // private LogReaderService logReaderService;
30 //
31 // private CmsOsgiLogger logger;
32
33 void init() {
34 // Runtime.getRuntime().addShutdownHook(new CmsShutdown());
35 // instance = this;
36 // this.bc = bundleContext;
37 // if (bundleContext != null)
38 // this.logReaderService = getService(LogReaderService.class);
39 // initArgeoLogger();
40 // this.internalExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
41 //
42 // try {
43 // initSecurity();
44 //// initArgeoLogger();
45 // initNode();
46 //
47 // if (log.isTraceEnabled())
48 // log.trace("Kernel bundle started");
49 // } catch (Throwable e) {
50 // log.error("## FATAL: CMS activator failed", e);
51 // }
52 }
53
54 void destroy() {
55 try {
56 bundleContext = null;
57 // this.logReaderService = null;
58 } catch (Exception e) {
59 log.error("CMS activator shutdown failed", e);
60 }
61
62 new GogoShellKiller().start();
63 }
64
65 private void initSecurity() {
66 // code-level permissions
67 String osgiSecurity = bundleContext.getProperty(Constants.FRAMEWORK_SECURITY);
68 if (osgiSecurity != null && Constants.FRAMEWORK_SECURITY_OSGI.equals(osgiSecurity)) {
69 // TODO rather use a tracker?
70 ConditionalPermissionAdmin permissionAdmin = bundleContext
71 .getService(bundleContext.getServiceReference(ConditionalPermissionAdmin.class));
72 if (!hardened) {
73 // All permissions to all bundles
74 ConditionalPermissionUpdate update = permissionAdmin.newConditionalPermissionUpdate();
75 update.getConditionalPermissionInfos().add(permissionAdmin.newConditionalPermissionInfo(null,
76 new ConditionInfo[] {
77 new ConditionInfo(BundleLocationCondition.class.getName(), new String[] { "*" }) },
78 new PermissionInfo[] { new PermissionInfo(AllPermission.class.getName(), null, null) },
79 ConditionalPermissionInfo.ALLOW));
80 // TODO data admin permission
81 // PermissionInfo dataAdminPerm = new PermissionInfo(AuthPermission.class.getName(),
82 // "createLoginContext." + NodeConstants.LOGIN_CONTEXT_DATA_ADMIN, null);
83 // update.getConditionalPermissionInfos().add(permissionAdmin.newConditionalPermissionInfo(null,
84 // new ConditionInfo[] {
85 // new ConditionInfo(BundleLocationCondition.class.getName(), new String[] { "*" }) },
86 // new PermissionInfo[] { dataAdminPerm }, ConditionalPermissionInfo.DENY));
87 // update.getConditionalPermissionInfos().add(permissionAdmin.newConditionalPermissionInfo(null,
88 // new ConditionInfo[] {
89 // 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" }) },
90 // new PermissionInfo[] { dataAdminPerm }, ConditionalPermissionInfo.ALLOW));
91 update.commit();
92 } else {
93 SecurityProfile securityProfile = new SecurityProfile() {
94 };
95 securityProfile.applySystemPermissions(permissionAdmin);
96 }
97 }
98
99 }
100
101 // private void initArgeoLogger() {
102 // logger = new CmsOsgiLogger(logReaderService);
103 // if (bundleContext != null)
104 // bundleContext.registerService(ArgeoLogger.class, logger, null);
105 // }
106
107
108 public static <T> void registerService(Class<T> clss, T service, Dictionary<String, ?> properties) {
109 if (bundleContext != null) {
110 bundleContext.registerService(clss, service, properties);
111 }
112
113 }
114
115 public static <T> T getService(Class<T> clss) {
116 if (bundleContext != null) {
117 return bundleContext.getService(bundleContext.getServiceReference(clss));
118 } else {
119 return null;
120 }
121 }
122
123 /*
124 * OSGi
125 */
126
127 @Override
128 public void start(BundleContext bc) throws Exception {
129 bundleContext = bc;
130
131 init();
132
133 }
134
135 @Override
136 public void stop(BundleContext bc) throws Exception {
137
138 destroy();
139 bundleContext = null;
140 }
141
142
143 public static BundleContext getBundleContext() {
144 return bundleContext;
145 }
146
147 }