Search in sources :

Example 6 with NameValuePair

use of org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair in project aries by apache.

the class ManifestHeaderProcessorTest method testParseExportString.

@Test
public void testParseExportString() {
    String exportPackage = "com.acme.foo,com.acme.bar;version=1,com.acme.bar;version=2;uses:=\"a.b.c,d.e.f\";security=false;mandatory:=security";
    List<NameValuePair> exportPackageReturn = ManifestHeaderProcessor.parseExportString(exportPackage);
    int i = 0;
    assertEquals("The number of the packages is wrong.", 3, exportPackageReturn.size());
    for (NameValuePair nvp : exportPackageReturn) {
        if (nvp.getName().equals("com.acme.foo")) {
            i++;
            assertTrue("The directive or attribute should not be set.", nvp.getAttributes().isEmpty());
        } else if ((nvp.getName().equals("com.acme.bar")) && ("2".equals(nvp.getAttributes().get("version")))) {
            i++;
            assertEquals("The directive is wrong.", "a.b.c,d.e.f", nvp.getAttributes().get("uses:"));
            assertEquals("The directive is wrong.", "false", nvp.getAttributes().get("security"));
            assertEquals("The directive is wrong.", "security", nvp.getAttributes().get("mandatory:"));
        } else if ((nvp.getName().equals("com.acme.bar")) && ("1".equals(nvp.getAttributes().get("version")))) {
            i++;
            assertNull("The directive is wrong.", nvp.getAttributes().get("uses:"));
            assertNull("The directive is wrong.", nvp.getAttributes().get("security"));
            assertNull("The directive is wrong.", nvp.getAttributes().get("mandatory:"));
        }
    }
    // make sure all three packages stored
    assertEquals("The names of the packages are wrong.", 3, i);
    exportPackage = "com.acme.foo";
    exportPackageReturn = ManifestHeaderProcessor.parseExportString(exportPackage);
    int k = 0;
    assertEquals("The number of the packages is wrong.", 1, exportPackageReturn.size());
    for (NameValuePair nvp : exportPackageReturn) {
        if (nvp.getName().equals("com.acme.foo")) {
            k++;
            assertTrue("The directive or attribute should not be set.", nvp.getAttributes().isEmpty());
        }
    }
    assertEquals("The names of the packages are wrong.", 1, k);
    // test multiple packages separated by ;
    exportPackage = "com.acme.foo;com.acme.bar;version=\"2\";resolution:=optional";
    exportPackageReturn = ManifestHeaderProcessor.parseExportString(exportPackage);
    k = 0;
    assertEquals("The number of the packages is wrong.", 2, exportPackageReturn.size());
    for (NameValuePair nvp : exportPackageReturn) {
        if (nvp.getName().equals("com.acme.foo")) {
            k++;
            assertEquals("The attribute is wrong.", "2", nvp.getAttributes().get("version"));
            assertEquals("The attribute is wrong.", "optional", nvp.getAttributes().get("resolution:"));
        } else if (nvp.getName().equals("com.acme.bar")) {
            k++;
            assertEquals("The attribute is wrong.", "2", nvp.getAttributes().get("version"));
            assertEquals("The attribute is wrong.", "optional", nvp.getAttributes().get("resolution:"));
        }
    }
    assertEquals("The names of the packages are wrong.", 2, k);
    exportPackageReturn = ManifestHeaderProcessor.parseExportString("some.export.with.space.in;directive := spacey");
    assertEquals(exportPackageReturn.toString(), "spacey", exportPackageReturn.get(0).getAttributes().get("directive:"));
}
Also used : NameValuePair(org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair) Test(org.junit.Test)

Example 7 with NameValuePair

use of org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair in project aries by apache.

the class ManifestHeaderProcessorTest method testParseBundleSymbolicName.

/**
   * Test the Bundle manifest header entry of
   * Bundle-SymbolicName: com.acme.foo;singleton:=true
   */
