Search in sources :

Example 1 with WrappedServiceMetadata

use of org.apache.aries.application.modelling.WrappedServiceMetadata in project aries by apache.

the class AbstractParserProxy method parseCDRForServices.

/**
 * Extract Service metadata from a ComponentDefinitionRegistry. When doing SCA modelling, we
 * need to suppress anonymous services. We don't want to do that when we're modelling for
 * provisioning dependencies.
 * @param cdr                       ComponentDefinitionRegistry
 * @param suppressAnonymousServices Unnamed services will not be returned if this is true
 * @return List<WrappedServiceMetadata>
 */
private List<ExportedService> parseCDRForServices(ComponentDefinitionRegistry cdr, boolean suppressAnonymousServices) {
    _logger.debug(LOG_ENTRY, "parseCDRForServices", new Object[] { cdr, suppressAnonymousServices });
    List<ExportedService> result = new ArrayList<ExportedService>();
    for (ComponentMetadata compMetadata : findAllComponents(cdr)) {
        if (compMetadata instanceof ServiceMetadata) {
            ServiceMetadata serviceMetadata = (ServiceMetadata) compMetadata;
            String serviceName;
            int ranking;
            Collection<String> interfaces = new ArrayList<String>();
            Map<String, Object> serviceProps = new HashMap<String, Object>();
            ranking = serviceMetadata.getRanking();
            for (Object i : serviceMetadata.getInterfaces()) {
                interfaces.add((String) i);
            }
            // get the service properties
            List<MapEntry> props = serviceMetadata.getServiceProperties();
            for (MapEntry entry : props) {
                String key = ((ValueMetadata) entry.getKey()).getStringValue();
                Metadata value = entry.getValue();
                if (value instanceof CollectionMetadata) {
                    processMultiValueProperty(serviceProps, key, value);
                } else {
                    serviceProps.put(key, ((ValueMetadata) entry.getValue()).getStringValue());
                }
            }
            // serviceName: use the service id unless that's not set,
            // in which case we use the bean id.
            serviceName = serviceMetadata.getId();
            // If the Service references a Bean, export the bean id as a service property
            // as per 121.6.5 p669 of the blueprint 1.0 specification
            Target t = serviceMetadata.getServiceComponent();
            String targetId = null;
            if (t instanceof RefMetadata) {
                targetId = ((RefMetadata) t).getComponentId();
            } else if (t instanceof BeanMetadata) {
                targetId = ((BeanMetadata) t).getId();
            }
            // or auto-generated for an anonymous service. This must ALWAYS be set.
            if (targetId != null && !targetId.startsWith(".")) {
                // Don't set this for anonymous inner components
                serviceProps.put("osgi.service.blueprint.compname", targetId);
                if (serviceName == null || serviceName.equals("") || serviceName.startsWith(".")) {
                    serviceName = targetId;
                }
            }
            if (serviceName != null && serviceName.startsWith("."))
                serviceName = null;
            // If suppressAnonymous services, do not expose services that have no name
            if (!suppressAnonymousServices || (serviceName != null)) {
                ExportedService wsm = _modellingManager.getExportedService(serviceName, ranking, interfaces, serviceProps);
                result.add(wsm);
            }
        }
    }
    _logger.debug(LOG_EXIT, "parseAllServiceElements", new Object[] { result });
    return result;
}
Also used : CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) MapEntry(org.osgi.service.blueprint.reflect.MapEntry) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) HashMap(java.util.HashMap) ExportedService(org.apache.aries.application.modelling.ExportedService) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) ArrayList(java.util.ArrayList) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) MapMetadata(org.osgi.service.blueprint.reflect.MapMetadata) ReferenceListMetadata(org.osgi.service.blueprint.reflect.ReferenceListMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) Target(org.osgi.service.blueprint.reflect.Target) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ServiceMetadata(org.osgi.service.blueprint.reflect.ServiceMetadata)

Example 2 with WrappedServiceMetadata

use of org.apache.aries.application.modelling.WrappedServiceMetadata in project aries by apache.

the class ParserProxyTest method basicTest1.

