Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: normal/engine/implementation/corba/CORBAShareableObject.java


1   
2   /**************************************************************************
3    *                                                                        *
4    *  Regina - A normal surface theory calculator                           *
5    *  Java user interface                                                   *
6    *                                                                        *
7    *  Copyright (c) 1999-2001, Ben Burton                                   *
8    *  For further details contact Ben Burton (benb@acm.org).                *
9    *                                                                        *
10   *  This program is free software; you can redistribute it and/or         *
11   *  modify it under the terms of the GNU General Public License as        *
12   *  published by the Free Software Foundation; either version 2 of the    *
13   *  License, or (at your option) any later version.                       *
14   *                                                                        *
15   *  This program is distributed in the hope that it will be useful, but   *
16   *  WITHOUT ANY WARRANTY; without even the implied warranty of            *
17   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
18   *  General Public License for more details.                              *
19   *                                                                        *
20   *  You should have received a copy of the GNU General Public             *
21   *  License along with this program; if not, write to the Free            *
22   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,        *
23   *  MA 02111-1307, USA.                                                   *
24   *                                                                        *
25   **************************************************************************/
26  
27  /* end stub */
28  
29  package normal.engine.implementation.corba;
30  
31  import java.math.BigInteger;
32  import normal.engine.utilities.NBoolSet;
33  import normal.engine.implementation.corba.Regina.*;
34  
35  /**
36   * Java object associated with an object in an external C++ CORBA server.
37   * <p>
38   * The protected member <tt>cppPtr</tt> is a memory pointer to the external
39   * C++ object. Through this member variable, a C++ implementation of a
40   * native method that is passed only the Java object can thus access the
41   * corresponding native C++ object.
42   * <p>
43   * When a new derivative of <tt>JNIShareableObject</tt> is created, many
44   * constructors
45   * may be called.  The corresponding native C++ object should <i>not</i> be
46   * created in more than one of these constructors.  Thus every derivative of
47   * <tt>JNIShareableObject</tt> should provide a constructor taking a single
48   * <tt>Sentry</tt> argument that does absolutely nothing.  All initialisation
49   * and creation of native C++ objects should take place in a constructor for
50   * the most derived class, and every constructor should call the do-nothing
51   * constructor for its parent class.
52   *
53   * @see #cppPtr
54   * @see normal.engine.implementation.jni.Sentry
55   */
56  public class CORBAShareableObject implements normal.engine.ShareableObject {
57      /**
58       * The CORBA client object for the underlying engine object that we are
59       * wrapping.
60       */
61      public ShareableObject data;
62  
63      /**
64       * The CORBA client class corresponding to this wrapper class.
65       */
66      public static final Class CORBAClass = ShareableObject.class;
67  
68      /**
69       * The CORBA helper class corresponding to this wrapper class.
70       */
71      public static final Class helperClass = ShareableObjectHelper.class;
72  
73      /**
74       * Converts the given string to a <tt>BigInteger</tt>.
75       * If the given string is <tt>inf</tt>, <tt>null</tt> will be
76       * returned.  Otherwise the string will be interpreted as the
77       * decimal representation of a large integer and the corresponding
78       * <tt>BigInteger</tt> will be returned.
79       *
80       * @param value the string to convert.
81       * @return the corresponding <tt>BigInteger</tt>.
82       */
83      public static BigInteger stringToLarge(String value) {
84          if (value.equals("inf"))
85              return null;
86          return new BigInteger(value);
87      }
88  
89      /**
90       * Creates a new wrapper for the given object.
91       *
92       * @param data the CORBA client object whose corresponding engine
93       * object we are wrapping.
94       */
95      protected CORBAShareableObject(ShareableObject data) {
96          this.data = data;
97      }
98  
99      public boolean sameObject(normal.engine.ShareableObject object) {
100         return data.sameObject(((CORBAShareableObject)object).data);
101     }
102     public boolean equals(Object obj) {
103         if (obj instanceof CORBAShareableObject)
104             return data.sameObject(((CORBAShareableObject)obj).data);
105         return false;
106     }
107     public normal.engine.ShareableObject castAs(Class cls) {
108         try {
109             // Get the helper and CORBA classes.
110             Class realHelperClass =
111                 (Class)cls.getField("helperClass").get(null);
112             Class realCORBAClass =
113                 (Class)cls.getField("CORBAClass").get(null);
114 
115             // Use the helper class to narrow [data].
116             Class[] narrowArgs = { org.omg.CORBA.Object.class };
117             Object[] narrowInvokeArgs = { data };
118             Object narrowed = realHelperClass.getMethod("narrow", narrowArgs).
119                 invoke(null, narrowInvokeArgs);
120 
121             // Create and return a new wrapper.
122             Class[] constrArgs = { realCORBAClass };
123             Object[] constrInvokeArgs = { narrowed };
124             return (normal.engine.ShareableObject)
125                 cls.getConstructor(constrArgs).newInstance(constrInvokeArgs);
126         } catch (Throwable th) {
127             th.printStackTrace();
128             return null;
129         }
130     }
131 
132     public void destroy() {
133         data.destroy();
134     }
135 
136     public void writeTextShort() {
137         System.out.println(toString());
138     }
139     public void writeTextLong() {
140         System.out.println(toStringLong());
141     }
142     public String toString() {
143         return data._toString();
144     }
145     public String toStringLong() {
146         return data.toStringLong();
147     }
148 }