/** * 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; } } }
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; }
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; }
/** * 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()); } } }
/* * 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(); }
/** * 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; } }
/** * 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); } }