RMI Hello Example from Sun Microsystems
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 14 May 2009 17:31:45 +0200
changeset 92 0bc2646daa82
parent 91 1181deef3bd6
child 93 66b3596f71b6
RMI Hello Example from Sun Microsystems This is a distributed Hello World Program and fully rmi powered committer: Markus Bröker <mbroeker@largo.homelinux.org>
rmihello/Hello.manifest
rmihello/build.xml
rmihello/example/hello/Client.java
rmihello/example/hello/Hello.java
rmihello/example/hello/Server.java
new file mode 100644
--- /dev/null
+++ b/rmihello/Hello.manifest
@@ -0,0 +1,2 @@
+Manifest-Version: 1.0
+Main-Class: example.hello.Server
new file mode 100644
--- /dev/null
+++ b/rmihello/build.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0"?>
+<project name="Hello" basedir="." default="jar">
+	<property name="project" value="Hello" />
+	<property name="package.name" value="example.hello" />
+	<property name="package.dir" value="example/hello" />
+	<property name="jar.file" value="bin/hello.jar" />
+	<property name="classpath.dir" value="/usr/local/lib/classes" />
+	<property name="www.dir" value="/var/www/localhost/htdocs/classes" />
+
+	<property name="distributable.exts"
+		value="**/*.class,**/*.gif,**/*.html,**/*.jar,**/*.jnlp,**/*.jpg,**/*.png" />
+	<path id="classpath">
+		<pathelement location="." />
+	</path>
+
+	<!-- Targets -->
+	<target name="clean">
+		<echo
+			message="deleting all class- and jar-files  in ${package.dir} tree" />
+		<delete>
+			<fileset dir="${package.dir}" includes="**/*.class" />
+			<fileset dir="." includes="**/*.*~" defaultexcludes="no" />
+		</delete>
+
+		<delete file="${jar.file}" />
+	</target>
+
+	<target name="compile">
+		<echo message="compiling ${package.dir} tree." />
+		<javac source="1.5" target="1.5" srcdir="${package.dir}"
+			sourcepath="${basedir}" debug="on">
+			<classpath refid="classpath" />
+		</javac>
+	</target>
+
+	<target name="jar" depends="compile">
+		<property environment="env" />
+		<jar destfile="${jar.file}" basedir="${basedir}"
+			includes="example/"
+			manifest="${project}.manifest" />
+	</target>
+
+	<target name="post" depends="jar">
+		<signjar jar="${jar.file}" alias="spectre" storepass="${env.jsp}" />
+		<copy file="${jar.file}" todir="${www.dir}"/>
+	</target>
+
+	<target name="client" depends="compile">
+		<java classname="${package.name}.Client"
+			failonerror="true" fork="yes">
+			<classpath refid="classpath" />
+		</java>
+	</target>
+
+	<target name="server" depends="compile">
+		<java classname="${package.name}.Server"
+			failonerror="true" fork="yes">
+			<classpath refid="classpath" />
+		</java>
+	</target>
+</project>
new file mode 100644
--- /dev/null
+++ b/rmihello/example/hello/Client.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc. All  Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * -Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ * -Redistribution in binary form must reproduce the above copyright
+ *  notice, this list of conditions and the following disclaimer in
+ *  the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ * Neither the name of Sun Microsystems, Inc. or the names of
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * This software is provided "AS IS," without a warranty of any
+ * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
+ * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
+ * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
+ * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
+ * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
+ * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
+ * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
+ * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
+ * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
+ * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * You acknowledge that Software is not designed, licensed or
+ * intended for use in the design, construction, operation or
+ * maintenance of any nuclear facility.
+ */
+package example.hello;
+
+import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
+
+public class Client {
+
+    private Client () {
+    }
+
+    public static void main (String[]args) {
+        String host = (args.length < 1) ? null : args[0];
+
+        try {
+            Registry registry = LocateRegistry.getRegistry (host);
+            Hello stub = (Hello) registry.lookup ("Hello");
+            String response = stub.sayHello ();
+            System.out.println ("response: " + response);
+        } catch (Exception e) {
+            System.err.println ("Client exception: " + e.toString ());
+            e.printStackTrace ();
+        }
+    }
+}
new file mode 100644
--- /dev/null
+++ b/rmihello/example/hello/Hello.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc. All  Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * -Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ * -Redistribution in binary form must reproduce the above copyright
+ *  notice, this list of conditions and the following disclaimer in
+ *  the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ * Neither the name of Sun Microsystems, Inc. or the names of
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * This software is provided "AS IS," without a warranty of any
+ * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
+ * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
+ * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
+ * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
+ * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
+ * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
+ * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
+ * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
+ * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
+ * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * You acknowledge that Software is not designed, licensed or
+ * intended for use in the design, construction, operation or
+ * maintenance of any nuclear facility.
+ */
+package example.hello;
+
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+
+public interface Hello extends Remote {
+    String sayHello () throws RemoteException;
+}
new file mode 100644
--- /dev/null
+++ b/rmihello/example/hello/Server.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2004 Sun Microsystems, Inc. All  Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * -Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ * -Redistribution in binary form must reproduce the above copyright
+ *  notice, this list of conditions and the following disclaimer in
+ *  the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ * Neither the name of Sun Microsystems, Inc. or the names of
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * This software is provided "AS IS," without a warranty of any
+ * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
+ * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
+ * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
+ * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
+ * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
+ * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
+ * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
+ * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
+ * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
+ * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ * You acknowledge that Software is not designed, licensed or
+ * intended for use in the design, construction, operation or
+ * maintenance of any nuclear facility.
+ */
+package example.hello;
+
+import java.rmi.registry.Registry;
+import java.rmi.registry.LocateRegistry;
+import java.rmi.RemoteException;
+import java.rmi.server.UnicastRemoteObject;
+
+public class Server implements Hello {
+
+    public Server () {
+    }
+
+    public String sayHello () {
+        return "Hello, world!";
+    }
+
+    public static void main (String args[]) {
+        try {
+            Server obj = new Server ();
+            Hello stub = (Hello) UnicastRemoteObject.exportObject (obj, 0);
+
+            // Bind the remote object's stub in the registry
+            Registry registry = LocateRegistry.getRegistry ();
+            registry.bind ("Hello", stub);
+
+            System.err.println ("Server ready");
+        } catch (Exception e) {
+            System.err.println ("Server exception: " + e.toString ());
+            e.printStackTrace ();
+        }
+    }
+}