blob: b665940d54135c4871b4e09b39c371ee6bf3f3b5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
// **********************************************************************
//
// Copyright (c) 2003-2009 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
{
// Recursively print the contents of directory "dir" in tree fashion.
// For files, show the contents of each file. The "depth"
// parameter is the current nesting level (for indentation).
static void
listRecursive(DirectoryPrx dir, int depth)
{
char[] indentCh = new char[++depth];
java.util.Arrays.fill(indentCh, '\t');
String indent = new String(indentCh);
NodeDesc[] contents = dir.list();
for (int i = 0; i < contents.length; ++i) {
DirectoryPrx subdir = DirectoryPrxHelper.checkedCast(contents[i].proxy);
FilePrx file = FilePrxHelper.uncheckedCast(contents[i].proxy);
System.out.println(indent + contents[i].name + (subdir != null ? " (directory):" : " (file):"));
if (subdir != null) {
listRecursive(subdir, depth);
} else {
String[] text = file.read();
for (int j = 0; j < text.length; ++j)
System.out.println(indent + "\t" + text[j]);
}
}
}
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.find("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:");
listRecursive(rootDir, 0);
//
// Destroy the filesystem.
//
NodeDesc[] contents = rootDir.list();
for(int i = 0; i < contents.length; ++i)
{
NodeDesc d = contents[i];
System.out.println("Destroying " + d.name);
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);
}
}
|