diff options
Diffstat (limited to 'java')
-rw-r--r-- | java/demo/book/README | 5 | ||||
-rw-r--r-- | java/demo/book/build.xml | 2 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/Client.java | 154 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/DirectoryI.java | 258 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/FileI.java | 55 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/Filesystem.ice | 69 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/NodeFactory.java | 36 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/NodeInitializer.java | 26 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/PersistentFilesystem.ice | 34 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/Server.java | 84 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/build.xml | 55 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/config.client | 28 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/config.server | 27 | ||||
-rw-r--r-- | java/demo/book/freeze_filesystem/db/.dummy | 0 |
14 files changed, 833 insertions, 0 deletions
diff --git a/java/demo/book/README b/java/demo/book/README index c269a7ead90..9bc5e919521 100644 --- a/java/demo/book/README +++ b/java/demo/book/README @@ -15,3 +15,8 @@ Demos in this directory: An implementation of the simple (non-persistent, non-life-cycle) version of the filesystem example. + +- freeze_filesystem + + An implementation of the persistent version of the filesystem + example described in the Freeze chapter. diff --git a/java/demo/book/build.xml b/java/demo/book/build.xml index 232a77ab9b2..f67c27bccac 100644 --- a/java/demo/book/build.xml +++ b/java/demo/book/build.xml @@ -14,11 +14,13 @@ <target name="all"> <ant dir="printer"/> <ant dir="simple_filesystem"/> + <ant dir="freeze_filesystem"/> </target> <target name="clean"> <ant dir="printer" target="clean"/> <ant dir="simple_filesystem" target="clean"/> + <ant dir="freeze_filesystem" target="clean"/> </target> </project> diff --git a/java/demo/book/freeze_filesystem/Client.java b/java/demo/book/freeze_filesystem/Client.java new file mode 100644 index 00000000000..10f58722fb7 --- /dev/null +++ b/java/demo/book/freeze_filesystem/Client.java @@ -0,0 +1,154 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2007 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 Filesystem.*; + +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) + { + // + // Since this is an interactive demo we want to clear the + // Application installed interrupt callback and install our + // own shutdown hook. + // + setInterruptHook(new ShutdownHook()); + + // + // Create a proxy for the root directory. + // + DirectoryPrx rootDir = DirectoryPrxHelper.checkedCast(communicator().propertyToProxy("RootDir.Proxy")); + if(rootDir == null) + { + System.err.println("Client: invalid proxy"); + return 1; + } + + try + { + // + // Create a file called "README" in the root directory. + // + try + { + FilePrx readme = rootDir.createFile("README"); + String[] text = new String[1]; + text[0] = "This file system contains a collection of poetry."; + readme.write(text); + System.out.println("Created README."); + } + catch(NameInUse ex) + { + // + // Ignore - file already exists. + // + } + + // + // Create a directory called "Coleridge" in the root directory. + // + DirectoryPrx coleridge = null; + try + { + coleridge = rootDir.createDirectory("Coleridge"); + System.out.println("Created Coleridge."); + } + catch(NameInUse ex) + { + NodeDesc desc = rootDir.resolve("Coleridge"); + coleridge = DirectoryPrxHelper.checkedCast(desc.proxy); + assert(coleridge != null); + } + + // + // Create a file called "Kubla_Khan" in the Coleridge directory. + // + try + { + FilePrx file = coleridge.createFile("Kubla_Khan"); + String[] text = new String[5]; + text[0] = "In Xanadu did Kubla Khan"; + text[1] = "A stately pleasure-dome decree:"; + text[2] = "Where Alph, the sacred river, ran"; + text[3] = "Through caverns measureless to man"; + text[4] = "Down to a sunless sea."; + file.write(text); + System.out.println("Created Coleridge/Kubla_Khan."); + } + catch(NameInUse ex) + { + // + // Ignore - file already exists. + // + } + + System.out.println("Contents of filesystem:"); + java.util.Map contents = rootDir.list(ListMode.RecursiveList); + java.util.Iterator p = contents.keySet().iterator(); + while(p.hasNext()) + { + System.out.println(" " + (String)p.next()); + } + + NodeDesc desc = rootDir.resolve("Coleridge/Kubla_Khan"); + FilePrx file = FilePrxHelper.checkedCast(desc.proxy); + assert(file != null); + String[] text = file.read(); + System.out.println("Contents of file Coleridge/Kubla_Khan:"); + for(int i = 0; i < text.length; ++i) + { + System.out.println(" " + text[i]); + } + + // + // Destroy the filesystem. + // + contents = rootDir.list(ListMode.NormalList); + p = contents.entrySet().iterator(); + while(p.hasNext()) + { + java.util.Map.Entry e = (java.util.Map.Entry)p.next(); + NodeDesc d = (NodeDesc)e.getValue(); + System.out.println("Destroying " + (String)e.getKey() + "..."); + d.proxy.destroy(); + } + } + catch(Ice.UserException ex) + { + ex.printStackTrace(); + } + + 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/book/freeze_filesystem/DirectoryI.java b/java/demo/book/freeze_filesystem/DirectoryI.java new file mode 100644 index 00000000000..6e63d6edf27 --- /dev/null +++ b/java/demo/book/freeze_filesystem/DirectoryI.java @@ -0,0 +1,258 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2007 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 Filesystem.*; + +public final class DirectoryI extends PersistentDirectory +{ + public + DirectoryI() + { + _destroyed = false; + } + + public + DirectoryI(Ice.Identity id) + { + _ID = id; + nodes = new java.util.HashMap(); + _destroyed = false; + } + + public synchronized String + name(Ice.Current current) + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(current.id, current.facet, current.operation); + } + + return nodeName; + } + + public void + destroy(Ice.Current current) + throws PermissionDenied + { + if(parent == null) + { + throw new PermissionDenied("cannot remove root directory"); + } + + java.util.Map children = null; + + synchronized(this) + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(current.id, current.facet, current.operation); + } + + children = (java.util.Map)((java.util.HashMap)nodes).clone(); + _destroyed = true; + } + + // + // For consistency with C++, we iterate over the children outside of synchronization. + // + java.util.Iterator p = children.values().iterator(); + while(p.hasNext()) + { + NodeDesc desc = (NodeDesc)p.next(); + desc.proxy.destroy(); + } + + assert(nodes.isEmpty()); + + parent.removeNode(nodeName); + _evictor.remove(_ID); + } + + public java.util.Map + list(ListMode mode, Ice.Current current) + { + java.util.Map result = null; + synchronized(this) + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(current.id, current.facet, current.operation); + } + + result = (java.util.Map)((java.util.HashMap)nodes).clone(); + } + + if(mode == ListMode.RecursiveList) + { + java.util.Map children = new java.util.HashMap(); + java.util.Iterator p = result.entrySet().iterator(); + while(p.hasNext()) + { + java.util.Map.Entry e = (java.util.Map.Entry)p.next(); + NodeDesc desc = (NodeDesc)e.getValue(); + if(desc.type == NodeType.DirType) + { + DirectoryPrx dir = DirectoryPrxHelper.uncheckedCast(desc.proxy); + try + { + java.util.Map d = dir.list(mode); + java.util.Iterator q = d.entrySet().iterator(); + while(q.hasNext()) + { + java.util.Map.Entry e2 = (java.util.Map.Entry)q.next(); + NodeDesc desc2 = (NodeDesc)e2.getValue(); + children.put(desc.name + "/" + desc2.name, desc2); + } + } + catch(Ice.ObjectNotExistException ex) + { + // This node may have been destroyed, so skip it. + } + } + } + result.putAll(children); + } + + return result; + } + + public NodeDesc + resolve(String path, Ice.Current current) + throws NoSuchName + { + int pos = path.indexOf('/'); + String child, remainder = null; + if(pos == -1) + { + child = path; + } + else + { + child = path.substring(0, pos); + while(pos < path.length() && path.charAt(pos) == '/') + { + ++pos; + } + if(pos < path.length()) + { + remainder = path.substring(pos); + } + } + + synchronized(this) + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(current.id, current.facet, current.operation); + } + + if(!nodes.containsKey(child)) + { + throw new NoSuchName("no node exists with name `" + child + "'"); + } + + NodeDesc desc = (NodeDesc)nodes.get(child); + if(remainder == null) + { + return desc; + } + else + { + if(desc.type != NodeType.DirType) + { + throw new NoSuchName("node `" + child + "' is not a directory"); + } + DirectoryPrx dir = DirectoryPrxHelper.checkedCast(desc.proxy); + assert(dir != null); + return dir.resolve(remainder); + } + } + } + + public synchronized DirectoryPrx + createDirectory(String name, Ice.Current current) + throws IllegalName, + NameInUse + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(current.id, current.facet, current.operation); + } + + checkName(name); + + Ice.Identity id = current.adapter.getCommunicator().stringToIdentity(Ice.Util.generateUUID()); + PersistentDirectory dir = new DirectoryI(id); + dir.nodeName = name; + dir.parent = PersistentDirectoryPrxHelper.uncheckedCast(current.adapter.createProxy(current.id)); + DirectoryPrx proxy = DirectoryPrxHelper.uncheckedCast(_evictor.add(dir, id)); + + NodeDesc nd = new NodeDesc(); + nd.name = name; + nd.type = NodeType.DirType; + nd.proxy = proxy; + nodes.put(name, nd); + + return proxy; + } + + public synchronized FilePrx + createFile(String name, Ice.Current current) + throws IllegalName, + NameInUse + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(current.id, current.facet, current.operation); + } + + checkName(name); + + Ice.Identity id = current.adapter.getCommunicator().stringToIdentity(Ice.Util.generateUUID()); + PersistentFile file = new FileI(id); + file.nodeName = name; + file.parent = PersistentDirectoryPrxHelper.uncheckedCast(current.adapter.createProxy(current.id)); + FilePrx proxy = FilePrxHelper.uncheckedCast(_evictor.add(file, id)); + + NodeDesc nd = new NodeDesc(); + nd.name = name; + nd.type = NodeType.FileType; + nd.proxy = proxy; + nodes.put(name, nd); + + return proxy; + } + + public synchronized void + removeNode(String name, Ice.Current current) + { + assert(nodes.containsKey(name)); + nodes.remove(name); + } + + private + void checkName(String name) + throws IllegalName, NameInUse + { + if(name.length() == 0 || name.indexOf('/') >= 0) + { + throw new IllegalName("illegal name `" + name + "'"); + } + + if(nodes.containsKey(name)) + { + throw new NameInUse("name `" + name + "' is already in use"); + } + } + + public static Ice.ObjectAdapter _adapter; + public static Freeze.Evictor _evictor; + public Ice.Identity _ID; + private boolean _destroyed; +} diff --git a/java/demo/book/freeze_filesystem/FileI.java b/java/demo/book/freeze_filesystem/FileI.java new file mode 100644 index 00000000000..6a59838ec2c --- /dev/null +++ b/java/demo/book/freeze_filesystem/FileI.java @@ -0,0 +1,55 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2007 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 Filesystem.*; + +public final class FileI extends PersistentFile +{ + public + FileI() + { + } + + public + FileI(Ice.Identity id) + { + _ID = id; + } + + public String + name(Ice.Current current) + { + return nodeName; + } + + public void + destroy(Ice.Current current) + throws PermissionDenied + { + parent.removeNode(nodeName); + _evictor.remove(_ID); + } + + public synchronized String[] + read(Ice.Current current) + { + return (String[])text.clone(); + } + + public synchronized void + write(String[] text, Ice.Current current) + throws GenericError + { + this.text = text; + } + + public static Ice.ObjectAdapter _adapter; + public static Freeze.Evictor _evictor; + public Ice.Identity _ID; +} diff --git a/java/demo/book/freeze_filesystem/Filesystem.ice b/java/demo/book/freeze_filesystem/Filesystem.ice new file mode 100644 index 00000000000..61917b1e888 --- /dev/null +++ b/java/demo/book/freeze_filesystem/Filesystem.ice @@ -0,0 +1,69 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2007 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. +// +// ********************************************************************** + +module Filesystem +{ + exception GenericError + { + string reason; + }; + exception PermissionDenied extends GenericError {}; + exception NameInUse extends GenericError {}; + exception IllegalName extends GenericError {}; + exception NoSuchName extends GenericError {}; + + interface Node + { + idempotent string name(); + + ["freeze:write"] + void destroy() + throws PermissionDenied; + }; + + sequence<string> Lines; + + interface File extends Node + { + idempotent Lines read(); + + ["freeze:write"] + idempotent void write(Lines text) + throws GenericError; + }; + + enum NodeType { DirType, FileType }; + + struct NodeDesc + { + string name; + NodeType type; + Node* proxy; + }; + + dictionary<string, NodeDesc> NodeDict; + + enum ListMode { NormalList, RecursiveList }; + + interface Directory extends Node + { + idempotent NodeDict list(ListMode mode); + + idempotent NodeDesc resolve(string path) + throws NoSuchName; + + ["freeze:write"] + File* createFile(string name) + throws NameInUse, IllegalName; + + ["freeze:write"] + Directory* createDirectory(string name) + throws NameInUse, IllegalName; + }; +}; diff --git a/java/demo/book/freeze_filesystem/NodeFactory.java b/java/demo/book/freeze_filesystem/NodeFactory.java new file mode 100644 index 00000000000..5711943a9fe --- /dev/null +++ b/java/demo/book/freeze_filesystem/NodeFactory.java @@ -0,0 +1,36 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2007 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 Filesystem.*; + +public class NodeFactory extends Ice.LocalObjectImpl implements Ice.ObjectFactory +{ + public Ice.Object + create(String type) + { + if(type.equals("::Filesystem::PersistentFile")) + { + return new FileI(); + } + else if(type.equals("::Filesystem::PersistentDirectory")) + { + return new DirectoryI(); + } + else + { + assert(false); + return null; + } + } + + public void + destroy() + { + } +} diff --git a/java/demo/book/freeze_filesystem/NodeInitializer.java b/java/demo/book/freeze_filesystem/NodeInitializer.java new file mode 100644 index 00000000000..c246f6e2b09 --- /dev/null +++ b/java/demo/book/freeze_filesystem/NodeInitializer.java @@ -0,0 +1,26 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2007 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 Filesystem.*; + +public class NodeInitializer extends Ice.LocalObjectImpl implements Freeze.ServantInitializer +{ + public void + initialize(Ice.ObjectAdapter adapter, Ice.Identity id, String facet, Ice.Object obj) + { + if(obj instanceof FileI) + { + ((FileI)obj)._ID = id; + } + else if(obj instanceof DirectoryI) + { + ((DirectoryI)obj)._ID = id; + } + } +} diff --git a/java/demo/book/freeze_filesystem/PersistentFilesystem.ice b/java/demo/book/freeze_filesystem/PersistentFilesystem.ice new file mode 100644 index 00000000000..174ba7aec3d --- /dev/null +++ b/java/demo/book/freeze_filesystem/PersistentFilesystem.ice @@ -0,0 +1,34 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2007 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. +// +// ********************************************************************** + +#include <Filesystem.ice> + +module Filesystem +{ + class PersistentDirectory; + + class PersistentNode implements Node + { + string nodeName; + PersistentDirectory* parent; + }; + + class PersistentFile extends PersistentNode implements File + { + Lines text; + }; + + class PersistentDirectory extends PersistentNode implements Directory + { + ["freeze:write"] + void removeNode(string name); + + NodeDict nodes; + }; +}; diff --git a/java/demo/book/freeze_filesystem/Server.java b/java/demo/book/freeze_filesystem/Server.java new file mode 100644 index 00000000000..d0a21c6caa4 --- /dev/null +++ b/java/demo/book/freeze_filesystem/Server.java @@ -0,0 +1,84 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2007 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 Filesystem.*; + +public class Server extends Ice.Application +{ + public + Server(String envName) + { + _envName = envName; + } + + public int + run(String[] args) + { + // + // Install object factories. + // + Ice.ObjectFactory factory = new NodeFactory(); + communicator().addObjectFactory(factory, PersistentFile.ice_staticId()); + communicator().addObjectFactory(factory, PersistentDirectory.ice_staticId()); + + // + // Create an object adapter (stored in the _adapter + // static member). + // + Ice.ObjectAdapter adapter = + communicator().createObjectAdapterWithEndpoints("FreezeFilesystem", "default -p 10000"); + DirectoryI._adapter = adapter; + FileI._adapter = adapter; + + // + // Create the Freeze evictor (stored in the _evictor + // static member). + // + Freeze.ServantInitializer init = new NodeInitializer(); + Freeze.Evictor evictor = Freeze.Util.createEvictor(adapter, _envName, "evictorfs", init, null, true); + DirectoryI._evictor = evictor; + FileI._evictor = evictor; + + adapter.addServantLocator(evictor, ""); + + // + // Create the root node if it doesn't exist. + // + Ice.Identity rootId = Ice.Util.stringToIdentity("RootDir"); + if(!evictor.hasObject(rootId)) + { + PersistentDirectory root = new DirectoryI(rootId); + root.nodeName = "/"; + root.nodes = new java.util.HashMap(); + evictor.add(root, rootId); + } + + // + // Ready to accept requests now. + // + adapter.activate(); + + // + // Wait until we are done. + // + communicator().waitForShutdown(); + + return 0; + } + + public static void + main(String[] args) + { + Server app = new Server("db"); + int status = app.main("Server", args, "config.server"); + System.exit(status); + } + + private String _envName; +} diff --git a/java/demo/book/freeze_filesystem/build.xml b/java/demo/book/freeze_filesystem/build.xml new file mode 100644 index 00000000000..9882a3bde40 --- /dev/null +++ b/java/demo/book/freeze_filesystem/build.xml @@ -0,0 +1,55 @@ +<!-- + ********************************************************************** + + Copyright (c) 2003-2007 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_book_freeze_filesystem" 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}"> + <meta value="${java2metadata}"/> + <includepath> + <pathelement path="." /> + </includepath> + <fileset dir="." includes="Filesystem.ice"/> + <fileset dir="." includes="PersistentFilesystem.ice"/> + </slice2java> + </target> + + <target name="compile" depends="generate"> + <mkdir dir="${class.dir}"/> + <javac srcdir="${generated.dir}" destdir="${class.dir}" + source="${jdk.version}" debug="${debug}"> + <classpath refid="ice.classpath"/> + <compilerarg value="${javac.lint}" compiler="${javac.lint.compiler}"/> + </javac> + <javac srcdir="." destdir="${class.dir}" source="${jdk.version}" + excludes="generated/**" debug="${debug}"> + <classpath refid="ice.classpath"/> + <compilerarg value="${javac.lint}" compiler="${javac.lint.compiler}"/> + </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/book/freeze_filesystem/config.client b/java/demo/book/freeze_filesystem/config.client new file mode 100644 index 00000000000..4bd7574216d --- /dev/null +++ b/java/demo/book/freeze_filesystem/config.client @@ -0,0 +1,28 @@ +# +# The client reads this property to create the reference to the +# "library" object in the server. +# +RootDir.Proxy=RootDir:default -p 10000 + +# +# Warn about connection exceptions +# +Ice.Warn.Connections=1 + +# +# Network Tracing +# +# 0 = no network tracing +# 1 = trace connection establishment and closure +# 2 = like 1, but more detailed +# 3 = like 2, but also trace data transfer +# +#Ice.Trace.Network=1 + +# +# Protocol Tracing +# +# 0 = no protocol tracing +# 1 = trace protocol messages +# +#Ice.Trace.Protocol=1 diff --git a/java/demo/book/freeze_filesystem/config.server b/java/demo/book/freeze_filesystem/config.server new file mode 100644 index 00000000000..2370acc99a5 --- /dev/null +++ b/java/demo/book/freeze_filesystem/config.server @@ -0,0 +1,27 @@ +FreezeFilesystem.Endpoints=default -p 10000 + +Freeze.Trace.Map=1 +Freeze.Trace.Evictor=2 + +# +# Warn about connection exceptions +# +Ice.Warn.Connections=1 + +# +# Network Tracing +# +# 0 = no network tracing +# 1 = trace connection establishment and closure +# 2 = like 1, but more detailed +# 3 = like 2, but also trace data transfer +# +#Ice.Trace.Network=1 + +# +# Protocol Tracing +# +# 0 = no protocol tracing +# 1 = trace protocol messages +# +#Ice.Trace.Protocol=1 diff --git a/java/demo/book/freeze_filesystem/db/.dummy b/java/demo/book/freeze_filesystem/db/.dummy new file mode 100644 index 00000000000..e69de29bb2d --- /dev/null +++ b/java/demo/book/freeze_filesystem/db/.dummy |