Java Code Examples for org.nutz.lang.Streams#safeClose()

The following examples show how to use org.nutz.lang.Streams#safeClose() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JedisSessionDAO.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(Session session) {
    if (session == null || session.getId() == null) {
        return;
    }

    Jedis jedis = null;
    try {
        jedis = jedisAgent.getResource();

        jedis.hdel(JedisUtils.getBytesKey(sessionKeyPrefix), JedisUtils.getBytesKey(session.getId().toString()));
        jedis.del(JedisUtils.getBytesKey(sessionKeyPrefix + session.getId()));

        logger.debug("delete {} ", session.getId());
    } catch (Exception e) {
        logger.error("delete {} ", session.getId(), e);
    } finally {
       Streams.safeClose(jedis);
    }
}
 
Example 2
Source File: JedisCacheManager.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
@Override
public V put(K key, V value) throws CacheException {
	if (key == null){
		return null;
	}
	
	Jedis jedis = null;
	try {
		jedis = jedisAgent.getResource();
		jedis.hset(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key), JedisUtils.toBytes(value));
		logger.debug("put {} {} = {}", cacheKeyName, key, value);
	} catch (Exception e) {
		logger.error("put {} {}", cacheKeyName, key, e);
	} finally {
		Streams.safeClose(jedis);
	}
	return value;
}
 
Example 3
Source File: JedisCacheManager.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public V remove(K key) throws CacheException {
	V value = null;
	Jedis jedis = null;
	try {
		jedis = jedisAgent.getResource();
		value = (V)JedisUtils.toObject(jedis.hget(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key)));
		jedis.hdel(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key));
		logger.debug("remove {} {}", cacheKeyName, key);
	} catch (Exception e) {
		logger.warn("remove {} {}", cacheKeyName, key, e);
	} finally {
		Streams.safeClose(jedis);
	}
	return value;
}
 
Example 4
Source File: JedisCacheManager.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Collection<V> values() {
	Collection<V> vals = Collections.emptyList();;
	Jedis jedis = null;
	try {
		jedis = jedisAgent.getResource();
		Collection<byte[]> col = jedis.hvals(JedisUtils.getBytesKey(cacheKeyName));
		for(byte[] val : col){
			Object obj = JedisUtils.toObject(val);
			if (obj != null){
				vals.add((V) obj);
			}
       	}
		logger.debug("values {} {} ", cacheKeyName, vals);
		return vals;
	} catch (Exception e) {
		logger.error("values {}",  cacheKeyName, e);
	} finally {
		Streams.safeClose(jedis);
	}
	return vals;
}
 
Example 5
Source File: JedisCacheManager.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
      @Aop("redis")
public V get(K key) throws CacheException {
	if (key == null){
		return null;
	}
	
	V v = null;
	HttpServletRequest request = Mvcs.getReq();
	if (request != null){
		v = (V)request.getAttribute(cacheKeyName);
		if (v != null){
			return v;
		}
	}
	
	V value = null;
	Jedis jedis = null;
	try {
		jedis = jedisAgent.getResource();
              value = (V) jedis().hget(JedisUtils.getBytesKey(cacheKeyName),JedisUtils.getBytesKey(cacheKeyName));
		logger.debug("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");
	} catch (Exception e) {
		logger.error("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "", e);
	} finally {
		Streams.safeClose(jedis);
	}
	
	if (request != null && value != null){
		request.setAttribute(cacheKeyName, value);
	}
	
	return value;
}
 
Example 6
Source File: JedisCacheManager.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() throws CacheException {
	Jedis jedis = null;
	try {
		jedis = jedisAgent.getResource();
		jedis.hdel(JedisUtils.getBytesKey(cacheKeyName));
		logger.debug("clear {}", cacheKeyName);
	} catch (Exception e) {
		logger.error("clear {}", cacheKeyName, e);
	} finally {
		Streams.safeClose(jedis);
	}
}
 
Example 7
Source File: JedisAgenAccessTokenStore.java    From nutzwx with Apache License 2.0 5 votes vote down vote up
public WxAccessToken get() {
    Map<String, String> map;
    Jedis jedis = null;
    try {
        jedis = jedisAgent.getResource();
        map = jedis.hgetAll(tokenKey);
    } finally {
        Streams.safeClose(jedis);
    }
    if (map == null || map.isEmpty())
        return null;
    return Lang.map2Object(map, WxAccessToken.class);
}
 
Example 8
Source File: JedisAgenAccessTokenStore.java    From nutzwx with Apache License 2.0 5 votes vote down vote up
public void save(String token, int expires, long lastCacheTimeMillis) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("token", token);
    map.put("expires", "" + expires);
    map.put("lastCacheTimeMillis", "" + lastCacheTimeMillis);
    Jedis jedis = null;
    try {
        jedis = jedisAgent.getResource();
        jedis.hmset(tokenKey, map);
    } finally {
        Streams.safeClose(jedis);
    }
}
 
