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