...
Code Block | ||||
---|---|---|---|---|
| ||||
public interface CallBack {
void callMethod();
}
class UserLookupCallBack implements CallBack {
private int uid;
private String name;
public UserLookupCallBack(int uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void callMethod() {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
try (InputStream fis = new FileInputStream("/etc/passwd")) {
// Look up userid & assign to UserLookupCallBack.this.name
} catch (IOException x) {
UserLookupCallBack.this.name = null;
}
return null;
}
});
}
}
class Client {
CallBack callback;
public void registerCallBack(CallBack callback) {
this.callback = callback;
}
public void doSomething() {
callback.callMethod();
}
public static void main(String[] args) {
int uid = Integer.parseInt(args[0]);
Client client = new Client();
CallBack callBack = new UserLookupCallBack(uid);
client.registerCallBack(callBack);
// ...
client.doSomething(); // Looks up user name
System.out.println("User " + uid + " is named " + callBack.getName());
}
} |
...