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