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
.osgi
.service
.http
.HttpService
;
13 /** Implementation of a CMS deployment. */
14 public class CmsDeploymentImpl
implements CmsDeployment
{
15 private final CmsLog log
= CmsLog
.getLog(getClass());
16 // private final BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext();
18 // private Long availableSince;
21 // private boolean nodeAvailable = false;
22 // private boolean userAdminAvailable = false;
23 private boolean httpExpected
= false;
24 // private boolean httpAvailable = false;
25 private HttpService httpService
;
27 private CmsState cmsState
;
28 private DeployConfig deployConfig
;
30 public CmsDeploymentImpl() {
31 // ServiceReference<NodeState> nodeStateSr = bc.getServiceReference(NodeState.class);
32 // if (nodeStateSr == null)
33 // throw new CmsException("No node state available");
35 // NodeState nodeState = bc.getService(nodeStateSr);
36 // cleanState = nodeState.isClean();
38 // nodeHttp = new NodeHttp();
42 private void initTrackers() {
43 // ServiceTracker<?, ?> httpSt = new ServiceTracker<HttpService, HttpService>(bc, HttpService.class, null) {
46 // public HttpService addingService(ServiceReference<HttpService> sr) {
47 // httpAvailable = true;
48 // Object httpPort = sr.getProperty("http.port");
49 // Object httpsPort = sr.getProperty("https.port");
50 // log.info(httpPortsMsg(httpPort, httpsPort));
52 // return super.addingService(sr);
56 // KernelUtils.asyncOpen(httpSt);
58 // ServiceTracker<?, ?> userAdminSt = new ServiceTracker<UserAdmin, UserAdmin>(bc, UserAdmin.class, null) {
60 // public UserAdmin addingService(ServiceReference<UserAdmin> reference) {
61 // UserAdmin userAdmin = super.addingService(reference);
62 // addStandardSystemRoles(userAdmin);
63 // userAdminAvailable = true;
68 // // userAdminSt.open();
69 // KernelUtils.asyncOpen(userAdminSt);
71 // ServiceTracker<?, ?> confAdminSt = new ServiceTracker<ConfigurationAdmin, ConfigurationAdmin>(bc,
72 // ConfigurationAdmin.class, null) {
74 // public ConfigurationAdmin addingService(ServiceReference<ConfigurationAdmin> reference) {
75 // ConfigurationAdmin configurationAdmin = bc.getService(reference);
78 //// Configuration[] confs = configurationAdmin
79 //// .listConfigurations("(service.factoryPid=" + CmsConstants.NODE_USER_ADMIN_PID + ")");
80 //// isClean = confs == null || confs.length == 0;
81 //// } catch (Exception e) {
82 //// throw new IllegalStateException("Cannot analyse clean state", e);
84 // deployConfig = new DeployConfig(configurationAdmin, isClean);
85 // Activator.registerService(CmsDeployment.class, CmsDeploymentImpl.this, null);
86 //// JcrInitUtils.addToDeployment(CmsDeployment.this);
87 // httpExpected = deployConfig.getProps(KernelConstants.JETTY_FACTORY_PID, "default") != null;
89 // Configuration[] configs = configurationAdmin
90 // .listConfigurations("(service.factoryPid=" + CmsConstants.NODE_USER_ADMIN_PID + ")");
92 // boolean hasDomain = false;
93 // for (Configuration config : configs) {
94 // Object realm = config.getProperties().get(UserAdminConf.realm.name());
95 // if (realm != null) {
96 // log.debug("Found realm: " + realm);
101 // loadIpaJaasConfiguration();
103 // } catch (Exception e) {
104 // throw new IllegalStateException("Cannot initialize config", e);
106 // return super.addingService(reference);
109 // // confAdminSt.open();
110 // KernelUtils.asyncOpen(confAdminSt);
113 public void start() {
114 httpExpected
= deployConfig
.getProps(KernelConstants
.JETTY_FACTORY_PID
, "default") != null;
115 if (deployConfig
.hasDomain()) {
116 loadIpaJaasConfiguration();
119 // while (!isHttpAvailableOrNotExpected()) {
121 // Thread.sleep(100);
122 // } catch (InterruptedException e) {
123 // log.error("Interrupted while waiting for http");
128 public void addFactoryDeployConfig(String factoryPid
, Dictionary
<String
, Object
> props
) {
129 deployConfig
.putFactoryDeployConfig(factoryPid
, props
);
132 deployConfig
.loadConfigs();
133 } catch (IOException e
) {
134 throw new IllegalStateException(e
);
138 public Dictionary
<String
, Object
> getProps(String factoryPid
, String cn
) {
139 return deployConfig
.getProps(factoryPid
, cn
);
142 // private void addStandardSystemRoles(UserAdmin userAdmin) {
143 // // we assume UserTransaction is already available (TODO make it more robust)
144 // WorkTransaction userTransaction = bc.getService(bc.getServiceReference(WorkTransaction.class));
146 // userTransaction.begin();
147 // Role adminRole = userAdmin.getRole(CmsConstants.ROLE_ADMIN);
148 // if (adminRole == null) {
149 // adminRole = userAdmin.createRole(CmsConstants.ROLE_ADMIN, Role.GROUP);
151 // if (userAdmin.getRole(CmsConstants.ROLE_USER_ADMIN) == null) {
152 // Group userAdminRole = (Group) userAdmin.createRole(CmsConstants.ROLE_USER_ADMIN, Role.GROUP);
153 // userAdminRole.addMember(adminRole);
155 // userTransaction.commit();
156 // } catch (Exception e) {
158 // userTransaction.rollback();
159 // } catch (Exception e1) {
162 // throw new IllegalStateException("Cannot add standard system roles", e);
166 public boolean isHttpAvailableOrNotExpected() {
167 return (httpExpected ? httpService
!= null : true);
170 private void loadIpaJaasConfiguration() {
171 if (System
.getProperty(KernelConstants
.JAAS_CONFIG_PROP
) == null) {
172 String jaasConfig
= KernelConstants
.JAAS_CONFIG_IPA
;
173 URL url
= getClass().getClassLoader().getResource(jaasConfig
);
174 KernelUtils
.setJaasConfiguration(url
);
175 log
.debug("Set IPA JAAS configuration.");
180 // if (nodeHttp != null)
181 // nodeHttp.destroy();
184 // JettyConfigurator.stopServer(KernelConstants.DEFAULT_JETTY_SERVER);
185 // } catch (Exception e) {
186 // log.error("Cannot stop default Jetty server.", e);
189 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
;