1 package org
.argeo
.cms
.internal
.kernel
;
3 import static org
.argeo
.node
.DataModelNamespace
.CMS_DATA_MODEL_NAMESPACE
;
5 import java
.io
.InputStreamReader
;
7 import java
.lang
.management
.ManagementFactory
;
9 import java
.util
.HashSet
;
10 import java
.util
.Hashtable
;
11 import java
.util
.List
;
15 import javax
.jcr
.Repository
;
16 import javax
.jcr
.Session
;
17 import javax
.security
.auth
.callback
.CallbackHandler
;
19 import org
.apache
.commons
.logging
.Log
;
20 import org
.apache
.commons
.logging
.LogFactory
;
21 import org
.apache
.jackrabbit
.commons
.cnd
.CndImporter
;
22 import org
.apache
.jackrabbit
.core
.RepositoryContext
;
23 import org
.argeo
.cms
.CmsException
;
24 import org
.argeo
.cms
.internal
.http
.NodeHttp
;
25 import org
.argeo
.jcr
.JcrUtils
;
26 import org
.argeo
.node
.DataModelNamespace
;
27 import org
.argeo
.node
.NodeConstants
;
28 import org
.argeo
.node
.NodeDeployment
;
29 import org
.argeo
.node
.NodeState
;
30 import org
.argeo
.node
.security
.CryptoKeyring
;
31 import org
.argeo
.osgi
.useradmin
.UserAdminConf
;
32 import org
.argeo
.util
.LangUtils
;
33 import org
.osgi
.framework
.Bundle
;
34 import org
.osgi
.framework
.BundleContext
;
35 import org
.osgi
.framework
.Constants
;
36 import org
.osgi
.framework
.FrameworkUtil
;
37 import org
.osgi
.framework
.ServiceReference
;
38 import org
.osgi
.framework
.wiring
.BundleCapability
;
39 import org
.osgi
.framework
.wiring
.BundleWire
;
40 import org
.osgi
.framework
.wiring
.BundleWiring
;
41 import org
.osgi
.service
.cm
.Configuration
;
42 import org
.osgi
.service
.cm
.ConfigurationAdmin
;
43 import org
.osgi
.service
.cm
.ManagedService
;
44 import org
.osgi
.service
.useradmin
.UserAdmin
;
45 import org
.osgi
.util
.tracker
.ServiceTracker
;
47 public class CmsDeployment
implements NodeDeployment
{
48 private final static String LEGACY_JCR_REPOSITORY_ALIAS
= "argeo.jcr.repository.alias";
50 private final Log log
= LogFactory
.getLog(getClass());
51 private final BundleContext bc
= FrameworkUtil
.getBundle(getClass()).getBundleContext();
53 private DeployConfig deployConfig
;
54 private HomeRepository homeRepository
;
56 private Long availableSince
;
58 private final boolean cleanState
;
60 private NodeHttp nodeHttp
;
63 private boolean nodeAvailable
= false;
64 private boolean userAdminAvailable
= false;
65 private boolean httpExpected
= false;
66 private boolean httpAvailable
= false;
68 public CmsDeployment() {
69 ServiceReference
<NodeState
> nodeStateSr
= bc
.getServiceReference(NodeState
.class);
70 if (nodeStateSr
== null)
71 throw new CmsException("No node state available");
73 NodeState nodeState
= bc
.getService(nodeStateSr
);
74 cleanState
= nodeState
.isClean();
76 nodeHttp
= new NodeHttp();
80 private void initTrackers() {
81 new ServiceTracker
<NodeHttp
, NodeHttp
>(bc
, NodeHttp
.class, null) {
84 public NodeHttp
addingService(ServiceReference
<NodeHttp
> reference
) {
87 return super.addingService(reference
);
90 new RepositoryContextStc().open();
91 new ServiceTracker
<UserAdmin
, UserAdmin
>(bc
, UserAdmin
.class, null) {
93 public UserAdmin
addingService(ServiceReference
<UserAdmin
> reference
) {
94 userAdminAvailable
= true;
96 return super.addingService(reference
);
99 new ServiceTracker
<ConfigurationAdmin
, ConfigurationAdmin
>(bc
, ConfigurationAdmin
.class, null) {
101 public ConfigurationAdmin
addingService(ServiceReference
<ConfigurationAdmin
> reference
) {
102 ConfigurationAdmin configurationAdmin
= bc
.getService(reference
);
103 deployConfig
= new DeployConfig(configurationAdmin
, cleanState
);
104 httpExpected
= deployConfig
.getProps(KernelConstants
.JETTY_FACTORY_PID
, "default") != null;
106 // Configuration[] configs = configurationAdmin
107 // .listConfigurations("(service.factoryPid=" +
108 // NodeConstants.NODE_REPOS_FACTORY_PID + ")");
109 // for (Configuration config : configs) {
110 // Object cn = config.getProperties().get(NodeConstants.CN);
111 // if (log.isDebugEnabled())
112 // log.debug("Standalone repo cn: " + cn);
114 Configuration
[] configs
= configurationAdmin
115 .listConfigurations("(service.factoryPid=" + NodeConstants
.NODE_USER_ADMIN_PID
+ ")");
117 boolean hasDomain
= false;
118 for (Configuration config
: configs
) {
119 Object realm
= config
.getProperties().get(UserAdminConf
.realm
.name());
121 log
.debug("Found realm: " + realm
);
126 loadIpaJaasConfiguration();
128 } catch (Exception e
) {
129 throw new CmsException("Cannot initialize config", e
);
131 return super.addingService(reference
);
136 private void loadIpaJaasConfiguration() {
137 if (System
.getProperty(KernelConstants
.JAAS_CONFIG_PROP
) == null) {
138 String jaasConfig
= KernelConstants
.JAAS_CONFIG_IPA
;
139 URL url
= getClass().getClassLoader().getResource(jaasConfig
);
140 KernelUtils
.setJaasConfiguration(url
);
141 log
.debug("Set IPA JAAS configuration.");
145 public void shutdown() {
146 if (nodeHttp
!= null)
148 if (deployConfig
!= null)
152 private void checkReadiness() {
153 if (nodeAvailable
&& userAdminAvailable
&& (httpExpected ? httpAvailable
: true)) {
154 availableSince
= System
.currentTimeMillis();
155 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
156 log
.info("## ARGEO CMS AVAILABLE in " + (jvmUptime
/ 1000) + "." + (jvmUptime
% 1000) + "s ##");
157 long begin
= bc
.getService(bc
.getServiceReference(NodeState
.class)).getAvailableSince();
158 long initDuration
= System
.currentTimeMillis() - begin
;
159 if (log
.isTraceEnabled())
160 log
.trace("Kernel initialization took " + initDuration
+ "ms");
161 tributeToFreeSoftware(initDuration
);
165 final private void tributeToFreeSoftware(long initDuration
) {
166 if (log
.isTraceEnabled()) {
167 long ms
= initDuration
/ 100;
168 log
.trace("Spend " + ms
+ "ms" + " reflecting on the progress brought to mankind" + " by Free Software...");
169 long beginNano
= System
.nanoTime();
172 } catch (InterruptedException e
) {
175 long durationNano
= System
.nanoTime() - beginNano
;
176 final double M
= 1000d
* 1000d
;
177 double sleepAccuracy
= ((double) durationNano
) / (ms
* M
);
178 log
.trace("Sleep accuracy: " + String
.format("%.2f", 100 - (sleepAccuracy
* 100 - 100)) + " %");
182 private void prepareNodeRepository(Repository deployedNodeRepository
) {
183 if (availableSince
!= null) {
184 throw new CmsException("Deployment is already available");
188 prepareDataModel(KernelUtils
.openAdminSession(deployedNodeRepository
));
191 private void prepareHomeRepository(Repository deployedRepository
) {
192 Hashtable
<String
, String
> regProps
= new Hashtable
<String
, String
>();
193 regProps
.put(NodeConstants
.CN
, NodeConstants
.HOME
);
194 regProps
.put(LEGACY_JCR_REPOSITORY_ALIAS
, NodeConstants
.HOME
);
195 homeRepository
= new HomeRepository(deployedRepository
);
197 bc
.registerService(Repository
.class, homeRepository
, regProps
);
199 new ServiceTracker
<CallbackHandler
, CallbackHandler
>(bc
, CallbackHandler
.class, null) {
202 public CallbackHandler
addingService(ServiceReference
<CallbackHandler
> reference
) {
203 NodeKeyRing nodeKeyring
= new NodeKeyRing(homeRepository
);
204 CallbackHandler callbackHandler
= bc
.getService(reference
);
205 nodeKeyring
.setDefaultCallbackHandler(callbackHandler
);
206 bc
.registerService(LangUtils
.names(CryptoKeyring
.class, ManagedService
.class), nodeKeyring
,
207 LangUtils
.dico(Constants
.SERVICE_PID
, NodeConstants
.NODE_KEYRING_PID
));
208 return callbackHandler
;
214 /** Session is logged out. */
215 private void prepareDataModel(Session adminSession
) {
217 Set
<String
> processed
= new HashSet
<String
>();
218 bundles
: for (Bundle bundle
: bc
.getBundles()) {
219 BundleWiring wiring
= bundle
.adapt(BundleWiring
.class);
222 processWiring(adminSession
, wiring
, processed
);
225 JcrUtils
.logoutQuietly(adminSession
);
229 private void processWiring(Session adminSession
, BundleWiring wiring
, Set
<String
> processed
) {
230 // recursively process requirements first
231 List
<BundleWire
> requiredWires
= wiring
.getRequiredWires(CMS_DATA_MODEL_NAMESPACE
);
232 for (BundleWire wire
: requiredWires
) {
233 processWiring(adminSession
, wire
.getProviderWiring(), processed
);
234 // registerCnd(adminSession, wire.getCapability(), processed);
236 List
<BundleCapability
> capabilities
= wiring
.getCapabilities(CMS_DATA_MODEL_NAMESPACE
);
237 for (BundleCapability capability
: capabilities
) {
238 registerDataModelCapability(adminSession
, capability
, processed
);
242 private void registerDataModelCapability(Session adminSession
, BundleCapability capability
, Set
<String
> processed
) {
243 Map
<String
, Object
> attrs
= capability
.getAttributes();
244 String name
= (String
) attrs
.get(DataModelNamespace
.CAPABILITY_NAME_ATTRIBUTE
);
245 if (processed
.contains(name
)) {
246 if (log
.isTraceEnabled())
247 log
.trace("Data model " + name
+ " has already been processed");
252 String path
= (String
) attrs
.get(DataModelNamespace
.CAPABILITY_CND_ATTRIBUTE
);
254 URL url
= capability
.getRevision().getBundle().getResource(path
);
256 throw new CmsException("No data model '" + name
+ "' found under path " + path
);
257 try (Reader reader
= new InputStreamReader(url
.openStream())) {
258 CndImporter
.registerNodeTypes(reader
, adminSession
, true);
260 if (log
.isDebugEnabled())
261 log
.debug("Registered CND " + url
);
262 } catch (Exception e
) {
263 throw new CmsException("Cannot import CND " + url
, e
);
267 if (!asBoolean((String
) attrs
.get(DataModelNamespace
.CAPABILITY_ABSTRACT_ATTRIBUTE
))) {
268 Hashtable
<String
, Object
> properties
= new Hashtable
<>();
269 properties
.put(LEGACY_JCR_REPOSITORY_ALIAS
, name
);
270 properties
.put(NodeConstants
.CN
, name
);
271 if (name
.equals(NodeConstants
.NODE
))
272 properties
.put(Constants
.SERVICE_RANKING
, Integer
.MAX_VALUE
);
273 LocalRepository localRepository
= new LocalRepository(adminSession
.getRepository(), capability
);
274 bc
.registerService(Repository
.class, localRepository
, properties
);
275 if (log
.isDebugEnabled())
276 log
.debug("Published data model " + name
);
280 private boolean asBoolean(String value
) {
289 throw new CmsException("Unsupported value for attribute " + DataModelNamespace
.CAPABILITY_ABSTRACT_ATTRIBUTE
295 public Long
getAvailableSince() {
296 return availableSince
;
299 private class RepositoryContextStc
extends ServiceTracker
<RepositoryContext
, RepositoryContext
> {
301 public RepositoryContextStc() {
302 super(bc
, RepositoryContext
.class, null);
306 public RepositoryContext
addingService(ServiceReference
<RepositoryContext
> reference
) {
307 RepositoryContext nodeRepo
= bc
.getService(reference
);
308 Object cn
= reference
.getProperty(NodeConstants
.CN
);
310 if (cn
.equals(NodeConstants
.NODE
)) {
311 prepareNodeRepository(nodeRepo
.getRepository());
312 prepareHomeRepository(nodeRepo
.getRepository());
313 nodeAvailable
= true;
323 public void modifiedService(ServiceReference
<RepositoryContext
> reference
, RepositoryContext service
) {
327 public void removedService(ServiceReference
<RepositoryContext
> reference
, RepositoryContext service
) {