1 package org
.argeo
.cms
.internal
.osgi
;
3 import java
.security
.AllPermission
;
4 import java
.util
.Dictionary
;
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
;
23 * Activates the kernel. Gives access to kernel information for the rest of the
24 * bundle (and only it)
26 public class CmsActivator
implements BundleActivator
{
27 private final static CmsLog log
= CmsLog
.getLog(CmsActivator
.class);
29 // private static Activator instance;
31 // TODO make it configurable
32 private boolean hardened
= false;
34 private static BundleContext bundleContext
;
36 private LogReaderService logReaderService
;
38 private CmsOsgiLogger logger
;
39 // private CmsStateImpl nodeState;
40 // private CmsDeploymentImpl nodeDeployment;
41 // private CmsContextImpl nodeInstance;
43 // private ServiceTracker<UserAdmin, NodeUserAdmin> userAdminSt;
46 // Bundle bundle = FrameworkUtil.getBundle(Activator.class);
47 // if (bundle != null) {
48 // bundleContext = bundle.getBundleContext();
53 // Runtime.getRuntime().addShutdownHook(new CmsShutdown());
55 // this.bc = bundleContext;
56 if (bundleContext
!= null)
57 this.logReaderService
= getService(LogReaderService
.class);
59 // this.internalExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
63 //// initArgeoLogger();
66 // if (log.isTraceEnabled())
67 // log.trace("Kernel bundle started");
68 // } catch (Throwable e) {
69 // log.error("## FATAL: CMS activator failed", e);
75 // if (nodeInstance != null)
76 // nodeInstance.shutdown();
77 // if (nodeDeployment != null)
78 // nodeDeployment.shutdown();
79 // if (nodeState != null)
80 // nodeState.shutdown();
82 // if (userAdminSt != null)
83 // userAdminSt.close();
85 // internalExecutorService.shutdown();
88 this.logReaderService
= null;
89 // this.configurationAdmin = null;
90 } catch (Exception e
) {
91 log
.error("CMS activator shutdown failed", e
);
94 new GogoShellKiller().start();
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));
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));
125 SecurityProfile securityProfile
= new SecurityProfile() {
127 securityProfile
.applySystemPermissions(permissionAdmin
);
133 private void initArgeoLogger() {
134 logger
= new CmsOsgiLogger(logReaderService
);
135 if (bundleContext
!= null)
136 bundleContext
.registerService(ArgeoLogger
.class, logger
, null);
139 // private void initNode() throws IOException {
141 // nodeState = new CmsStateImpl();
142 // registerService(CmsState.class, nodeState, null);
144 // // Node deployment
145 // nodeDeployment = new CmsDeploymentImpl();
146 //// registerService(NodeDeployment.class, nodeDeployment, null);
149 // nodeInstance = new CmsContextImpl();
150 // registerService(CmsContext.class, nodeInstance, null);
153 public static <T
> void registerService(Class
<T
> clss
, T service
, Dictionary
<String
, ?
> properties
) {
154 if (bundleContext
!= null) {
155 bundleContext
.registerService(clss
, service
, properties
);
160 public static <T
> T
getService(Class
<T
> clss
) {
161 if (bundleContext
!= null) {
162 return bundleContext
.getService(bundleContext
.getServiceReference(clss
));
173 public void start(BundleContext bc
) throws Exception
{
175 // if (!bc.getBundle().equals(bundleContext.getBundle()))
176 // throw new IllegalStateException(
177 // "Bundle " + bc.getBundle() + " is not consistent with " + bundleContext.getBundle());
179 // userAdminSt = new ServiceTracker<>(bundleContext, UserAdmin.class, null);
180 // userAdminSt.open();
182 ServiceTracker
<?
, ?
> httpSt
= new ServiceTracker
<HttpService
, HttpService
>(bc
, HttpService
.class, null) {
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
));
190 return super.addingService(sr
);
196 private String
httpPortsMsg(Object httpPort
, Object httpsPort
) {
197 return (httpPort
!= null ?
"HTTP " + httpPort
+ " " : " ") + (httpsPort
!= null ?
"HTTPS " + httpsPort
: "");
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());
206 bundleContext
= null;
209 // private <T> T getService(Class<T> clazz) {
210 // ServiceReference<T> sr = bundleContext.getServiceReference(clazz);
212 // throw new IllegalStateException("No service available for " + clazz);
213 // return bundleContext.getService(sr);
216 // public static GSSCredential getAcceptorCredentials() {
217 // return getNodeUserAdmin().getAcceptorCredentials();
221 // public static boolean isSingleUser() {
222 // return getNodeUserAdmin().isSingleUser();
225 // public static UserAdmin getUserAdmin() {
226 // return (UserAdmin) getNodeUserAdmin();
229 // public static String getHttpProxySslHeader() {
230 // return KernelUtils.getFrameworkProp(CmsConstants.HTTP_PROXY_SSL_DN);
233 // private static NodeUserAdmin getNodeUserAdmin() {
234 // NodeUserAdmin res;
236 // res = instance.userAdminSt.waitForService(60000);
237 // } catch (InterruptedException e) {
238 // throw new IllegalStateException("Cannot retrieve Node user admin", e);
241 // throw new IllegalStateException("No Node user admin found");
244 // // ServiceReference<UserAdmin> sr =
245 // // instance.bc.getServiceReference(UserAdmin.class);
246 // // NodeUserAdmin userAdmin = (NodeUserAdmin) instance.bc.getService(sr);
247 // // return userAdmin;
251 // public static ExecutorService getInternalExecutorService() {
252 // return instance.internalExecutorService;
255 // static CmsSecurity getCmsSecurity() {
256 // return instance.nodeSecurity;
259 // public String[] getLocales() {
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();
268 public static BundleContext
getBundleContext() {
269 return bundleContext
;