View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package net.jini.lookup;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  import net.jini.core.lookup.ServiceItem;
23  import net.jini.core.lookup.ServiceRegistrar;
24  
25  /**
26   * Used in the LookupCache. For each LookupCache, there is a HashMap that
27   * maps ServiceId to a ServiceItemReg. The ServiceItemReg class helps track
28   * where the ServiceItem comes from.
29   */
30  final class ServiceItemReg {
31      /* Maps ServiceRegistrars to their latest registered item */
32      private final Map<ServiceRegistrar, ServiceItem> items;
33      /* The ServiceRegistrar currently being used to track changes */
34      private volatile ServiceRegistrar proxy;
35      /* Flag that indicates that the ServiceItem has been discarded. */
36      private volatile boolean bDiscarded;
37      /* The discovered service, prior to filtering. */
38      private volatile ServiceItem item;
39      /* The discovered service, after filtering. */
40      private volatile ServiceItem filteredItem;
41      /* Creates an instance of this class, and associates it with the given
42       * lookup service proxy.
43       */
44  
45      public ServiceItemReg(ServiceRegistrar proxy, ServiceItem item) {
46          this.bDiscarded = false;
47          items = new HashMap<ServiceRegistrar, ServiceItem>();
48          this.proxy = proxy;
49          items.put(proxy, item);
50          this.item = item;
51          filteredItem = null;
52      }
53  
54      /* Adds the given proxy to the 'proxy-to-item' map. This method is
55       * called by the newOldService method.  Returns false if the proxy is being used
56       * to track changes, true otherwise.
57       */
58      public boolean proxyNotUsedToTrackChange(ServiceRegistrar proxy, ServiceItem item) {
59          items.put(proxy, item);
60          return !proxy.equals(this.proxy);
61      }
62  
63      /**
64       * Replaces the proxy used to track change if the proxy passed in is non
65       * null, also replaces the ServiceItem.
66       *
67       * @param proxy replacement proxy
68       * @param item replacement item.
69       */
70      public void replaceProxyUsedToTrackChange(ServiceRegistrar proxy, ServiceItem item) {
71          if (proxy != null) {
72              this.proxy = proxy;
73          }
74          this.item = item;
75      }
76      /* Removes the given proxy from the 'proxy-to-item' map. This method
77       * is called from the lookup, handleMatchNoMatch methods and
78       * ProxyRegDropTask.  If this proxy was being used to track changes,
79       * then pick a new one and return its current item, else return null.
80       */
81  
82      public ServiceItem removeProxy(ServiceRegistrar proxy) {
83          items.remove(proxy);
84          if (proxy.equals(this.proxy)) {
85              if (items.isEmpty()) {
86                  this.proxy = null;
87              } else {
88                  Map.Entry ent = (Map.Entry) items.entrySet().iterator().next();
89                  this.proxy = (ServiceRegistrar) ent.getKey();
90                  return (ServiceItem) ent.getValue();
91              } //endif
92          } //endif
93          return null;
94      }
95      /* Determines if the 'proxy-to-item' map contains any mappings.
96       */
97  
98      public boolean hasNoProxys() {
99          return items.isEmpty();
100     }
101     /* Returns the flag indicating whether the ServiceItem is discarded. */
102 
103     public boolean isDiscarded() {
104         return bDiscarded;
105     }
106 
107     /* Discards if not discarded and returns true if successful */
108     public boolean discard() {
109         if (!bDiscarded) {
110             bDiscarded = true;
111             return true;
112         }
113         return false;
114     }
115 
116     /* Undiscards if discarded and returns true if successful */
117     public boolean unDiscard() {
118         if (bDiscarded) {
119             bDiscarded = false;
120             return true;
121         }
122         return false;
123     }
124 
125     /**
126      * @return the proxy
127      */
128     public ServiceRegistrar getProxy() {
129         return proxy;
130     }
131 
132     /**
133      * @return the filteredItem
134      */
135     public ServiceItem getFilteredItem() {
136         return filteredItem == null ? null : filteredItem.clone();
137     }
138 
139     /**
140      * @param filteredItem the filteredItem to set
141      */
142     public void setFilteredItem(ServiceItem filteredItem) {
143         this.filteredItem = filteredItem == null ? null : filteredItem.clone();
144     }
145 
146     /**
147      * @return the item
148      */
149     public ServiceItem getItem() {
150         return item;
151     }
152     
153 }