1 /* 2 * Copyright (c) 1998, 2012, 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 /* 25 */ 26 import java.rmi.*; 27 import java.rmi.activation.*; 28 import java.rmi.server.*; 29 import java.security.CodeSource; 30 import java.net.URL; 31 32 public class EchoImpl 33 extends Activatable 34 implements Echo, Runnable 35 { 36 private static final byte[] pattern = { (byte) 'A' }; 37 38 /** 39 * Initialization constructor. 40 */ 41 public EchoImpl(String protocol) 42 throws ActivationException, RemoteException 43 { 44 super(null, makeMarshalledObject(protocol), false, 0, 45 new MultiSocketFactory.ClientFactory(protocol, pattern), 46 new MultiSocketFactory.ServerFactory(protocol, pattern)); 47 } 48 49 /** 50 * Activation constructor. 51 */ 52 public EchoImpl(ActivationID id, MarshalledObject obj) 53 throws RemoteException 54 { 55 super(id, 0, 56 new MultiSocketFactory.ClientFactory(getProtocol(obj), pattern), 57 new MultiSocketFactory.ServerFactory(getProtocol(obj), pattern)); 58 } 59 60 private static MarshalledObject makeMarshalledObject(String protocol) { 61 MarshalledObject obj = null; 62 try { 63 obj = new MarshalledObject(protocol); 64 } catch (Exception willNotHappen) { 65 } 66 67 return obj; 68 } 69 70 private static String getProtocol(MarshalledObject obj) { 71 String protocol = ""; 72 try { 73 protocol = (String) obj.get(); 74 } catch (Exception willNotHappen) { 75 } 76 77 return protocol; 78 } 79 80 public byte[] echoNot(byte[] data) { 81 byte[] result = new byte[data.length]; 82 for (int i = 0; i < data.length; i++) 83 result[i] = (byte) ~data[i]; 84 return result; 85 } 86 87 /** 88 * Spawns a thread to deactivate the object. 89 */ 90 public void shutdown() throws Exception 91 { 92 (new Thread(this,"Echo.shutdown")).start(); 93 } 94 95 /** 96 * Thread to deactivate object. First attempts to make object 97 * inactive (via the inactive method). If that fails (the 98 * object may still have pending/executing calls), then 99 * unexport the object forcibly. 100 */ 101 public void run() 102 { 103 ActivationLibrary.deactivate(this, getID()); 104 } 105 106 public static void main(String[] args) { 107 /* 108 * The following line is required with the JDK 1.2 VM so that the 109 * VM can exit gracefully when this test completes. Otherwise, the 110 * conservative garbage collector will find a handle to the server 111 * object on the native stack and not clear the weak reference to 112 * it in the RMI runtime's object table. 113 */ 114 Object dummy = new Object(); 115 116 System.setSecurityManager(new RMISecurityManager()); 117 118 try { 119 String protocol = ""; 120 if (args.length >= 1) 121 protocol = args[0]; 122 123 System.out.println("EchoServer: creating remote object"); 124 ActivationGroupDesc groupDesc = 125 new ActivationGroupDesc(null, null); 126 ActivationSystem system = ActivationGroup.getSystem(); 127 ActivationGroupID groupID = system.registerGroup(groupDesc); 128 ActivationGroup.createGroup(groupID, groupDesc, 0); 129 130 EchoImpl impl = new EchoImpl(protocol); 131 int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port")); 132 System.out.println("EchoServer: binding in registry on port:" + registryPort); 133 Naming.rebind("//:" + registryPort + 134 "/EchoServer", impl); 135 System.out.println("EchoServer ready."); 136 } catch (Exception e) { 137 System.err.println("EXCEPTION OCCURRED:"); 138 e.printStackTrace(); 139 } 140 } 141 }