Skip to content

Commit c1a782e

Browse files
Finished the filesystem demo.
1 parent 10faedf commit c1a782e

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

java/Ice/filesystem/client/src/main/java/com/example/ice/filesystem/client/Client.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package com.example.ice.filesystem.client;
44

55
import com.example.filesystem.DirectoryPrx;
6+
import com.example.filesystem.FilePrx;
7+
import com.example.filesystem.NodePrx;
68
import com.example.filesystem.WriteException;
79

810
import com.zeroc.Ice.ClassSliceLoader;
@@ -32,11 +34,35 @@ public static void main(String[] args) {
3234

3335
/**
3436
* Recursively prints the contents of a directory in tree fashion. For files, show the contents of each file.
37+
*
3538
* @param dir the directory to list
3639
* @param depth the current nesting level (for indentation)
3740
*/
38-
private static void listRecursive(DirectoryPrx dir, int depth)
39-
{
41+
private static void listRecursive(DirectoryPrx dir, int depth) {
42+
String indent = "\t".repeat(++depth);
4043

44+
// The node proxies returned by list() are never null.
45+
NodePrx[] contents = dir.list();
46+
47+
for (NodePrx node : contents) {
48+
// Check if this node is a directory by asking the remote object.
49+
DirectoryPrx subdir = DirectoryPrx.checkedCast(node);
50+
51+
// We assume it's a file if it's not a directory.
52+
String kind = (subdir != null ? "(directory)" : "(file)");
53+
String name = node.name();
54+
55+
System.out.println(indent + name + " " + kind);
56+
57+
if (subdir != null) {
58+
listRecursive(subdir, depth);
59+
} else {
60+
FilePrx file = FilePrx.uncheckedCast(node);
61+
String[] lines = file.read();
62+
for (String line : lines) {
63+
System.out.println(indent + '\t' + line);
64+
}
65+
}
66+
}
4167
}
4268
}

java/Ice/filesystem/server/src/main/java/com/example/ice/filesystem/server/MDirectory.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,24 @@ public class MDirectory extends MNode implements Directory {
1818

1919
/**
2020
* Constructs an {@code MDirectory}.
21+
*
2122
* @param name the name of this directory
2223
*/
23-
MDirectory(String name)
24-
{
24+
MDirectory(String name) {
2525
super(name);
2626
}
2727

2828
@Override
29-
public List<NodePrx> list(Current current)
30-
{
31-
return _contents;
29+
public NodePrx[] list(Current current) {
30+
return _contents.toArray(new NodePrx[0]);
3231
}
3332

3433
/**
35-
* Adds a node to this directory.
34+
* Adds a node to this directory
35+
*
3636
* @param child the node proxy to add
3737
*/
38-
void addChild(NodePrx child)
39-
{
38+
void addChild(NodePrx child) {
4039
_contents.add(child);
4140
}
4241
}

java/Ice/filesystem/server/src/main/java/com/example/ice/filesystem/server/MFile.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,29 @@ public class MFile extends MNode implements File {
1515

1616
/**
1717
* Constructs an {@code MFile}.
18+
*
1819
* @param name the name of this file
1920
*/
20-
MFile(String name)
21-
{
21+
MFile(String name) {
2222
super(name);
2323
}
2424

2525
@Override
26-
public String[] read(Current current)
27-
{
26+
public String[] read(Current current) {
2827
return _lines;
2928
}
3029

3130
@Override
32-
public void write(String[] text, Current current) throws WriteException
33-
{
31+
public void write(String[] text, Current current) throws WriteException {
3432
writeDirect(text);
3533
}
3634

3735
/**
3836
* Writes directly to this file, without going through an Ice operation.
37+
*
3938
* @param text the text to write
4039
*/
41-
void writeDirect(String[] text)
42-
{
40+
void writeDirect(String[] text) {
4341
_lines = text;
4442
}
4543
}

java/Ice/filesystem/server/src/main/java/com/example/ice/filesystem/server/MNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public class MNode implements Node {
1414

1515
/**
1616
* Constructs an {@code MNode}.
17+
*
1718
* @param name the name of this node
1819
*/
19-
MNode(String name)
20-
{
20+
MNode(String name) {
2121
_name = name;
2222
}
2323

java/Ice/filesystem/settings.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ dependencyResolutionManagement {
1919
rootProject.name = "filesystem"
2020
include("client")
2121
include("server")
22-
include("serveramd")
2322

2423
includeBuild("../../build-logic")

0 commit comments

Comments
 (0)