ICE 3.4.2
[php5-ice-freebsdport.git] / java / src / IceInternal / EndpointFactoryManager.java
blob4fc683bebc8842aa3294f6f34c768a7c835aad2e
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 package IceInternal;
12 public final class EndpointFactoryManager
14 EndpointFactoryManager(Instance instance)
16 _instance = instance;
19 public synchronized void
20 add(EndpointFactory factory)
22 for(int i = 0; i < _factories.size(); i++)
24 EndpointFactory f = _factories.get(i);
25 if(f.type() == factory.type())
27 assert(false);
30 _factories.add(factory);
33 public synchronized EndpointFactory
34 get(short type)
36 for(int i = 0; i < _factories.size(); i++)
38 EndpointFactory f = _factories.get(i);
39 if(f.type() == type)
41 return f;
44 return null;
47 public synchronized EndpointI
48 create(String str, boolean oaEndpoint)
50 String s = str.trim();
51 if(s.length() == 0)
53 Ice.EndpointParseException e = new Ice.EndpointParseException();
54 e.str = "value has no non-whitespace characters";
55 throw e;
58 java.util.regex.Pattern p = java.util.regex.Pattern.compile("([ \t\n\r]+)|$");
59 java.util.regex.Matcher m = p.matcher(s);
60 boolean b = m.find();
61 assert(b);
63 String protocol = s.substring(0, m.start());
65 if(protocol.equals("default"))
67 protocol = _instance.defaultsAndOverrides().defaultProtocol;
70 for(int i = 0; i < _factories.size(); i++)
72 EndpointFactory f = _factories.get(i);
73 if(f.protocol().equals(protocol))
75 return f.create(s.substring(m.end()), oaEndpoint);
77 // Code below left in place for debugging.
80 EndpointI e = f.create(s.substring(m.end()), oaEndpoint);
81 BasicStream bs = new BasicStream(_instance, true, false);
82 e.streamWrite(bs);
83 java.nio.ByteBuffer buf = bs.getBuffer();
84 buf.position(0);
85 short type = bs.readShort();
86 EndpointI ue = new IceInternal.OpaqueEndpointI(type, bs);
87 System.err.println("Normal: " + e);
88 System.err.println("Opaque: " + ue);
89 return e;
95 // If the stringified endpoint is opaque, create an unknown endpoint,
96 // then see whether the type matches one of the known endpoints.
98 if(protocol.equals("opaque"))
100 EndpointI ue = new OpaqueEndpointI(s.substring(m.end()));
101 for(int i = 0; i < _factories.size(); i++)
103 EndpointFactory f = _factories.get(i);
104 if(f.type() == ue.type())
107 // Make a temporary stream, write the opaque endpoint data into the stream,
108 // and ask the factory to read the endpoint data from that stream to create
109 // the actual endpoint.
111 BasicStream bs = new BasicStream(_instance, true, false);
112 ue.streamWrite(bs);
113 Buffer buf = bs.getBuffer();
114 buf.b.position(0);
115 bs.readShort(); // type
116 return f.read(bs);
119 return ue; // Endpoint is opaque, but we don't have a factory for its type.
122 return null;
125 public synchronized EndpointI
126 read(BasicStream s)
128 short type = s.readShort();
130 for(int i = 0; i < _factories.size(); i++)
132 EndpointFactory f = _factories.get(i);
133 if(f.type() == type)
135 return f.read(s);
138 return new OpaqueEndpointI(type, s);
141 void
142 destroy()
144 for(int i = 0; i < _factories.size(); i++)
146 EndpointFactory f = _factories.get(i);
147 f.destroy();
149 _factories.clear();
152 private Instance _instance;
153 private java.util.List<EndpointFactory> _factories = new java.util.ArrayList<EndpointFactory>();