Example 9
Source File: BeetlViewMaker.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void init() throws IOException {
    log.debug("beetl init ....");
    Configuration cfg = Configuration.defaultConfiguration();
    Properties prop = new Properties();
    InputStream ins = Configuration.class.getResourceAsStream("/beetl.properties");
    if (ins != null) {
        log.debug("found beetl.properties, loading ...");
        try {
            prop.load(ins);
        }
        finally {
            Streams.safeClose(ins);
        }
    }
    if (!prop.containsKey(Configuration.RESOURCE_LOADER)) {
        // 默认选用WebAppResourceLoader,除非用户自定义了RESOURCE_LOADER
        log.debug("no custom RESOURCE_LOADER found , select WebAppResourceLoader");
        cfg.setResourceLoader(WebAppResourceLoader.class.getName());
    }
    if (!prop.containsKey(Configuration.DIRECT_BYTE_OUTPUT)) {
        // 默认启用DIRECT_BYTE_OUTPUT,除非用户自定义, 一般不会.
        log.debug("no custom DIRECT_BYTE_OUTPUT found , set to true");
        // 当DIRECT_BYTE_OUTPUT为真时, beetl渲染会通过getOutputStream获取输出流
        // 而BeetlView会使用LazyResponseWrapper代理getOutputStream方法
        // 从而实现在模板输出之前,避免真正调用getOutputStream
        // 这样@Fail视图就能正常工作了
        cfg.setDirectByteOutput(true);
    }
    if (!prop.containsKey(Configuration.ERROR_HANDLER)) {
        // 没有自定义ERROR_HANDLER,用定制的
        cfg.setErrorHandlerClass(LogErrorHandler.class.getName());
    }
    groupTemplate = new GroupTemplate(cfg);
    render = new WebRender(groupTemplate);
    log.debug("beetl init complete");
}
 
Example 10
Source File: JedisSessionDAO.java    From NutzSite with Apache License 2.0 4 votes vote down vote up
@Override
    public void update(Session session) throws UnknownSessionException {
        if (session == null || session.getId() == null) {
            return;
        }

        HttpServletRequest request = Mvcs.getReq();
        if (request != null){
            String uri = request.getServletPath();
            // 如果是静态文件,则不更新SESSION
            if (isStaticFile(uri)){
                return;
            }

            // 手动控制不更新SESSION
//            if (Global.NO.equals(request.getParameter("updateSession"))){
//                return;
//            }
        }

        Jedis jedis = null;
        try {

            jedis = jedisAgent.getResource();

            // 获取登录者编号
            PrincipalCollection pc = (PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
            String principalId = pc != null ? pc.getPrimaryPrincipal().toString() : StringUtils.EMPTY;

            jedis.hset(sessionKeyPrefix, session.getId().toString(), principalId + "|" + session.getTimeout() + "|" + session.getLastAccessTime().getTime());
            jedis.set(JedisUtils.getBytesKey(sessionKeyPrefix + session.getId()), JedisUtils.toBytes(session));

            // 设置超期时间
            int timeoutSeconds = (int)(session.getTimeout() / 1000);
            jedis.expire((sessionKeyPrefix + session.getId()), timeoutSeconds);

            logger.debug("update {} {}", session.getId(), request != null ? request.getRequestURI() : "");
        } catch (Exception e) {
            logger.error("update {} {}", session.getId(), request != null ? request.getRequestURI() : "", e);
        } finally {
           Streams.safeClose(jedis);
        }
    }
 
Example 11
Source File: TcpRpcEndpoint.java    From nutzcloud with Apache License 2.0 4 votes vote down vote up
public void destroyObject(SocketAddress key, PooledObject<SocketHolder> p) throws Exception {
    Streams.safeClose(p.getObject().socket);
}