private final class ServiceDiscoveryManager.LookupCacheImpl extends Object implements LookupCache
Modifier and Type | Class and Description |
---|---|
private class |
ServiceDiscoveryManager.LookupCacheImpl.DiscardServiceTask
Task class used to asynchronously notify service discard.
|
private class |
ServiceDiscoveryManager.LookupCacheImpl.LookupListener |
private class |
ServiceDiscoveryManager.LookupCacheImpl.LookupTask
This class requests a "snapshot" of the given registrar's state.
|
private class |
ServiceDiscoveryManager.LookupCacheImpl.NewOldServiceTask
Task class used to asynchronously process the service state
("snapshot"), matching this cache's template, that was retrieved
from the given lookup service.
|
private class |
ServiceDiscoveryManager.LookupCacheImpl.NotifyEventTask
Task class used to asynchronously notify all registered service
discovery listeners of serviceAdded/serviceRemoved/serviceChanged
events.
|
private class |
ServiceDiscoveryManager.LookupCacheImpl.ProxyRegDropTask
When the given registrar is discarded, this Task class is used to
remove the registrar from the various maps maintained by this
cache.
|
private class |
ServiceDiscoveryManager.LookupCacheImpl.RegisterListenerTask
This task class, when executed, first registers to receive
ServiceEvents from the given ServiceRegistrar.
|
private class |
ServiceDiscoveryManager.LookupCacheImpl.ServiceDiscardTimerTask
Task class used to determine whether or not to "commit" a service
discard request, increasing the chances that the service will
eventually be re-discovered.
|
private class |
ServiceDiscoveryManager.LookupCacheImpl.UnmapProxyTask
Task class used to asynchronously disassociate the given lookup
service proxy from the given ServiceItemReg.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
bCacheTerminated |
private TaskManager |
cacheTaskMgr
Task manager for the various tasks executed by this LookupCache
|
private HashMap |
eventRegMap |
private ServiceItemFilter |
filter |
private static int |
ITEM_ADDED |
private static int |
ITEM_CHANGED |
private static int |
ITEM_REMOVED |
private long |
leaseDuration |
private ServiceDiscoveryManager.LookupCacheImpl.LookupListener |
lookupListener |
private Exporter |
lookupListenerExporter |
private RemoteEventListener |
lookupListenerProxy |
private Object |
serviceDiscardMutex |
private TaskManager |
serviceDiscardTimerTaskMgr
For tasks waiting on verification events after service discard
|
private HashMap |
serviceIdMap |
private ArrayList |
sItemListeners |
private long |
startTime |
private long |
taskSeqN
Whenever a ServiceIdTask is created in this cache, it is assigned
a unique sequence number to allow such tasks associated with the
same ServiceID to be executed in the order in which they were
queued in the TaskManager.
|
private ServiceTemplate |
tmpl |
Constructor and Description |
---|
ServiceDiscoveryManager.LookupCacheImpl(ServiceTemplate tmpl,
ServiceItemFilter filter,
ServiceDiscoveryListener sListener,
long leaseDuration) |
Modifier and Type | Method and Description |
---|---|
private void |
addFilteredItemToMap(ServiceItem item,
ServiceItem filteredItem)
Convenience method called by
filterMaybeDiscard
and ServiceDiscardTimerTask.run that finds
the ServiceItemReg element in the
serviceIdMap that corresponds to the given
ServiceItem and, if such an element is found,
replaces the item field of that element with
the given item parameter; and sets the
filteredItem field of that element to the value
contained in the filteredItem parameter. |
void |
addListener(ServiceDiscoveryListener listener)
Registers a
ServiceDiscoveryListener object with
the event mechanism of a LookupCache . |
void |
addProxyReg(ServiceDiscoveryManager.ProxyReg reg)
Add a new ProxyReg to the lookupCache.
|
private void |
addServiceNotify(ServiceItem item)
Sends a notification to all listeners when a ServiceItem has
been added.
|
private void |
addServiceNotify(ServiceItem item,
ServiceDiscoveryListener srvcListener)
Sends a notification to the given listener when a ServiceItem has
been added.
|
private void |
cancelDiscardTask(ServiceID sid)
Wake up service discard task if running, else remove from mgr.
|
private void |
changeServiceNotify(ServiceItem newItem,
ServiceItem oldItem)
Sends a notification when a ServiceItem has been changed, but
still matches.
|
private void |
checkCacheTerminated() |
void |
discard(Object serviceReference)
Deletes a service reference from the cache and causes a notification
to be sent to all registered listeners indicating that the service
has been discarded.
|
private void |
discardRetryLater(ServiceItem item,
ServiceRegistrar proxy,
boolean sendEvent)
Convenience method called by
filterMaybeDiscard
that finds in the serviceIdMap , the
ServiceItemReg element corresponding to the
given ServiceItem , sets a service removed event,
and queues a ServiceDiscardTimerTask to retry the
filter at a later time. |
private ServiceItem |
filterMaybeDiscard(ServiceItem item,
ServiceRegistrar proxy,
boolean sendEvent)
Applies the first-stage
filter associated with
the current instance of LookupCache to the given
item and returns the resulting filtered item if
the filter is passed (or is null );
otherwise, returns null and sends a service removed
event if the sendEvent parameter is true . |
private ServiceItem |
findItem(ServiceID sid,
ServiceItem[] items)
Returns the element in the given items array having the given
ServiceID.
|
long |
getLeaseDuration()
Gets the remaining time left on the current cache's "lifespan".
|
private Iterator |
getServiceIdMapEntrySetIterator() |
private ServiceItem[] |
getServiceItems(ServiceItemFilter filter2)
This method returns a
ServiceItem array containing
elements that satisfy the following conditions:
- is referenced by one of the itemReg elements
contained in the serviceIdMap
- is not currently discarded
- satisfies the given ServiceItemFilter
Note that the filter parameter is a "2nd stage"
filter. |
private void |
handleMatchNoMatch(ServiceRegistrar proxy,
ServiceID srvcID,
ServiceItem item)
Convenience method called by
NotifyEventTask.run (only
when a TRANSITION_MATCH_NOMATCH event is received) that removes
the given item from the serviceIdMap
and wakes up the ServiceDiscardTimerTask if the given
item is discarded; otherwise, sends a removed event. |
private void |
initCache() |
private void |
itemMatchMatchChange(ServiceRegistrar proxy,
ServiceItem newItem,
ServiceDiscoveryManager.ServiceItemReg itemReg,
boolean matchMatchEvent)
With respect to a given service (referenced by both the parameter
newItem and the parameter itemReg), if either an event has been
received from the given lookup service (referenced by the proxy
parameter), or a snapshot of the given lookup service's state
has been retrieved, this method determines whether the service's
attributes have changed, or whether a new version of the service
has been registered.
|
ServiceItem |
lookup(ServiceItemFilter myFilter)
Finds a
ServiceItem object that satisfies the given
filter parameter. |
ServiceItem[] |
lookup(ServiceItemFilter myFilter,
int maxMatches)
Finds an array of instances of
ServiceItem that each
satisfy the given filter parameter. |
private void |
notifyServiceMap(Object eventSource,
long eventID,
long seqNo,
ServiceID sid,
ServiceItem item,
int transition)
Called by the lookupListener's notify() method.
|
void |
removeListener(ServiceDiscoveryListener listener)
Removes a
ServiceDiscoveryListener object from the set
of listeners currently registered with the LookupCache . |
void |
removeProxyReg(ServiceDiscoveryManager.ProxyReg reg)
Remove a ProxyReg from the lookupCache.
|
private void |
removeServiceIdMap(ServiceID sid,
ServiceItem item)
Removes an entry from the serviceIdMap, and sends a notification.
|
private void |
removeServiceIdMapSendNoEvent(ServiceID sid)
Removes an entry in the serviceIdMap, but sends no notification.
|
private void |
removeServiceNotify(ServiceItem item)
Sends a notification when a ServiceItem has been removed.
|
private void |
removeUselessTask(ServiceDiscoveryManager.ProxyReg reg)
Removes from the cache's task manager, all pending tasks
associated with the given ProxyReg.
|
private boolean |
sameVersion(ServiceItem item0,
ServiceItem item1)
Convenience method that performs a byte-wise comparison, including
codebases, of the services referenced by the given service items,
and returns the result.
|
private void |
serviceNotifyDo(ServiceItem oldItem,
ServiceItem item,
int action)
Common code for performing service notification to all listeners.
|
private void |
serviceNotifyDo(ServiceItem oldItem,
ServiceItem item,
int action,
ServiceDiscoveryListener sl)
Common code for performing service notification to one listener.
|
void |
terminate()
Performs cleanup duties related to the termination of
the processing being performed by a particular instance of
LookupCache . |
private void |
terminateTaskMgr(TaskManager taskMgr)
For the given TaskManager, this method removes all pending and
active tasks.
|
private static final int ITEM_ADDED
private static final int ITEM_REMOVED
private static final int ITEM_CHANGED
private ServiceDiscoveryManager.LookupCacheImpl.LookupListener lookupListener
private Exporter lookupListenerExporter
private RemoteEventListener lookupListenerProxy
private TaskManager cacheTaskMgr
private boolean bCacheTerminated
private final ArrayList sItemListeners
private final HashMap serviceIdMap
private final HashMap eventRegMap
private ServiceTemplate tmpl
private ServiceItemFilter filter
private long leaseDuration
private final long startTime
private TaskManager serviceDiscardTimerTaskMgr
private Object serviceDiscardMutex
private long taskSeqN
public ServiceDiscoveryManager.LookupCacheImpl(ServiceTemplate tmpl, ServiceItemFilter filter, ServiceDiscoveryListener sListener, long leaseDuration) throws RemoteException
RemoteException
public void terminate()
LookupCache
LookupCache
. For that instance, this method cancels
all event leases granted by the lookup services that supplied the
contents of the cache, and un-exports all remote listener objects
registered with those lookup services. The terminate
method is typically called when the entity is no longer interested
in the contents of the LookupCache
.terminate
in interface LookupCache
public ServiceItem lookup(ServiceItemFilter myFilter)
LookupCache
ServiceItem
object that satisfies the given
filter
parameter.
The service item returned must have been previously discovered to be both registered with one or more of the lookup services in the managed set, and to match criteria defined by the entity.
The semantics of the filter
argument are identical
to those of the filter
argument specified for a
number of the methods defined in the interface of the
ServiceDiscoveryManager
utility class. This argument
is intended to allow an entity to separate its filtering into two
steps: an initial filter applied during the discovery phase, and
a finer resolution filter applied upon retrieval from the cache.
As with the methods of the ServiceDiscoveryManager
, if
null
is the value of this argument, then no additional
filtering will be performed.
lookup
in interface LookupCache
myFilter
- used for matching ServiceItem
s. A null
value means no additional filtering should be applied.null
value will be returned if no
ServiceItem
is found that matches
the criteria or if the cache is empty.public ServiceItem[] lookup(ServiceItemFilter myFilter, int maxMatches)
LookupCache
ServiceItem
that each
satisfy the given filter
parameter.
Each service item contained in the returned array must have been previously discovered to be both registered with one or more of the lookup services in the managed set, and to match criteria defined by the entity.
The semantics of the filter
argument are
identical to those of the filter
argument specified
for a number of the methods defined in the interface of the
ServiceDiscoveryManager
utility class. This argument is
intended to allow an entity to separate its filtering into two
steps: an initial filter applied during the discovery phase, and
a finer resolution filter applied upon retrieval from the cache.
As with the methods of the ServiceDiscoveryManager
, if
null
is the value of this argument, then no
additional filtering will be performed.
lookup
in interface LookupCache
myFilter
- used for matching ServiceItem
s.
A null value means no additional filtering should
be applied.maxMatches
- maximum number of matches to return. If this
value is set to Integer.MAX_VALUE
then all elements in the cache that match the
criteria will be returned.ServiceItem
is found that
matches the criteria or if the cache is empty.public void discard(Object serviceReference)
LookupCache
discard
in interface LookupCache
serviceReference
- the service reference to discard.private Iterator getServiceIdMapEntrySetIterator()
private ServiceItem[] getServiceItems(ServiceItemFilter filter2)
ServiceItem
array containing
elements that satisfy the following conditions:
- is referenced by one of the itemReg
elements
contained in the serviceIdMap
- is not currently discarded
- satisfies the given ServiceItemFilter
Note that the filter
parameter is a "2nd stage"
filter. That is, for each itemReg
element in the
serviceIdMap
, the "1st stage" filter corresponding
to the current instance of LookupCache
has already
been applied to the ServiceItem
referenced in
that itemReg
. The ServiceItemFilter
applied here is supplied by the entity interacting with the cache,
and provides a second filtering process. Thus, this method
applies the given filter
parameter to the
filteredItem
field (not the item
field)
of each non-discarded itemReg
element in the
serviceIdMap
.
This method returns all the instances of ServiceItem
that pass the given filter
; and it discards all the
items that produce an indefinite result when that
filter
is applied.public void addListener(ServiceDiscoveryListener listener)
LookupCache
ServiceDiscoveryListener
object with
the event mechanism of a LookupCache
. The listener
object will receive a ServiceDiscoveryEvent
upon the
discovery, removal, or modification of one of the cache's
services. Once a listener is registered, it will be notified of
all service references discovered to date, and will be notified as
new services are discovered and existing services are modified or
discarded.
If the parameter value duplicates (using equals
) another
element in the set of listeners, no action is taken. If the parameter
value is null
, a NullPointerException
is
thrown.addListener
in interface LookupCache
listener
- the ServiceDiscoveryListener
object to
register.LookupCache.removeListener(net.jini.lookup.ServiceDiscoveryListener)
public void removeListener(ServiceDiscoveryListener listener)
LookupCache
ServiceDiscoveryListener
object from the set
of listeners currently registered with the LookupCache
.
Once all listeners are removed from the cache's set of listeners,
the cache will send no more ServiceDiscoveryEvent
notifications.
If the parameter value is null
, or if the parameter value
does not exist in the managed set of listeners, no action is taken.removeListener
in interface LookupCache
listener
- the ServiceDiscoveryListener
object to
remove.LookupCache.addListener(net.jini.lookup.ServiceDiscoveryListener)
public void addProxyReg(ServiceDiscoveryManager.ProxyReg reg)
reg
- a ProxyReg to add.public void removeProxyReg(ServiceDiscoveryManager.ProxyReg reg)
reg
- a ProxyReg to remove.private void checkCacheTerminated()
private void notifyServiceMap(Object eventSource, long eventID, long seqNo, ServiceID sid, ServiceItem item, int transition)
private void removeUselessTask(ServiceDiscoveryManager.ProxyReg reg)
private void terminateTaskMgr(TaskManager taskMgr)
private void removeServiceIdMap(ServiceID sid, ServiceItem item)
private void removeServiceIdMapSendNoEvent(ServiceID sid)
private ServiceItem findItem(ServiceID sid, ServiceItem[] items)
private void itemMatchMatchChange(ServiceRegistrar proxy, ServiceItem newItem, ServiceDiscoveryManager.ServiceItemReg itemReg, boolean matchMatchEvent)
private boolean sameVersion(ServiceItem item0, ServiceItem item1)
false
is returned.public long getLeaseDuration()
private void addServiceNotify(ServiceItem item)
private void addServiceNotify(ServiceItem item, ServiceDiscoveryListener srvcListener)
private void removeServiceNotify(ServiceItem item)
private void changeServiceNotify(ServiceItem newItem, ServiceItem oldItem)
private void serviceNotifyDo(ServiceItem oldItem, ServiceItem item, int action)
private void serviceNotifyDo(ServiceItem oldItem, ServiceItem item, int action, ServiceDiscoveryListener sl)
private void initCache() throws RemoteException
RemoteException
private ServiceItem filterMaybeDiscard(ServiceItem item, ServiceRegistrar proxy, boolean sendEvent)
filter
associated with
the current instance of LookupCache
to the given
item
and returns the resulting filtered item if
the filter
is passed (or is null
);
otherwise, returns null
and sends a service removed
event if the sendEvent
parameter is true
.
This method is called only when the item
to be
filtered corresponds to an element that currently exists in
the serviceIdMap
.
As described in the ServiceItemFilter
specification,
when the item
passes the filter
, the
service
field of the item
is replaced
with the filtered form of the object previously contained in
that field. In this case, the filteredItem
field
of the corresponding ServiceItemReg
element of the
serviceIdMap
is set to this new filtered item.
If the filter
returns indefinite
,
then that specification states that the service
field is replaced with null
. In this case, the
filteredItem
field of the corresponding
ServiceItemReg
element of the
serviceIdMap
is left unchanged.
private void addFilteredItemToMap(ServiceItem item, ServiceItem filteredItem)
filterMaybeDiscard
and ServiceDiscardTimerTask.run
that finds
the ServiceItemReg
element in the
serviceIdMap
that corresponds to the given
ServiceItem
and, if such an element is found,
replaces the item
field of that element with
the given item
parameter; and sets the
filteredItem
field of that element to the value
contained in the filteredItem
parameter.private void discardRetryLater(ServiceItem item, ServiceRegistrar proxy, boolean sendEvent)
filterMaybeDiscard
that finds in the serviceIdMap
, the
ServiceItemReg
element corresponding to the
given ServiceItem
, sets a service removed event,
and queues a ServiceDiscardTimerTask
to retry the
filter at a later time. If the serviceIdMap
does not
contain a ServiceItemReg
corresponding to the
given ServiceItem
, then this method simply returns.private void handleMatchNoMatch(ServiceRegistrar proxy, ServiceID srvcID, ServiceItem item)
NotifyEventTask.run
(only
when a TRANSITION_MATCH_NOMATCH event is received) that removes
the given item
from the serviceIdMap
and wakes up the ServiceDiscardTimerTask
if the given
item
is discarded; otherwise, sends a removed event.private void cancelDiscardTask(ServiceID sid)
Copyright 2007-2013, multiple authors.
Licensed under the Apache License, Version 2.0, see the NOTICE file for attributions.