private static final class ServiceDiscoveryManager.LookupCacheImpl extends Object implements LookupCache
Modifier and Type | Class and Description |
---|---|
private class |
ServiceDiscoveryManager.LookupCacheImpl.LookupListener |
private static 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 static class |
ServiceDiscoveryManager.LookupCacheImpl.RegisterListenerTask
This task class, when executed, first registers to receive
ServiceEvents from the given ServiceRegistrar.
|
private static 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 static class |
ServiceDiscoveryManager.LookupCacheImpl.ServiceNotifyDo
Common code for performing service notification to one listener in
an executor task thread.
|
Constructor and Description |
---|
LookupCacheImpl(ServiceTemplate tmpl,
ServiceItemFilter filter,
ServiceDiscoveryListener sListener,
long leaseDuration,
ServiceDiscoveryManager sdm) |
Modifier and Type | Method and Description |
---|---|
private ServiceItem |
addFilteredItemToMap(ServiceItem item,
ServiceItem filteredItem,
ServiceDiscoveryManager.ServiceItemReg itemReg)
Convenience method called by
filterMaybeDiscard 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,
boolean sendEvent,
ServiceDiscoveryManager.ServiceItemReg itemReg)
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(ServiceID srvcID,
ServiceDiscoveryManager.ServiceItemReg itemReg,
ServiceItem item,
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<Map.Entry<ServiceID,ServiceDiscoveryManager.ServiceItemReg>> |
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)
Convenience method called (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(ServiceID srvcID,
ServiceDiscoveryManager.ServiceItemReg itemReg,
ServiceRegistrar proxy,
ServiceItem newItem,
boolean matchMatchEvent)
With respect to a given service (referenced by the parameter
newItem), 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.
|
private void |
lookup(ServiceDiscoveryManager.ProxyReg reg,
ServiceDiscoveryManager.EventReg eReg)
Requests a "snapshot" of the given registrar's state.
|
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 |
newOldService(ServiceDiscoveryManager.ProxyReg reg,
ServiceItem item,
boolean matchMatchEvent)
Method used to process the service state
("snapshot"), matching this cache's template, retrieved from
the given lookup service.
|
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 boolean |
removeServiceIdMapSendNoEvent(ServiceID sid,
ServiceDiscoveryManager.ServiceItemReg itemReg)
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 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.
|
void |
terminate()
Performs cleanup duties related to the termination of
the processing being performed by a particular instance of
LookupCache . |
private java.util.concurrent.ExecutorService eventNotificationExecutor
private static final int ITEM_ADDED
private static final int ITEM_REMOVED
private static final int ITEM_CHANGED
private final ServiceDiscoveryManager.LookupCacheImpl.LookupListener lookupListener
private volatile Exporter lookupListenerExporter
private volatile RemoteEventListener lookupListenerProxy
private volatile java.util.concurrent.ExecutorService cacheTaskMgr
private volatile ServiceDiscoveryManager.CacheTaskDependencyManager cacheTaskDepMgr
private volatile boolean bCacheTerminated
private final Collection<ServiceDiscoveryListener> sItemListeners
private final java.util.concurrent.ConcurrentMap<ServiceID,ServiceDiscoveryManager.ServiceItemReg> serviceIdMap
private final java.util.concurrent.ConcurrentMap<ServiceDiscoveryManager.ProxyReg,ServiceDiscoveryManager.EventReg> eventRegMap
private final ServiceTemplate tmpl
private final ServiceItemFilter filter
private final long leaseDuration
private final long startTime
private volatile java.util.concurrent.ExecutorService serviceDiscardTimerTaskMgr
private final java.util.concurrent.ConcurrentMap<ServiceID,java.util.concurrent.Future> serviceDiscardFutures
private final java.util.concurrent.atomic.AtomicLong taskSeqN
private final ServiceDiscoveryManager sdm
public LookupCacheImpl(ServiceTemplate tmpl, ServiceItemFilter filter, ServiceDiscoveryListener sListener, long leaseDuration, ServiceDiscoveryManager sdm) 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<Map.Entry<ServiceID,ServiceDiscoveryManager.ServiceItemReg>> 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 lookup(ServiceDiscoveryManager.ProxyReg reg, ServiceDiscoveryManager.EventReg eReg)
private void newOldService(ServiceDiscoveryManager.ProxyReg reg, ServiceItem item, boolean matchMatchEvent)
private boolean removeServiceIdMapSendNoEvent(ServiceID sid, ServiceDiscoveryManager.ServiceItemReg itemReg)
private ServiceItem findItem(ServiceID sid, ServiceItem[] items)
private void itemMatchMatchChange(ServiceID srvcID, ServiceDiscoveryManager.ServiceItemReg itemReg, ServiceRegistrar proxy, ServiceItem newItem, 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 initCache() throws RemoteException
RemoteException
private ServiceItem filterMaybeDiscard(ServiceID srvcID, ServiceDiscoveryManager.ServiceItemReg itemReg, ServiceItem item, 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 ServiceItem addFilteredItemToMap(ServiceItem item, ServiceItem filteredItem, ServiceDiscoveryManager.ServiceItemReg itemReg)
filterMaybeDiscard
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, boolean sendEvent, ServiceDiscoveryManager.ServiceItemReg itemReg)
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)
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.