KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > server > ServerNamingContext


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop.server;
20
21 import gcc.rmi.iiop.*;
22 import gcc.org.omg.CosNaming.*;
23 import gcc.org.omg.CosNaming.NamingContextPackage.*;
24 import gcc.org.omg.CosNaming.NamingContextExtPackage.*;
25 import gcc.naming.NameService;
26 import gcc.naming.NameServiceLog;
27 import gcc.adapter.Adapter;
28
29 import javax.naming.*;
30
31 public class ServerNamingContext
32     implements gcc.rmi.iiop.NameServiceOperations
33 {
34     protected static ServerNamingContext _snc = null;
35
36     public static ServerNamingContext getInstance()
37     {
38         if (_snc == null)
39         {
40             synchronized( ServerNamingContext.class )
41             {
42                 _snc = new ServerNamingContext();
43                 _snc.init();
44             }
45         }
46
47         return _snc;
48     }
49
50     // -----------------------------------------------------------------------
51
// private data
52
// -----------------------------------------------------------------------
53

54     private NameService _nameService;
55
56     private volatile int _cycle;
57
58     // -----------------------------------------------------------------------
59
// public methods from interface NamingContextExtOperations
60
// -----------------------------------------------------------------------
61

62     public org.omg.CORBA.Object resolve_str(String name) throws NotFound, CannotProceed, InvalidName
63     {
64         return lookup(name, null);
65     }
66
67
68     public String to_string (NameComponent[] n) throws InvalidName
69     {
70         throw NoImplement();
71     }
72
73     public NameComponent[] to_name (String sn) throws InvalidName
74     {
75         throw NoImplement();
76     }
77
78     public String to_url (String addr, String sn) throws InvalidAddress, InvalidName
79     {
80         throw NoImplement();
81     }
82
83     // -----------------------------------------------------------------------
84
// public methods from interface NamingContextOperations
85
// -----------------------------------------------------------------------
86

87     public org.omg.CORBA.Object resolve(NameComponent[] name) throws NotFound, CannotProceed, InvalidName
88     {
89         return lookup(toString(name), name);
90     }
91
92     public void bind (NameComponent[] n, org.omg.CORBA.Object obj) throws NotFound, CannotProceed, InvalidName, AlreadyBound
93     {
94         throw NoImplement();
95     }
96
97     public void bind_context (gcc.org.omg.CosNaming.NameComponent[] n, gcc.org.omg.CosNaming.NamingContext nc) throws NotFound, CannotProceed, InvalidName, AlreadyBound
98     {
99         throw NoImplement();
100     }
101
102     public void rebind (gcc.org.omg.CosNaming.NameComponent[] n, org.omg.CORBA.Object obj) throws NotFound, CannotProceed, InvalidName
103     {
104         throw NoImplement();
105     }
106
107     public void rebind_context (gcc.org.omg.CosNaming.NameComponent[] n, gcc.org.omg.CosNaming.NamingContext nc) throws NotFound, CannotProceed, InvalidName
108     {
109         throw NoImplement();
110     }
111
112     public void unbind (NameComponent[] n) throws NotFound, CannotProceed, InvalidName
113     {
114         throw NoImplement();
115     }
116
117     public void list (int how_many, BindingListHolder bl, BindingIteratorHolder bi)
118     {
119         throw NoImplement();
120     }
121
122     public NamingContext new_context ()
123     {
124         throw NoImplement();
125     }
126
127     public NamingContext bind_new_context (NameComponent[] n) throws NotFound, AlreadyBound, CannotProceed, InvalidName
128     {
129         throw NoImplement();
130     }
131
132     protected org.omg.CORBA.NO_IMPLEMENT NoImplement()
133     {
134         return new org.omg.CORBA.NO_IMPLEMENT();
135     }
136
137     // -----------------------------------------------------------------------
138
// public methods from interface NameServiceOperations (Sybase proprietary)
139
// -----------------------------------------------------------------------
140

141     public String resolve_host(String host)
142     {
143         System.out.println( "ServerNamingContext.resolve_host(): TODO host = " + host );
144
145         //String resolvedHost = ClusterPartition.getInstance(host).resolveHost();
146
//return "cycle=" + Math.max(1, ++_cycle) + ";" + resolvedHost;
147

148         // Cycle prefix for round-robin load balancing.
149
// Any weighted balancing is applied by client.
150

151         return host;
152     }
153
154     // -----------------------------------------------------------------------
155
// protected methods
156
// -----------------------------------------------------------------------
157

158     protected void init()
159     {
160         _nameService = NameService.getInstance();
161     }
162
163     protected org.omg.CORBA.Object lookup(String nameString, NameComponent[] name) throws NotFound
164     {
165         try
166         {
167             Object object = _nameService.lookup(nameString);
168
169             /*
170             if (object instanceof RemoteInterface)
171             {
172                 RemoteInterface remote = (RemoteInterface)object;
173                 return remote.$getObjectRef();
174             }
175             else
176             {
177                 NameServiceLog.getInstance().warnObjectHasNoRemoteInterface(nameString, object.getClass().getName());
178                 throw new NotFound(NotFoundReason.not_object, name);
179             }
180             */

181
182             if (object instanceof Adapter)
183             {
184                 Adapter a = (Adapter)object;
185                 RemoteInterface remote = a.getRemoteInterface();
186                 return remote.$getObjectRef();
187             }
188             else
189             {
190                 NameServiceLog.getInstance().warnObjectHasNoRemoteInterface(nameString, object.getClass().getName());
191                 throw new NotFound(NotFoundReason.not_object, name);
192             }
193         }
194         catch (NameNotFoundException notFound)
195         {
196             // Assume warning message has already been logged.
197
throw new NotFound(NotFoundReason.missing_node, name);
198         }
199         catch (NamingException ex)
200         {
201             NameServiceLog.getInstance().warnNameNotFound(nameString, ex);
202             throw new NotFound(NotFoundReason.missing_node, name);
203         }
204     }
205
206     protected String toString(NameComponent[] name)
207     {
208         int n = name.length;
209         if (n == 1)
210         {
211             return name[0].id;
212         }
213         else
214         {
215             StringBuffer nameBuffer = new StringBuffer();
216             for (int i = 0; i < n; i++)
217             {
218                 if (i > 0)
219                 {
220                     nameBuffer.append('/');
221                 }
222                 nameBuffer.append(name[i].id);
223                 if (name[i].kind.length() > 0)
224                 {
225                     nameBuffer.append(",kind=");
226                     nameBuffer.append(name[i].kind);
227                 }
228             }
229             return nameBuffer.toString();
230         }
231     }
232 }
233
Popular Tags