/* * Launches against the agent& main */ public void testAgentAndMain() throws Exception { Project project = workspace.getProject("p1"); Run bndrun = new Run(workspace, project.getBase(), project.getFile("one.bndrun")); bndrun.setProperty("-runpath", "biz.aQute.remote.launcher"); bndrun.setProperty("-runbundles", "bsn-1,bsn-2"); bndrun.setProperty("-runremote", "agent,main;agent=1090"); final RemoteProjectLauncherPlugin pl = (RemoteProjectLauncherPlugin) bndrun.getProjectLauncher(); pl.prepare(); List<? extends RunSession> sessions = pl.getRunSessions(); assertEquals(2, sessions.size()); RunSession agent = sessions.get(0); RunSession main = sessions.get(1); CountDownLatch agentLatch = launch(agent); CountDownLatch mainLatch = launch(main); agent.waitTillStarted(1000); main.waitTillStarted(1000); Thread.sleep(500); agent.cancel(); main.cancel(); agentLatch.await(); mainLatch.await(); assertEquals(-3, agent.getExitCode()); assertEquals(-3, main.getExitCode()); bndrun.close(); }
private ConditionalPermissionInfo setConditionalPermissionInfo( String name, ConditionInfo[] conds, PermissionInfo[] perms, boolean firstTry) { ConditionalPermissionUpdate update = newConditionalPermissionUpdate(); List rows = update.getConditionalPermissionInfos(); ConditionalPermissionInfo newInfo = newConditionalPermissionInfo(name, conds, perms, ConditionalPermissionInfo.ALLOW); int index = -1; if (name != null) { for (int i = 0; i < rows.size() && index < 0; i++) { ConditionalPermissionInfo info = (ConditionalPermissionInfo) rows.get(i); if (name.equals(info.getName())) { index = i; } } } if (index < 0) { // must always add to the beginning (bug 303930) rows.add(0, newInfo); index = 0; } else { rows.set(index, newInfo); } synchronized (lock) { if (!update.commit()) { if (firstTry) // try again setConditionalPermissionInfo(name, conds, perms, false); } return condAdminTable.getRow(index); } }
public static BundleRevisionDTO[] newArrayBundleRevisionDTO(BundleRevisions revisions) { if (revisions == null) { return null; } List<BundleRevision> revs = revisions.getRevisions(); final int size = revs.size(); BundleRevisionDTO[] dtos = new BundleRevisionDTO[size]; for (int i = 0; i < size; i++) { dtos[i] = new DTOBuilder().getBundleRevisionDTO(revs.get(i)); } return dtos; }
/* * Launches against the agent */ public void testSimpleLauncher() throws Exception { Project project = workspace.getProject("p1"); Run bndrun = new Run(workspace, project.getBase(), project.getFile("one.bndrun")); bndrun.setProperty("-runpath", "biz.aQute.remote.launcher"); bndrun.setProperty("-runbundles", "bsn-1,bsn-2"); bndrun.setProperty("-runremote", "test"); final RemoteProjectLauncherPlugin pl = (RemoteProjectLauncherPlugin) bndrun.getProjectLauncher(); pl.prepare(); final CountDownLatch latch = new CountDownLatch(1); final AtomicInteger exitCode = new AtomicInteger(-1); List<? extends RunSession> sessions = pl.getRunSessions(); assertEquals(1, sessions.size()); final RunSession session = sessions.get(0); Thread t = new Thread("test-launch") { public void run() { try { exitCode.set(session.launch()); } catch (Exception e) { e.printStackTrace(); } finally { latch.countDown(); } } }; t.start(); Thread.sleep(500); for (Bundle b : context.getBundles()) { System.out.println(b.getLocation()); } assertEquals(4, context.getBundles().length); String p1 = t1.getAbsolutePath(); System.out.println(p1); assertNotNull(context.getBundle(p1)); assertNotNull(context.getBundle(t2.getAbsolutePath())); pl.cancel(); latch.await(); assertEquals(-3, exitCode.get()); bndrun.close(); }
public static BundleWiringDTO[] newArrayBundleWiringDTO(BundleRevisions revisions) { if (revisions == null) { return null; } List<BundleRevision> revs = revisions.getRevisions(); final int size = revs.size(); List<BundleWiringDTO> dtos = new ArrayList<BundleWiringDTO>(size); for (int i = 0; i < size; i++) { BundleWiring wiring = revs.get(i).getWiring(); if (wiring != null) { dtos.add( new DTOBuilder().getBundleWiringDTO(wiring)); // use new DTOBuilder for each wiring dto } } return dtos.toArray(new BundleWiringDTO[dtos.size()]); }
boolean commit(List rows, long updateStamp) { checkAllPermission(); synchronized (lock) { if (updateStamp != timeStamp) return false; SecurityRow[] newRows = new SecurityRow[rows.size()]; Collection names = new ArrayList(); for (int i = 0; i < newRows.length; i++) { Object rowObj = rows.get(i); if (!(rowObj instanceof ConditionalPermissionInfo)) throw new IllegalStateException( "Invalid type \"" + rowObj.getClass().getName() + "\" at row: " + i); //$NON-NLS-1$//$NON-NLS-2$ ConditionalPermissionInfo infoBaseRow = (ConditionalPermissionInfo) rowObj; String name = infoBaseRow.getName(); if (name == null) name = generateName(); if (names.contains(name)) throw new IllegalStateException( "Duplicate name \"" + name + "\" at row: " + i); // $NON-NLS-1$//$NON-NLS-2$ newRows[i] = new SecurityRow( this, name, infoBaseRow.getConditionInfos(), infoBaseRow.getPermissionInfos(), infoBaseRow.getAccessDecision()); } condAdminTable = new SecurityTable(this, newRows); try { permissionStorage.saveConditionalPermissionInfos(condAdminTable.getEncodedRows()); } catch (IOException e) { // TODO log e.printStackTrace(); } timeStamp += 1; return true; } }
/* * unlocks the global lock and notifies the first waiting thread that they * now have the lock */ private static void unlock() { Thread waitingThread = null; Object loader = null; synchronized (BundleLoader.class) { lockCount--; if (lockCount != 0) return; if (waitingList.isEmpty()) { lockThread = null; return; } Object[] waiting = (Object[]) waitingList.get(0); waitingThread = (Thread) waiting[0]; loader = waiting[1]; } synchronized (loader) { synchronized (BundleLoader.class) { lockThread = waitingThread; waitingList.remove(0); loader.notifyAll(); } } }
/** * Registers/publishes services annotated with @OSGiServiceProvider. * * @param managedClass The managed class to instantiate and register as an OSGi service. * @param context The bundle context. * @param serviceRegs The list to save all service registrations to for later unregistration. * @throws Exception */ protected void registerServices( Class managedClass, BundleContext context, List<ServiceRegistration> serviceRegs) throws Exception { OSGiServiceProvider serviceProvider = (OSGiServiceProvider) managedClass.getAnnotation(OSGiServiceProvider.class); if (serviceProvider != null) { List<Tuple3<Properties, Object, List<String>>> serviceInstData = new LinkedList<>(); if (serviceProvider.properties().length > 0) { Tuple3<Properties, Object, List<String>> sd = new Tuple3<>(); sd.t1 = osgiPropertiesToProperties(serviceProvider.properties()); serviceInstData.add(sd); } else if (serviceProvider.instances().length > 0) { for (OSGiServiceInstance serviceInst : serviceProvider.instances()) { Tuple3<Properties, Object, List<String>> sd = new Tuple3<>(); sd.t1 = osgiPropertiesToProperties(serviceInst.properties()); serviceInstData.add(sd); } } else if (!serviceProvider.instanceFactoryClass().equals(InstanceFactory.class)) { InstanceFactory instanceFactory = (InstanceFactory) getManagedInstance(serviceProvider.instanceFactoryClass()); if (instanceFactory == null) { instanceFactory = serviceProvider.instanceFactoryClass().newInstance(); } for (Properties props : instanceFactory.getPropertiesPerInstance()) { Tuple3<Properties, Object, List<String>> sd = new Tuple3<>(); sd.t1 = props; serviceInstData.add(sd); } } else { Tuple3<Properties, Object, List<String>> sd = new Tuple3<>(); sd.t1 = new Properties(); serviceInstData.add(sd); } // The number of managedInstances and serviceInstData will always be the same. They both // originate // from the same information. List<Object> managedInstances = getManagedInstances(managedClass); for (int i = 0; i < serviceInstData.size(); i++) { List<String> serviceAPIs = new LinkedList<>(); if (serviceProvider.serviceAPIs().length > 0) { for (Class svcAPI : serviceProvider.serviceAPIs()) { serviceAPIs.add(svcAPI.getName()); } } else if (serviceProvider.instances().length > 0) { OSGiServiceInstance svsInstAnn = serviceProvider.instances()[i]; for (Class svcAPI : svsInstAnn.serviceAPIs()) { serviceAPIs.add(svcAPI.getName()); } } else if (!serviceProvider.instanceFactoryClass().equals(InstanceFactory.class)) { String svcAPIList = serviceInstData.get(i).t1.getProperty(InstanceFactory.SERVICE_API_CLASSES_PROPERTY); if (svcAPIList != null) { for (String svcAPI : svcAPIList.split(":")) { serviceAPIs.add(svcAPI); } } else { Class[] interfaces = managedClass.getInterfaces(); if (interfaces != null && interfaces.length > 0) { serviceAPIs.add(interfaces[0].getName()); } } } else { Class[] interfaces = managedClass.getInterfaces(); if (interfaces != null && interfaces.length > 0) { serviceAPIs.add(interfaces[0].getName()); } } Tuple3<Properties, Object, List<String>> sd = serviceInstData.get(i); sd.t3 = serviceAPIs; sd.t2 = managedInstances.get(i); } for (Tuple3<Properties, Object, List<String>> sd : serviceInstData) { sd.t1.put(Constants.SERVICE_PID, managedClass.getName()); if (!sd.t3.isEmpty()) { injectInstanceProps(sd.t2, sd.t1); for (String svcAPI : sd.t3) { ServiceRegistration serviceReg = context.registerService(svcAPI, sd.t2, sd.t1); serviceRegs.add(serviceReg); this.activatorLogger.info( "Registered '" + managedClass.getName() + "' as a service provider of '" + svcAPI + "' for bundle: " + context.getBundle().getSymbolicName() + "!"); } } else { throw new IllegalArgumentException( "The @OSGiServiceProvider annotated service of class '" + managedClass.getName() + "' does not implement a service interface!"); } } } }