use of org.apache.felix.ipojo.Factory in project felix by apache.
the class SvcInstance method match.
/**
* Does the service instance match with the given factory ?
* @param fact : the factory to test.
* @return true if the factory match, false otherwise.
*/
public boolean match(ServiceReference fact) {
// Check if the factory can provide the specification
ComponentTypeDescription desc = (ComponentTypeDescription) fact.getProperty("component.description");
if (desc == null) {
// No component type description.
return false;
}
String[] provides = desc.getprovidedServiceSpecification();
for (int i = 0; provides != null && i < provides.length; i++) {
if (provides[i].equals(m_specification)) {
// Check that the factory needs every properties contained in
// the configuration
PropertyDescription[] props = desc.getProperties();
Properties conf = new Properties();
Enumeration keys = m_configuration.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
if (!containsProperty(key, props)) {
return false;
}
conf.put(key, m_configuration.get(key));
}
Factory factory = (Factory) getService(fact);
return factory.isAcceptable(conf);
}
}
return false;
}
use of org.apache.felix.ipojo.Factory in project felix by apache.
the class SvcInstance method onServiceArrival.
/**
* A new service is injected.
* This method create the sub-service instance in the composite.
* @param ref : service reference.
* @see org.apache.felix.ipojo.util.DependencyModel#onServiceArrival(org.osgi.framework.ServiceReference)
*/
public void onServiceArrival(ServiceReference ref) {
// The given factory matches.
try {
Factory fact = (Factory) getService(ref);
if (m_factories.get(ref) == null) {
ComponentInstance instance = createInstance(fact);
m_factories.put(ref, instance);
} else {
m_handler.info("An arriving factory is already used, ignore the creation : " + fact.getName());
}
} catch (UnacceptableConfiguration e) {
m_handler.error("A matching factory refuses the actual configuration : " + e.getMessage());
m_handler.getCompositeManager().stop();
} catch (MissingHandlerException e) {
m_handler.error("A matching factory is no more valid : " + e.getMessage());
m_handler.getCompositeManager().stop();
} catch (ConfigurationException e) {
m_handler.error("A matching configuration is refused by the instance : " + e.getMessage());
m_handler.getCompositeManager().stop();
}
}
use of org.apache.felix.ipojo.Factory 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");
}
}
}
use of org.apache.felix.ipojo.Factory in project felix by apache.
the class TestBothProperties method setUp.
@Before
public void setUp() {
Factory fact = ipojoHelper.getFactory("CONFIG-BothConfigurableCheckService");
Properties props = new Properties();
props.put("instance.name", "under-test");
props.put("b", "1");
props.put("s", "1");
props.put("i", "1");
props.put("l", "1");
props.put("d", "1");
props.put("f", "1");
props.put("c", "a");
props.put("bool", "true");
props.put("bs", "{1,2,3}");
props.put("ss", "{1,2,3}");
props.put("is", "{1,2,3}");
props.put("ls", "{1,2,3}");
props.put("ds", "{1,2,3}");
props.put("fs", "{1,2,3}");
props.put("cs", "{a,b,c}");
props.put("bools", "{true,true,true}");
props.put("string", "foo");
props.put("strings", "{foo, bar, baz}");
try {
instance = fact.createComponentInstance(props);
} catch (Exception e) {
fail("Cannot create the under-test instance : " + e.getMessage());
}
try {
instance2 = fact.createComponentInstance(null);
} catch (Exception e) {
fail("Cannot create the under-test instance2 : " + e.getMessage());
}
}
use of org.apache.felix.ipojo.Factory in project felix by apache.
the class TestFieldProperties method setUp.
@Before
public void setUp() {
osgiHelper = new OSGiHelper(bc);
ipojoHelper = new IPOJOHelper(bc);
Factory fact = ipojoHelper.getFactory("CONFIG-FieldConfigurableCheckService");
Properties props = new Properties();
props.put("instance.name", "under-test");
props.put("b", "1");
props.put("s", "1");
props.put("i", "1");
props.put("l", "1");
props.put("d", "1");
props.put("f", "1");
props.put("c", "a");
props.put("bool", "true");
props.put("bs", "{1,2,3}");
props.put("ss", "{1,2,3}");
props.put("is", "{1,2,3}");
props.put("ls", "{1,2,3}");
props.put("ds", "{1,2,3}");
props.put("fs", "{1,2,3}");
props.put("cs", "{a,b,c}");
props.put("bools", "{true,true,true}");
props.put("string", "foo");
props.put("strings", "{foo, bar, baz}");
try {
instance = fact.createComponentInstance(props);
} catch (Exception e) {
fail("Cannot create the under-test instance : " + e.getMessage());
}
try {
instance2 = fact.createComponentInstance(null);
} catch (Exception e) {
e.printStackTrace();
fail("Cannot create the instance : " + e.getMessage());
}
}
Aggregations