@Test
public void basicTest1() throws Exception {
    File bpXml = new File(resourceDir, "appModeller/test1.eba/bundle1.jar/OSGI-INF/blueprint/bp.xml");
    File bp2Xml = new File(resourceDir, "appModeller/test1.eba/bundle1.jar/OSGI-INF/blueprint/bp2.xml");
    List<URL> urls = new ArrayList<URL>();
    urls.add((bpXml.toURI()).toURL());
    urls.add((bp2Xml.toURI()).toURL());
    List<? extends WrappedServiceMetadata> results = _parserProxy.parse(urls);
    assertTrue("Four results expected, not " + results.size(), results.size() == 4);
    Set<WrappedServiceMetadata> resultSet = new HashSet<WrappedServiceMetadata>(results);
    Set<WrappedServiceMetadata> expectedResults = getTest1ExpectedResults();
    assertEquals("Blueprint parsed xml is not as expected: " + resultSet.toString() + " != " + expectedResults, resultSet, expectedResults);
}
Also used : WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ArrayList(java.util.ArrayList) File(java.io.File) URL(java.net.URL) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 3 with WrappedServiceMetadata

use of org.apache.aries.application.modelling.WrappedServiceMetadata in project aries by apache.

the class ParserProxyTest method testParseAllServiceElements.

@Test
public void testParseAllServiceElements() throws Exception {
    File bpXml = new File(resourceDir, "appModeller/test1.eba/bundle1.jar/OSGI-INF/blueprint/bp.xml");
    File bp2Xml = new File(resourceDir, "appModeller/test1.eba/bundle1.jar/OSGI-INF/blueprint/bp2.xml");
    List<WrappedServiceMetadata> services = new ArrayList<WrappedServiceMetadata>();
    List<WrappedReferenceMetadata> references = new ArrayList<WrappedReferenceMetadata>();
    FileInputStream fis = new FileInputStream(bpXml);
    ParsedServiceElements bpelem = _parserProxy.parseAllServiceElements(fis);
    services.addAll(bpelem.getServices());
    references.addAll(bpelem.getReferences());
    fis = new FileInputStream(bp2Xml);
    bpelem = _parserProxy.parseAllServiceElements(fis);
    services.addAll(bpelem.getServices());
    references.addAll(bpelem.getReferences());
    // We expect:
    // bp.xml: 3 services and 2 references
    // bp2.xml: 3 services and a reference list
    // 
    assertTrue("Six services expected, not " + services.size(), services.size() == 6);
    assertTrue("Three references expected, not " + references.size(), references.size() == 3);
    Set<WrappedServiceMetadata> expectedServices = getTest2ExpectedServices();
    // ServiceResultSet will contain some services with autogenerated names starting '.' so we can't
    // use a straight Set.equals(). We could add the autogenerated names to the expected results but instead
    // let's test that differsOnlyByName() works
    int serviceMatchesFound = 0;
    for (WrappedServiceMetadata result : services) {
        Iterator<WrappedServiceMetadata> it = expectedServices.iterator();
        while (it.hasNext()) {
            WrappedServiceMetadata next = it.next();
            if (result.equals(next) || result.identicalOrDiffersOnlyByName(next)) {
                serviceMatchesFound++;
                it.remove();
            }
        }
    }
    assertEquals("Parsed services are wrong: " + expectedServices + " unmatched ", 6, serviceMatchesFound);
    Set<WrappedReferenceMetadata> expectedReferences = getTest2ExpectedReferences();
    Set<WrappedReferenceMetadata> results = new HashSet<WrappedReferenceMetadata>(references);
    assertTrue("Parsed references are not as we'd expected: " + results.toString() + " != " + expectedReferences, results.equals(expectedReferences));
}
Also used : WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ArrayList(java.util.ArrayList) WrappedReferenceMetadata(org.apache.aries.application.modelling.WrappedReferenceMetadata) FileInputStream(java.io.FileInputStream) ParsedServiceElements(org.apache.aries.application.modelling.ParsedServiceElements) File(java.io.File) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 4 with WrappedServiceMetadata

use of org.apache.aries.application.modelling.WrappedServiceMetadata in project aries by apache.

