001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.iiop.rmi.ir;
023:
024: import org.omg.CORBA.ContainerOperations;
025: import org.omg.CORBA.Contained;
026: import org.omg.CORBA.ContainedHelper;
027: import org.omg.CORBA.Container;
028: import org.omg.CORBA.ContainerPackage.Description;
029: import org.omg.CORBA.ContainerHelper;
030: import org.omg.CORBA.IDLType;
031: import org.omg.CORBA.DefinitionKind;
032: import org.omg.CORBA.StructMember;
033: import org.omg.CORBA.UnionMember;
034: import org.omg.CORBA.InterfaceDef;
035: import org.omg.CORBA.EnumDef;
036: import org.omg.CORBA.ValueDef;
037: import org.omg.CORBA.StructDef;
038: import org.omg.CORBA.UnionDef;
039: import org.omg.CORBA.ConstantDef;
040: import org.omg.CORBA.ModuleDef;
041: import org.omg.CORBA.ValueBoxDef;
042: import org.omg.CORBA.Initializer;
043: import org.omg.CORBA.AliasDef;
044: import org.omg.CORBA.NativeDef;
045: import org.omg.CORBA.ExceptionDef;
046: import org.omg.CORBA.BAD_INV_ORDER;
047:
048: import java.util.Map;
049: import java.util.HashMap;
050: import java.util.Collection;
051: import java.util.ArrayList;
052: import java.util.Iterator;
053:
054: /**
055: * Delegate for Container functionality.
056: *
057: * @author <a href="mailto:osh@sparre.dk">Ole Husgaard</a>
058: * @version $Revision: 57194 $
059: */
060: class ContainerImplDelegate implements ContainerOperations {
061: // Constants -----------------------------------------------------
062:
063: // Attributes ----------------------------------------------------
064:
065: // Static --------------------------------------------------------
066:
067: private static final org.jboss.logging.Logger logger = org.jboss.logging.Logger
068: .getLogger(ContainerImplDelegate.class);
069:
070: // Constructors --------------------------------------------------
071:
072: /**
073: * Create a new delegate.
074: */
075: ContainerImplDelegate(LocalContainer delegateFor) {
076: this .delegateFor = delegateFor;
077: }
078:
079: // Public --------------------------------------------------------
080:
081: // LocalContainer delegation implementation ----------------------
082:
083: public LocalContained _lookup(String search_name) {
084: logger.debug("ContainerImplDelegate._lookup(\"" + search_name
085: + "\") entered.");
086: if (search_name.startsWith("::"))
087: return delegateFor.getRepository()._lookup(
088: search_name.substring(2));
089:
090: int idx = search_name.indexOf("::");
091: if (idx > 0) {
092: String first = search_name.substring(0, idx);
093: logger.debug("ContainerImplDelegate._lookup(\""
094: + search_name + "\") looking for \"" + first
095: + "\".");
096: Object o = contMap.get(first);
097:
098: if (o == null || !(o instanceof LocalContainer))
099: return null;
100: else {
101: LocalContainer next = (LocalContainer) o;
102: String 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 limit_type,
111: boolean exclude_inherited) {
112: int target = limit_type.value();
113: Collection found;
114:
115: if (target == DefinitionKind._dk_all)
116: found = cont;
117: else {
118: found = new ArrayList();
119: for (int i = 0; i < cont.size(); ++i) {
120: LocalContained val = (LocalContained) cont.get(i);
121:
122: if (target == val.def_kind().value()) {
123: if (!exclude_inherited
124: || 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 search_name,
137: int levels_to_search, DefinitionKind limit_type,
138: boolean exclude_inherited) {
139: if (levels_to_search == 0)
140: return null;
141:
142: if (levels_to_search == -1)
143: ++levels_to_search; // One more level (recursively) == all levels
144:
145: Collection found = new ArrayList();
146: LocalContained[] here = _contents(limit_type, exclude_inherited);
147:
148: for (int i = 0; i < here.length; ++i)
149: if (here[i].name().equals(search_name))
150: found.add(here[i]);
151:
152: if (levels_to_search >= 0) {
153: // More levels to search, or unlimited depth search
154: for (int i = 0; i < here.length; ++i) {
155: if (here[i] instanceof Container) { // search here
156: LocalContainer container = (LocalContainer) here[i];
157:
158: LocalContained[] c;
159: c = container._lookup_name(search_name,
160: levels_to_search - 1, limit_type,
161: exclude_inherited);
162: if (c != null)
163: for (int j = 0; j < c.length; ++j)
164: found.add(c[j]);
165:
166: }
167: }
168:
169: }
170:
171: LocalContained[] res = new LocalContained[found.size()];
172: res = (LocalContained[]) found.toArray(res);
173:
174: return res;
175: }
176:
177: public void shutdown() {
178: for (int i = 0; i < cont.size(); ++i)
179: ((LocalContained) cont.get(i)).shutdown();
180: }
181:
182: // ContainerOperations implementation ----------------------------
183:
184: public Contained lookup(String search_name) {
185: LocalContained c = _lookup(search_name);
186:
187: if (c == null)
188: return null;
189: else
190: return ContainedHelper.narrow(c.getReference());
191: }
192:
193: public Contained[] contents(DefinitionKind limit_type,
194: boolean exclude_inherited) {
195: LocalContained[] c = _contents(limit_type, exclude_inherited);
196: Contained[] res = new Contained[c.length];
197:
198: for (int i = 0; i < c.length; ++i)
199: res[i] = ContainedHelper.narrow(c[i].getReference());
200:
201: return res;
202: }
203:
204: public Contained[] lookup_name(String search_name,
205: int levels_to_search, DefinitionKind limit_type,
206: boolean exclude_inherited) {
207: LocalContained[] c = _lookup_name(search_name,
208: levels_to_search, limit_type, exclude_inherited);
209: Contained[] res = new Contained[c.length];
210:
211: for (int i = 0; i < c.length; ++i)
212: res[i] = ContainedHelper.narrow(c[i].getReference());
213:
214: return res;
215: }
216:
217: public Description[] describe_contents(DefinitionKind limit_type,
218: boolean exclude_inherited, int max_returned_objs) {
219: Contained[] c = contents(limit_type, exclude_inherited);
220: int returnSize;
221:
222: if (max_returned_objs != -1 && c.length > max_returned_objs)
223: returnSize = max_returned_objs;
224: else
225: returnSize = c.length;
226:
227: Description[] ret = new Description[returnSize];
228:
229: for (int i = 0; i < returnSize; ++i) {
230: org.omg.CORBA.ContainedPackage.Description d = c[i]
231: .describe();
232:
233: ret[i] = new Description(c[i], d.kind, d.value);
234: }
235:
236: return ret;
237: }
238:
239: public ModuleDef create_module(String id, String name,
240: String version) {
241: throw new BAD_INV_ORDER("Cannot change RMI/IIOP mapping.");
242: }
243:
244: public ConstantDef create_constant(String id, String name,
245: String version, IDLType type, org.omg.CORBA.Any value) {
246: throw new BAD_INV_ORDER("Cannot change RMI/IIOP mapping.");
247: }
248:
249: public StructDef create_struct(String id, String name,
250: String version, StructMember[] members) {
251: throw new BAD_INV_ORDER("Cannot change RMI/IIOP mapping.");
252: }
253:
254: public UnionDef create_union(String id, String name,
255: String version, IDLType discriminator_type,
256: UnionMember[] members) {
257: throw new BAD_INV_ORDER("Cannot change RMI/IIOP mapping.");
258: }
259:
260: public EnumDef create_enum(String id, String name, String version,
261: String[] members) {
262: throw new BAD_INV_ORDER("Cannot change RMI/IIOP mapping.");
263: }
264:
265: public AliasDef create_alias(String id, String name,
266: String version, IDLType original_type) {
267: throw new BAD_INV_ORDER("Cannot change RMI/IIOP mapping.");
268: }
269:
270: public InterfaceDef create_interface(String id, String name,
271: String version, InterfaceDef[] base_interfaces,
272: boolean is_abstract) {
273: throw new BAD_INV_ORDER("Cannot change RMI/IIOP mapping.");
274: }
275:
276: public ValueDef create_value(String id, String name,
277: String version, boolean is_custom, boolean is_abstract,
278: ValueDef base_value, boolean is_truncatable,
279: ValueDef[] abstract_base_values,
280: InterfaceDef[] supported_interfaces,
281: Initializer[] initializers) {
282: throw new BAD_INV_ORDER("Cannot change RMI/IIOP mapping.");
283: }
284:
285: public ValueBoxDef create_value_box(String id, String name,
286: String version, IDLType original_type_def) {
287: throw new BAD_INV_ORDER("Cannot change RMI/IIOP mapping.");
288: }
289:
290: public ExceptionDef create_exception(String id, String name,
291: String version, StructMember[] members) {
292: throw new BAD_INV_ORDER("Cannot change RMI/IIOP mapping.");
293: }
294:
295: public NativeDef create_native(String id, String name,
296: String version) {
297: throw new BAD_INV_ORDER("Cannot change RMI/IIOP mapping.");
298: }
299:
300: // Dummy IRObjectOperations implementation -----------------------
301:
302: public DefinitionKind def_kind() {
303: throw new RuntimeException("Should not be called.");
304: }
305:
306: public void destroy() {
307: throw new RuntimeException("Should not be called.");
308: }
309:
310: // Package protected ---------------------------------------------
311:
312: void add(String name, LocalContained contained)
313: throws IRConstructionException {
314: if (contained.getRepository() != delegateFor.getRepository())
315: throw new IRConstructionException("Wrong repository");
316: if (contMap.get(name) != null)
317: throw new IRConstructionException("Duplicate name: " + name);
318: cont.add(contained);
319: contMap.put(name, contained);
320: logger.debug("ContainerDelegateImpl.add() added \"" + name
321: + "\".");
322: }
323:
324: /**
325: * Finalize build process, and export.
326: */
327: void allDone() throws IRConstructionException {
328: logger.debug("ContainerDelegateImpl.allDone() entered ");
329: for (int i = 0; i < cont.size(); ++i) {
330: LocalContained item = (LocalContained) cont.get(i);
331:
332: logger.debug("Container[" + item.id()
333: + "].allDone() calling [" + item.id()
334: + "].allDone()");
335: item.allDone();
336: }
337: logger.debug("ContainerDelegateImpl.allDone() done ");
338: }
339:
340: // Protected -----------------------------------------------------
341:
342: // Private -------------------------------------------------------
343:
344: /**
345: * The contents of the Container.
346: */
347: private ArrayList cont = new ArrayList();
348:
349: /**
350: * Maps names to the contents of the Container.
351: */
352: private Map contMap = new HashMap();
353:
354: /**
355: * The Container I am a delegate for.
356: */
357: private LocalContainer delegateFor;
358:
359: // Inner classes -------------------------------------------------
360: }
|