]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsDeploymentImpl.java
83f688a6a89c0e6ec89bd2b4bfceabcc1e391512
[lgpl/argeo-commons.git] / CmsDeploymentImpl.java
1 package org.argeo.cms.internal.runtime;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.Dictionary;
6
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;
13
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();
18
19 // private Long availableSince;
20
21 // Readiness
22 // private boolean nodeAvailable = false;
23 // private boolean userAdminAvailable = false;
24 private boolean httpExpected = false;
25 // private boolean httpAvailable = false;
26 private HttpService httpService;
27
28 private CmsState cmsState;
29 private DeployConfig deployConfig;
30
31 public CmsDeploymentImpl() {
32 // ServiceReference<NodeState> nodeStateSr = bc.getServiceReference(NodeState.class);
33 // if (nodeStateSr == null)
34 // throw new CmsException("No node state available");
35
36 // NodeState nodeState = bc.getService(nodeStateSr);
37 // cleanState = nodeState.isClean();
38
39 // nodeHttp = new NodeHttp();
40 initTrackers();
41 }
42
43 private void initTrackers() {
44 // ServiceTracker<?, ?> httpSt = new ServiceTracker<HttpService, HttpService>(bc, HttpService.class, null) {
45 //
46 // @Override
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));
52 // checkReadiness();
53 // return super.addingService(sr);
54 // }
55 // };
56 // // httpSt.open();
57 // KernelUtils.asyncOpen(httpSt);
58
59 // ServiceTracker<?, ?> userAdminSt = new ServiceTracker<UserAdmin, UserAdmin>(bc, UserAdmin.class, null) {
60 // @Override
61 // public UserAdmin addingService(ServiceReference<UserAdmin> reference) {
62 // UserAdmin userAdmin = super.addingService(reference);
63 // addStandardSystemRoles(userAdmin);
64 // userAdminAvailable = true;
65 // checkReadiness();
66 // return userAdmin;
67 // }
68 // };
69 // // userAdminSt.open();
70 // KernelUtils.asyncOpen(userAdminSt);
71
72 // ServiceTracker<?, ?> confAdminSt = new ServiceTracker<ConfigurationAdmin, ConfigurationAdmin>(bc,
73 // ConfigurationAdmin.class, null) {
74 // @Override
75 // public ConfigurationAdmin addingService(ServiceReference<ConfigurationAdmin> reference) {
76 // ConfigurationAdmin configurationAdmin = bc.getService(reference);
77 //// boolean isClean;
78 //// try {
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);
84 //// }
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;
89 // try {
90 // Configuration[] configs = configurationAdmin
91 // .listConfigurations("(service.factoryPid=" + CmsConstants.NODE_USER_ADMIN_PID + ")");
92 //
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);
98 // hasDomain = true;
99 // }
100 // }
101 // if (hasDomain) {
102 // loadIpaJaasConfiguration();
103 // }
104 // } catch (Exception e) {
105 // throw new IllegalStateException("Cannot initialize config", e);
106 // }
107 // return super.addingService(reference);
108 // }
109 // };
110 // // confAdminSt.open();
111 // KernelUtils.asyncOpen(confAdminSt);
112 }
113
114 public void init() {
115 httpExpected = deployConfig.getProps(KernelConstants.JETTY_FACTORY_PID, "default") != null;
116 if (deployConfig.hasDomain()) {
117 loadIpaJaasConfiguration();
118 }
119
120 // while (!isHttpAvailableOrNotExpected()) {
121 // try {
122 // Thread.sleep(100);
123 // } catch (InterruptedException e) {
124 // log.error("Interrupted while waiting for http");
125 // }
126 // }
127 }
128
129 public void addFactoryDeployConfig(String factoryPid, Dictionary<String, Object> props) {
130 deployConfig.putFactoryDeployConfig(factoryPid, props);
131 deployConfig.save();
132 try {
133 deployConfig.loadConfigs();
134 } catch (IOException e) {
135 throw new IllegalStateException(e);
136 }
137 }
138
139 public Dictionary<String, Object> getProps(String factoryPid, String cn) {
140 return deployConfig.getProps(factoryPid, cn);
141 }
142
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));
146 // try {
147 // userTransaction.begin();
148 // Role adminRole = userAdmin.getRole(CmsConstants.ROLE_ADMIN);
149 // if (adminRole == null) {
150 // adminRole = userAdmin.createRole(CmsConstants.ROLE_ADMIN, Role.GROUP);
151 // }
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);
155 // }
156 // userTransaction.commit();
157 // } catch (Exception e) {
158 // try {
159 // userTransaction.rollback();
160 // } catch (Exception e1) {
161 // // silent
162 // }
163 // throw new IllegalStateException("Cannot add standard system roles", e);
164 // }
165 // }
166
167 public boolean isHttpAvailableOrNotExpected() {
168 return (httpExpected ? httpService != null : true);
169 }
170
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.");
177 }
178 }
179
180 public void destroy() {
181 // if (nodeHttp != null)
182 // nodeHttp.destroy();
183
184 try {
185 JettyConfigurator.stopServer(KernelConstants.DEFAULT_JETTY_SERVER);
186 } catch (Exception e) {
187 log.error("Cannot stop default Jetty server.", e);
188 }
189
190 if (deployConfig != null) {
191 new Thread(() -> deployConfig.save(), "Save Argeo Deploy Config").start();
192 }
193 }
194
195 public void setDeployConfig(DeployConfig deployConfig) {
196 this.deployConfig = deployConfig;
197 }
198
199 public void setCmsState(CmsState cmsState) {
200 this.cmsState = cmsState;
201 }
202
203 public void setHttpService(HttpService httpService) {
204 this.httpService = httpService;
205 }
206
207 }