@Test
public void testParseBundleSymbolicName() {
    String bundleSymbolicNameEntry = "com.acme.foo;singleton:=true;fragment-attachment:=always";
    NameValuePair nvp = ManifestHeaderProcessor.parseBundleSymbolicName(bundleSymbolicNameEntry);
    assertEquals("The symbolic name is wrong.", nvp.getName(), "com.acme.foo");
    assertEquals("The value is wrong.", "true", nvp.getAttributes().get("singleton:"));
    assertEquals("The directive is wrong.", "always", nvp.getAttributes().get("fragment-attachment:"));
    String bundleSymbolicNameEntry2 = "com.acme.foo";
    NameValuePair nvp2 = ManifestHeaderProcessor.parseBundleSymbolicName(bundleSymbolicNameEntry2);
    assertEquals("The symbolic name is wrong.", nvp2.getName(), "com.acme.foo");
}
Also used : NameValuePair(org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair) Test(org.junit.Test)

Example 8 with NameValuePair

use of org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair in project aries by apache.

the class EquinoxFrameworkUtils method getExportPackages.

public static Collection<Content> getExportPackages(BundleContext isolatedBundleContext) {
    Set<Content> exports = new HashSet<Content>();
    Bundle sysBundle = isolatedBundleContext.getBundle(0);
    if (sysBundle != null && sysBundle.getHeaders() != null) {
        String exportString = (String) sysBundle.getHeaders().get(Constants.EXPORT_PACKAGE);
        if (exportString != null) {
            for (NameValuePair nvp : ManifestHeaderProcessor.parseExportString(exportString)) exports.add(ContentFactory.parseContent(nvp.getName(), nvp.getAttributes()));
        }
    }
    return Collections.unmodifiableSet(exports);
}
Also used : NameValuePair(org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair) Content(org.apache.aries.application.Content) Bundle(org.osgi.framework.Bundle) HashSet(java.util.HashSet)

Example 9 with NameValuePair

use of org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair in project aries by apache.

the class RunningApplication method registerEJBs.

private void registerEJBs() {
    Collection<String> names = new HashSet<String>();
    Dictionary<String, String> d = bundle.getHeaders();
    String valueOfExportEJBHeader = d.get("Export-EJB");
    if ((valueOfExportEJBHeader == null)) {
        return;
    }
    if (names.contains(NONE)) {
        return;
    }
    List<NameValuePair> contentsOfExportEJBHeader = ManifestHeaderProcessor.parseExportString(valueOfExportEJBHeader);
    for (NameValuePair nvp : contentsOfExportEJBHeader) {
        names.add(nvp.getName());
    }
    if (valueOfExportEJBHeader.trim().equals("")) {
        names = new AllCollection<String>();
    }
    //Register our session beans
    for (BeanContext beanContext : ctx.getDeployments()) {
        String ejbName = beanContext.getEjbName();
        //Skip if not a Singleton or stateless bean
        ContainerType type = beanContext.getContainer().getContainerType();
        boolean register = type == ContainerType.SINGLETON || type == ContainerType.STATELESS;
        //Skip if not allowed name
        register &= names.contains(ejbName);
        if (!register) {
            continue;
        }
        if (beanContext.isLocalbean()) {
            BeanContext.BusinessLocalBeanHome home = beanContext.getBusinessLocalBeanHome();
            Dictionary<String, Object> props = new Hashtable<String, Object>();
            props.put("ejb.name", ejbName);
            props.put("ejb.type", getCasedType(type));
            regs.add(bundle.getBundleContext().registerService(beanContext.getBeanClass().getName(), new EJBServiceFactory(home), props));
        }
        for (Class<?> interfce : beanContext.getBusinessLocalInterfaces()) {
            BeanContext.BusinessLocalHome home = beanContext.getBusinessLocalHome(interfce);
            Dictionary<String, Object> props = new Hashtable<String, Object>();
            props.put("ejb.name", ejbName);
            props.put("ejb.type", getCasedType(type));
            regs.add(bundle.getBundleContext().registerService(interfce.getName(), new EJBServiceFactory(home), props));
        }
        for (Class<?> interfce : beanContext.getBusinessRemoteInterfaces()) {
            List<Class> interfaces = ProxyInterfaceResolver.getInterfaces(beanContext.getBeanClass(), interfce, beanContext.getBusinessRemoteInterfaces());
            BeanContext.BusinessRemoteHome home = beanContext.getBusinessRemoteHome(interfaces, interfce);
            Dictionary<String, Object> props = new Hashtable<String, Object>();
            props.put("sevice.exported.interfaces", interfce.getName());
            props.put("ejb.name", ejbName);
            props.put("ejb.type", getCasedType(type));
            regs.add(bundle.getBundleContext().registerService(interfce.getName(), new EJBServiceFactory(home), props));
        }
    }
}
Also used : NameValuePair(org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair) Hashtable(java.util.Hashtable) ContainerType(org.apache.openejb.ContainerType) BeanContext(org.apache.openejb.BeanContext) HashSet(java.util.HashSet)

