use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.
the class WovenClassImpl method add.
public synchronized void add(int i, String s) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
}
try {
List<BundleRequirement> reqs = ManifestParser.parseDynamicImportHeader(null, null, s);
} catch (Exception ex) {
RuntimeException re = new IllegalArgumentException("Unable to parse dynamic import.");
re.initCause(ex);
throw re;
}
checkImport(s);
m_imports.add(i, s);
}
use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.
the class WovenClassImpl method set.
public synchronized String set(int i, String s) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new AdminPermission(m_wiring.getBundle(), AdminPermission.WEAVE));
}
try {
List<BundleRequirement> reqs = ManifestParser.parseDynamicImportHeader(null, null, s);
} catch (Exception ex) {
RuntimeException re = new IllegalArgumentException("Unable to parse dynamic import.");
re.initCause(ex);
throw re;
}
checkImport(s);
return m_imports.set(i, s);
}
use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.
the class Util method isConfigurerBundle.
/**
* Check if the bundle contains configurations for the configurator
* @param bundle The bundle
* @param configuratorBundleId The bundle id of the configurator bundle to check the wiring
* @return Set of locations or {@code null}
*/
@SuppressWarnings("unchecked")
public static Set<String> isConfigurerBundle(final Bundle bundle, final long configuratorBundleId) {
// check for bundle wiring
final BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
if (bundleWiring == null) {
return null;
}
// check for bundle requirement to implementation namespace
final List<BundleRequirement> requirements = bundleWiring.getRequirements(NS_OSGI_EXTENDER);
if (requirements == null || requirements.isEmpty()) {
return null;
}
// get all wires for the implementation namespace
final List<BundleWire> wires = bundleWiring.getRequiredWires(NS_OSGI_EXTENDER);
for (final BundleWire wire : wires) {
// requirement (no need to do additional checks like version etc.)
if (wire.getProviderWiring() != null && wire.getProviderWiring().getBundle().getBundleId() == configuratorBundleId) {
final Object val = wire.getRequirement().getAttributes().get(PROP_CONFIGURATIONS);
if (val != null) {
if (val instanceof String) {
return Collections.singleton((String) val);
}
if (val instanceof List) {
final List<String> paths = (List<String>) val;
final Set<String> result = new HashSet<>();
for (final String p : paths) {
result.add(p);
}
return result;
}
SystemLogger.error("Attribute " + PROP_CONFIGURATIONS + " for configurator requirement has an invalid type: " + val + ". Using default configuration.");
}
return Collections.singleton(DEFAULT_PATH);
}
}
return null;
}
use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.
the class ConfiguratorTest method setupBundle.
private Bundle setupBundle(final long id) throws Exception {
final Bundle b = mock(Bundle.class);
when(b.getBundleId()).thenReturn(id);
when(b.getLastModified()).thenReturn(5L);
when(b.getState()).thenReturn(Bundle.ACTIVE);
final BundleWiring wiring = mock(BundleWiring.class);
when(b.adapt(BundleWiring.class)).thenReturn(wiring);
final BundleRequirement req = mock(BundleRequirement.class);
when(wiring.getRequirements(Util.NS_OSGI_EXTENDER)).thenReturn(Collections.singletonList(req));
final BundleWire wire = mock(BundleWire.class);
when(wire.getProviderWiring()).thenReturn(wiring);
when(wire.getRequirement()).thenReturn(req);
when(wiring.getBundle()).thenReturn(bundle);
when(wiring.getRequiredWires(Util.NS_OSGI_EXTENDER)).thenReturn(Collections.singletonList(wire));
final Vector<URL> urls = new Vector<>();
urls.add(this.getClass().getResource("/bundles/" + id + ".json"));
when(b.findEntries("OSGI-INF/configurator", "*.json", false)).thenReturn(urls.elements());
final BundleContext bContext = mock(BundleContext.class);
when(b.getBundleContext()).thenReturn(bContext);
when(bContext.getServiceReferences(ConfigurationAdmin.class, null)).thenReturn(Collections.singleton(caRef));
when(bundleContext.getBundle(id)).thenReturn(b);
return b;
}
use of org.osgi.framework.wiring.BundleRequirement in project felix by apache.
the class ManifestParserTest method testAttributes.
@SuppressWarnings("unchecked")
public void testAttributes() throws BundleException {
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(Constants.BUNDLE_MANIFESTVERSION, "2");
headers.put(Constants.BUNDLE_SYMBOLICNAME, "com.example.test.sample");
headers.put(Constants.PROVIDE_CAPABILITY, "com.example;theList:List<String>=\"red,green,blue\";theLong:Long=111");
headers.put(Constants.REQUIRE_CAPABILITY, "com.example.other;theList:List<String>=\"one,two,three\";theLong:Long=999");
BundleRevision mockBundleRevision = mock(BundleRevision.class);
when(mockBundleRevision.getSymbolicName()).thenReturn("com.example.test.sample");
ManifestParser mp = new ManifestParser(null, null, mockBundleRevision, headers);
BundleCapability bc = findCapability(mp.getCapabilities(), "com.example");
Long cLong = (Long) bc.getAttributes().get("theLong");
assertEquals(Long.valueOf(111), cLong);
List<String> cList = (List<String>) bc.getAttributes().get("theList");
assertEquals(3, cList.size());
assertTrue(cList.contains("red"));
BundleRequirement br = findRequirement(mp.getRequirements(), "com.example.other");
Long rLong = (Long) br.getAttributes().get("theLong");
assertEquals(Long.valueOf(999), rLong);
List<String> rList = (List<String>) br.getAttributes().get("theList");
assertEquals(3, rList.size());
}
Aggregations