summaryrefslogtreecommitdiff
path: root/gnu/java/rmi/server/ActivatableServerRef.java
blob: 31e825a25ae487ab7f8758c87b8a3f779f5896d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* ActivatableServerRef.java -- The activatable server reference
   Copyright (C) 2006 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package gnu.java.rmi.server;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.activation.ActivationID;
import java.rmi.server.ObjID;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.RemoteStub;
import java.rmi.server.Skeleton;

/**
 * The activatable server reference works like UnicastServerReference, but it
 * additionally activates the associated object on demand, during the first
 * incoming call. When UnicastServerReference takes the working reference,
 * the ActivatableServerRef takes the activation id instead.
 *
 * @author Audrius Meskauskas (Audriusa@Bioinformatics.org)
 */
public class ActivatableServerRef extends UnicastServerRef
{
  /**
   * Use SVUID for interoperability
   */
  private static final long serialVersionUID = 1;

  /**
   * The object activation id.
   */
  public ActivationID actId;

  /**
   * Used by serialization only
   */
  public ActivatableServerRef()
  {
    super();
  }

  /**
   * Create the new activatable server reference that will activate object on
   * the first call using the given activation id.
   */
  public ActivatableServerRef(ObjID id, ActivationID anId, int aPort,
                              RMIServerSocketFactory ssFactory)
      throws RemoteException
  {
    super(id, aPort, ssFactory);
    actId = anId;

    // The object ID will be placed in the object map and should deliver
    // incoming call to {@link #incommingMessageCall}. The object itself
    // is currently null.
    UnicastServer.exportActivatableObject(this);
  }

  /**
   * Inactivate the object (stop the server).
   */
  public void inactivate()
  {
    manager.stopServer();
  }

  /**
   * Activate the object (normally during the first call).
   */
  protected void activate() throws RemoteException
  {
    try
      {
        Remote self = actId.activate(false);

        // This will call UnicastServer.exportObject, replacing null by
        // the activated object (self) in the object map.
        exportObject(self);
      }
    catch (RemoteException rex)
      {
        throw rex;
      }
    catch (Exception exc)
      {
        RemoteException rx = new RemoteException("Activation failed.");
        rx.detail = exc;
        throw rx;
      }
  }

  /**
   * If the object is not active, activate it first.
   */
  public Object incomingMessageCall(UnicastConnection conn, int method,
                                    long hash) throws Exception
  {
    if (myself == null)
      activate();
    return super.incomingMessageCall(conn, method, hash);
  }

  /**
   * Export object and ensure it is present in the server activation table
   * as well.
   */
  public Remote exportObject(Remote obj) throws RemoteException
  {
    Remote r = super.exportObject(obj);
    UnicastServer.registerActivatable(this);
    return r;
  }

  /**
   * Export object and ensure it is present in the server activation table as
   * well.
   *
   * @param aClass the class being exported, must implement Remote.
   */
  public Remote exportClass(Class aClass) throws RemoteException
  {
    if (!Remote.class.isAssignableFrom(aClass))
      throw new InternalError(aClass.getName()+" must implement Remote");

        String ignoreStubs;

        ClassLoader loader =aClass.getClassLoader();

        // Stubs are always searched for the bootstrap classes that may have
        // obsolete pattern and may still need also skeletons.
        if (loader==null)
          ignoreStubs = "false";
        else
          ignoreStubs = System.getProperty("java.rmi.server.ignoreStubClasses",
                                           "false");

        if (! ignoreStubs.equals("true"))
          {
            // Find and install the stub
            Class cls = aClass;

            // where ist the _Stub? (check superclasses also)
            Class expCls = findStubSkelClass(cls);

            if (expCls != null)
              {
                stub = (RemoteStub) getHelperClass(expCls, "_Stub");
                // Find and install the skeleton (if there is one)
                skel = (Skeleton) getHelperClass(expCls, "_Skel");
              }
          }

        if (stub == null)
          stub = createProxyStub(aClass, this);

        // Build hash of methods which may be called.
        buildMethodHash(aClass, true);

    UnicastServer.registerActivatable(this);
    return stub;
  }

  /**
   * Get the referencing class.
   */
  public String getRefClass(ObjectOutput out)
  {
    return "ActivatableRef";
  }

  /**
   * Read the content from the input stream.
   */
  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
  {
    super.readExternal(in);
    actId = (ActivationID) in.readObject();
  }

  /**
   * Write the content to the output stream.
   */
  public void writeExternal(ObjectOutput out) throws IOException
  {
    super.writeExternal(out);
    out.writeObject(actId);
  }

}