public static void closeCache() { try { if (cache != null && !cache.isClosed()) { cache.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (ds != null) ds.disconnect(); } catch (Exception e) { getLogWriter().fine("Error in disconnecting from Distributed System"); } }
public static void runTest3() throws Exception { boolean exceptionOccured = false; try { Context ctx = cache.getJNDIContext(); DataSource ds1 = null; DataSource ds2 = null; ds1 = (DataSource) ctx.lookup("java:/XAPooledDataSource"); ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource"); ds2.getConnection(); UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction"); utx.begin(); utx.setTransactionTimeout(2); ds1.getConnection(); Thread.sleep(4000); try { utx.commit(); } catch (Exception e) { exceptionOccured = true; } if (!exceptionOccured) fail("Exception (Transaction-Time-Out)did not occur although was supposed" + "to occur"); } catch (Exception e) { fail("failed in runTest3 due to " + e); } }
@Test public void shouldCheckRepoExistenceBeforeTryingPurge() throws IOException, IllegalAccessException { factory.createSuiteTimeRepo("foo", LATEST_VERSION); Cache<EntryRepo> repos = (Cache<EntryRepo>) deref("cache", factory); List<String> keys = repos.keys(); assertThat(keys.size(), is(1)); String fooKey = keys.get(0); repos.clear(); try { factory.purge(fooKey); } catch (IOException e) { e.printStackTrace(); fail("Should not fail when trying to purge already purged entry"); } }
public static void runTest1() throws Exception { boolean exceptionOccured = false; try { Context ctx = cache.getJNDIContext(); DataSource ds1 = null; DataSource ds2 = null; ds1 = (DataSource) ctx.lookup("java:/XAPooledDataSource"); ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource"); ds2.getConnection(); ds1 = (DataSource) ctx.lookup("java:/XAPooledDataSource"); UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction"); utx.begin(); ds1.getConnection(); Thread.sleep(8000); try { utx.commit(); } catch (Exception e) { exceptionOccured = true; } if (!exceptionOccured) fail("Exception did not occur on commit although was supposed" + "occur"); } catch (Exception e) { getLogWriter().fine("Exception caught in runTest1 due to : " + e); fail("failed in runTest1 due to " + e); } }
public static void startCache() { try { if (cache == null || cache.isClosed()) { cache = CacheFactory.create(ds); } } catch (Exception e) { e.printStackTrace(); } }
float[] get_Q(int i, int len) { float[][] data = new float[1][]; int start; if((start = cache.get_data(i,data,len)) < len) { for(int j=start;j<len;j++) data[0][j] = (float)kernel_function(i,j); } return data[0]; }
private final void maybeAddRecord(Record record, Cache cache, Object source) throws IOException { int type = record.getType(); Name name = record.getName(); if (type == Type.SOA) { if (!name.equals(origin)) throw new IOException("SOA owner " + name + " does not match zone origin " + origin); else { setOrigin(origin); dclass = record.getDClass(); } } if (origin == null && type != Type.SOA) throw new IOException("non-SOA record seen at " + name + " with no origin set"); if (name.subdomain(origin)) addRecord(record); else if (cache != null) cache.addRecord(record, Credibility.GLUE, source); }
float[] get_Q(int i, int len) { float[][] data = new float[1][]; int real_i = index[i]; if(cache.get_data(real_i,data,l) < l) { for(int j=0;j<l;j++) data[0][j] = (float)kernel_function(real_i,j); } // reorder and copy float buf[] = buffer[next_buffer]; next_buffer = 1 - next_buffer; byte si = sign[i]; for(int j=0;j<len;j++) buf[j] = si * sign[j] * data[0][index[j]]; return buf; }
private void verifyRecords(Cache tcache) { Iterator it; it = tcache.names(); while (it.hasNext()) { Name name = (Name) it.next(); Object[] elements = findExactSets(name); for (int i = 0; i < elements.length; i++) { Element element = (Element) elements[i]; if (element instanceof PositiveElement) continue; RRset rrset = ((PositiveElement) element).rrset; /* for now, ignore negative cache entries */ if (rrset == null) continue; if (verifier != null) rrset.setSecurity(verifier.verify(rrset, this)); if (rrset.getSecurity() < DNSSEC.Secure) continue; addSet(name, rrset.getType(), element); } } }
/** * Creates a Zone by performing a zone transfer to the specified host. All records that do not * belong in the Zone are added to the specified Cache. * * @see Cache * @see Master */ public Zone(Name zone, short dclass, String remote, Cache cache) throws IOException { super(false); origin = zone; this.dclass = dclass; type = SECONDARY; Resolver res = new SimpleResolver(remote); Record rec = Record.newRecord(zone, Type.AXFR, dclass); Message query = Message.newQuery(rec); Message response = res.send(query); Record[] recs = response.getSectionArray(Section.ANSWER); for (int i = 0; i < recs.length; i++) { if (!recs[i].getName().subdomain(origin)) { if (Options.check("verbose")) System.err.println(recs[i].getName() + "is not in zone " + origin); continue; } addRecord(recs[i]); } if (cache != null) { recs = response.getSectionArray(Section.ADDITIONAL); for (int i = 0; i < recs.length; i++) cache.addRecord(recs[i], Credibility.GLUE, recs); } validate(); }
public static void runTest2() throws Exception { boolean exceptionOccured1 = false; boolean exceptionOccured2 = false; try { Context ctx = cache.getJNDIContext(); DataSource ds1 = null; DataSource ds2 = null; ds1 = (DataSource) ctx.lookup("java:/XAPooledDataSource"); ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource"); ds2.getConnection(); ds1.getConnection(); ds1.getConnection(); ds1.getConnection(); ds1.getConnection(); ds1.getConnection(); UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction"); utx.begin(); try { ds1.getConnection(); Thread.sleep(8000); } catch (SQLException e) { exceptionOccured1 = true; } try { utx.commit(); } catch (Exception e) { exceptionOccured2 = true; } if (!exceptionOccured1) fail("Exception (Login-Time-Out)did not occur although was supposed" + "to occur"); if (exceptionOccured2) fail("Exception did occur on commit, although was not supposed" + "to occur"); } catch (Exception e) { fail("failed in runTest2 due to " + e); } }
void swap_index(int i, int j) { cache.swap_index(i,j); super.swap_index(i,j); }
void swap_index(int i, int j) { cache.swap_index(i,j); super.swap_index(i,j); do {byte _=y[i]; y[i]=y[j]; y[j]=_;} while(false); }
/** * Adds all data from a Message into the Cache. Each record is added with the appropriate * credibility, and negative answers are cached as such. * * @param in The Message to be added * @see Message */ public void addMessage(Message in) { boolean isAuth = in.getHeader().getFlag(Flags.AA); Name qname = in.getQuestion().getName(); Name curname = qname; short qtype = in.getQuestion().getType(); short qclass = in.getQuestion().getDClass(); byte cred; short rcode = in.getHeader().getRcode(); boolean haveAnswer = false; boolean completed = false; boolean restart = false; RRset[] answers, auth, addl; if (rcode != Rcode.NOERROR && rcode != Rcode.NXDOMAIN) return; if (secure) { Cache c = new Cache(dclass); c.addMessage(in); verifyRecords(c); return; } answers = in.getSectionRRsets(Section.ANSWER); for (int i = 0; i < answers.length; i++) { if (answers[i].getDClass() != qclass) continue; short type = answers[i].getType(); Name name = answers[i].getName(); cred = getCred(Section.ANSWER, isAuth); if (type == Type.CNAME && name.equals(curname)) { CNAMERecord cname; addRRset(answers[i], cred); cname = (CNAMERecord) answers[i].first(); curname = cname.getTarget(); restart = true; haveAnswer = true; } else if (type == Type.DNAME && curname.subdomain(name)) { DNAMERecord dname; addRRset(answers[i], cred); dname = (DNAMERecord) answers[i].first(); try { curname = curname.fromDNAME(dname); } catch (NameTooLongException e) { break; } restart = true; haveAnswer = true; } else if ((type == qtype || qtype == Type.ANY) && name.equals(curname)) { addRRset(answers[i], cred); completed = true; haveAnswer = true; } if (restart) { restart = false; i = 0; } } auth = in.getSectionRRsets(Section.AUTHORITY); if (!completed) { /* This is a negative response or a referral. */ RRset soa = null, ns = null; for (int i = 0; i < auth.length; i++) { if (auth[i].getType() == Type.SOA && curname.subdomain(auth[i].getName())) soa = auth[i]; else if (auth[i].getType() == Type.NS && curname.subdomain(auth[i].getName())) ns = auth[i]; } short cachetype = (rcode == Rcode.NXDOMAIN) ? (short) 0 : qtype; if (soa != null || ns == null) { /* Negative response */ cred = getCred(Section.AUTHORITY, isAuth); SOARecord soarec = null; if (soa != null) soarec = (SOARecord) soa.first(); addNegative(curname, cachetype, soarec, cred); /* NXT records are not cached yet. */ } else { /* Referral response */ cred = getCred(Section.AUTHORITY, isAuth); addRRset(ns, cred); } } addl = in.getSectionRRsets(Section.ADDITIONAL); for (int i = 0; i < addl.length; i++) { short type = addl[i].getType(); if (type != Type.A && type != Type.AAAA && type != Type.A6) continue; /* XXX check the name */ Name name = addl[i].getName(); cred = getCred(Section.ADDITIONAL, isAuth); addRRset(addl[i], cred); } }
public void clear() throws IOException { hashFile.setSize(0); stringFile.setLength(0); cache.clear(); }
/** * Returns the long associated with the String or 0 if there is no long associated with the * string. If newValue is not equal to 0 then this becomes the new value associated with the * string. * * @param str the String. * @param newValue if not equal to 0, the new value to be associated with the string. * @return the long currently associated with the String or 0 if there is no long associated with * the string. * @throws IOException if an I/O error occurs or the hash table is full. */ public long getAndPut(String str, long newValue) throws IOException { if (str == null) { throw new IllegalArgumentException("str is null"); } if (newValue == 0) { Long l = cache.get(str); if (l != null) { return l.longValue(); } } int hashCode = str.hashCode(); int startBucket = calcBucket(hashCode); int bucket = startBucket; long value; // Try buckets until we find the correct string or an empty bucket. while ((value = getValue(bucket)) != 0) { // Check the hash code. long offset = (long) bucket * 4; if (hashCode == hashFile.getInt(offset)) { // Fetch the string and compare with the target string. String bucketStr = readString(hashFile.getUInt(offset + 1)); if (str.equals(bucketStr)) { if (newValue != 0) { hashFile.putLong((long) bucket * 2 + 1, newValue); // Add the new value to the cache. cache.put(str, newValue); } else { // Add the value to the cache. cache.put(str, value); } return value; } } // Try the next bucket. bucket = (bucket + 1) % nrBuckets; if (bucket == startBucket) { throw new IOException("Hash table full"); } } if (newValue != 0) { // Add the new value to the cache. cache.put(str, newValue); // Add a new hash bucket. long bucketOffset = (long) bucket * 4; hashFile.putInt(bucketOffset, hashCode); hashFile.putUInt(bucketOffset + 1, writeString(str)); hashFile.putLong((long) bucket * 2 + 1, newValue); ++nrUsedBuckets; if (nrUsedBuckets >= (int) (nrBuckets * REHASH_LIMIT)) { rehash(findPrime(nrBuckets)); } } return 0; }
public static Record[] getRecords(String namestr, short type, short dclass, byte cred) { Message query; Message response; Record question; Record[] answers; int answerCount = 0, i = 0; Enumeration e; Name name = new Name(namestr); /*System.out.println("lookup of " + name + " " + Type.string(type));*/ if (!Type.isRR(type) && type != Type.ANY) return null; if (res == null) { try { eres = new ExtendedResolver(); } catch (UnknownHostException uhe) { System.out.println("Failed to initialize resolver"); System.exit(-1); } } if (cache == null) cache = new Cache(); CacheResponse cached = cache.lookupRecords(name, type, dclass, cred); /*System.out.println(cached);*/ if (cached.isSuccessful()) { RRset rrset = cached.answer(); answerCount = rrset.size(); e = rrset.rrs(); } else if (cached.isNegative()) { answerCount = 0; e = null; } else { question = Record.newRecord(name, type, dclass); query = Message.newQuery(question); if (res != null) response = res.send(query); else response = eres.send(query); short rcode = response.getHeader().getRcode(); if (rcode == Rcode.NOERROR || rcode == Rcode.NXDOMAIN) cache.addMessage(response); if (rcode != Rcode.NOERROR) return null; e = response.getSection(Section.ANSWER); while (e.hasMoreElements()) { Record r = (Record) e.nextElement(); if (matchType(r.getType(), type)) answerCount++; } e = response.getSection(Section.ANSWER); } if (answerCount == 0) return null; answers = new Record[answerCount]; while (e.hasMoreElements()) { Record r = (Record) e.nextElement(); if (matchType(r.getType(), type)) answers[i++] = r; } return answers; }