2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.CORBA.CDR;
41 import gnu.CORBA.Minor;
43 import gnu.java.lang.CPStringBuilder;
45 import org.omg.CORBA.LocalObject;
46 import org.omg.CORBA.MARSHAL;
48 import java.io.Serializable;
49 import java.util.Comparator;
50 import java.util.HashMap;
51 import java.util.IdentityHashMap;
52 import java.util.Iterator;
54 import java.util.TreeMap;
55 import java.util.TreeSet;
58 * Our implementation of the sending context runtime.
60 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
62 public class gnuRuntime
64 implements org.omg.SendingContext.RunTime
67 * The data entry about the object that was written.
72 * The stream position, where the object was written.
77 * The object that was written.
81 public String toString()
83 return object + "[" + at + "] "+object.getClass().getName();
88 * The instruction that the actual object is stored at different location.
89 * Used when processing chunked data where positions shifts due removing the
92 static class Redirection
95 public String toString()
102 * Use serialVersionUID for interoperability.
104 private static final long serialVersionUID = 1;
107 * The history of the written objects, maps object to records. The different
108 * objects must be treated as different regardless that .equals returns.
110 private Map sh_objects = new IdentityHashMap();
113 * The written repository Ids that can be shared.
115 private Map sh_ids = new TreeMap(new Comparator()
117 public int compare(Object a, Object b)
119 if (a instanceof String && b instanceof String)
120 // Comparing string with string.
121 return ((String) a).compareTo((String) b);
122 else if (a instanceof String[] && b instanceof String[])
124 // Comparing array with array.
125 String[] sa = (String[]) a;
126 String[] sb = (String[]) b;
128 if (sa.length != sb.length)
129 return sa.length - sb.length;
133 for (int i = 0; i < sa.length; i++)
135 c = sa[i].compareTo(sb[i]);
143 // Comparing string with array.
144 return a instanceof String ? 1 : -1;
149 * The history of the written objects, maps positions to records. The
150 * different objects must be treated as different regardless that .equals
153 private Map positions = new HashMap();
158 private String codebase;
161 * The pre-created instance of the object being written (avoid
164 public Serializable target;
169 * @param a_id a repository Id, if only one Id was specified in the stream.
170 * @param a_ids a repository Ids, if the multiple Ids were specified in te
172 * @param a_codebase a codebase, if it was specified in the stream.
174 public gnuRuntime(String a_codebase, Object a_target)
176 if (a_target instanceof Serializable)
177 target = (Serializable) a_target;
179 codebase = a_codebase;
183 * Mark the given object as written at the given position.
185 public void objectWritten(Object object, int at)
187 if (object == null || at < 0)
188 return; // No positional information provided.
189 if (sh_objects.containsKey(object))
190 throw new AssertionError("Repetetive writing of the same object "
191 + object + " at " + at + dump());
193 Entry e = new Entry();
197 sh_objects.put(object, e);
198 positions.put(new Integer(at), e);
202 * Check if the object is already written.
204 * @return the position, at that the object is allready written or -1 if it is
207 public int isWrittenAt(Object x)
209 Entry e = (Entry) sh_objects.get(x);
210 return e == null ? -1 : e.at;
214 * Set redirection, indicating that the object, searched at the p_searched
215 * position can be actually found at the p_present position.
217 public void redirect(int p_searched, int p_present)
219 Redirection redirection = new Redirection();
220 redirection.at = p_present;
221 positions.put(new Integer(p_searched), redirection);
225 * Get the object, written at the given position. This returs both shared
226 * objects and repository Ids.
228 * @return the position, at that the object is allready written.
230 * @throws MARSHAL if there is no object written at that position.
232 public Object isObjectWrittenAt(int x, int offset)
234 Entry e = (Entry) positions.get(new Integer(x));
235 if (e instanceof Redirection)
236 return isObjectWrittenAt(e.at, offset);
241 MARSHAL m = new MARSHAL("No object was written at " + x +
242 " (offset " + offset + ") r " + this + dump());
243 m.minor = Minor.Graph;
249 * Mark the given object as written at the given position.
251 public void singleIdWritten(String id, int at)
253 if (sh_ids.containsKey(id))
254 throw new InternalError("Repetetive writing of the same string " +
257 Entry e = new Entry();
262 positions.put(new Integer(at), e);
266 * Mark the given object as written at the given position.
268 public void multipleIdsWritten(String[] ids, int at)
270 if (sh_ids.containsKey(ids))
271 throw new InternalError("Repetetive writing of the same string " +
274 Entry e = new Entry();
279 positions.put(new Integer(at), e);
283 * Check if the object is already written.
285 * @return the position, at that the object is allready written or -1 if it is
288 public int idWrittenAt(Object x)
290 Entry e = (Entry) sh_ids.get(x);
291 return e == null ? -1 : e.at;
297 public String getCodeBase()
303 * Set the codebase, preserving the old value if the passed parameter is null
304 * and forming the space delimited list if both new and old values are not
307 public void addCodeBase(String base)
311 if (codebase == null)
314 codebase = codebase + " " + base;
319 * Dump all objects that are currently stored.
323 CPStringBuilder b = new CPStringBuilder(" Stream content: \n");
326 TreeSet t = new TreeSet(positions.keySet());
327 Iterator p = t.iterator();
332 b.append(" " + k + ": " + ((Entry) positions.get(k)).toString()