KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > transport > CorbaTransportManagerImpl


1 /*
2  * @(#)CorbaTransportManagerImpl.java 1.14 06/09/15
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.transport;
9
10 import java.net.ServerSocket JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.omg.CORBA.INITIALIZE JavaDoc;
19 import org.omg.CORBA.INTERNAL JavaDoc;
20 import org.omg.CORBA.CompletionStatus JavaDoc;
21
22 import com.sun.corba.se.pept.transport.Acceptor;
23 import com.sun.corba.se.pept.transport.ByteBufferPool;
24 import com.sun.corba.se.pept.transport.ContactInfo;
25 import com.sun.corba.se.pept.transport.InboundConnectionCache;
26 import com.sun.corba.se.pept.transport.OutboundConnectionCache;
27 import com.sun.corba.se.pept.transport.Selector;
28
29 import com.sun.corba.se.spi.ior.IORTemplate;
30 import com.sun.corba.se.spi.ior.ObjectAdapterId;
31 import com.sun.corba.se.spi.orb.ORB;
32 import com.sun.corba.se.spi.transport.CorbaAcceptor;
33 import com.sun.corba.se.spi.transport.CorbaTransportManager;
34 import com.sun.corba.se.pept.transport.Connection;
35 import com.sun.corba.se.pept.transport.ConnectionCache;
36
37 // REVISIT - impl/poa specific:
38
import com.sun.corba.se.impl.oa.poa.Policies;
39 import com.sun.corba.se.impl.orbutil.ORBUtility;
40
41 /**
42  * @author Harold Carr
43  */

