1 package org
.argeo
.cms
.internal
.runtime
;
3 import java
.io
.IOException
;
5 import java
.util
.Dictionary
;
7 import org
.argeo
.api
.cms
.CmsDeployment
;
8 import org
.argeo
.api
.cms
.CmsLog
;
9 import org
.argeo
.api
.cms
.CmsState
;
10 import org
.argeo
.cms
.internal
.osgi
.DeployConfig
;
11 import org
.eclipse
.equinox
.http
.jetty
.JettyConfigurator
;
12 import org
.osgi
.service
.http
.HttpService
;
14 /** Implementation of a CMS deployment. */
15 public class CmsDeploymentImpl
implements CmsDeployment
{
16 private final CmsLog log
= CmsLog
.getLog(getClass());
17 // private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
19 // private Long availableSince;
22 // private boolean nodeAvailable = false;
23 // private boolean userAdminAvailable = false;
24 private boolean httpExpected
= false;
25 // private boolean httpAvailable = false;
26 private HttpService httpService
;
28 private CmsState cmsState
;
29 private DeployConfig deployConfig
;
31 public CmsDeploymentImpl() {
32 // ServiceReference<NodeState> nodeStateSr = bc.getServiceReference(NodeState.class);
33 // if (nodeStateSr == null)
34 // throw new CmsException("No node state available");
36 // NodeState nodeState = bc.getService(nodeStateSr);
37 // cleanState = nodeState.isClean();
39 // nodeHttp = new NodeHttp();
43 private void initTrackers() {
44 // ServiceTracker<?, ?> httpSt = new ServiceTracker<HttpService, HttpService>(bc, HttpService.class, null) {
47 // public HttpService addingService(ServiceReference<HttpService> sr) {
48 // httpAvailable = true;
49 // Object httpPort = sr.getProperty("http.port");
50 // Object httpsPort = sr.getProperty("https.port");
51 // log.info(httpPortsMsg(httpPort, httpsPort));
53 // return super.addingService(sr);
57 // KernelUtils.asyncOpen(httpSt);
59 // ServiceTracker<?, ?> userAdminSt = new ServiceTracker<UserAdmin, UserAdmin>(bc, UserAdmin.class, null) {
61 // public UserAdmin addingService(ServiceReference<UserAdmin> reference) {
62 // UserAdmin userAdmin = super.addingService(reference);
63 // addStandardSystemRoles(userAdmin);
64 // userAdminAvailable = true;
69 // // userAdminSt.open();
70 // KernelUtils.asyncOpen(userAdminSt);
72 // ServiceTracker<?, ?> confAdminSt = new ServiceTracker<ConfigurationAdmin, ConfigurationAdmin>(bc,
73 // ConfigurationAdmin.class, null) {
75 // public ConfigurationAdmin addingService(ServiceReference<ConfigurationAdmin> reference) {
76 // ConfigurationAdmin configurationAdmin = bc.getService(reference);
79 //// Configuration[] confs = configurationAdmin
80 //// .listConfigurations("(service.factoryPid=" + CmsConstants.NODE_USER_ADMIN_PID + ")");
81 //// isClean = confs == null || confs.length == 0;
82 //// } catch (Exception e) {
83 //// throw new IllegalStateException("Cannot analyse clean state", e);
85 // deployConfig = new DeployConfig(configurationAdmin, isClean);
86 // Activator.registerService(CmsDeployment.class, CmsDeploymentImpl.this, null);
87 //// JcrInitUtils.addToDeployment(CmsDeployment.this);
88 // httpExpected = deployConfig.getProps(KernelConstants.JETTY_FACTORY_PID, "default") != null;
90 // Configuration[] configs = configurationAdmin
91 // .listConfigurations("(service.factoryPid=" + CmsConstants.NODE_USER_ADMIN_PID + ")");
93 // boolean hasDomain = false;
94 // for (Configuration config : configs) {
95 // Object realm = config.getProperties().get(UserAdminConf.realm.name());
96 // if (realm != null) {
97 // log.debug("Found realm: " + realm);
102 // loadIpaJaasConfiguration();
104 // } catch (Exception e) {
105 // throw new IllegalStateException("Cannot initialize config", e);
107 // return super.addingService(reference);
110 // // confAdminSt.open();
111 // KernelUtils.asyncOpen(confAdminSt);
115 httpExpected
= deployConfig
.getProps(KernelConstants
.JETTY_FACTORY_PID
, "default") != null;
116 if (deployConfig
.hasDomain()) {
117 loadIpaJaasConfiguration();
120 // while (!isHttpAvailableOrNotExpected()) {
122 // Thread.sleep(100);
123 // } catch (InterruptedException e) {
124 // log.error("Interrupted while waiting for http");
129 public void addFactoryDeployConfig(String factoryPid
, Dictionary
<String
, Object
> props
) {
130 deployConfig
.putFactoryDeployConfig(factoryPid
, props
);
133 deployConfig
.loadConfigs();
134 } catch (IOException e
) {
135 throw new IllegalStateException(e
);
139 public Dictionary
<String
, Object
> getProps(String factoryPid
, String cn
) {
140 return deployConfig
.getProps(factoryPid
, cn
);
143 // private void addStandardSystemRoles(UserAdmin userAdmin) {
144 // // we assume UserTransaction is already available (TODO make it more robust)
145 // WorkTransaction userTransaction = bc.getService(bc.getServiceReference(WorkTransaction.class));
147 // userTransaction.begin();
148 // Role adminRole = userAdmin.getRole(CmsConstants.ROLE_ADMIN);
149 // if (adminRole == null) {
150 // adminRole = userAdmin.createRole(CmsConstants.ROLE_ADMIN, Role.GROUP);
152 // if (userAdmin.getRole(CmsConstants.ROLE_USER_ADMIN) == null) {
153 // Group userAdminRole = (Group) userAdmin.createRole(CmsConstants.ROLE_USER_ADMIN, Role.GROUP);
154 // userAdminRole.addMember(adminRole);
156 // userTransaction.commit();
157 // } catch (Exception e) {
159 // userTransaction.rollback();
160 // } catch (Exception e1) {
163 // throw new IllegalStateException("Cannot add standard system roles", e);
167 public boolean isHttpAvailableOrNotExpected() {
168 return (httpExpected ? httpService
!= null : true);
171 private void loadIpaJaasConfiguration() {
172 if (System
.getProperty(KernelConstants
.JAAS_CONFIG_PROP
) == null) {
173 String jaasConfig
= KernelConstants
.JAAS_CONFIG_IPA
;
174 URL url
= getClass().getClassLoader().getResource(jaasConfig
);
175 KernelUtils
.setJaasConfiguration(url
);
176 log
.debug("Set IPA JAAS configuration.");
180 public void destroy() {
181 // if (nodeHttp != null)
182 // nodeHttp.destroy();
185 JettyConfigurator
.stopServer(KernelConstants
.DEFAULT_JETTY_SERVER
);
186 } catch (Exception e
) {
187 log
.error("Cannot stop default Jetty server.", e
);
190 if (deployConfig
!= null) {
191 new Thread(() -> deployConfig
.save(), "Save Argeo Deploy Config").start();
195 public void setDeployConfig(DeployConfig deployConfig
) {
196 this.deployConfig
= deployConfig
;
199 public void setCmsState(CmsState cmsState
) {
200 this.cmsState
= cmsState
;
203 public void setHttpService(HttpService httpService
) {
204 this.httpService
= httpService
;