KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > iiop > rmi > ir > ContainerImplDelegate


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.iiop.rmi.ir;
23
24 import org.omg.CORBA.ContainerOperations JavaDoc;
25 import org.omg.CORBA.Contained JavaDoc;
26 import org.omg.CORBA.ContainedHelper;
27 import org.omg.CORBA.Container JavaDoc;
28 import org.omg.CORBA.ContainerPackage.Description;
29 import org.omg.CORBA.IDLType JavaDoc;
30 import org.omg.CORBA.DefinitionKind JavaDoc;
31 import org.omg.CORBA.StructMember JavaDoc;
32 import org.omg.CORBA.UnionMember JavaDoc;
33 import org.omg.CORBA.InterfaceDef JavaDoc;
34 import org.omg.CORBA.EnumDef JavaDoc;
35 import org.omg.CORBA.ValueDef JavaDoc;
36 import org.omg.CORBA.StructDef JavaDoc;
37 import org.omg.CORBA.UnionDef JavaDoc;
38 import org.omg.CORBA.ConstantDef JavaDoc;
39 import org.omg.CORBA.ModuleDef JavaDoc;
40 import org.omg.CORBA.ValueBoxDef JavaDoc;
41 import org.omg.CORBA.Initializer JavaDoc;
42 import org.omg.CORBA.AliasDef JavaDoc;
43 import org.omg.CORBA.NativeDef JavaDoc;
44 import org.omg.CORBA.ExceptionDef JavaDoc;
45 import org.omg.CORBA.BAD_INV_ORDER JavaDoc;
46
47 import java.util.Map JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Collection JavaDoc;
50 import java.util.ArrayList JavaDoc;
51
52 /**
53  * Delegate for Container functionality.
54  *
55  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
56  * @version $Revision: 37459 $
57  */

