public void testMbeans() throws MalformedObjectNameException, BundleException, InstanceNotFoundException, ReflectionException, MBeanException, AttributeNotFoundException { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName digraphName = new ObjectName(REGION_DOMAIN_PROP + ":type=RegionDigraph,*"); ObjectName regionNameAllQuery = new ObjectName(REGION_DOMAIN_PROP + ":type=Region,name=*,*"); Set<ObjectInstance> digraphs = server.queryMBeans(null, digraphName); assertEquals("Expected only one instance of digraph", 1, digraphs.size()); Set<ObjectInstance> regions = server.queryMBeans(null, regionNameAllQuery); assertEquals("Expected only one instance of region", 1, regions.size()); Region pp1Region = digraph.createRegion(PP1); Bundle pp1Bundle = bundleInstaller.installBundle(PP1, pp1Region); Region sp1Region = digraph.createRegion(SP1); Bundle sp1Bundle = bundleInstaller.installBundle(SP1, sp1Region); regions = server.queryMBeans(null, regionNameAllQuery); assertEquals("Wrong number of regions", 3, regions.size()); Set<ObjectInstance> pp1Query = server.queryMBeans(null, new ObjectName(REGION_DOMAIN_PROP + ":type=Region,name=" + PP1 + ",*")); assertEquals("Expected only one instance of: " + PP1, 1, pp1Query.size()); Set<ObjectInstance> sp1Query = server.queryMBeans(null, new ObjectName(REGION_DOMAIN_PROP + ":type=Region,name=" + SP1 + ",*")); assertEquals("Expected only one instance of: " + SP1, 1, sp1Query.size()); ObjectName pp1Name = (ObjectName) server.invoke(digraphs.iterator().next().getObjectName(), "getRegion", new Object[] {PP1}, new String[] {String.class.getName()}); assertEquals(PP1 + " regions not equal.", pp1Query.iterator().next().getObjectName(), pp1Name); ObjectName sp1Name = (ObjectName) server.invoke(digraphs.iterator().next().getObjectName(), "getRegion", new Object[] {SP1}, new String[] {String.class.getName()}); assertEquals(SP1 + " regions not equal.", sp1Query.iterator().next().getObjectName(), sp1Name); // test non existing region ObjectName shouldNotExistName = (ObjectName) server.invoke(digraphs.iterator().next().getObjectName(), "getRegion", new Object[] {"ShouldNotExist"}, new String[] {String.class.getName()}); assertNull("Should not exist", shouldNotExistName); long[] bundleIds = (long[]) server.getAttribute(pp1Name, "BundleIds"); assertEquals("Wrong number of bundles", 1, bundleIds.length); assertEquals("Wrong bundle", pp1Bundle.getBundleId(), bundleIds[0]); String name = (String) server.getAttribute(pp1Name, "Name"); assertEquals("Wrong name", PP1, name); bundleIds = (long[]) server.getAttribute(sp1Name, "BundleIds"); assertEquals("Wrong number of bundles", 1, bundleIds.length); assertEquals("Wrong bundle", sp1Bundle.getBundleId(), bundleIds[0]); name = (String) server.getAttribute(sp1Name, "Name"); assertEquals("Wrong name", SP1, name); regionBundle.stop(); // Now make sure we have no mbeans digraphs = server.queryMBeans(digraphName, null); assertEquals("Wrong number of digraphs", 0, digraphs.size()); regions = server.queryMBeans(null, regionNameAllQuery); assertEquals("Wrong number of regions", 0, regions.size()); }
private MetaTypeProviderWrapper[] getMetaTypeProviders() { Map<ServiceReference<Object>, Object> services = _tracker.getTracked(); if (services.isEmpty()) return new MetaTypeProviderWrapper[0]; Set<ServiceReference<Object>> serviceReferences = services.keySet(); Set<MetaTypeProviderWrapper> result = new HashSet<MetaTypeProviderWrapper>(); for (ServiceReference<Object> serviceReference : serviceReferences) { if (serviceReference.getBundle() == _bundle) { Object service = services.get(serviceReference); // If the service is not a MetaTypeProvider, we're not interested in it. if (service instanceof MetaTypeProvider) { // Include the METATYPE_PID, if present, to return as part of getPids(). Also, include the // METATYPE_FACTORY_PID, if present, to return as part of getFactoryPids(). // The filter ensures at least one of these properties was set for a standalone // MetaTypeProvider. addMetaTypeProviderWrappers( MetaTypeProvider.METATYPE_PID, serviceReference, (MetaTypeProvider) service, false, result); addMetaTypeProviderWrappers( MetaTypeProvider.METATYPE_FACTORY_PID, serviceReference, (MetaTypeProvider) service, true, result); // If the service is a ManagedService, include the SERVICE_PID to return as part of // getPids(). // The filter ensures the SERVICE_PID property was set. if (service instanceof ManagedService) { addMetaTypeProviderWrappers( Constants.SERVICE_PID, serviceReference, (MetaTypeProvider) service, false, result); } // If the service is a ManagedServiceFactory, include the SERVICE_PID to return as part of // getFactoryPids(). // The filter ensures the SERVICE_PID property was set. else if (service instanceof ManagedServiceFactory) { addMetaTypeProviderWrappers( Constants.SERVICE_PID, serviceReference, (MetaTypeProvider) service, true, result); } } } } return result.toArray(new MetaTypeProviderWrapper[result.size()]); }
private static Object mapValue(Object v) { if ((v == null) || v instanceof Number || v instanceof Boolean || v instanceof Character || v instanceof String || v instanceof DTO) { return v; } if (v instanceof Map) { Map<?, ?> m = (Map<?, ?>) v; Map<Object, Object> map = newMap(m.size()); for (Map.Entry<?, ?> e : m.entrySet()) { map.put(mapValue(e.getKey()), mapValue(e.getValue())); } return map; } if (v instanceof List) { List<?> c = (List<?>) v; List<Object> list = newList(c.size()); for (Object o : c) { list.add(mapValue(o)); } return list; } if (v instanceof Set) { Set<?> c = (Set<?>) v; Set<Object> set = newSet(c.size()); for (Object o : c) { set.add(mapValue(o)); } return set; } if (v.getClass().isArray()) { final int length = Array.getLength(v); final Class<?> componentType = mapComponentType(v.getClass().getComponentType()); Object array = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Array.set(array, i, mapValue(Array.get(v, i))); } return array; } return String.valueOf(v); }
public void testPersistenceBug343020() throws BundleException, InvalidSyntaxException { // get the system region Region systemRegion = digraph.getRegion(0); // create a test region Region testRegion = digraph.createRegion(getName()); RegionFilterBuilder builder = digraph.createRegionFilterBuilder(); // Import the system bundle from the systemRegion builder.allow(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, "(id=0)"); // import PP1 builder.allow(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, "(" + RegionFilter.VISIBLE_PACKAGE_NAMESPACE + "=pkg1.*)"); digraph.connect(testRegion, builder.build(), systemRegion); // install CP2 Bundle cp2 = bundleInstaller.installBundle(CP2, testRegion); bundleInstaller.resolveBundles(new Bundle[] {cp2}); assertEquals("Wrong state for pc1.", Bundle.INSTALLED, cp2.getState()); regionBundle.stop(); // install PP1 there is no region alive bundleInstaller.installBundle(PP1); // start region bundle and confirm we can resolve cp2 now startRegionBundle(); bundleInstaller.refreshPackages(new Bundle[] {cp2}); assertEquals("Wrong state for pc1.", Bundle.RESOLVED, cp2.getState()); // stop region bundle to test uninstalling bundles while stopped regionBundle.stop(); cp2.uninstall(); startRegionBundle(); testRegion = digraph.getRegion(getName()); assertNotNull("No test region found.", testRegion); Set<Long> testIds = testRegion.getBundleIds(); assertEquals("Wrong number of test ids.", 0, testIds.size()); }
public static String[] getApplicationNames() { Set result = getApplicationNameSet(); return (String[]) result.toArray(new String[result.size()]); }