use of org.apache.felix.ipojo.PolicyServiceContext in project felix by apache.
the class ServiceDependencyHandler method createServiceImport.
/**
* Create a Service importer object from the given Element.
* This method parse the given element and configure the service importer object.
* @param imp : Element describing the import
* @param confFilter : instance filter customization
* @throws ConfigurationException : the service importer cannot be created correctly
*/
private void createServiceImport(Element imp, Dictionary confFilter) throws ConfigurationException {
boolean optional = false;
boolean aggregate = false;
String specification = imp.getAttribute("specification");
if (specification == null) {
// Malformed import
error("Malformed import: the specification attribute is mandatory");
throw new ConfigurationException("Malformed import : the specification attribute is mandatory");
} else {
String opt = imp.getAttribute("optional");
optional = opt != null && opt.equalsIgnoreCase("true");
String agg = imp.getAttribute("aggregate");
aggregate = agg != null && agg.equalsIgnoreCase("true");
// Cannot import yourself
String original = "(&(objectClass=" + specification + ")(!(instance.name=" + getCompositeManager().getInstanceName() + ")))";
String filter = original;
String givenFilter = imp.getAttribute("filter");
if (givenFilter != null) {
// NOPMD
filter = "(&" + filter + givenFilter + ")";
}
String identitity = imp.getAttribute("id");
String scope = imp.getAttribute("scope");
// Get the default bundle context.
BundleContext context = getCompositeManager().getGlobalContext();
if (scope != null) {
if (scope.equalsIgnoreCase("global")) {
context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.GLOBAL);
} else if (scope.equalsIgnoreCase("composite")) {
context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.LOCAL);
} else if (scope.equalsIgnoreCase("composite+global")) {
context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.LOCAL_AND_GLOBAL);
}
}
// Configure instance filter if available
if (confFilter != null && identitity != null && confFilter.get(identitity) != null) {
filter = "(&" + original + (String) confFilter.get(identitity) + ")";
}
Filter fil = null;
if (filter != null) {
try {
fil = getCompositeManager().getGlobalContext().createFilter(filter);
} catch (InvalidSyntaxException e) {
throw new ConfigurationException("A required filter " + filter + " is malformed", e);
}
}
Comparator cmp = DependencyMetadataHelper.getComparator(imp, getCompositeManager().getGlobalContext());
Class spec = DependencyMetadataHelper.loadSpecification(specification, getCompositeManager().getGlobalContext());
int policy = DependencyMetadataHelper.getPolicy(imp);
ServiceImporter importer = new ServiceImporter(spec, fil, aggregate, optional, cmp, policy, context, identitity, this);
m_importers.add(importer);
String sources = imp.getAttribute("context-source");
if (sources != null) {
SourceManager source = new SourceManager(sources, filter, importer, getCompositeManager());
if (m_sources == null) {
m_sources = new ArrayList(1);
}
m_sources.add(source);
}
}
}
use of org.apache.felix.ipojo.PolicyServiceContext in project felix by apache.
the class ServiceImporter method setServiceLevelDependency.
/**
* Set that this dependency is a service level dependency.
* This forces the scoping policy to be STRICT.
*/
public void setServiceLevelDependency() {
m_specLevelReq = true;
PolicyServiceContext context = new PolicyServiceContext(m_handler.getCompositeManager().getGlobalContext(), m_handler.getCompositeManager().getParentServiceContext(), PolicyServiceContext.LOCAL);
setBundleContext(context);
}
use of org.apache.felix.ipojo.PolicyServiceContext in project felix by apache.
the class ProvidedServiceHandler method checkRequirement.
/**
* Check the correctness of the composite requirement against the service level dependency.
* @param imp : requirement to check
* @param elem : service-level dependency metadata
* @throws CompositionException : occurs if the requirement does not match with service-level specification requirement
*/
private void checkRequirement(ServiceImporter imp, Element elem) throws CompositionException {
String optional = elem.getAttribute("optional");
boolean opt = optional != null && optional.equalsIgnoreCase("true");
String aggregate = elem.getAttribute("aggregate");
boolean agg = aggregate != null && aggregate.equalsIgnoreCase("true");
if (imp == null) {
// Add the missing requirement
ServiceDependencyHandler handler = (ServiceDependencyHandler) getHandler(HandlerFactory.IPOJO_NAMESPACE + ":subservice");
if (handler == null) {
// Look for the ServiceDependencyHandler factory
HandlerManager handlerManager = null;
try {
ServiceReference[] refs = m_context.getServiceReferences(Factory.class.getName(), "(&(handler.name=subservice)(handler.namespace=" + HandlerFactory.IPOJO_NAMESPACE + ")(handler.type=composite))");
Factory factory = (Factory) m_context.getService(refs[0]);
handlerManager = (HandlerManager) factory.createComponentInstance(null, getCompositeManager().getServiceContext());
} catch (InvalidSyntaxException e) {
// Should not happen
} catch (UnacceptableConfiguration e) {
// Should not happen
} catch (MissingHandlerException e) {
// Should not happen
} catch (ConfigurationException e) {
// Should not happen
}
// Add the required handler
try {
handlerManager.init(getCompositeManager(), new Element("composite", ""), new Properties());
} catch (ConfigurationException e) {
error("Internal error : cannot configure the Import Handler : " + e.getMessage());
throw new CompositionException("Internal error : cannot configure the Import Handler", e);
}
handler = (ServiceDependencyHandler) handlerManager.getHandler();
getCompositeManager().addCompositeHandler(handlerManager);
}
String spec = elem.getAttribute("specification");
// Cannot import yourself
String filter = "(&(objectClass=" + spec + ")(!(instance.name=" + getCompositeManager().getInstanceName() + ")))";
String givenFilter = elem.getAttribute("filter");
if (givenFilter != null) {
// NOPMD
filter = "(&" + filter + givenFilter + ")";
}
BundleContext context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.GLOBAL);
Filter fil = null;
try {
fil = getCompositeManager().getGlobalContext().createFilter(filter);
} catch (InvalidSyntaxException e) {
throw new CompositionException("A required filter " + filter + " is malformed", e);
}
Class specToImport = null;
try {
specToImport = getCompositeManager().getGlobalContext().getBundle().loadClass(spec);
} catch (ClassNotFoundException e) {
throw new CompositionException("A required specification cannot be loaded : " + spec, e);
}
ServiceImporter importer = new ServiceImporter(specToImport, fil, agg, opt, null, DependencyModel.DYNAMIC_BINDING_POLICY, context, null, handler);
handler.getRequirements().add(importer);
SpecificationMetadata specMeta = new SpecificationMetadata(spec, m_context, agg, opt, this);
// Update the available types
m_services.add(specMeta);
return;
}
if (imp.isAggregate() && !agg) {
error("[" + getCompositeManager().getInstanceName() + "] The requirement " + elem.getAttribute("specification") + " is aggregate in the implementation and is declared as a simple service-level requirement");
throw new CompositionException("The requirement " + elem.getAttribute("specification") + " is aggregate in the implementation and is declared as a simple service-level requirement");
}
String filter = elem.getAttribute("filter");
if (filter != null) {
String filter2 = imp.getFilter();
if (filter2 == null || !filter2.equalsIgnoreCase(filter)) {
error("[" + getCompositeManager().getInstanceName() + "] The specification requirement " + elem.getAttribute("specification") + " as not the same filter as declared in the service-level requirement");
throw new CompositionException("The specification requirement " + elem.getAttribute("specification") + " as not the same filter as declared in the service-level requirement");
}
}
}
Aggregations