1 package org
.argeo
.cms
.internal
.kernel
;
3 import static org
.argeo
.node
.DataModelNamespace
.CMS_DATA_MODEL_NAMESPACE
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Collections
;
9 import java
.util
.TreeMap
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.cms
.CmsException
;
14 import org
.argeo
.node
.DataModelNamespace
;
15 import org
.osgi
.framework
.Bundle
;
16 import org
.osgi
.framework
.BundleContext
;
17 import org
.osgi
.framework
.BundleEvent
;
18 import org
.osgi
.framework
.BundleListener
;
19 import org
.osgi
.framework
.wiring
.BundleCapability
;
20 import org
.osgi
.framework
.wiring
.BundleWire
;
21 import org
.osgi
.framework
.wiring
.BundleWiring
;
23 class DataModels
implements BundleListener
{
24 private final static Log log
= LogFactory
.getLog(DataModels
.class);
26 private Map
<String
, DataModel
> dataModels
= new TreeMap
<>();
28 public DataModels(BundleContext bc
) {
29 for (Bundle bundle
: bc
.getBundles())
30 processBundle(bundle
);
31 bc
.addBundleListener(this);
34 public List
<DataModel
> getNonAbstractDataModels() {
35 List
<DataModel
> res
= new ArrayList
<>();
36 for (String name
: dataModels
.keySet()) {
37 DataModel dataModel
= dataModels
.get(name
);
38 if (!dataModel
.isAbstract())
46 public void bundleChanged(BundleEvent event
) {
47 if (event
.getType() == Bundle
.RESOLVED
) {
48 processBundle(event
.getBundle());
49 } else if (event
.getType() == Bundle
.UNINSTALLED
) {
50 BundleWiring wiring
= event
.getBundle().adapt(BundleWiring
.class);
51 List
<BundleCapability
> providedDataModels
= wiring
.getCapabilities(CMS_DATA_MODEL_NAMESPACE
);
52 if (providedDataModels
.size() == 0)
54 for (BundleCapability bundleCapability
: providedDataModels
) {
55 dataModels
.remove(bundleCapability
.getAttributes().get(DataModelNamespace
.NAME
));
61 protected void processBundle(Bundle bundle
) {
62 BundleWiring wiring
= bundle
.adapt(BundleWiring
.class);
64 int bundleState
= bundle
.getState();
65 if (bundleState
!= Bundle
.INSTALLED
&& bundleState
!= Bundle
.UNINSTALLED
) {// ignore unresolved bundles
66 log
.warn("Bundle " + bundle
.getSymbolicName() + " #" + bundle
.getBundleId() + " ("
67 + bundle
.getLocation() + ") cannot be adapted to a wiring");
69 if (log
.isTraceEnabled())
70 log
.warn("Bundle " + bundle
.getSymbolicName() + " is not resolved.");
74 List
<BundleCapability
> providedDataModels
= wiring
.getCapabilities(CMS_DATA_MODEL_NAMESPACE
);
75 if (providedDataModels
.size() == 0)
77 List
<BundleWire
> requiredDataModels
= wiring
.getRequiredWires(CMS_DATA_MODEL_NAMESPACE
);
78 // process requirements first
79 for (BundleWire bundleWire
: requiredDataModels
) {
80 processBundle(bundleWire
.getProvider().getBundle());
82 for (BundleCapability bundleCapability
: providedDataModels
) {
83 String name
= (String
) bundleCapability
.getAttributes().get(DataModelNamespace
.NAME
);
85 if (!dataModels
.containsKey(name
)) {
86 DataModel dataModel
= new DataModel(name
, bundleCapability
, requiredDataModels
);
87 dataModels
.put(dataModel
.getName(), dataModel
);
92 /** Return a negative depth if dataModel is required by ref, 0 otherwise. */
93 static int required(DataModel ref
, DataModel dataModel
, int depth
) {
94 for (DataModel dm
: ref
.getRequired()) {
95 if (dm
.equals(dataModel
))// found here
97 int d
= required(dm
, dataModel
, depth
- 1);
98 if (d
!= 0)// found deeper
101 return 0;// not found
105 private final String name
;
106 private final boolean abstrct
;
107 // private final boolean standalone;
108 private final String cnd
;
109 private final List
<DataModel
> required
;
111 private DataModel(String name
, BundleCapability bundleCapability
, List
<BundleWire
> requiredDataModels
) {
112 assert CMS_DATA_MODEL_NAMESPACE
.equals(bundleCapability
.getNamespace());
114 Map
<String
, Object
> attrs
= bundleCapability
.getAttributes();
115 abstrct
= KernelUtils
.asBoolean((String
) attrs
.get(DataModelNamespace
.ABSTRACT
));
116 // standalone = KernelUtils.asBoolean((String)
117 // attrs.get(DataModelNamespace.CAPABILITY_STANDALONE_ATTRIBUTE));
118 cnd
= (String
) attrs
.get(DataModelNamespace
.CND
);
119 List
<DataModel
> req
= new ArrayList
<>();
120 for (BundleWire wire
: requiredDataModels
) {
121 String requiredDataModelName
= (String
) wire
.getCapability().getAttributes()
122 .get(DataModelNamespace
.NAME
);
123 assert requiredDataModelName
!= null;
124 DataModel requiredDataModel
= dataModels
.get(requiredDataModelName
);
125 if (requiredDataModel
== null)
126 throw new CmsException("No required data model " + requiredDataModelName
);
127 req
.add(requiredDataModel
);
129 required
= Collections
.unmodifiableList(req
);
132 public String
getName() {
136 public boolean isAbstract() {
140 // public boolean isStandalone() {
141 // return !isAbstract();
144 public String
getCnd() {
148 public List
<DataModel
> getRequired() {
153 // public int compareTo(DataModel o) {
156 // int res = required(this, o, 0);
159 // // the other way round
160 // res = required(o, this, 0);
167 public int hashCode() {
168 return name
.hashCode();
172 public boolean equals(Object obj
) {
173 if (obj
instanceof DataModel
)
174 return ((DataModel
) obj
).name
.equals(name
);
179 public String
toString() {
180 return "Data model " + name
;