List of usage examples for java.util.concurrent.locks Lock lock
lock
From source file:com.mastfrog.netty.http.client.CookieStore.java
void extract(HttpHeaders headers) { List<String> hdrs = headers.getAll(Headers.SET_COOKIE.name()); if (!hdrs.isEmpty()) { Lock writeLock = lock.writeLock(); try {// w w w . ja v a2 s.c o m writeLock.lock(); for (String header : hdrs) { try { Cookie cookie = Headers.SET_COOKIE.toValue(header); add(cookie); } catch (Exception e) { if (errorHandler != null) { errorHandler.receive(e); } else { Exceptions.chuck(e); } } } } finally { writeLock.unlock(); } } }
From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java
@Test @RedisAvailable//from w ww .j av a 2s . com public void testEquals() throws Exception { RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest(); RedisLockRegistry registry1 = new RedisLockRegistry(connectionFactory, this.registryKey); registry1.setUseWeakReferences(true); RedisLockRegistry registry2 = new RedisLockRegistry(connectionFactory, this.registryKey); RedisLockRegistry registry3 = new RedisLockRegistry(connectionFactory, this.registryKey2); Lock lock1 = registry1.obtain("foo"); Lock lock2 = registry1.obtain("foo"); assertEquals(lock1, lock2); lock1.lock(); lock2.lock(); assertEquals(lock1, lock2); lock1.unlock(); lock2.unlock(); assertEquals(lock1, lock2); lock1 = registry1.obtain("foo"); lock2 = registry2.obtain("foo"); assertNotEquals(lock1, lock2); lock1.lock(); assertFalse(lock2.tryLock()); lock1.unlock(); lock1 = registry1.obtain("foo"); lock2 = registry3.obtain("foo"); assertNotEquals(lock1, lock2); lock1.lock(); lock2.lock(); lock1.unlock(); lock2.unlock(); }
From source file:com.mastfrog.netty.http.client.CookieStore.java
@Override public Iterator<Cookie> iterator() { Lock readLock = lock.readLock(); List<Cookie> cks = new ArrayList<Cookie>(); readLock.lock(); try {//from w w w . ja v a2 s . com cks.addAll(cookies); } finally { readLock.unlock(); } Collections.sort(cks); return cks.iterator(); }
From source file:com.mastfrog.netty.http.client.CookieStore.java
public String toString() { Lock readLock = lock.readLock(); List<Cookie> cks = new ArrayList<Cookie>(); readLock.lock(); try {/*w w w.j av a 2 s . co m*/ if (cookies.isEmpty()) { return "[no cookies]"; } cks.addAll(cookies); } finally { readLock.unlock(); } Collections.sort(cks); return Headers.COOKIE.toString(cks.toArray(new Cookie[cks.size()])); }
From source file:org.jivesoftware.openfire.spi.PresenceManagerImpl.java
/** * Loads offline presence data for the user into cache. * * @param username the username./* ww w . ja v a2 s. c om*/ */ private void loadOfflinePresence(String username) { Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; Lock lock = CacheFactory.getLock(username, offlinePresenceCache); try { lock.lock(); if (!offlinePresenceCache.containsKey(username) || !lastActivityCache.containsKey(username)) { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(LOAD_OFFLINE_PRESENCE); pstmt.setString(1, username); rs = pstmt.executeQuery(); if (rs.next()) { String offlinePresence = DbConnectionManager.getLargeTextField(rs, 1); if (rs.wasNull()) { offlinePresence = NULL_STRING; } long offlineDate = Long.parseLong(rs.getString(2).trim()); offlinePresenceCache.put(username, offlinePresence); lastActivityCache.put(username, offlineDate); } else { offlinePresenceCache.put(username, NULL_STRING); lastActivityCache.put(username, NULL_LONG); } } } catch (SQLException sqle) { Log.error(sqle.getMessage(), sqle); } finally { DbConnectionManager.closeConnection(rs, pstmt, con); lock.unlock(); } }
From source file:com.mastfrog.netty.http.client.CookieStore.java
public void store(OutputStream out) throws IOException { ObjectMapper om = new ObjectMapper(); Lock readLock = lock.readLock(); List<DateCookie> cks = new ArrayList<>(); readLock.lock(); try {//from w w w. j av a 2s . com cks.addAll(cookies); } finally { readLock.unlock(); } List<Map<String, Object>> list = new LinkedList<>(); for (DateCookie ck : cks) { Map<String, Object> m = new HashMap<>(); m.put("domain", ck.getDomain()); m.put("maxAge", ck.getMaxAge()); m.put("timestamp", ck.getTimestamp().getMillis()); m.put("path", ck.getPath()); m.put("name", ck.getName()); m.put("value", ck.getValue()); m.put("httpOnly", ck.isHttpOnly()); m.put("secure", ck.isSecure()); if (ck.getComment() != null) { m.put("comment", ck.getComment()); } if (ck.getCommentUrl() != null) { m.put("commentUrl", ck.getCommentUrl()); } if (ck.getPorts() != null && !ck.getPorts().isEmpty()) { m.put("ports", ck.getPorts().toArray(new Integer[0])); } list.add(m); } om.writeValue(out, list); }
From source file:com.mastfrog.netty.http.client.CookieStore.java
@SuppressWarnings("unchecked") public void read(InputStream in) throws IOException { ObjectMapper om = new ObjectMapper(); List<Map<String, Object>> l = om.readValue(in, List.class); List<DateCookie> cks = new ArrayList<>(); for (Map<String, Object> m : l) { String domain = (String) m.get("domain"); Number maxAge = (Number) m.get("maxAge"); Number timestamp = (Number) m.get("timestamp"); String path = (String) m.get("path"); String name = (String) m.get("name"); String value = (String) m.get("value"); Boolean httpOnly = (Boolean) m.get("httpOnly"); Boolean secure = (Boolean) m.get("secure"); String comment = (String) m.get("comment"); String commentUrl = (String) m.get("commentUrl"); List<Integer> ports = (List<Integer>) m.get("ports"); DateTime ts = timestamp == null ? DateTime.now() : new DateTime(timestamp.longValue()); DateCookie cookie = new DateCookie(new DefaultCookie(name, value), ts); if (cookie.isExpired()) { continue; }/*from w w w .j ava2s .c o m*/ if (domain != null) { cookie.setDomain(domain); } if (maxAge != null) { cookie.setMaxAge(maxAge.longValue()); } if (path != null) { cookie.setPath(path); } if (httpOnly != null) { cookie.setHttpOnly(httpOnly); } if (secure != null) { cookie.setSecure(secure); } if (comment != null) { cookie.setComment(comment); } if (commentUrl != null) { cookie.setCommentUrl(commentUrl); } if (ports != null) { cookie.setPorts(ports); } cks.add(cookie); } if (!cks.isEmpty()) { Lock writeLock = lock.writeLock(); try { writeLock.lock(); cookies.addAll(cks); } finally { writeLock.unlock(); } } }
From source file:org.apache.bookkeeper.bookie.EntryLogManagerForEntryLogPerLedger.java
@Override public long addEntry(long ledger, ByteBuf entry, boolean rollLog) throws IOException { Lock lock = getLock(ledger); lock.lock(); try {/*from w w w . j a v a 2 s . c o m*/ return super.addEntry(ledger, entry, rollLog); } finally { lock.unlock(); } }
From source file:org.apache.bookkeeper.bookie.EntryLogManagerForEntryLogPerLedger.java
@Override void createNewLog(long ledgerId) throws IOException { Lock lock = getLock(ledgerId); lock.lock(); try {/*from w w w .j a v a 2 s . c o m*/ super.createNewLog(ledgerId); } finally { lock.unlock(); } }
From source file:org.apache.bookkeeper.bookie.EntryLogManagerForEntryLogPerLedger.java
public BufferedLogChannelWithDirInfo getCurrentLogWithDirInfoForLedger(long ledgerId) throws IOException { Lock lock = getLock(ledgerId); lock.lock(); try {/* ww w.j a va 2 s .c o m*/ EntryLogAndLockTuple entryLogAndLockTuple = ledgerIdEntryLogMap.get(ledgerId); return entryLogAndLockTuple.getEntryLogWithDirInfo(); } catch (Exception e) { log.error("Received unexpected exception while fetching entry from map for ledger: " + ledgerId, e); throw new IOException("Received unexpected exception while fetching entry from map", e); } finally { lock.unlock(); } }