summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2012-11-21 14:42:56 -0330
committerMatthew Newhook <matthew@zeroc.com>2012-11-21 14:42:56 -0330
commitfb6bfca6b217731852624fd95723cf2054d12a12 (patch)
tree23a50f94aa9f0a0ed5d62fe0ce642c19a72ba445
parentSimplified vsaddin/INSTALL.txt (diff)
downloadice-fb6bfca6b217731852624fd95723cf2054d12a12.tar.bz2
ice-fb6bfca6b217731852624fd95723cf2054d12a12.tar.xz
ice-fb6bfca6b217731852624fd95723cf2054d12a12.zip
Added java optional demo.
-rw-r--r--java/demo/Ice/README4
-rw-r--r--java/demo/Ice/build.xml2
-rw-r--r--java/demo/Ice/optional/.gitignore7
-rw-r--r--java/demo/Ice/optional/Client.java242
-rw-r--r--java/demo/Ice/optional/Contact.ice43
-rw-r--r--java/demo/Ice/optional/ContactDBI.java93
-rw-r--r--java/demo/Ice/optional/README10
-rw-r--r--java/demo/Ice/optional/Server.java37
-rw-r--r--java/demo/Ice/optional/build.xml44
-rw-r--r--java/demo/Ice/optional/config.client10
-rw-r--r--java/demo/Ice/optional/config.server11
-rwxr-xr-xjava/demo/Ice/optional/expect.py29
12 files changed, 532 insertions, 0 deletions
diff --git a/java/demo/Ice/README b/java/demo/Ice/README
index bdb3cf488b5..b584fe0aafb 100644
--- a/java/demo/Ice/README
+++ b/java/demo/Ice/README
@@ -88,3 +88,7 @@ Demos in this directory:
This demo shows how to use classes, class factories, and the
difference between local and remote invocations of class operations.
+
+- optional
+
+ This demo shows the use of the optional keyword.
diff --git a/java/demo/Ice/build.xml b/java/demo/Ice/build.xml
index cd0cabb8840..a15f52dcd38 100644
--- a/java/demo/Ice/build.xml
+++ b/java/demo/Ice/build.xml
@@ -29,6 +29,7 @@
<ant dir="swing"/>
<ant dir="throughput"/>
<ant dir="value"/>
+ <ant dir="optional"/>
</target>
<target name="clean">
@@ -49,6 +50,7 @@
<ant dir="swing" target="clean"/>
<ant dir="throughput" target="clean"/>
<ant dir="value" target="clean"/>
+ <ant dir="optional" target="clean"/>
</target>
</project>
diff --git a/java/demo/Ice/optional/.gitignore b/java/demo/Ice/optional/.gitignore
new file mode 100644
index 00000000000..7a51764475c
--- /dev/null
+++ b/java/demo/Ice/optional/.gitignore
@@ -0,0 +1,7 @@
+// Generated by makegitignore.py
+
+// IMPORTANT: Do not edit this file -- any edits made here will be lost!
+client
+server
+Contact.cpp
+Contact.h
diff --git a/java/demo/Ice/optional/Client.java b/java/demo/Ice/optional/Client.java
new file mode 100644
index 00000000000..45e88587709
--- /dev/null
+++ b/java/demo/Ice/optional/Client.java
@@ -0,0 +1,242 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
+//
+// This copy of Ice is licensed to you under the terms described in the
+// ICE_LICENSE file included in this distribution.
+//
+// **********************************************************************
+
+import Demo.*;
+
+public class Client extends Ice.Application
+{
+ class ShutdownHook extends Thread
+ {
+ public void
+ run()
+ {
+ try
+ {
+ communicator().destroy();
+ }
+ catch(Ice.LocalException ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+ }
+
+ public int
+ run(String[] args)
+ {
+ if(args.length > 0)
+ {
+ System.err.println(appName() + ": too many arguments");
+ return 1;
+ }
+
+ ContactDBPrx contactdb = ContactDBPrxHelper.checkedCast(communicator().propertyToProxy("ContactDB.Proxy"));
+ if(contactdb == null)
+ {
+ System.err.println(appName() + ": invalid proxy");
+ return 1;
+ }
+
+ //
+ // Add a contact for "john". All parameters are provided.
+ //
+ String johnNumber = "123-456-7890";
+ contactdb.addContact("john", new Ice.Optional<NumberType>(NumberType.HOME),
+ new Ice.Optional<String>(johnNumber), new Ice.IntOptional(0));
+
+ System.out.print("Checking john... ");
+
+ //
+ // Find the phone number for "john". Note the use of the Ice.Optional
+ // for non-generic types.
+ //
+ Ice.Optional<String> number = contactdb.queryNumber("john");
+
+ //
+ // isSet() tests if an optional value is set.
+ //
+ if(!number.isSet())
+ {
+ System.out.print("number is incorrect ");
+ }
+
+ //
+ // Call get() to retrieve the optional value.
+ //
+ if(!number.get().equals(johnNumber))
+ {
+ System.out.print("number is incorrect ");
+ }
+
+ // Optional can also be used in an out parameter. Note that
+ // primitive types don't use Ice.Optional.
+ Ice.IntOptional dialgroup = new Ice.IntOptional();
+ contactdb.queryDialgroup("john", dialgroup);
+ if(!dialgroup.isSet() || dialgroup.get() != 0)
+ {
+ System.out.print("dialgroup is incorrect ");
+ }
+
+ Contact info = contactdb.query("john");
+
+ //
+ // All of the info parameters should be set. On a class call
+ // has<name> to find out whether the value is set.
+ //
+ if(!info.hasType() || !info.hasNumber() || !info.hasDialGroup())
+ {
+ System.out.print("info is incorrect ");
+ }
+ // Call get<name> to retrieve the value.
+ if(info.getType() != NumberType.HOME || !info.getNumber().equals(johnNumber) || info.getDialGroup() != 0)
+ {
+ System.out.print("info is incorrect ");
+ }
+ System.out.println("ok");
+
+ //
+ // Add a contact for "steve". The behavior of the server is to
+ // default construct the Contact, and then assign all set parameters.
+ // Since the default value of NumberType in the slice definition
+ // is NumberType.HOME and in this case the NumberType is unset it will take
+ // the default value.
+ //
+ // The java mapping permits null to be passed to unset optional values.
+ //
+ String steveNumber = "234-567-8901";
+ contactdb.addContact("steve", null, new Ice.Optional<String>(steveNumber), new Ice.IntOptional(1));
+
+ System.out.print("Checking steve... ");
+ number = contactdb.queryNumber("steve");
+ if(!number.get().equals(steveNumber))
+ {
+ System.out.print("number is incorrect ");
+ }
+
+ info = contactdb.query("steve");
+ //
+ // Check the value for the NumberType.
+ //
+ if(!info.hasType() || info.getType() != NumberType.HOME)
+ {
+ System.out.print("info is incorrect ");
+ }
+
+ if(!info.getNumber().equals(steveNumber) || info.getDialGroup() != 1)
+ {
+ System.out.print("info is incorrect ");
+ }
+
+ contactdb.queryDialgroup("steve", dialgroup);
+ if(!dialgroup.isSet() || dialgroup.get() != 1)
+ {
+ System.out.print("dialgroup is incorrect ");
+ }
+
+ System.out.println("ok");
+
+ //
+ // Add a contact from "frank". Here the dialGroup field isn't set.
+ //
+ String frankNumber = "345-678-9012";
+ contactdb.addContact("frank", new Ice.Optional<NumberType>(NumberType.CELL),
+ new Ice.Optional<String>(frankNumber), null);
+
+ System.out.print("Checking frank... ");
+
+ number = contactdb.queryNumber("frank");
+ if(!number.get().equals(frankNumber))
+ {
+ System.out.print("number is incorrect ");
+ }
+
+ info = contactdb.query("frank");
+ //
+ // The dial group field should be unset.
+ //
+ if(info.hasDialGroup())
+ {
+ System.out.print("info is incorrect ");
+ }
+ if(info.getType() != NumberType.CELL || !info.getNumber().equals(frankNumber))
+ {
+ System.out.print("info is incorrect ");
+ }
+
+ contactdb.queryDialgroup("frank", dialgroup);
+ if(dialgroup.isSet())
+ {
+ System.out.print("dialgroup is incorrect ");
+ }
+ System.out.println("ok");
+
+ //
+ // Add a contact from "anne". The number field isn't set.
+ //
+ contactdb.addContact("anne", new Ice.Optional<NumberType>(NumberType.OFFICE), null, new Ice.IntOptional(2));
+
+ System.out.print("Checking anne... ");
+ number = contactdb.queryNumber("anne");
+ if(number.isSet())
+ {
+ System.out.print("number is incorrect ");
+ }
+
+ info = contactdb.query("anne");
+ //
+ // The number field should be unset.
+ //
+ if(info.hasNumber())
+ {
+ System.out.print("info is incorrect ");
+ }
+ if(info.getType() != NumberType.OFFICE || info.getDialGroup() != 2)
+ {
+ System.out.print("info is incorrect ");
+ }
+
+ contactdb.queryDialgroup("anne", dialgroup);
+ if(!dialgroup.isSet() || dialgroup.get() != 2)
+ {
+ System.out.print("dialgroup is incorrect ");
+ }
+
+ //
+ // The optional fields can be used to determine what fields to
+ // update on the contact. Here we update only the number for anne,
+ // the remainder of the fields are unchanged.
+ //
+ String anneNumber = "456-789-0123";
+ contactdb.updateContact("anne", null, new Ice.Optional<String>(anneNumber), null);
+ number = contactdb.queryNumber("anne");
+ if(!number.get().equals(anneNumber))
+ {
+ System.out.print("number is incorrect ");
+ }
+ info = contactdb.query("anne");
+ if(!info.getNumber().equals(anneNumber) || info.getType() != NumberType.OFFICE || info.getDialGroup() != 2)
+ {
+ System.out.print("info is incorrect ");
+ }
+ System.out.println("ok");
+
+ contactdb.shutdown();
+
+ return 0;
+ }
+
+ public static void
+ main(String[] args)
+ {
+ Client app = new Client();
+ int status = app.main("Client", args, "config.client");
+ System.exit(status);
+ }
+}
+
diff --git a/java/demo/Ice/optional/Contact.ice b/java/demo/Ice/optional/Contact.ice
new file mode 100644
index 00000000000..958e7446363
--- /dev/null
+++ b/java/demo/Ice/optional/Contact.ice
@@ -0,0 +1,43 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
+//
+// This copy of Ice is licensed to you under the terms described in the
+// ICE_LICENSE file included in this distribution.
+//
+// **********************************************************************
+
+#pragma once
+
+module Demo
+{
+
+enum NumberType
+{
+ HOME,
+ OFFICE,
+ CELL
+};
+
+class Contact
+{
+ string name;
+ optional(1) NumberType type = HOME;
+ optional(2) string number;
+ optional(3) int dialGroup;
+};
+
+interface ContactDB
+{
+ ["java:optional"] void addContact(string name, optional(1) NumberType type, optional(2) string number, optional(3) int dialGroup);
+ ["java:optional"] void updateContact(string name, optional(1) NumberType type, optional(2) string number, optional(3) int dialGroup);
+
+ Contact query(string name);
+ ["java:optional"] optional(1) string queryNumber(string name);
+ ["java:optional"] void queryDialgroup(string name, out optional(1) int dialGroup);
+
+ void shutdown();
+};
+
+};
+
diff --git a/java/demo/Ice/optional/ContactDBI.java b/java/demo/Ice/optional/ContactDBI.java
new file mode 100644
index 00000000000..37da9f3b470
--- /dev/null
+++ b/java/demo/Ice/optional/ContactDBI.java
@@ -0,0 +1,93 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
+//
+// This copy of Ice is licensed to you under the terms described in the
+// ICE_LICENSE file included in this distribution.
+//
+// **********************************************************************
+
+import Demo.*;
+
+public class ContactDBI extends _ContactDBDisp
+{
+ private java.util.Map<String, Contact> _contacts = new java.util.HashMap<String, Contact>();
+
+ public final void
+ addContact(String name, Ice.Optional<NumberType> type, Ice.Optional<String> number, Ice.IntOptional dialGroup,
+ Ice.Current current)
+ {
+ Contact contact = new Contact();
+ contact.name = name;
+ if(type.isSet())
+ {
+ contact.setType(type.get());
+ }
+ if(number.isSet())
+ {
+ contact.setNumber(number.get());
+ }
+ if(dialGroup.isSet())
+ {
+ contact.setDialGroup(dialGroup.get());
+ }
+ _contacts.put(name, contact);
+ }
+
+ public final void
+ updateContact(String name, Ice.Optional<NumberType> type, Ice.Optional<String> number, Ice.IntOptional dialGroup,
+ Ice.Current current)
+ {
+ Contact c = _contacts.get(name);
+ if(c != null)
+ {
+ if(type.isSet())
+ {
+ c.setType(type.get());
+ }
+ if(number.isSet())
+ {
+ c.setNumber(number.get());
+ }
+ if(dialGroup.isSet())
+ {
+ c.setDialGroup(dialGroup.get());
+ }
+ }
+ }
+
+ public final Contact
+ query(String name, Ice.Current current)
+ {
+ return _contacts.get(name);
+ }
+
+ public final void
+ queryDialgroup(String name, Ice.IntOptional dialGroup, Ice.Current current)
+ {
+ Contact c = _contacts.get(name);
+ if(c != null && c.hasDialGroup())
+ {
+ dialGroup.set(c.getDialGroup());
+ }
+ }
+
+ public final Ice.Optional<String>
+ queryNumber(String name, Ice.Current current)
+ {
+ Ice.Optional<String> ret = new Ice.Optional<String>();
+ Contact c = _contacts.get(name);
+ if(c != null && c.hasNumber())
+ {
+ ret.set(c.getNumber());
+ }
+ return ret;
+ }
+
+ public void
+ shutdown(Ice.Current current)
+ {
+ System.out.println("Shutting down...");
+ current.adapter.getCommunicator().shutdown();
+ }
+}
diff --git a/java/demo/Ice/optional/README b/java/demo/Ice/optional/README
new file mode 100644
index 00000000000..8e1ff26e319
--- /dev/null
+++ b/java/demo/Ice/optional/README
@@ -0,0 +1,10 @@
+This demo illustrates optional class members and the use of the optional
+keyword for in/out and return values in method invocations.
+
+To run the demo, first start the server:
+
+$ server
+
+In a separate window, start the client:
+
+$ client
diff --git a/java/demo/Ice/optional/Server.java b/java/demo/Ice/optional/Server.java
new file mode 100644
index 00000000000..d51292f4fb1
--- /dev/null
+++ b/java/demo/Ice/optional/Server.java
@@ -0,0 +1,37 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
+//
+// This copy of Ice is licensed to you under the terms described in the
+// ICE_LICENSE file included in this distribution.
+//
+// **********************************************************************
+
+import Demo.*;
+
+public class Server extends Ice.Application
+{
+ public int
+ run(String[] args)
+ {
+ if(args.length > 0)
+ {
+ System.err.println(appName() + ": too many arguments");
+ return 1;
+ }
+
+ Ice.ObjectAdapter adapter = communicator().createObjectAdapter("ContactDB");
+ adapter.add(new ContactDBI(), communicator().stringToIdentity("contactdb"));
+ adapter.activate();
+ communicator().waitForShutdown();
+ return 0;
+ }
+
+ public static void
+ main(String[] args)
+ {
+ Server app = new Server();
+ int status = app.main("Server", args, "config.server");
+ System.exit(status);
+ }
+}
diff --git a/java/demo/Ice/optional/build.xml b/java/demo/Ice/optional/build.xml
new file mode 100644
index 00000000000..3ab1752dc98
--- /dev/null
+++ b/java/demo/Ice/optional/build.xml
@@ -0,0 +1,44 @@
+<!--
+ **********************************************************************
+
+ Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
+
+ This copy of Ice is licensed to you under the terms described in the
+ ICE_LICENSE file included in this distribution.
+
+ **********************************************************************
+-->
+
+<project name="demo_Ice_optional" default="all" basedir=".">
+
+ <!-- set global properties for this build -->
+ <property name="top.dir" value="../../.."/>
+
+ <!-- import common definitions -->
+ <import file="${top.dir}/config/common.xml"/>
+
+ <target name="generate" depends="init">
+ <!-- Create the output directory for generated code -->
+ <mkdir dir="${generated.dir}"/>
+ <slice2java outputdir="${generated.dir}">
+ <fileset dir="." includes="Contact.ice"/>
+ </slice2java>
+ </target>
+
+ <target name="compile" depends="generate">
+ <mkdir dir="${class.dir}"/>
+ <javac srcdir=".:${generated.dir}" destdir="${class.dir}" debug="${debug}">
+ <exclude name="${generated.dir}/**"/>
+ <classpath refid="ice.classpath"/>
+ <compilerarg value="${javac.lint}"/>
+ </javac>
+ </target>
+
+ <target name="all" depends="compile"/>
+
+ <target name="clean">
+ <delete dir="${generated.dir}"/>
+ <delete dir="${class.dir}"/>
+ </target>
+
+</project>
diff --git a/java/demo/Ice/optional/config.client b/java/demo/Ice/optional/config.client
new file mode 100644
index 00000000000..596855e5e00
--- /dev/null
+++ b/java/demo/Ice/optional/config.client
@@ -0,0 +1,10 @@
+#
+# The client reads this property to create the reference to the
+# "ContactDB" object in the server.
+#
+ContactDB.Proxy=contactdb:default -p 10000 -h localhost
+
+#
+# Warn about connection exceptions
+#
+Ice.Warn.Connections=1
diff --git a/java/demo/Ice/optional/config.server b/java/demo/Ice/optional/config.server
new file mode 100644
index 00000000000..28294ccd18e
--- /dev/null
+++ b/java/demo/Ice/optional/config.server
@@ -0,0 +1,11 @@
+#
+# The server creates one single object adapter with the name
+# "ContactDB". The following line sets the endpoints for this
+# adapter.
+#
+ContactDB.Endpoints=default -p 10000 -h localhost
+
+#
+# Warn about connection exceptions
+#
+Ice.Warn.Connections=1
diff --git a/java/demo/Ice/optional/expect.py b/java/demo/Ice/optional/expect.py
new file mode 100755
index 00000000000..f7e128e4418
--- /dev/null
+++ b/java/demo/Ice/optional/expect.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# **********************************************************************
+#
+# Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
+#
+# This copy of Ice is licensed to you under the terms described in the
+# ICE_LICENSE file included in this distribution.
+#
+# **********************************************************************
+
+import sys, os
+
+path = [ ".", "..", "../..", "../../..", "../../../.." ]
+head = os.path.dirname(sys.argv[0])
+if len(head) > 0:
+ path = [os.path.join(head, p) for p in path]
+path = [os.path.abspath(p) for p in path if os.path.exists(os.path.join(p, "demoscript")) ]
+if len(path) == 0:
+ raise RuntimeError("can't find toplevel directory!")
+sys.path.append(path[0])
+
+from demoscript import Util
+from demoscript.Ice import optional
+
+server = Util.spawn('java Server --Ice.PrintAdapterReady --Ice.Warn.Connections=0')
+server.expect('.* ready')
+client = Util.spawn('java Client --Ice.Warn.Connections=0')
+
+optional.run(client, server)