void asyncUpdate() throws BundleException { if (getEquinoxContainer().getConfiguration().getDebug().DEBUG_SYSTEM_BUNDLE) { Debug.printStackTrace( new Exception("Framework has been requested to update (restart).")); // $NON-NLS-1$ } lockStateChange(ModuleEvent.UPDATED); try { if (Module.ACTIVE_SET.contains(getState())) { Thread t = new Thread( new Runnable() { @Override public void run() { try { update(); } catch (Throwable e) { SystemBundle.this .getEquinoxContainer() .getLogServices() .log( EquinoxContainer.NAME, FrameworkLogEntry.ERROR, "Error updating the framework.", e); //$NON-NLS-1$ } } }, "Framework update"); //$NON-NLS-1$ t.start(); } } finally { unlockStateChange(ModuleEvent.UPDATED); } }
@Override protected void setUp() throws Exception { tmp = IO.getFile("generated/tmp"); tmp.mkdirs(); IO.copy(IO.getFile("testdata/ws"), tmp); workspace = Workspace.getWorkspace(tmp); workspace.refresh(); InfoRepository repo = workspace.getPlugin(InfoRepository.class); t1 = create("bsn-1", new Version(1, 0, 0)); t2 = create("bsn-2", new Version(1, 0, 0)); repo.put(new FileInputStream(t1), null); repo.put(new FileInputStream(t2), null); t1 = repo.get("bsn-1", new Version(1, 0, 0), null); t2 = repo.get("bsn-2", new Version(1, 0, 0), null); repo.put(new FileInputStream(IO.getFile("generated/biz.aQute.remote.launcher.jar")), null); workspace.getPlugins().add(repo); File storage = IO.getFile("generated/storage-1"); storage.mkdirs(); configuration = new HashMap<String, Object>(); configuration.put( Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT); configuration.put(Constants.FRAMEWORK_STORAGE, storage.getAbsolutePath()); configuration.put( Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.osgi.framework.launch;version=1.2"); framework = new org.apache.felix.framework.FrameworkFactory().newFramework(configuration); framework.init(); framework.start(); context = framework.getBundleContext(); location = "reference:" + IO.getFile("generated/biz.aQute.remote.agent.jar").toURI().toString(); agent = context.installBundle(location); agent.start(); thread = new Thread() { @Override public void run() { try { Main.main( new String[] { "-s", "generated/storage", "-c", "generated/cache", "-p", "1090", "-et" }); } catch (Exception e) { e.printStackTrace(); } } }; thread.setDaemon(true); thread.start(); super.setUp(); }
Object setContextFinder() { if (!SET_TCCL) return Boolean.FALSE; Thread currentThread = Thread.currentThread(); ClassLoader previousTCCL = currentThread.getContextClassLoader(); ClassLoader contextFinder = framework.getContextFinder(); if (previousTCCL != contextFinder) { currentThread.setContextClassLoader(framework.getContextFinder()); return previousTCCL; } return Boolean.FALSE; }
/** * Creates a shell channel to the remote machine a new jsch session is also created if the current * one is invalid * * @param sshContact the contact of the remote machine * @param firstMessage the first message */ public void connectShell(final ContactSSH sshContact, final Message firstMessage) { sshContact.setConnectionInProgress(true); final UIService uiService = this.uiService; final Thread newConnection = new Thread( (new Runnable() { public void run() { OperationSetPersistentPresenceSSHImpl persistentPresence = (OperationSetPersistentPresenceSSHImpl) sshContact.getParentPresenceOperationSet(); persistentPresence.changeContactPresenceStatus( sshContact, SSHStatusEnum.CONNECTING); try { if (!isSessionValid(sshContact)) createSSHSessionAndLogin(sshContact); createShellChannel(sshContact); // initalizing the reader and writers of ssh contact persistentPresence.changeContactPresenceStatus( sshContact, SSHStatusEnum.CONNECTED); showWelcomeMessage(sshContact); sshContact.setMessageType(sshContact.CONVERSATION_MESSAGE_RECEIVED); sshContact.setConnectionInProgress(false); Thread.sleep(1500); sshContact.setCommandSent(true); basicInstantMessaging.sendInstantMessage(sshContact, firstMessage); } // rigoruos Exception Checking in future catch (Exception ex) { persistentPresence.changeContactPresenceStatus( sshContact, SSHStatusEnum.NOT_AVAILABLE); ex.printStackTrace(); } finally { sshContact.setConnectionInProgress(false); } } })); newConnection.start(); }
/* * 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(); }
/* * 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(); }
@SuppressWarnings("unchecked") private <T> T getService(Class<T> serviceType, String filter, long timeout) throws TestContainerException { assert m_framework != null : "Framework should be up"; assert serviceType != null : "serviceType not be null"; final Long start = System.currentTimeMillis(); LOG.info("Aquiring Service " + serviceType.getName() + " " + (filter != null ? filter : "")); do { try { ServiceReference[] reference = m_framework.getBundleContext().getServiceReferences(serviceType.getName(), filter); if (reference != null && reference.length > 0) { return ((T) m_framework.getBundleContext().getService(reference[0])); } Thread.sleep(200); } catch (Exception e) { LOG.error("Some problem during looking up service from framework: " + m_framework, e); } // wait a bit } while ((System.currentTimeMillis()) < start + timeout); printAvailableAlternatives(serviceType); throw new TestContainerException( "Not found a matching Service " + serviceType.getName() + " for Filter:" + (filter != null ? filter : "")); }
public boolean checkEvents(BundleEvent[] expevents) { boolean res = true; for (int i = 0; i < 20; i++) { try { Thread.sleep(100); } catch (InterruptedException ignore) { } if (events.size() == expevents.length) { break; } } if (events.size() == expevents.length) { for (int i = 0; i < events.size(); i++) { BundleEvent be = (BundleEvent) events.elementAt(i); if (!(be.getBundle().equals(expevents[i].getBundle()) && be.getType() == expevents[i].getType())) { res = false; } } } else { res = false; } if (!res) { out.println("Real events"); for (int i = 0; i < events.size(); i++) { BundleEvent be = (BundleEvent) events.elementAt(i); out.println("Event " + be.getBundle() + ", Type " + be.getType()); } out.println("Expected events"); for (int i = 0; i < expevents.length; i++) { out.println("Event " + expevents[i].getBundle() + ", Type " + expevents[i].getType()); } } return res; }
/** * Closes given {@link #transportManagers} of this <tt>Conference</tt> and removes corresponding * channel bundle. */ void closeTransportManager(TransportManager transportManager) { synchronized (transportManagers) { for (Iterator<IceUdpTransportManager> i = transportManagers.values().iterator(); i.hasNext(); ) { if (i.next() == transportManager) { i.remove(); // Presumably, we have a single association for // transportManager. break; } } // Close manager try { transportManager.close(); } catch (Throwable t) { logger.warn( "Failed to close an IceUdpTransportManager of" + " conference " + getID() + "!", t); // The whole point of explicitly closing the // transportManagers of this Conference is to prevent memory // leaks. Hence, it does not make sense to possibly leave // TransportManagers open because a TransportManager has // failed to close. if (t instanceof InterruptedException) Thread.currentThread().interrupt(); else if (t instanceof ThreadDeath) throw (ThreadDeath) t; } } }
private CountDownLatch launch(final RunSession session) { final CountDownLatch latch = new CountDownLatch(1); Thread t = new Thread("test-launch") { public void run() { try { session.launch(); } catch (Exception e) { e.printStackTrace(); } finally { latch.countDown(); } } }; t.start(); return latch; }
/* * see bug 121737 * To ensure that we do not enter a deadly embrace between classloader cycles * we attempt to obtain a global lock before do normal osgi delegation. * This approach ensures that only one thread has a classloader locked at a time */ private static void lock(Object loader) { Thread currentThread = Thread.currentThread(); boolean interrupted = false; synchronized (loader) { if (tryLock(currentThread, loader)) return; // this thread has the lock do { try { // we wait on the loader object here to release its lock incase we have it. // we do not way to wait while holding this lock because that will cause deadlock loader.wait(); } catch (InterruptedException e) { interrupted = true; // we still want to try again } } while (!tryLock(currentThread)); } if (interrupted) currentThread.interrupt(); }
public TestContainer start() throws TestContainerException { ClassLoader parent = null; try { final Map<String, String> p = new HashMap<String, String>(m_properties); String folder = p.get("org.osgi.framework.storage"); if (folder == null) { folder = System.getProperty("org.osgi.framework.storage"); } if (folder == null) { // folder = System.getProperty( "user.home" ) + File.separator + "osgi"; folder = getCache(); } LOG.debug("Cache folder set to " + folder); FileUtils.delete(new File(folder)); // load default stuff p.put("org.osgi.framework.storage", folder); // System.setProperty( "org.osgi.vendor.framework", "org.ops4j.pax.exam" ); p.put( "org.osgi.framework.system.packages.extra", "org.ops4j.pax.exam;version=" + skipSnapshotFlag(Info.getPaxExamVersion())); parent = Thread.currentThread().getContextClassLoader(); // Thread.currentThread().setContextClassLoader( null ); m_framework = m_frameworkFactory.newFramework(p); m_framework.init(); installAndStartBundles(m_framework.getBundleContext()); Thread.currentThread().setContextClassLoader(parent); } catch (Exception e) { throw new TestContainerException("Problem starting test container.", e); } finally { if (parent != null) { Thread.currentThread().setContextClassLoader(parent); } } return this; }
public FrameworkEvent waitForStop(long timeout) throws InterruptedException { long deadline = System.currentTimeMillis() + timeout; while (state != Bundle.UNINSTALLED) { if (timeout != 0) { long wait = deadline - System.currentTimeMillis(); if (wait <= 0) return new FrameworkEvent(FrameworkEvent.WAIT_TIMEDOUT, this, null); } Thread.sleep(100); } return new FrameworkEvent(FrameworkEvent.STOPPED, this, null); }
void asyncStop() throws BundleException { if (getEquinoxContainer().getConfiguration().getDebug().DEBUG_SYSTEM_BUNDLE) { Debug.printStackTrace( new Exception("Framework has been requested to stop.")); // $NON-NLS-1$ } lockStateChange(ModuleEvent.STOPPED); try { if (Module.ACTIVE_SET.contains(getState())) { // TODO this still has a chance of a race condition: // multiple threads could get started if stop is called over and over Thread t = new Thread( new Runnable() { @Override public void run() { try { stop(); } catch (Throwable e) { SystemBundle.this .getEquinoxContainer() .getLogServices() .log( EquinoxContainer.NAME, FrameworkLogEntry.ERROR, "Error stopping the framework.", e); //$NON-NLS-1$ } } }, "Framework stop"); //$NON-NLS-1$ t.start(); } } finally { unlockStateChange(ModuleEvent.STOPPED); } }
public void stop() { if (reaper != null) { bReap = false; try { reaper.wait(1000); } catch (Exception ignored) { } reaper = null; } if (reg != null) { reg.unregister(); reg = null; slTracker.close(); } }
/** * Expires this <tt>Conference</tt>, its <tt>Content</tt>s and their respective <tt>Channel</tt>s. * Releases the resources acquired by this instance throughout its life time and prepares it to be * garbage collected. */ public void expire() { synchronized (this) { if (expired) return; else expired = true; } EventAdmin eventAdmin = videobridge.getEventAdmin(); if (eventAdmin != null) eventAdmin.sendEvent(EventFactory.conferenceExpired(this)); setRecording(false); if (recorderEventHandler != null) { recorderEventHandler.close(); recorderEventHandler = null; } Videobridge videobridge = getVideobridge(); try { videobridge.expireConference(this); } finally { // Expire the Contents of this Conference. for (Content content : getContents()) { try { content.expire(); } catch (Throwable t) { logger.warn( "Failed to expire content " + content.getName() + " of conference " + getID() + "!", t); if (t instanceof InterruptedException) Thread.currentThread().interrupt(); else if (t instanceof ThreadDeath) throw (ThreadDeath) t; } } // Close the transportManagers of this Conference. Normally, there // will be no TransportManager left to close at this point because // all Channels have expired and the last Channel to be removed from // a TransportManager closes the TransportManager. However, a // Channel may have expired before it has learned of its // TransportManager and then the TransportManager will not close. closeTransportManagers(); if (logger.isInfoEnabled()) { logger.info( "Expired conference " + getID() + ". " + videobridge.getConferenceCountString()); } } }
/** * Makes home folder and the configuration file readable and writable only to the owner. * * @param cs the <tt>ConfigurationService</tt> instance to check for home folder and configuration * file. */ private static void fixPermissions(ConfigurationService cs) { if (!OSUtils.IS_LINUX && !OSUtils.IS_MAC) return; try { // let's check config file and config folder File homeFolder = new File(cs.getScHomeDirLocation(), cs.getScHomeDirName()); Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>() { { add(PosixFilePermission.OWNER_READ); add(PosixFilePermission.OWNER_WRITE); add(PosixFilePermission.OWNER_EXECUTE); } }; Files.setPosixFilePermissions(Paths.get(homeFolder.getAbsolutePath()), perms); String fileName = cs.getConfigurationFilename(); if (fileName != null) { File cf = new File(homeFolder, fileName); if (cf.exists()) { perms = new HashSet<PosixFilePermission>() { { add(PosixFilePermission.OWNER_READ); add(PosixFilePermission.OWNER_WRITE); } }; Files.setPosixFilePermissions(Paths.get(cf.getAbsolutePath()), perms); } } } catch (Throwable t) { logger.error("Error creating c lib instance for fixing file permissions", t); if (t instanceof InterruptedException) Thread.currentThread().interrupt(); else if (t instanceof ThreadDeath) throw (ThreadDeath) t; } }
/** * Creates a SSH Session with a remote machine and tries to login according to the details * specified by Contact An appropriate message is shown to the end user in case the login fails * * @param sshContact ID of SSH Contact * @throws JSchException if a JSch is unable to create a SSH Session with the remote machine * @throws InterruptedException if the thread is interrupted before session connected or is timed * out * @throws OperationFailedException if not of above reasons :-) */ public void createSSHSessionAndLogin(ContactSSH sshContact) throws JSchException, OperationFailedException, InterruptedException { logger.info("Creating a new SSH Session to " + sshContact.getHostName()); // creating a new JSch Stack identifier for contact JSch jsch = new JSch(); String knownHosts = (String) accountID.getAccountProperties().get("KNOWN_HOSTS_FILE"); if (!knownHosts.equals("Optional")) jsch.setKnownHosts(knownHosts); String identitiyKey = (String) accountID.getAccountProperties().get("IDENTITY_FILE"); String userName = sshContact.getUserName(); // use the name of system user if the contact has not supplied SSH // details if (userName.equals("")) userName = System.getProperty("user.name"); if (!identitiyKey.equals("Optional")) jsch.addIdentity(identitiyKey); // creating a new session for the contact Session session = jsch.getSession( userName, sshContact.getHostName(), sshContact.getSSHConfigurationForm().getPort()); /** * Creating and associating User Info with the session User Info passes authentication from * sshContact to SSH Stack */ SSHUserInfo sshUserInfo = new SSHUserInfo(sshContact); session.setUserInfo(sshUserInfo); /** initializing the session */ session.connect(connectionTimeout); int count = 0; // wait for session to get connected while (!session.isConnected() && count <= 30000) { Thread.sleep(1000); count += 1000; logger.trace("SSH:" + sshContact.getHostName() + ": Sleep zzz .. "); } // if timeout have exceeded if (count > 30000) { sshContact.setSSHSession(null); JOptionPane.showMessageDialog( null, "SSH Connection attempt to " + sshContact.getHostName() + " timed out"); // error codes are not defined yet throw new OperationFailedException( "SSH Connection attempt to " + sshContact.getHostName() + " timed out", 2); } sshContact.setJSch(jsch); sshContact.setSSHSession(session); logger.info("A new SSH Session to " + sshContact.getHostName() + " Created"); }
/** * Bottom level event dispatcher for the BundleContext. * * @param originalListener listener object registered under. * @param l listener to call (may be filtered). * @param action Event class type * @param object Event object */ public void dispatchEvent(Object originalListener, Object l, int action, Object object) { // save the bundle ref to a local variable // to avoid interference from another thread closing this context AbstractBundle tmpBundle = bundle; Object previousTCCL = setContextFinder(); try { if (isValid()) /* if context still valid */ { switch (action) { case Framework.BUNDLEEVENT: case Framework.BUNDLEEVENTSYNC: { BundleListener listener = (BundleListener) l; if (Debug.DEBUG_EVENTS) { String listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener)); // $NON-NLS-1$ Debug.println( "dispatchBundleEvent[" + tmpBundle + "](" + listenerName + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } BundleEvent event = (BundleEvent) object; switch (event.getType()) { case Framework.BATCHEVENT_BEGIN: { if (listener instanceof BatchBundleListener) ((BatchBundleListener) listener).batchBegin(); break; } case Framework.BATCHEVENT_END: { if (listener instanceof BatchBundleListener) ((BatchBundleListener) listener).batchEnd(); break; } default: { listener.bundleChanged((BundleEvent) object); } } break; } case ServiceRegistry.SERVICEEVENT: { ServiceEvent event = (ServiceEvent) object; ServiceListener listener = (ServiceListener) l; if (Debug.DEBUG_EVENTS) { String listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener)); // $NON-NLS-1$ Debug.println( "dispatchServiceEvent[" + tmpBundle + "](" + listenerName + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } listener.serviceChanged(event); break; } case Framework.FRAMEWORKEVENT: { FrameworkListener listener = (FrameworkListener) l; if (Debug.DEBUG_EVENTS) { String listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener)); // $NON-NLS-1$ Debug.println( "dispatchFrameworkEvent[" + tmpBundle + "](" + listenerName + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } listener.frameworkEvent((FrameworkEvent) object); break; } default: { throw new InternalError(); } } } } catch (Throwable t) { if (Debug.DEBUG_GENERAL) { Debug.println( "Exception in bottom level event dispatcher: " + t.getMessage()); // $NON-NLS-1$ Debug.printStackTrace(t); } // allow the adaptor to handle this unexpected error framework.adaptor.handleRuntimeError(t); publisherror: { if (action == Framework.FRAMEWORKEVENT) { FrameworkEvent event = (FrameworkEvent) object; if (event.getType() == FrameworkEvent.ERROR) { break publisherror; // avoid infinite loop } } framework.publishFrameworkEvent(FrameworkEvent.ERROR, tmpBundle, t); } } finally { if (previousTCCL != Boolean.FALSE) Thread.currentThread().setContextClassLoader((ClassLoader) previousTCCL); } }
public void start() { if (reg == null) { slTracker = new ServiceTracker(Activator.bc, StartLevel.class.getName(), null); slTracker.open(); pkgTracker = new ServiceTracker(Activator.bc, PackageAdmin.class.getName(), null); pkgTracker.open(); Hashtable props = new Hashtable(); props.put("SOAP.service.name", "OSGiFramework"); reg = Activator.bc.registerService(RemoteFW.class.getName(), this, props); Activator.bc.addBundleListener( new BundleListener() { public void bundleChanged(BundleEvent event) { synchronized (bundleEvents) { bundleEvents.add(event); } } }); Activator.bc.addServiceListener( new ServiceListener() { public void serviceChanged(ServiceEvent event) { synchronized (serviceEvents) { serviceEvents.add(event); } } }); Activator.bc.addFrameworkListener( new FrameworkListener() { public void frameworkEvent(FrameworkEvent ev) { synchronized (frameworkEvents) { int type = ev.getType(); Bundle b = ev.getBundle(); if (b == null) { Object obj = ev.getSource(); if (bDebug) { System.out.println("obj=" + obj); if (obj != null) { System.out.println("source class=" + obj.getClass().getName()); } } if (obj != null && (obj instanceof Bundle)) { b = (Bundle) obj; } } if (bDebug) { System.out.println( "server: add fw event: " + ev + ", type=" + type + ", bundle=" + b); } if (b != null) { frameworkEvents.add(new FrameworkEvent(type, b, null)); } } } }); reaper = new Thread() { public void run() { while (bReap) { try { reapEvents(); Thread.sleep(reapDelay); } catch (Exception e) { } } } }; bReap = true; reaper.start(); } }