1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 // RMI Activation Functional Test
  25 
  26 import java.rmi.*;
  27 import java.rmi.server.*;
  28 import java.rmi.activation.*;
  29 import java.util.*;
  30 
  31 // CountServerImpl
  32 
  33 public class CountServerImpl
  34     extends Activatable
  35     implements CountInterface {
  36 
  37     private static final String PROG_NAME       = "CountServerImpl";
  38     private static final String SERVER_OBJECT   = "CountServer";
  39     private static final String CLASS_NAME      = "activation.CountServerImpl";
  40 
  41     private static final String POLICY_FILE   = "policy_file";
  42 
  43     private static final String USER_DIR      =
  44                         System.getProperty("user.dir").replace('\\', '/');
  45 
  46     private static final String CODE_LOCATION = "file:"+USER_DIR+"/";
  47 
  48     private static final MarshalledObject DATA = null;
  49     private static ActivationDesc ACTIVATION_DESC = null;
  50 
  51     // Class variable
  52     private static int classCount = 0;
  53 
  54     // Instance variable
  55     private int instanceCount;
  56     private TestInterface ref;
  57 
  58     public CountServerImpl(ActivationID id, MarshalledObject data)
  59         throws RemoteException {
  60         super(id, 0);
  61         instanceCount = 0;
  62         classCount++;
  63         if (data != null) {
  64             try {
  65                 ref = (TestInterface)data.get();
  66                 ref.ping(SERVER_OBJECT);
  67             }
  68             catch (Exception e) {
  69                 System.err.println("Exception: " + e);
  70             }
  71         }
  72     }
  73 
  74     public void ping() throws RemoteException {}
  75 
  76     public int getCount() throws RemoteException {
  77         return instanceCount;
  78     }
  79 
  80     public int incrementCount() throws RemoteException {
  81         return ++instanceCount;
  82     }
  83 
  84     public int decrementCount() throws RemoteException {
  85         return --instanceCount;
  86     }
  87 
  88     public int getClassCount() throws RemoteException {
  89         return classCount;
  90     }
  91 
  92     public String getProperty(String s) throws RemoteException {
  93         return System.getProperty(s);
  94     }
  95 
  96     public void exit() throws RemoteException {
  97         System.exit(0);
  98     }
  99 
 100     public boolean unexportObject(boolean force) {
 101         boolean succeeded = false;
 102         try {
 103             succeeded = Activatable.unexportObject(this, force);
 104         }
 105         catch (Exception e) {
 106             System.err.println("Exception: " + e);
 107             e.printStackTrace();
 108         }
 109         return succeeded;
 110     }
 111 
 112     public ActivationID getActivationID() throws RemoteException {
 113         return super.getID();
 114     }
 115 
 116     public void inactive()
 117         throws RemoteException, ActivationException, UnknownObjectException {
 118 
 119         //ShutDown s = new ShutDown(super.getID(),this,ShutDown.NORMAL_SHUTDOWN);
 120     }
 121 
 122     public void unregister()
 123         throws RemoteException, ActivationException, UnknownObjectException {
 124         unregister(super.getID());
 125     }
 126 
 127     public void register()
 128         throws RemoteException, ActivationException, UnknownObjectException {
 129         register(ACTIVATION_DESC);
 130     }
 131 
 132     public ActivationGroupID getCurrentGroupID() throws RemoteException {
 133         return ActivationGroup.currentGroupID();
 134     }
 135 
 136     private static void setup() {
 137 
 138         try {
 139 
 140           CountInterface rsi;   // Remote server interface
 141 
 142           System.setSecurityManager(new RMISecurityManager());
 143 
 144           rsi = (CountInterface)Activatable.register(ACTIVATION_DESC);
 145           System.out.println("Got stub for "+SERVER_OBJECT+" implementation");
 146 
 147           Naming.rebind(SERVER_OBJECT, rsi);
 148           System.out.println("Exported "+SERVER_OBJECT+" implementation");
 149 
 150         } catch (Exception e) {
 151             System.err.println("Exception: " + e);
 152             e.printStackTrace();
 153         }
 154     }
 155 
 156     public static void main(String[] args) {
 157 
 158         try {
 159             Properties props = new Properties();
 160             props.setProperty("java.security.policy", POLICY_FILE);
 161 
 162             ActivationGroupDesc agd = new ActivationGroupDesc(props, null);
 163 
 164             ActivationGroupID agid = ActivationGroup.getSystem().registerGroup(agd);
 165 
 166             ACTIVATION_DESC = new ActivationDesc(agid,
 167                         CLASS_NAME, CODE_LOCATION, DATA, false);
 168         }
 169         catch (Exception e) {
 170             System.err.println("Exception: " + e);
 171             e.printStackTrace();
 172         }
 173 
 174         setup();
 175     }
 176 }