44 public class CorbaTransportManagerImpl
45     implements
46     CorbaTransportManager
47 {
48     protected ORB orb;
49     protected List JavaDoc acceptors;
50     protected Map JavaDoc outboundConnectionCaches;
51     protected Map JavaDoc inboundConnectionCaches;
52     protected Selector selector;
53     
54     public CorbaTransportManagerImpl(ORB orb)
55     {
56     this.orb = orb;
57     acceptors = new ArrayList JavaDoc();
58     outboundConnectionCaches = new HashMap JavaDoc();
59     inboundConnectionCaches = new HashMap JavaDoc();
60     selector = new SelectorImpl(orb);
61     }
62
63     ////////////////////////////////////////////////////
64
//
65
// pept TransportManager
66
//
67

68     public ByteBufferPool getByteBufferPool(int id)
69     {
70     throw new RuntimeException JavaDoc();
71     }
72
73     public OutboundConnectionCache getOutboundConnectionCache(
74         ContactInfo contactInfo)
75     {
76     synchronized (contactInfo) {
77         if (contactInfo.getConnectionCache() == null) {
78         OutboundConnectionCache connectionCache = null;
79         synchronized (outboundConnectionCaches) {
80             connectionCache = (OutboundConnectionCache)
81             outboundConnectionCaches.get(
82                             contactInfo.getConnectionCacheType());
83             if (connectionCache == null) {
84             // REVISIT: Would like to be able to configure
85
// the connection cache type used.
86
connectionCache =
87                 new CorbaOutboundConnectionCacheImpl(orb,
88                                  contactInfo);
89             outboundConnectionCaches.put(
90                             contactInfo.getConnectionCacheType(),
91                 connectionCache);
92             }
93         }
94         contactInfo.setConnectionCache(connectionCache);
95         }
96         return contactInfo.getConnectionCache();
97     }
98     }
99
100     public Collection JavaDoc getOutboundConnectionCaches()
101     {
102     return outboundConnectionCaches.values();
103     }
104
105     public InboundConnectionCache getInboundConnectionCache(
106         Acceptor acceptor)
107     {
108     synchronized (acceptor) {
109         if (acceptor.getConnectionCache() == null) {
110         InboundConnectionCache connectionCache = null;
111         synchronized (inboundConnectionCaches) {
112             connectionCache = (InboundConnectionCache)
113             inboundConnectionCaches.get(
114                             acceptor.getConnectionCacheType());
115             if (connectionCache == null) {
116             // REVISIT: Would like to be able to configure
117
// the connection cache type used.
118
connectionCache =
119                 new CorbaInboundConnectionCacheImpl(orb,
120                                 acceptor);
121             inboundConnectionCaches.put(
122                             acceptor.getConnectionCacheType(),
123                 connectionCache);
124             }
125         }
126         acceptor.setConnectionCache(connectionCache);
127         }
128         return acceptor.getConnectionCache();
129     }
130     }
131
132     public Collection JavaDoc getInboundConnectionCaches()
133     {
134     return inboundConnectionCaches.values();
135     }
136
137     public Selector getSelector(int id)
138     {
139     return selector;
140     }
141
142     public synchronized void registerAcceptor(Acceptor acceptor)
143     {
144     if (orb.transportDebugFlag) {
145         dprint(".registerAcceptor->: " + acceptor);
146     }
147     acceptors.add(acceptor);
148     if (orb.transportDebugFlag) {
149         dprint(".registerAcceptor<-: " + acceptor);
150     }
151     }
152
153     public Collection JavaDoc getAcceptors()
154     {
155     return getAcceptors(null, null);
156     }
157
158     public synchronized void unregisterAcceptor(Acceptor acceptor)
159     {
160     acceptors.remove(acceptor);
161     }
162
163     public void close()
164     {
165     try {
166         if (orb.transportDebugFlag) {
167         dprint(".close->");
168         }
169         getSelector(0).close();
170         Collection JavaDoc c = getOutboundConnectionCaches();
171         Iterator JavaDoc iter=c.iterator();
172         
173         for (;iter.hasNext();) {
174         CorbaConnectionCacheBase cc = (CorbaConnectionCacheBase)iter.next();
175         Iterator JavaDoc iterator = cc.values().iterator();
176         
177         // Find any used and not busy connection in cache and close it.
178
while (iterator.hasNext()) {
179             Connection conn = (Connection) iterator.next();
180             if (!conn.isBusy()) {
181             try {
182                 conn.close();
183                 iterator = cc.values().iterator();
184             } catch (Exception JavaDoc ex) { // REVISIT - log
185
if (orb.transportDebugFlag) {
186                 dprint("Exception while closing connection" + conn);
187                 //the idea is to ease troubleshooting in case of probelm while closing
188
// connection
189
}
190             }
191             }
192         }
193         }
194             
195     } finally {
196         if (orb.transportDebugFlag) {
197         dprint(".close<-");
198         }
199     }
200     }
201
202     ////////////////////////////////////////////////////
203
//
204
// CorbaTransportManager
205
//
206

207     public Collection JavaDoc getAcceptors(String JavaDoc objectAdapterManagerId,
208                    ObjectAdapterId objectAdapterId)
209     {
210     // REVISIT - need to filter based on arguments.
211

212     // REVISIT - initialization will be moved to OA.
213
// Lazy initialization of acceptors.
214
Iterator JavaDoc iterator = acceptors.iterator();
215     while (iterator.hasNext()) {
216         Acceptor acceptor = (Acceptor) iterator.next();
217         if (acceptor.initialize()) {
218         if (acceptor.shouldRegisterAcceptEvent()) {
219             orb.getTransportManager().getSelector(0)
220             .registerForEvent(acceptor.getEventHandler());
221         }
222         }
223     }
224     return acceptors;
225     }
226
227     // REVISIT - POA specific policies
228
public void addToIORTemplate(IORTemplate iorTemplate,
229                  Policies policies,
230                  String JavaDoc codebase,
231                  String JavaDoc objectAdapterManagerId,
232                  ObjectAdapterId objectAdapterId)
233     {
234     Iterator JavaDoc iterator =
235         getAcceptors(objectAdapterManagerId, objectAdapterId).iterator();
236     while (iterator.hasNext()) {
237         CorbaAcceptor acceptor = (CorbaAcceptor) iterator.next();
238         acceptor.addToIORTemplate(iorTemplate, policies, codebase);
239     }
240     }
241
242
243     ////////////////////////////////////////////////////
244
//
245
// implemenation
246
//
247

248     protected void dprint(String JavaDoc msg)
249     {
250     ORBUtility.dprint("CorbaTransportManagerImpl", msg);
251     }
252 }
253
254 // End of file.
255
Popular Tags