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
.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
;
20 * Activates the kernel. Gives access to kernel information for the rest of the
21 * bundle (and only it)
23 public class CmsActivator
implements BundleActivator
{
24 private final static CmsLog log
= CmsLog
.getLog(CmsActivator
.class);
26 // private static Activator instance;
28 // TODO make it configurable
29 private boolean hardened
= false;
31 private static BundleContext bundleContext
;
33 private LogReaderService logReaderService
;
35 private CmsOsgiLogger logger
;
36 // private CmsStateImpl nodeState;
37 // private CmsDeploymentImpl nodeDeployment;
38 // private CmsContextImpl nodeInstance;
40 // private ServiceTracker<UserAdmin, NodeUserAdmin> userAdminSt;
43 // Bundle bundle = FrameworkUtil.getBundle(Activator.class);
44 // if (bundle != null) {
45 // bundleContext = bundle.getBundleContext();
50 // Runtime.getRuntime().addShutdownHook(new CmsShutdown());
52 // this.bc = bundleContext;
53 if (bundleContext
!= null)
54 this.logReaderService
= getService(LogReaderService
.class);
56 // this.internalExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
60 //// initArgeoLogger();
63 // if (log.isTraceEnabled())
64 // log.trace("Kernel bundle started");
65 // } catch (Throwable e) {
66 // log.error("## FATAL: CMS activator failed", e);
72 // if (nodeInstance != null)
73 // nodeInstance.shutdown();
74 // if (nodeDeployment != null)
75 // nodeDeployment.shutdown();
76 // if (nodeState != null)
77 // nodeState.shutdown();
79 // if (userAdminSt != null)
80 // userAdminSt.close();
82 // internalExecutorService.shutdown();
85 this.logReaderService
= null;
86 // this.configurationAdmin = null;
87 } catch (Exception e
) {
88 log
.error("CMS activator shutdown failed", e
);
91 new GogoShellKiller().start();
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));
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));
122 SecurityProfile securityProfile
= new SecurityProfile() {
124 securityProfile
.applySystemPermissions(permissionAdmin
);
130 private void initArgeoLogger() {
131 logger
= new CmsOsgiLogger(logReaderService
);
132 if (bundleContext
!= null)
133 bundleContext
.registerService(ArgeoLogger
.class, logger
, null);
136 // private void initNode() throws IOException {
138 // nodeState = new CmsStateImpl();
139 // registerService(CmsState.class, nodeState, null);
141 // // Node deployment
142 // nodeDeployment = new CmsDeploymentImpl();
143 //// registerService(NodeDeployment.class, nodeDeployment, null);
146 // nodeInstance = new CmsContextImpl();
147 // registerService(CmsContext.class, nodeInstance, null);
150 public static <T
> void registerService(Class
<T
> clss
, T service
, Dictionary
<String
, ?
> properties
) {
151 if (bundleContext
!= null) {
152 bundleContext
.registerService(clss
, service
, properties
);
157 public static <T
> T
getService(Class
<T
> clss
) {
158 if (bundleContext
!= null) {
159 return bundleContext
.getService(bundleContext
.getServiceReference(clss
));
170 public void start(BundleContext bc
) throws Exception
{
172 // if (!bc.getBundle().equals(bundleContext.getBundle()))
173 // throw new IllegalStateException(
174 // "Bundle " + bc.getBundle() + " is not consistent with " + bundleContext.getBundle());
176 // userAdminSt = new ServiceTracker<>(bundleContext, UserAdmin.class, null);
177 // userAdminSt.open();
179 // ServiceTracker<?, ?> httpSt = new ServiceTracker<HttpService, HttpService>(bc, HttpService.class, null) {
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));
187 // return super.addingService(sr);
193 private String
httpPortsMsg(Object httpPort
, Object httpsPort
) {
194 return (httpPort
!= null ?
"HTTP " + httpPort
+ " " : " ") + (httpsPort
!= null ?
"HTTPS " + httpsPort
: "");
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());
203 bundleContext
= null;
206 // private <T> T getService(Class<T> clazz) {
207 // ServiceReference<T> sr = bundleContext.getServiceReference(clazz);
209 // throw new IllegalStateException("No service available for " + clazz);
210 // return bundleContext.getService(sr);
213 // public static GSSCredential getAcceptorCredentials() {
214 // return getNodeUserAdmin().getAcceptorCredentials();
218 // public static boolean isSingleUser() {
219 // return getNodeUserAdmin().isSingleUser();
222 // public static UserAdmin getUserAdmin() {
223 // return (UserAdmin) getNodeUserAdmin();
226 // public static String getHttpProxySslHeader() {
227 // return KernelUtils.getFrameworkProp(CmsConstants.HTTP_PROXY_SSL_DN);
230 // private static NodeUserAdmin getNodeUserAdmin() {
231 // NodeUserAdmin res;
233 // res = instance.userAdminSt.waitForService(60000);
234 // } catch (InterruptedException e) {
235 // throw new IllegalStateException("Cannot retrieve Node user admin", e);
238 // throw new IllegalStateException("No Node user admin found");
241 // // ServiceReference<UserAdmin> sr =
242 // // instance.bc.getServiceReference(UserAdmin.class);
243 // // NodeUserAdmin userAdmin = (NodeUserAdmin) instance.bc.getService(sr);
244 // // return userAdmin;
248 // public static ExecutorService getInternalExecutorService() {
249 // return instance.internalExecutorService;
252 // static CmsSecurity getCmsSecurity() {
253 // return instance.nodeSecurity;
256 // public String[] getLocales() {
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();
265 public static BundleContext
getBundleContext() {
266 return bundleContext
;