if (relToResolve.getTargetKind() == ComponentKind.SPECIFICATION) {
return new Resolved<Specification>(spec);
}
impls = spec.getImpls();
} else if (relToResolve.getTarget() instanceof ImplementationReference) {
Implementation impl = CST.componentBroker.getImpl(name);
if (impl == null) {
return null;
}
if (relToResolve.getTargetKind() == ComponentKind.IMPLEMENTATION) {
return new Resolved<Implementation>(impl);
}
impls = new HashSet<Implementation>();
impls.add(impl);
} else if (relToResolve.getTarget() instanceof InstanceReference) {
Instance inst = CST.componentBroker.getInst(name);
if (inst == null) {
return null;
}
if (relToResolve.getTargetKind() == ComponentKind.INSTANCE) {
return new Resolved<Instance>(inst);
}
logger.debug("if (relToResolve.getTarget() instanceof InstanceReference) ") ;
return null;
} else if (relToResolve.getTarget() instanceof ComponentReference<?>) {
logger.error("Invalid target reference : " + relToResolve.getTarget());
return null;
}
/*
* We have computed all component references It is either already
* resolved, or the implems are in impls. Now Resolve by resource.
*/
else if (relToResolve.getTarget() instanceof ResourceReference) {
if (relToResolve.getTargetKind() == ComponentKind.SPECIFICATION) {
Set<Specification> specs = new HashSet<Specification>();
for (Specification spec : CST.componentBroker.getSpecs()) {
if (spec.getProvidedResources().contains(relToResolve.getTarget())) {
specs.add(spec);
}
}
return relToResolve.getResolved(specs, false);
}
/*
* target Kind is implem or instance get all the implems that
* implement the resource
*/
impls = new HashSet<Implementation>();
for (Implementation impl : CST.componentBroker.getImpls()) {
if (impl.getProvidedResources().contains((relToResolve.getTarget()))) {
impls.add(impl);
}
}
}
// TargetKind is implem or instance, but no implem found.
if (impls == null || impls.isEmpty()) {
return null;
}
// If TargetKind is implem, select the good one(s)
if (relToResolve.getTargetKind() == ComponentKind.IMPLEMENTATION) {
return relToResolve.getResolved(impls, false);
}
/*
* We have in impls all the implementations satisfying the relation
* target (type and name only). We are looking for instances. Take all
* the instances of these implementations satisfying the relation
* constraints and visibility.
*/
/*
* Fast track : if looking for one instance and no preferences, return the first valid instance
*/
boolean fast = (!relToResolve.isMultiple() && !relToResolve.hasPreferences());
Set<Instance> insts = new HashSet<Instance>();
for (Implementation impl : impls) {
for (Instance inst : impl.getInsts()) {
if (inst.isSharable() && source.canSee(inst) && inst.matchRelationConstraints(relToResolve)) {
if (fast) {
return new Resolved<Instance>(inst);
}
insts.add(inst);
}
}
}
if (!insts.isEmpty()) {
/*
* If relation is singleton, select the best instance.
*/
if (relToResolve.isMultiple()) {
return new Resolved<Instance>(insts);
}
return new Resolved<Instance>(relToResolve.getPrefered(insts));
}
/*
* Keep only the implementations satisfying the constraints of the
* relation
*/
Set<Implementation> valid = new HashSet<Implementation>();
for (Implementation impl : impls) {
if (relToResolve.matchRelationConstraints(ComponentKind.IMPLEMENTATION, impl.getAllProperties())) {
valid.add(impl);
}
}
if (!!!valid.isEmpty()) {