58 class ContainerImplDelegate
59    implements ContainerOperations JavaDoc
60 {
61    // Constants -----------------------------------------------------
62

63    // Attributes ----------------------------------------------------
64

65    // Static --------------------------------------------------------
66

67    private static final org.jboss.logging.Logger logger =
68                org.jboss.logging.Logger.getLogger(ContainerImplDelegate.class);
69
70    // Constructors --------------------------------------------------
71

72    /**
73     * Create a new delegate.
74     */

75    ContainerImplDelegate(LocalContainer delegateFor)
76    {
77       this.delegateFor = delegateFor;
78    }
79
80    // Public --------------------------------------------------------
81

82    // LocalContainer delegation implementation ----------------------
83

84    public LocalContained _lookup(String JavaDoc search_name)
85    {
86       logger.debug("ContainerImplDelegate._lookup(\"" + search_name +
87                    "\") entered.");
88       if (search_name.startsWith("::"))
89          return delegateFor.getRepository()._lookup(search_name.substring(2));
90
91       int idx = search_name.indexOf("::");
92       if (idx > 0) {
93          String JavaDoc first = search_name.substring(0, idx);
94          logger.debug("ContainerImplDelegate._lookup(\"" + search_name +
95                       "\") looking for \"" + first + "\".");
96          Object JavaDoc o = contMap.get(first);
97
98          if (o == null || !(o instanceof LocalContainer))
99             return null;
100          else {
101             LocalContainer next = (LocalContainer)o;
102             String JavaDoc rest = search_name.substring(idx + 2);
103
104             return next._lookup(rest);
105          }
106       } else
107          return (LocalContained)contMap.get(search_name);
108    }
109
110    public LocalContained[] _contents(DefinitionKind JavaDoc limit_type,
111                                      boolean exclude_inherited)
112    {
113       int target = limit_type.value();
114       Collection JavaDoc found;
115
116       if (target == DefinitionKind._dk_all)
117          found = cont;
118       else {
119          found = new ArrayList JavaDoc();
120          for (int i = 0; i < cont.size(); ++i) {
121             LocalContained val = (LocalContained)cont.get(i);
122
123             if (target == val.def_kind().value()) {
124                if (!exclude_inherited || val.defined_in() == delegateFor)
125                   found.add(val);
126             }
127          }
128       }
129
130       LocalContained[] res = new LocalContained[found.size()];
131       res = (LocalContained[])found.toArray(res);
132
133       return res;
134    }
135
136    public LocalContained[] _lookup_name(String JavaDoc search_name,
137                                         int levels_to_search,
138                                         DefinitionKind JavaDoc limit_type,
139                                         boolean exclude_inherited)
140    {
141       if (levels_to_search == 0)
142          return null;
143
144       if (levels_to_search == -1)
145          ++levels_to_search; // One more level (recursively) == all levels
146

147       Collection JavaDoc found = new ArrayList JavaDoc();
148       LocalContained[] here = _contents(limit_type, exclude_inherited);
149
150       for (int i = 0; i < here.length; ++i)
151          if (here[i].name().equals(search_name))
152             found.add(here[i]);
153
154       if (levels_to_search >= 0) {
155          // More levels to search, or unlimited depth search
156
for (int i = 0; i < here.length; ++i) {
157             if (here[i] instanceof Container JavaDoc) { // search here
158
LocalContainer container = (LocalContainer)here[i];
159
160                LocalContained[] c;
161                c = container._lookup_name(search_name, levels_to_search - 1,
162                                           limit_type, exclude_inherited);
163                if (c != null)
164                   for (int j = 0; j < c.length; ++j)
165                      found.add(c[j]);
166                 
167             }
168          }
169          
170       }
171
172       LocalContained[] res = new LocalContained[found.size()];
173       res = (LocalContained[])found.toArray(res);
174
175       return res;
176    }
177
178    public void shutdown()
179    {
180       for (int i = 0; i < cont.size(); ++i)
181          ((LocalContained)cont.get(i)).shutdown();
182    }
183
184    // ContainerOperations implementation ----------------------------
185

186    public Contained JavaDoc lookup(String JavaDoc search_name)
187    {
188       LocalContained c = _lookup(search_name);
189
190       if (c == null)
191          return null;
192       else
193          return ContainedHelper.narrow(c.getReference());
194    }
195
196    public Contained JavaDoc[] contents(DefinitionKind JavaDoc limit_type,
197                                boolean exclude_inherited)
198    {
199       LocalContained[] c = _contents(limit_type, exclude_inherited);
200       Contained JavaDoc[] res = new Contained JavaDoc[c.length];
201
202       for (int i = 0; i < c.length; ++i)
203          res[i] = ContainedHelper.narrow(c[i].getReference());
204
205       return res;
206    }
207
208    public Contained JavaDoc[] lookup_name(String JavaDoc search_name, int levels_to_search,
209                                   DefinitionKind JavaDoc limit_type,
210                                   boolean exclude_inherited)
211    {
212       LocalContained[] c = _lookup_name(search_name, levels_to_search,
213                                         limit_type, exclude_inherited);
214       Contained JavaDoc[] res = new Contained JavaDoc[c.length];
215
216       for (int i = 0; i < c.length; ++i)
217          res[i] = ContainedHelper.narrow(c[i].getReference());
218
219       return res;
220    }
221
222    public Description[] describe_contents(DefinitionKind JavaDoc limit_type,
223                                           boolean exclude_inherited,
224                                           int max_returned_objs)
225    {
226       Contained JavaDoc[] c = contents(limit_type, exclude_inherited);
227       int returnSize;
228
229       if (max_returned_objs != -1 && c.length > max_returned_objs)
230          returnSize = max_returned_objs;
231       else
232          returnSize = c.length;
233
234       Description[] ret = new Description[returnSize];
235
236       for (int i = 0; i < returnSize; ++i) {
237          org.omg.CORBA.ContainedPackage.Description d = c[i].describe();
238
239          ret[i] = new Description(c[i], d.kind, d.value);
240       }
241       
242       return ret;
243    }
244
245    public ModuleDef JavaDoc create_module(String JavaDoc id, String JavaDoc name, String JavaDoc version)
246    {
247       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
248    }
249
250    public ConstantDef JavaDoc create_constant(String JavaDoc id, String JavaDoc name, String JavaDoc version,
251                                       IDLType JavaDoc type, org.omg.CORBA.Any JavaDoc value)
252    {
253       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
254    }
255
256    public StructDef JavaDoc create_struct(String JavaDoc id, String JavaDoc name, String JavaDoc version,
257                                   StructMember JavaDoc[] members)
258    {
259       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
260    }
261
262    public UnionDef JavaDoc create_union(String JavaDoc id, String JavaDoc name, String JavaDoc version,
263                                 IDLType JavaDoc discriminator_type,
264                                 UnionMember JavaDoc[] members)
265    {
266       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
267    }
268
269    public EnumDef JavaDoc create_enum(String JavaDoc id, String JavaDoc name, String JavaDoc version,
270                               String JavaDoc[] members)
271    {
272       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
273    }
274
275    public AliasDef JavaDoc create_alias(String JavaDoc id, String JavaDoc name, String JavaDoc version,
276                                 IDLType JavaDoc original_type)
277    {
278       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
279    }
280
281    public InterfaceDef JavaDoc create_interface(String JavaDoc id, String JavaDoc name, String JavaDoc version,
282                                         InterfaceDef JavaDoc[] base_interfaces,
283                                         boolean is_abstract)
284    {
285       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
286    }
287
288    public ValueDef JavaDoc create_value(String JavaDoc id, String JavaDoc name, String JavaDoc version,
289                                 boolean is_custom, boolean is_abstract,
290                                 ValueDef JavaDoc base_value, boolean is_truncatable,
291                                 ValueDef JavaDoc[] abstract_base_values,
292                                 InterfaceDef JavaDoc[] supported_interfaces,
293                                 Initializer JavaDoc[] initializers)
294    {
295       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
296    }
297
298    public ValueBoxDef JavaDoc create_value_box(String JavaDoc id, String JavaDoc name, String JavaDoc version,
299                                        IDLType JavaDoc original_type_def)
300    {
301       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
302    }
303
304    public ExceptionDef JavaDoc create_exception(String JavaDoc id, String JavaDoc name, String JavaDoc version,
305                                         StructMember JavaDoc[] members)
306    {
307       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
308    }
309
310    public NativeDef JavaDoc create_native(String JavaDoc id, String JavaDoc name, String JavaDoc version)
311    {
312       throw new BAD_INV_ORDER JavaDoc("Cannot change RMI/IIOP mapping.");
313    }
314
315
316    // Dummy IRObjectOperations implementation -----------------------
317

318    public DefinitionKind JavaDoc def_kind()
319    {
320       throw new RuntimeException JavaDoc("Should not be called.");
321    }
322  
323    public void destroy()
324    {
325       throw new RuntimeException JavaDoc("Should not be called.");
326    }
327
328
329    // Package protected ---------------------------------------------
330

331    void add(String JavaDoc name, LocalContained contained)
332       throws IRConstructionException
333    {
334       if (contained.getRepository() != delegateFor.getRepository())
335          throw new IRConstructionException("Wrong repository");
336       if (contMap.get(name) != null)
337          throw new IRConstructionException("Duplicate name: " + name);
338       cont.add(contained);
339       contMap.put(name, contained);
340       logger.debug("ContainerDelegateImpl.add() added \"" + name + "\".");
341    }
342
343    /**
344     * Finalize build process, and export.
345     */

346    void allDone()
347       throws IRConstructionException
348    {
349       logger.debug("ContainerDelegateImpl.allDone() entered ");
350       for (int i = 0; i < cont.size(); ++i) {
351          LocalContained item = (LocalContained)cont.get(i);
352
353          logger.debug("Container[" + item.id() + "].allDone() calling [" +
354                       item.id() + "].allDone()");
355          item.allDone();
356       }
357       logger.debug("ContainerDelegateImpl.allDone() done ");
358    }
359
360    // Protected -----------------------------------------------------
361

362    // Private -------------------------------------------------------
363

364    /**
365     * The contents of the Container.
366     */

367    private ArrayList JavaDoc cont = new ArrayList JavaDoc();
368
369    /**
370     * Maps names to the contents of the Container.
371     */

372    private Map JavaDoc contMap = new HashMap JavaDoc();
373
374    /**
375     * The Container I am a delegate for.
376     */

377    private LocalContainer delegateFor;
378
379    
380    // Inner classes -------------------------------------------------
381
}
382
Popular Tags