ICE 3.4.2
[php5-ice-freebsdport.git] / cs / src / Ice / EndpointFactoryManager.cs
blobf305416121606a59a0ace7909aa67665d9004089
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 namespace IceInternal
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Text.RegularExpressions;
17 public sealed class EndpointFactoryManager
19 internal EndpointFactoryManager(Instance instance)
21 instance_ = instance;
22 _factories = new ArrayList();
25 public void add(EndpointFactory factory)
27 lock(this)
29 for(int i = 0; i < _factories.Count; i++)
31 EndpointFactory f = (EndpointFactory)_factories[i];
32 if(f.type() == factory.type())
34 Debug.Assert(false);
37 _factories.Add(factory);
41 public EndpointFactory get(short type)
43 lock(this)
45 for(int i = 0; i < _factories.Count; i++)
47 EndpointFactory f = (EndpointFactory)_factories[i];
48 if(f.type() == type)
50 return f;
53 return null;
57 public EndpointI create(string str, bool oaEndpoint)
59 lock(this)
61 string s = str.Trim();
62 if(s.Length == 0)
64 Ice.EndpointParseException e = new Ice.EndpointParseException();
65 e.str = "value has no non-whitespace characters";
66 throw e;
69 Regex p = new Regex("([ \t\n\r]+)|$");
70 Match m = p.Match(s);
71 Debug.Assert(m.Success);
73 string protocol = s.Substring(0, m.Index);
75 if(protocol.Equals("default"))
77 protocol = instance_.defaultsAndOverrides().defaultProtocol;
80 for(int i = 0; i < _factories.Count; i++)
82 EndpointFactory f = (EndpointFactory)_factories[i];
83 if(f.protocol().Equals(protocol))
85 return f.create(s.Substring(m.Index + m.Length), oaEndpoint);
87 // Code below left in place for debugging.
90 EndpointI e = f.create(s.Substring(m.Index + m.Length), oaEndpoint);
91 BasicStream bs = new BasicStream(instance_, true);
92 e.streamWrite(bs);
93 Buffer buf = bs.getBuffer();
94 buf.b.position(0);
95 short type = bs.readShort();
96 EndpointI ue = new IceInternal.OpaqueEndpointI(type, bs);
97 System.Console.Error.WriteLine("Normal: " + e);
98 System.Console.Error.WriteLine("Opaque: " + ue);
99 return e;
105 // If the stringified endpoint is opaque, create an unknown endpoint,
106 // then see whether the type matches one of the known endpoints.
108 if(protocol.Equals("opaque"))
110 EndpointI ue = new OpaqueEndpointI(s.Substring(m.Index + m.Length));
111 for(int i = 0; i < _factories.Count; i++)
113 EndpointFactory f = (EndpointFactory)_factories[i];
114 if(f.type() == ue.type())
117 // Make a temporary stream, write the opaque endpoint data into the stream,
118 // and ask the factory to read the endpoint data from that stream to create
119 // the actual endpoint.
121 BasicStream bs = new BasicStream(instance_, true);
122 ue.streamWrite(bs);
123 Buffer buf = bs.getBuffer();
124 buf.b.position(0);
125 bs.readShort(); // type
126 return f.read(bs);
129 return ue; // Endpoint is opaque, but we don't have a factory for its type.
131 return null;
135 public EndpointI read(BasicStream s)
137 lock(this)
139 short type = s.readShort();
141 for(int i = 0; i < _factories.Count; i++)
143 EndpointFactory f = (EndpointFactory)_factories[i];
144 if(f.type() == type)
146 return f.read(s);
150 return new OpaqueEndpointI(type, s);
154 internal void destroy()
156 for(int i = 0; i < _factories.Count; i++)
158 EndpointFactory f = (EndpointFactory)_factories[i];
159 f.destroy();
161 _factories.Clear();
164 private readonly Instance instance_;
165 private readonly ArrayList _factories;