/** * {@inheritDoc} */ @Override protected Object clone() throws CloneNotSupportedException { return new DNSCache(this); }
/** * {@inheritDoc} */ @Override public List<? extends DNSEntry> put(String key, List<? extends DNSEntry> value) { synchronized (this) { List<? extends DNSEntry> oldValue = null; Map.Entry<String, List<? extends DNSEntry>> oldEntry = this.getEntry(key); if (oldEntry != null) { oldValue = oldEntry.setValue(value); } else { this.entrySet().add(new _CacheEntry(key, value)); } return oldValue; } }
/** * Iterate only over items with matching name. Returns an list of DNSEntry or null. To retrieve all entries, one must iterate over this linked list. * * @param name * @return list of DNSEntries */ public synchronized Collection<? extends DNSEntry> getDNSEntryList(String name) { return this.get(name != null ? name.toLowerCase() : null); }
/** * Adds an entry to the table. * * @param dnsEntry * @return true if the entry was added */ public boolean addDNSEntry(final DNSEntry dnsEntry) { boolean result = false; if (dnsEntry != null) { List<DNSEntry> entryList = this.get(dnsEntry.getKey()); if (entryList == null) { this.putIfAbsent(dnsEntry.getKey(), new ArrayList<DNSEntry>()); entryList = this.get(dnsEntry.getKey()); } synchronized (entryList) { entryList.add(dnsEntry); } // This is probably not very informative result = true; } return result; }
final DNSRecord cachedRecord = (DNSRecord) this.getCache().getDNSEntry(newRecord); logger.debug("{} handle response cached record: {}", this.getName(), cachedRecord); for (DNSEntry entry : this.getCache().getDNSEntryList(newRecord.getKey())) { if ( newRecord.getRecordType().equals(entry.getRecordType()) && newRecord.getRecordClass().equals(entry.getRecordClass()) && cacheOperation = Operation.Remove; logger.trace("Record is expired - removeDNSEntry() on:\n\t{}", cachedRecord); this.getCache().removeDNSEntry(cachedRecord); cacheOperation = Operation.Update; logger.trace("Record (singleValued) has changed - replaceDNSEntry() on:\n\t{}\n\t{}", newRecord, cachedRecord); this.getCache().replaceDNSEntry(newRecord, cachedRecord); } else { this.getCache().addDNSEntry(newRecord); cacheOperation = Operation.Add; logger.trace("Record not cached - addDNSEntry on:\n\t{}", newRecord); this.getCache().addDNSEntry(newRecord);
/** * Get all matching DNS entries from the table. * * @param name * @param type * @param recordClass * @return list of entries */ public synchronized Collection<? extends DNSEntry> getDNSEntryList(String name, DNSRecordType type, DNSRecordClass recordClass) { Collection<? extends DNSEntry> entryList = this.getDNSEntryList(name); if (entryList != null) { entryList = new ArrayList<DNSEntry>(entryList); for (Iterator<? extends DNSEntry> i = entryList.iterator(); i.hasNext();) { DNSEntry testDNSEntry = i.next(); if (!testDNSEntry.getRecordType().equals(type) || ((DNSRecordClass.CLASS_ANY != recordClass) && !testDNSEntry.getRecordClass().equals(recordClass))) { i.remove(); } } } else { entryList = Collections.emptyList(); } return entryList; }
public void cleanCache() { long now = System.currentTimeMillis(); for (DNSEntry entry : this.getCache().allValues()) { try { DNSRecord record = (DNSRecord) entry; if (record.isExpired(now)) { this.updateRecord(now, record, Operation.Remove); this.getCache().removeDNSEntry(record); } else if (record.isStale(now)) { // we should query for the record we care about i.e. those in the service collectors this.renewServiceCollector(record); } } catch (Exception exception) { logger.log(Level.SEVERE, this.getName() + ".Error while reaping records: " + entry, exception); logger.severe(this.toString()); } } }
/** * Does the necessary actions, when this as a response. */ @Override boolean handleResponse(JmDNSImpl dns) { if (dns.getLocalHost().conflictWithRecord(this)) { logger1.debug("handleResponse() Denial detected"); if (dns.isProbing()) { dns.getLocalHost().incrementHostName(); dns.getCache().clear(); for (ServiceInfo serviceInfo : dns.getServices().values()) { ServiceInfoImpl info = (ServiceInfoImpl) serviceInfo; info.revertState(); } } dns.revertState(); return true; } return false; }
this.getCache().logCachedContent(); for (final DNSEntry entry : this.getCache().allValues()) { try { final DNSRecord record = (DNSRecord) entry; this.updateRecord(now, record, Operation.Remove); logger.trace("Removing DNSEntry from cache: {}", entry); this.getCache().removeDNSEntry(record); } else if (record.isStaleAndShouldBeRefreshed(now)) { record.incrementRefreshPercentage();
@Override protected DNSOutgoing addAnswers(DNSOutgoing out) throws IOException { DNSOutgoing newOut = out; if (!_info.hasData()) { long now = System.currentTimeMillis(); newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getQualifiedName(), DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_IN), now); newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getQualifiedName(), DNSRecordType.TYPE_TXT, DNSRecordClass.CLASS_IN), now); if (_info.getServer().length() > 0) { for (DNSEntry addressEntry : this.getDns().getCache().getDNSEntryList(_info.getServer(), DNSRecordType.TYPE_A, DNSRecordClass.CLASS_IN)) { newOut = this.addAnswer(newOut, (DNSRecord) addressEntry, now); } for (DNSEntry addressEntry : this.getDns().getCache().getDNSEntryList(_info.getServer(), DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_IN)) { newOut = this.addAnswer(newOut, (DNSRecord) addressEntry, now); } } } return newOut; }
/** * {@inheritDoc} */ @Override public synchronized String toString() { StringBuffer aLog = new StringBuffer(2000); aLog.append("\t---- cache ----"); for (Map.Entry<String, List<? extends DNSEntry>> entry : this.entrySet()) { aLog.append("\n\t\t"); aLog.append(entry.toString()); } return aLog.toString(); }
@Override protected DNSOutgoing addAnswers(DNSOutgoing out) throws IOException { DNSOutgoing newOut = out; if (!_info.hasData()) { long now = System.currentTimeMillis(); newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getQualifiedName(), DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_IN), now); newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getQualifiedName(), DNSRecordType.TYPE_TXT, DNSRecordClass.CLASS_IN), now); if (_info.getServer().length() > 0) { newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getServer(), DNSRecordType.TYPE_A, DNSRecordClass.CLASS_IN), now); newOut = this.addAnswer(newOut, (DNSRecord) this.getDns().getCache().getDNSEntry(_info.getServer(), DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_IN), now); } } return newOut; }
Collection<DNSEntry> dnsEntryLits = this.getCache().allValues(); for (DNSEntry entry : dnsEntryLits) { final DNSRecord record = (DNSRecord) entry;
final DNSRecord cachedRecord = (DNSRecord) this.getCache().getDNSEntry(newRecord); if (logger.isLoggable(Level.FINE)) { logger.fine(this.getName() + " handle response cached record: " + cachedRecord); for (DNSEntry entry : this.getCache().getDNSEntryList(newRecord.getKey())) { if (newRecord.getRecordType().equals(entry.getRecordType()) && newRecord.getRecordClass().equals(entry.getRecordClass()) && (entry != cachedRecord)) { ((DNSRecord) entry).setWillExpireSoon(now); this.getCache().removeDNSEntry(cachedRecord); if (newRecord.isSingleValued()) { cacheOperation = Operation.Update; this.getCache().replaceDNSEntry(newRecord, cachedRecord); } else { this.getCache().addDNSEntry(newRecord); if (!expired) { cacheOperation = Operation.Add; this.getCache().addDNSEntry(newRecord);
/** * Get a matching DNS entry from the table (using isSameEntry). Returns the entry that was found. * * @param dnsEntry * @return DNSEntry */ public synchronized DNSEntry getDNSEntry(DNSEntry dnsEntry) { DNSEntry result = null; if (dnsEntry != null) { Collection<? extends DNSEntry> entryList = this.getDNSEntryList(dnsEntry.getKey()); if (entryList != null) { for (DNSEntry testDNSEntry : entryList) { if (testDNSEntry.isSameEntry(dnsEntry)) { result = testDNSEntry; break; } } } } return result; }
public void cleanCache() { long now = System.currentTimeMillis(); for (DNSEntry entry : this.getCache().allValues()) { try { DNSRecord record = (DNSRecord) entry; if (record.isExpired(now)) { this.updateRecord(now, record, Operation.Remove); this.getCache().removeDNSEntry(record); } else if (record.isStale(now)) { // we should query for the record we care about i.e. those in the service collectors this.renewServiceCollector(record); } } catch (Exception exception) { logger.log(Level.SEVERE, this.getName() + ".Error while reaping records: " + entry, exception); logger.severe(this.toString()); } } }
/** * Does the necessary actions, when this as a response. */ @Override boolean handleResponse(JmDNSImpl dns) { if (dns.getLocalHost().conflictWithRecord(this)) { logger1.finer("handleResponse() Denial detected"); if (dns.isProbing()) { dns.getLocalHost().incrementHostName(); dns.getCache().clear(); for (ServiceInfo serviceInfo : dns.getServices().values()) { ServiceInfoImpl info = (ServiceInfoImpl) serviceInfo; info.revertState(); } } dns.revertState(); return true; } return false; }
ServiceInfoImpl getServiceInfoFromCache(String type, String name, String subtype, boolean persistent) { DNSEntry pointerEntry = this.getCache().getDNSEntry(new DNSRecord.Pointer(type, DNSRecordClass.CLASS_ANY, false, 0, info.getQualifiedName())); if (pointerEntry instanceof DNSRecord) { ServiceInfoImpl cachedInfo = (ServiceInfoImpl) ((DNSRecord) pointerEntry).getServiceInfo(persistent); byte[] srvBytes = null; String server = ""; DNSEntry serviceEntry = this.getCache().getDNSEntry(info.getQualifiedName(), DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_ANY); if (serviceEntry instanceof DNSRecord) { ServiceInfo cachedServiceEntryInfo = ((DNSRecord) serviceEntry).getServiceInfo(persistent); for (DNSEntry addressEntry : this.getCache().getDNSEntryList(server, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_ANY)) { if (addressEntry instanceof DNSRecord) { ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(persistent); for (DNSEntry addressEntry : this.getCache().getDNSEntryList(server, DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_ANY)) { if (addressEntry instanceof DNSRecord) { ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(persistent); DNSEntry textEntry = this.getCache().getDNSEntry(cachedInfo.getQualifiedName(), DNSRecordType.TYPE_TXT, DNSRecordClass.CLASS_ANY); if (textEntry instanceof DNSRecord) { ServiceInfo cachedTextInfo = ((DNSRecord) textEntry).getServiceInfo(persistent);