the class ParserProxyTest method checkMultiValues.

@Test
public void checkMultiValues() throws Exception {
    File bpXml = new File(resourceDir, "appModeller/test1.eba/bundle1.jar/OSGI-INF/blueprint/bpMultiValues.xml");
    List<WrappedServiceMetadata> services = new ArrayList<WrappedServiceMetadata>();
    FileInputStream fis = new FileInputStream(bpXml);
    ParsedServiceElements bpelem = _parserProxy.parseAllServiceElements(fis);
    services.addAll(bpelem.getServices());
    assertEquals("Multi valued service not parsed correctly", services.size(), 1);
    WrappedServiceMetadata wsm = services.get(0);
    Map<String, Object> props = wsm.getServiceProperties();
    String[] intents = (String[]) props.get("service.intents");
    assertEquals("Service.intents[0] wrong", intents[0], "propagatesTransaction");
    assertEquals("Service.intents[1] wrong", intents[1], "confidentiality");
}
Also used : WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ArrayList(java.util.ArrayList) File(java.io.File) FileInputStream(java.io.FileInputStream) ParsedServiceElements(org.apache.aries.application.modelling.ParsedServiceElements) Test(org.junit.Test)

Example 5 with WrappedServiceMetadata

use of org.apache.aries.application.modelling.WrappedServiceMetadata in project aries by apache.

the class ExportedServiceTest method checkEquality.

@Test
public void checkEquality() {
    // public ExportedService (String name, int ranking, Collection<String> ifaces,
    // Map<String, String> serviceProperties ) {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put("away", "www.away.com");
    props.put("home", "www.home.net");
    WrappedServiceMetadata wsm1 = new ExportedServiceImpl(null, 0, Arrays.asList("a.b.c", "d.e.f"), props);
    WrappedServiceMetadata wsm2 = new ExportedServiceImpl(null, 0, Arrays.asList("d.e.f", "a.b.c"), props);
    assertTrue("Basic equality test", wsm1.equals(wsm2));
    assertTrue("Basic equality test", wsm2.equals(wsm1));
    assertTrue("Hashcodes equal", wsm1.hashCode() == wsm2.hashCode());
    wsm2 = new ExportedServiceImpl(null, 0, Arrays.asList("d.e.f", "a.b.c", "g.e.f"), props);
    assertFalse("Adding an interface makes them different", wsm1.equals(wsm2));
    assertFalse("Adding an interface makes them different", wsm2.equals(wsm1));
    assertFalse("Hashcodes should differ", wsm1.hashCode() == wsm2.hashCode());
    props = new HashMap<String, Object>(props);
    props.put("interim", "w3.interim.org");
    wsm1 = new ExportedServiceImpl(null, 0, Arrays.asList("a.b.c", "d.e.f", "g.e.f"), props);
    assertFalse("Adding a service property makes them different", wsm1.equals(wsm2));
    assertFalse("Adding a service property makes them different", wsm2.equals(wsm1));
    assertFalse("Hashcodes still different", wsm1.hashCode() == wsm2.hashCode());
}
Also used : HashMap(java.util.HashMap) WrappedServiceMetadata(org.apache.aries.application.modelling.WrappedServiceMetadata) ExportedServiceImpl(org.apache.aries.application.modelling.impl.ExportedServiceImpl) Test(org.junit.Test)

Aggregations

WrappedServiceMetadata (org.apache.aries.application.modelling.WrappedServiceMetadata)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 ParsedServiceElements (org.apache.aries.application.modelling.ParsedServiceElements)2 URL (java.net.URL)1 ExportedService (org.apache.aries.application.modelling.ExportedService)1 WrappedReferenceMetadata (org.apache.aries.application.modelling.WrappedReferenceMetadata)1 ExportedServiceImpl (org.apache.aries.application.modelling.impl.ExportedServiceImpl)1 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)1 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)1 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)1 MapEntry (org.osgi.service.blueprint.reflect.MapEntry)1 MapMetadata (org.osgi.service.blueprint.reflect.MapMetadata)1 Metadata (org.osgi.service.blueprint.reflect.Metadata)1 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)1