diff options
author | Benoit Foucher <benoit@zeroc.com> | 2013-01-18 03:01:16 -0800 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2013-01-18 03:01:16 -0800 |
commit | 9ddaa33a6e20f70b83b1350cfcacdd78d0097bf6 (patch) | |
tree | 2a8f7420f85c5d869a3020e5ff829b4ec3bb5218 | |
parent | Fixed IceGrid compilation with G++ 4.6 (diff) | |
download | ice-9ddaa33a6e20f70b83b1350cfcacdd78d0097bf6.tar.bz2 ice-9ddaa33a6e20f70b83b1350cfcacdd78d0097bf6.tar.xz ice-9ddaa33a6e20f70b83b1350cfcacdd78d0097bf6.zip |
Improved EJB demo to test user exceptions
-rw-r--r-- | java/test/ejb/build.xml | 16 | ||||
-rw-r--r-- | java/test/ejb/src/common/com/zeroc/ice/IceAdapter.java | 7 | ||||
-rw-r--r-- | java/test/ejb/src/ejb1/com/zeroc/ejb/Client.java | 13 | ||||
-rw-r--r-- | java/test/ejb/src/ejb1/com/zeroc/ejb/DatabaseI.java | 35 | ||||
-rw-r--r-- | java/test/ejb/src/ejb1/com/zeroc/ejb/DatabaseServer.java | 43 | ||||
-rw-r--r-- | java/test/ejb/src/ejb1/com/zeroc/ejb/Service.java | 5 | ||||
-rw-r--r-- | java/test/ejb/src/ejb1/com/zeroc/ejb/ServiceBean.java | 30 | ||||
-rw-r--r-- | java/test/ejb/src/ejb1/com/zeroc/ejb/ServiceI.java | 9 | ||||
-rw-r--r-- | java/test/ejb/src/ejb2/com/zeroc/ejb/Client.java | 15 | ||||
-rw-r--r-- | java/test/ejb/src/ejb2/com/zeroc/ejb/DatabaseI.java | 35 | ||||
-rw-r--r-- | java/test/ejb/src/ejb2/com/zeroc/ejb/DatabaseServer.java | 43 | ||||
-rw-r--r-- | java/test/ejb/src/ejb2/com/zeroc/ejb/Service.java | 5 | ||||
-rw-r--r-- | java/test/ejb/src/ejb2/com/zeroc/ejb/ServiceBean.java | 31 | ||||
-rw-r--r-- | java/test/ejb/src/ejb2/com/zeroc/ejb/ServiceI.java | 11 | ||||
-rw-r--r-- | java/test/ejb/src/slice/ejb1.ice | 16 | ||||
-rw-r--r-- | java/test/ejb/src/slice/ejb2.ice | 17 |
16 files changed, 294 insertions, 37 deletions
diff --git a/java/test/ejb/build.xml b/java/test/ejb/build.xml index 5d1805e0104..06909e5fa4c 100644 --- a/java/test/ejb/build.xml +++ b/java/test/ejb/build.xml @@ -123,6 +123,22 @@ </java> </target> + <target name="run.server1" depends="deploy"> + <java classname="com.zeroc.ejb.DatabaseServer" fork="yes" dir="."> + <classpath refid="ice.classpath" /> + <classpath path="${build.dir}/${ant.project.name}-common.jar"/> + <classpath path="${build.dir}/${ant.project.name}-ejb1.jar"/> + </java> + </target> + + <target name="run.server2" depends="deploy"> + <java classname="com.zeroc.ejb.DatabaseServer" fork="yes" dir="."> + <classpath refid="ice.classpath" /> + <classpath path="${build.dir}/${ant.project.name}-common.jar"/> + <classpath path="${build.dir}/${ant.project.name}-ejb2.jar"/> + </java> + </target> + <target name="clean.db"> <delete dir="${jboss.base}/data/hypersonic" /> </target> diff --git a/java/test/ejb/src/common/com/zeroc/ice/IceAdapter.java b/java/test/ejb/src/common/com/zeroc/ice/IceAdapter.java index 782337f0bc9..afd82bb0871 100644 --- a/java/test/ejb/src/common/com/zeroc/ice/IceAdapter.java +++ b/java/test/ejb/src/common/com/zeroc/ice/IceAdapter.java @@ -73,6 +73,13 @@ public class IceAdapter } } + synchronized static public Ice.ObjectPrx + stringToProxy(String str) + { + assert _objectAdapter != null; + return _objectAdapter.getCommunicator().stringToProxy(str); + } + static private Ice.ObjectAdapter _objectAdapter = null; static private int _servantCount; }; diff --git a/java/test/ejb/src/ejb1/com/zeroc/ejb/Client.java b/java/test/ejb/src/ejb1/com/zeroc/ejb/Client.java index 1ba69a9975c..7c9ae9ebf3b 100644 --- a/java/test/ejb/src/ejb1/com/zeroc/ejb/Client.java +++ b/java/test/ejb/src/ejb1/com/zeroc/ejb/Client.java @@ -38,12 +38,19 @@ public class Client extends Ice.Application System.out.flush(); String str = "ejb1/service:tcp -h localhost -p 10000"; ServicePrx proxy = ServicePrxHelper.checkedCast(communicator().stringToProxy(str)); - proxy.setAccount(new Account("id1")); - Account s = proxy.getAccount(); + proxy.addAccount(new Account("id1")); + Account s = proxy.getAccount("id1"); if(!s.id.equals("id1")) { - throw new RuntimeException("invalid value"); + throw new RuntimeException("invalid value: " + s.id); } + + s = proxy.getAccount("id2"); + if(!s.id.equals("id2")) + { + throw new RuntimeException("invalid value: " + s.id); + } + System.out.println("ok"); return 0; } diff --git a/java/test/ejb/src/ejb1/com/zeroc/ejb/DatabaseI.java b/java/test/ejb/src/ejb1/com/zeroc/ejb/DatabaseI.java new file mode 100644 index 00000000000..e45834d53eb --- /dev/null +++ b/java/test/ejb/src/ejb1/com/zeroc/ejb/DatabaseI.java @@ -0,0 +1,35 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +package com.zeroc.ejb; + +import com.zeroc.ice.Test.*; + +public class DatabaseI extends _DatabaseDisp +{ + public final Account + getAccount(String id, Ice.Current current) + throws AccountNotExistException + { + Account account = accounts.get(id); + if(account == null) + { + throw new AccountNotExistException(); + } + return account; + } + + public final void + addAccount(Account s, Ice.Current current) + { + accounts.put(s.id, s); + } + + private java.util.Map<String, Account> accounts = new java.util.HashMap<String, Account>(); +} diff --git a/java/test/ejb/src/ejb1/com/zeroc/ejb/DatabaseServer.java b/java/test/ejb/src/ejb1/com/zeroc/ejb/DatabaseServer.java new file mode 100644 index 00000000000..5924f38cfca --- /dev/null +++ b/java/test/ejb/src/ejb1/com/zeroc/ejb/DatabaseServer.java @@ -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. +// +// ********************************************************************** + +package com.zeroc.ejb; + +import com.zeroc.ice.Test.*; + +public class DatabaseServer 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().createObjectAdapterWithEndpoints("DB", "tcp -h localhost -p 10001"); + adapter.add(new DatabaseI(), communicator().stringToIdentity("db")); + adapter.activate(); + communicator().waitForShutdown(); + return 0; + } + + public static void + main(String[] args) + { + Ice.InitializationData initData = new Ice.InitializationData(); + initData.properties = Ice.Util.createProperties(); + initData.properties.setProperty("Ice.Default.Package", "com.zeroc.ice"); + + DatabaseServer app = new DatabaseServer(); + int status = app.main("Server", args, initData); + System.exit(status); + } +} diff --git a/java/test/ejb/src/ejb1/com/zeroc/ejb/Service.java b/java/test/ejb/src/ejb1/com/zeroc/ejb/Service.java index cef2e79dbaa..60a097a45e5 100644 --- a/java/test/ejb/src/ejb1/com/zeroc/ejb/Service.java +++ b/java/test/ejb/src/ejb1/com/zeroc/ejb/Service.java @@ -11,11 +11,12 @@ package com.zeroc.ejb; import javax.ejb.Local; import com.zeroc.ice.Test.Account; +import com.zeroc.ice.Test.AccountNotExistException; @Local public interface Service { - Account getAccount(); + Account getAccount(String id); - void setAccount(Account s); + void addAccount(Account s); } diff --git a/java/test/ejb/src/ejb1/com/zeroc/ejb/ServiceBean.java b/java/test/ejb/src/ejb1/com/zeroc/ejb/ServiceBean.java index 66093514cec..4dd5642817c 100644 --- a/java/test/ejb/src/ejb1/com/zeroc/ejb/ServiceBean.java +++ b/java/test/ejb/src/ejb1/com/zeroc/ejb/ServiceBean.java @@ -11,22 +11,40 @@ package com.zeroc.ejb; import javax.annotation.*; import javax.ejb.*; -import com.zeroc.ice.Test.Account; +import com.zeroc.ice.IceAdapter; +import com.zeroc.ice.Test.*; @Stateless(name="EJB1ServiceBean") public class ServiceBean implements Service { + @PostConstruct + public void + create() + { + Ice.ObjectPrx db = IceAdapter.stringToProxy("db:tcp -h localhost -p 10001"); + database = DatabasePrxHelper.uncheckedCast(db); + } + public final Account - getAccount() + getAccount(String id) { - return this.account; + try + { + return database.getAccount(id); + } + catch(AccountNotExistException ex) + { + Account a = new Account(id); + database.addAccount(a); + return a; + } } public final void - setAccount(Account s) + addAccount(Account s) { - this.account = s; + database.addAccount(s); } - private Account account; + private DatabasePrx database; } diff --git a/java/test/ejb/src/ejb1/com/zeroc/ejb/ServiceI.java b/java/test/ejb/src/ejb1/com/zeroc/ejb/ServiceI.java index e5045a9a8dd..e5240ce1383 100644 --- a/java/test/ejb/src/ejb1/com/zeroc/ejb/ServiceI.java +++ b/java/test/ejb/src/ejb1/com/zeroc/ejb/ServiceI.java @@ -13,6 +13,7 @@ import javax.ejb.*; import com.zeroc.ice.Test._ServiceDisp; import com.zeroc.ice.Test.Account; +import com.zeroc.ice.Test.AccountNotExistException; // // This Ice servant delegates the calls to the Service EJB @@ -27,14 +28,14 @@ public class ServiceI extends _ServiceDisp } public final Account - getAccount(Ice.Current current) + getAccount(String id, Ice.Current current) { - return service.getAccount(); + return service.getAccount(id); } public final void - setAccount(Account s, Ice.Current current) + addAccount(Account s, Ice.Current current) { - service.setAccount(s); + service.addAccount(s); } }
\ No newline at end of file diff --git a/java/test/ejb/src/ejb2/com/zeroc/ejb/Client.java b/java/test/ejb/src/ejb2/com/zeroc/ejb/Client.java index 35016970473..95169cf8d02 100644 --- a/java/test/ejb/src/ejb2/com/zeroc/ejb/Client.java +++ b/java/test/ejb/src/ejb2/com/zeroc/ejb/Client.java @@ -38,12 +38,19 @@ public class Client extends Ice.Application System.out.flush(); String str = "ejb2/service:tcp -h localhost -p 10000"; ServicePrx proxy = ServicePrxHelper.checkedCast(communicator().stringToProxy(str)); - proxy.setAccount(new Account("id2", "foo")); - Account s = proxy.getAccount(); - if(!s.id.equals("id2") || !s.foo.equals("foo")) + proxy.addAccount(new Account("id1", "foo")); + Account s = proxy.getAccount("id1"); + if(!s.id.equals("id1") || !s.foo.equals("foo")) { - throw new RuntimeException("invalid value"); + throw new RuntimeException("invalid value: " + s.id); } + + s = proxy.getAccount("id2"); + if(!s.id.equals("id2")) + { + throw new RuntimeException("invalid value: " + s.id); + } + System.out.println("ok"); return 0; } diff --git a/java/test/ejb/src/ejb2/com/zeroc/ejb/DatabaseI.java b/java/test/ejb/src/ejb2/com/zeroc/ejb/DatabaseI.java new file mode 100644 index 00000000000..dd5f066a640 --- /dev/null +++ b/java/test/ejb/src/ejb2/com/zeroc/ejb/DatabaseI.java @@ -0,0 +1,35 @@ +// ********************************************************************** +// +// 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. +// +// ********************************************************************** + +package com.zeroc.ejb; + +import com.zeroc.ice.Test.*; + +public class DatabaseI extends _DatabaseDisp +{ + public final Account + getAccount(String id, Ice.Current current) + throws AccountNotExistException + { + Account account = accounts.get(id); + if(account == null) + { + throw new AccountNotExistException(id); + } + return account; + } + + public final void + addAccount(Account s, Ice.Current current) + { + accounts.put(s.id, s); + } + + private java.util.Map<String, Account> accounts = new java.util.HashMap<String, Account>(); +} diff --git a/java/test/ejb/src/ejb2/com/zeroc/ejb/DatabaseServer.java b/java/test/ejb/src/ejb2/com/zeroc/ejb/DatabaseServer.java new file mode 100644 index 00000000000..1052166594d --- /dev/null +++ b/java/test/ejb/src/ejb2/com/zeroc/ejb/DatabaseServer.java @@ -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. +// +// ********************************************************************** + +package com.zeroc.ejb; + +import com.zeroc.ice.Test.*; + +public class DatabaseServer 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().createObjectAdapterWithEndpoints("DB", "tcp -h localhost -p 10002"); + adapter.add(new DatabaseI(), communicator().stringToIdentity("db")); + adapter.activate(); + communicator().waitForShutdown(); + return 0; + } + + public static void + main(String[] args) + { + Ice.InitializationData initData = new Ice.InitializationData(); + initData.properties = Ice.Util.createProperties(); + initData.properties.setProperty("Ice.Default.Package", "com.zeroc.ice"); + + DatabaseServer app = new DatabaseServer(); + int status = app.main("Server", args, initData); + System.exit(status); + } +} diff --git a/java/test/ejb/src/ejb2/com/zeroc/ejb/Service.java b/java/test/ejb/src/ejb2/com/zeroc/ejb/Service.java index cef2e79dbaa..60a097a45e5 100644 --- a/java/test/ejb/src/ejb2/com/zeroc/ejb/Service.java +++ b/java/test/ejb/src/ejb2/com/zeroc/ejb/Service.java @@ -11,11 +11,12 @@ package com.zeroc.ejb; import javax.ejb.Local; import com.zeroc.ice.Test.Account; +import com.zeroc.ice.Test.AccountNotExistException; @Local public interface Service { - Account getAccount(); + Account getAccount(String id); - void setAccount(Account s); + void addAccount(Account s); } diff --git a/java/test/ejb/src/ejb2/com/zeroc/ejb/ServiceBean.java b/java/test/ejb/src/ejb2/com/zeroc/ejb/ServiceBean.java index b0a70043e3e..90e52894e90 100644 --- a/java/test/ejb/src/ejb2/com/zeroc/ejb/ServiceBean.java +++ b/java/test/ejb/src/ejb2/com/zeroc/ejb/ServiceBean.java @@ -11,23 +11,40 @@ package com.zeroc.ejb; import javax.annotation.*; import javax.ejb.*; - -import com.zeroc.ice.Test.Account; +import com.zeroc.ice.IceAdapter; +import com.zeroc.ice.Test.*; @Stateless(name="EJB2ServiceBean") public class ServiceBean implements Service { + @PostConstruct + public void + create() + { + Ice.ObjectPrx db = IceAdapter.stringToProxy("db:tcp -h localhost -p 10002"); + database = DatabasePrxHelper.uncheckedCast(db); + } + public final Account - getAccount() + getAccount(String id) { - return this.account; + try + { + return database.getAccount(id); + } + catch(AccountNotExistException ex) + { + Account a = new Account(id, ""); + database.addAccount(a); + return a; + } } public final void - setAccount(Account s) + addAccount(Account s) { - this.account = s; + database.addAccount(s); } - private Account account; + private DatabasePrx database; } diff --git a/java/test/ejb/src/ejb2/com/zeroc/ejb/ServiceI.java b/java/test/ejb/src/ejb2/com/zeroc/ejb/ServiceI.java index bac03e85847..e5240ce1383 100644 --- a/java/test/ejb/src/ejb2/com/zeroc/ejb/ServiceI.java +++ b/java/test/ejb/src/ejb2/com/zeroc/ejb/ServiceI.java @@ -11,8 +11,9 @@ package com.zeroc.ejb; import javax.ejb.*; -import com.zeroc.ice.Test.Account; import com.zeroc.ice.Test._ServiceDisp; +import com.zeroc.ice.Test.Account; +import com.zeroc.ice.Test.AccountNotExistException; // // This Ice servant delegates the calls to the Service EJB @@ -27,14 +28,14 @@ public class ServiceI extends _ServiceDisp } public final Account - getAccount(Ice.Current current) + getAccount(String id, Ice.Current current) { - return service.getAccount(); + return service.getAccount(id); } public final void - setAccount(Account s, Ice.Current current) + addAccount(Account s, Ice.Current current) { - service.setAccount(s); + service.addAccount(s); } }
\ No newline at end of file diff --git a/java/test/ejb/src/slice/ejb1.ice b/java/test/ejb/src/slice/ejb1.ice index 7dec291ba40..69924713d50 100644 --- a/java/test/ejb/src/slice/ejb1.ice +++ b/java/test/ejb/src/slice/ejb1.ice @@ -13,16 +13,28 @@ module Test { +exception AccountNotExistException +{ +}; + class Account extends Base { string id; }; +interface Database +{ + void addAccount(Account s); + + Account getAccount(string id) + throws AccountNotExistException; +}; + interface Service { - void setAccount(Account s); + void addAccount(Account s); - Account getAccount(); + Account getAccount(string id); }; }; diff --git a/java/test/ejb/src/slice/ejb2.ice b/java/test/ejb/src/slice/ejb2.ice index 4236a578713..b5d99acab5d 100644 --- a/java/test/ejb/src/slice/ejb2.ice +++ b/java/test/ejb/src/slice/ejb2.ice @@ -13,17 +13,30 @@ module Test { +exception AccountNotExistException +{ + string id; +}; + class Account extends Base { string id; string foo; }; +interface Database +{ + void addAccount(Account s); + + Account getAccount(string id) + throws AccountNotExistException; +}; + interface Service { - void setAccount(Account s); + void addAccount(Account s); - Account getAccount(); + Account getAccount(string id); }; }; |