) {
listener = new FilterListener(listener, eventNames);
}
// identify the wireContext
WireContext wireContext = null;
if (contextName!=null) {
Environment environment = targetWireContext.getEnvironment();
if (environment!=null) {
try {
wireContext = (WireContext) environment.getContext(contextName);
if (wireContext==null) {
throw new WireException("couldn't subscribe because context "+contextName+" doesn't exist");
}
} catch (ClassCastException e) {
throw new WireException("couldn't subscribe because context "+contextName+" is not a WireContext", e);
}
} else {
throw new WireException("couldn't get context "+contextName+" for subscribe because no environment available in context "+targetWireContext);
}
} else {
wireContext = targetWireContext;
}
if (wireEvents) {
WireDefinition wireDefinition = wireContext.getWireDefinition();
// if there are objectNames specified
if (objectNames!=null) {
// subscribe to the descriptors for the all objectNames
for (String objectName: objectNames) {
Descriptor descriptor = wireDefinition.getDescriptor(objectName);
subscribe(listener, descriptor);
}
// if no objectNames are specified, subscribe to all the descriptors
} else {
Set<Descriptor> descriptors = new HashSet<Descriptor>(wireDefinition.getDescriptors().values());
for(Descriptor descriptor: descriptors) {
subscribe(listener, descriptor);
}
}
} else if ( (objectNames!=null)
&& (!objectNames.isEmpty())
) {
// for every objectName
for (String objectName: objectNames) {
// subscribe to the objects themselves
Object object = wireContext.get(objectName);
if (object==null) {
throw new WireException("couldn't subscribe to object in context "+wireContext.getName()+": object "+objectName+" unavailable");
}
if (! (object instanceof Observable)) {
throw new WireException("couldn't subscribe to object in context "+wireContext.getName()+": object "+objectName+" ("+object.getClass().getName()+") isn't "+Observable.class.getName());
}
subscribe(listener, (Observable)object);
}
} else {