Example 10 with NameValuePair

use of org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair in project aries by apache.

the class ManifestHeaderProcessorTest method testNameValuePair.

@Test
public void testNameValuePair() throws Exception {
    HashMap<String, String> attrs = new HashMap<String, String>();
    attrs.put("some", "value");
    NameValuePair nvp = new NameValuePair("key", attrs);
    assertEquals("The name value pair is not set properly.", nvp.getName(), "key");
    assertEquals("The value is not set properly.", nvp.getAttributes().get("some"), "value");
    attrs = new HashMap<String, String>();
    attrs.put("some", "value");
    NameValuePair anotherNvp = new NameValuePair("key", attrs);
    assertEquals("The two objects of NameValuePair is not equal.", nvp, anotherNvp);
    nvp.setName("newKey");
    attrs = new HashMap<String, String>();
    attrs.put("some", "newValue");
    nvp.setAttributes(attrs);
    assertEquals("The name value pair is not set properly.", nvp.getName(), "newKey");
    assertEquals("The value is not set properly.", nvp.getAttributes().get("some"), "newValue");
    Map<String, String> nvm1 = new HashMap<String, String>();
    nvm1.put("a", "b");
    nvm1.put("c", "d");
    Map<String, String> nvm2 = new HashMap<String, String>();
    nvm2.put("c", "d");
    nvm2.put("a", "b");
    assertEquals("The maps are not equal.", nvm1, nvm2);
    nvm2.put("e", "f");
    assertNotSame("The maps are the same.", nvm1, nvm2);
    NameValuePair nvp1 = new NameValuePair("one", nvm1);
    NameValuePair nvp2 = new NameValuePair("one", nvm2);
    assertNotSame("The pairs are identical ", nvp1, nvp2);
    nvm1.put("e", "f");
    assertEquals("The pairs are not equal.", nvp1, nvp2);
    List<NameValuePair> bundleInfoList1 = new ArrayList<NameValuePair>();
    bundleInfoList1.add(nvp1);
    List<NameValuePair> bundleInfoList2 = new ArrayList<NameValuePair>();
    bundleInfoList2.add(nvp1);
    bundleInfoList1.removeAll(bundleInfoList2);
    assertEquals("The List should be empty", bundleInfoList1.isEmpty(), true);
    assertNotSame("The two objects of NameValuePair is not equal.", nvp, anotherNvp);
}
Also used : NameValuePair(org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

NameValuePair (org.apache.aries.util.manifest.ManifestHeaderProcessor.NameValuePair)10 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Content (org.apache.aries.application.Content)2 Hashtable (java.util.Hashtable)1 IDirectory (org.apache.aries.util.filesystem.IDirectory)1 IFile (org.apache.aries.util.filesystem.IFile)1 BeanContext (org.apache.openejb.BeanContext)1 ContainerType (org.apache.openejb.ContainerType)1 Bundle (org.osgi.framework.Bundle)1