Java Reference
In-Depth Information
Listing 10.2 OBR repository shell command example
public class RepositoryCommand extends BasicCommand {
public void exec(String args, PrintStream out, PrintStream err)
throws Exception {
args = args.trim();
RepositoryAdmin admin = getRepositoryAdmin();
if (admin != null) {
if ("list-urls".equalsIgnoreCase(args)) {
for (Repository repo : admin.listRepositories()) {
out.println(repo.getName() + " (" + repo.getURL() + ")");
}
} else if (args != null) {
if (args.startsWith("add-url")) {
admin.addRepository(
new URL(args.substring("add-url".length())));
} else if (args.startsWith("remove-url")) {
admin.removeRepository(
new URL(args.substring("remove-url".length())));
} else if (args.startsWith("list")) {
String query = (args.equals("list"))
? "(symbolicname=*)"
: args.substring("list".length()).trim();
for (Resource res : admin.discoverResources(query)) {
out.println(res.getPresentationName() + " ("
+ res.getSymbolicName() + ") " + res.getVersion());
}
}
} else {
out.println(
"Unknown command - use {list-urls|add-url|remove-url|list}");
}
} else {
out.println("No RepositoryAdmin service found...");
}
}
B
Lists
repositories
D
Removes
repository
E
Discovers
resources
private RepositoryAdmin getRepositoryAdmin() {
...
}
}
The
obr-repo
command has the following subcommands:
list-url
,
add-url
,
remove-url
, and
list
. A
RepositoryAdmin
provides access to a number of reposito-
ries referenced by
URL
s. You implement the
list-url
subcommand
B
to list these
repositories by retrieving the
RepositoryAdmin
service and calling its
listReposito-
ries()
method, which gives you access to the associated
Repository
objects. In this
case, you loop through the repositories and print their names and
URL
s.
You can add or remove repository
URL
s with the
add-url
and
remove-url
subcom-
mands, respectively. As you can see at
C
and
D
, there's a one-to-one mapping to the
addRepository()
and
removeRepository()
methods of the
RepositoryAdmin
service.
Finally, the
list
subcommand expects an
LDAP
query which it passes to
discover-
Repositories()
to discover resources
E
. If no query is specified, all resources are