public TransportWrapper(TransportProtocol outerProtocol) { if (outerProtocol == null) throw new ArgumentNullException("outerProtocol"); _outerProtocol = outerProtocol; }
public virtual void TestSpi() { IList <TransportProtocol> protocols = NGit.Transport.Transport.GetTransportProtocols (); NUnit.Framework.Assert.IsNotNull(protocols); NUnit.Framework.Assert.IsFalse(protocols.IsEmpty()); TransportProtocol found = null; foreach (TransportProtocol protocol in protocols) { if (protocol.GetSchemes().Contains(SpiTransport.SCHEME)) { found = protocol; break; } } NUnit.Framework.Assert.AreEqual(SpiTransport.PROTO, found); }
/// <summary>Unregister a TransportProtocol instance.</summary> /// <remarks> /// Unregister a TransportProtocol instance. /// <p> /// Unregistering a protocol usually isn't necessary, as protocols are held /// by weak references and will automatically clear when they are garbage /// collected by the JVM. Matching is handled by reference equality, so the /// exact reference given to /// <see cref="Register(TransportProtocol)">Register(TransportProtocol)</see> /// must be /// used. /// </remarks> /// <param name="proto">the exact object previously given to register.</param> public static void Unregister(TransportProtocol proto) { foreach (WeakReference<TransportProtocol> @ref in protocols) { TransportProtocol refProto = @ref.Get(); if (refProto == null || refProto == proto) { protocols.Remove(@ref); } } }
/* private static void RegisterByService() { ClassLoader ldr = Sharpen.Thread.CurrentThread().GetContextClassLoader(); if (ldr == null) { ldr = typeof(NGit.Transport.Transport).GetClassLoader(); } Enumeration<Uri> catalogs = Catalogs(ldr); while (catalogs.MoveNext()) { Scan(ldr, catalogs.Current); } } private static Enumeration<Uri> Catalogs(ClassLoader ldr) { try { string prefix = "META-INF/services/"; string name = prefix + typeof(NGit.Transport.Transport).FullName; return ldr.GetResources(name); } catch (IOException) { return new Vector<Uri>().GetEnumerator(); } } private static void Scan(ClassLoader ldr, Uri url) { BufferedReader br; try { InputStream urlIn = url.OpenStream(); br = new BufferedReader(new InputStreamReader(urlIn, "UTF-8")); } catch (IOException) { // If we cannot read from the service list, go to the next. // return; } try { string line; while ((line = br.ReadLine()) != null) { line = line.Trim(); if (line.Length == 0) { continue; } int comment = line.IndexOf('#'); if (comment == 0) { continue; } if (comment != -1) { line = Sharpen.Runtime.Substring(line, 0, comment).Trim(); } Load(ldr, line); } } catch (IOException) { } finally { // If we failed during a read, ignore the error. // try { br.Close(); } catch (IOException) { } } } // Ignore the close error; we are only reading. private static void Load(ClassLoader ldr, string cn) { Type clazz; try { clazz = Sharpen.Runtime.GetType(cn, false, ldr); } catch (TypeLoadException) { // Doesn't exist, even though the service entry is present. // return; } foreach (FieldInfo f in Sharpen.Runtime.GetDeclaredFields(clazz)) { if ((f.GetModifiers() & Modifier.STATIC) == Modifier.STATIC && typeof(TransportProtocol ).IsAssignableFrom(f.FieldType)) { TransportProtocol proto; try { proto = (TransportProtocol)f.GetValue(null); } catch (ArgumentException) { // If we cannot access the field, don't. continue; } catch (MemberAccessException) { // If we cannot access the field, don't. continue; } if (proto != null) { Register(proto); } } } } */ /// <summary>Register a TransportProtocol instance for use during open.</summary> /// <remarks> /// Register a TransportProtocol instance for use during open. /// <p> /// Protocol definitions are held by WeakReference, allowing them to be /// garbage collected when the calling application drops all strongly held /// references to the TransportProtocol. Therefore applications should use a /// singleton pattern as described in /// <see cref="TransportProtocol">TransportProtocol</see> /// 's class /// documentation to ensure their protocol does not get disabled by garbage /// collection earlier than expected. /// <p> /// The new protocol is registered in front of all earlier protocols, giving /// it higher priority than the built-in protocol definitions. /// </remarks> /// <param name="proto">the protocol definition. Must not be null.</param> public static void Register(TransportProtocol proto) { protocols.Add(0, new WeakReference<TransportProtocol>(proto)); }