private void setupTraceLog(HttpContext httpContext, String queue, AccessLog accessLog) throws IOException { String clientIp = httpContext.getClientIp(); // DBへのアクセスログ採取有無 if (!isLogging(clientIp, "accessDb." + queue)) { return; } // DBに記録する accessLog.insert(); // リクエストStreamのpeek処理 if (isLogging(clientIp, "accessTrace.request." + queue)) { PeekStream requestPeeker = accessLog.setupPeekRequest(); httpContext.peekRequest(requestPeeker); context.enque(requestPeeker, Config.QUEUE_PEEK); // 既に受け取っているヘッダ部分をPeekStreamに流し込む HttpParser requestParser = httpContext.getRequestParser(); OutputStream os = requestPeeker.getPeekOutputStream(); requestParser.writeSeriarizeHeader(os); } // レスポンスStreamのpeek処理 if (isLogging(clientIp, "accessTrace.response." + queue)) { PeekStream responsePeeker = accessLog.setupPeekResponse(); httpContext.peekResponse(responsePeeker); context.enque(responsePeeker, Config.QUEUE_PEEK); } }
@Test public void test() throws InterruptedException { for (int i = 0; i < 30; i++) { service.report1(i, new Date()); } Thread.sleep(1 * 1000); List<?> logs = permissionHelper.getEntities("from AccessLog"); Assert.assertTrue(logs.size() >= 0); AccessLog firstLog = (AccessLog) logs.get(0); Assert.assertEquals(firstLog.getId(), firstLog.getPK()); SQLExcutor ex = new SQLExcutor(); String sql = "select id, methodName as 方法名称 from dm_access_log"; ex.excuteQuery(sql); ex.excuteQuery( sql, new AbstractSO() { private static final long serialVersionUID = 1L; public String[] getParameterNames() { return null; } }); ex.excuteQuery("test1", 1, new HashMap<Integer, Object>()); ex.excuteQuery(sql, new HashMap<Integer, Object>()); Assert.assertTrue(ex.result.size() > 10); }
private void executeInsert(PersistenceManager pm, AccessLog accessLog) { if (accessLog == null) { return; } // ログに出力 accessLog.log(false); accessLog.persist(pm); accessLog.unref(); }
// TODO非同期 public void executeDelete(PersistenceManager pm, Collection<Long> accessLogsIds) { pm.currentTransaction().begin(); for (Long id : accessLogsIds) { AccessLog accessLog = pm.getObjectById(AccessLog.class, id); StoreManager.unref(accessLog.getRequestHeaderDigest()); StoreManager.unref(accessLog.getRequestBodyDigest()); StoreManager.unref(accessLog.getResponseHeaderDigest()); StoreManager.unref(accessLog.getResponseBodyDigest()); pm.deletePersistent(accessLog); } pm.currentTransaction().commit(); }
private void endBuffer(ZipEntry ze) { if (ze == null) { return; } if (store != null) { store.close(); // 終了処理は別スレッドで実行中 String digest = store.getDigest(); logPersister.addDigest(addDigests, digest); store = null; } else if (charsetDecoder != null) { charBuffer.flip(); charBuffer.array(); String accessLogJson = new String(charBuffer.array(), charBuffer.position(), charBuffer.limit()); charsetDecoder = null; charBuffer = null; AccessLog accessLog = AccessLog.fromJson(accessLogJson); if (accessLog == null) { return; } logPersister.addDigest(refDigests, accessLog.getRequestHeaderDigest()); logPersister.addDigest(refDigests, accessLog.getRequestBodyDigest()); logPersister.addDigest(refDigests, accessLog.getResponseHeaderDigest()); logPersister.addDigest(refDigests, accessLog.getResponseBodyDigest()); accessLog.setId(null); accessLog.setPersist(true); PersistenceManager pm = JdoUtil.getPersistenceManager(); logPersister.executeInsert(pm, accessLog); if (pm.currentTransaction().isActive()) { pm.currentTransaction().rollback(); } } }
public void excute() { List<Map<Integer, Object>> paramsMapList = new ArrayList<Map<Integer, Object>>(); for (Object temp : records) { AccessLog log = (AccessLog) temp; Map<Integer, Object> paramsMap = new HashMap<Integer, Object>(); int index = 1; paramsMap.put(index++, log.getClassName()); paramsMap.put(index++, log.getMethodName()); paramsMap.put(index++, log.getMethodCnName()); paramsMap.put(index++, new Timestamp(log.getAccessTime().getTime())); paramsMap.put(index++, log.getRunningTime()); paramsMap.put(index++, log.getParams()); paramsMap.put(index++, log.getUserId()); paramsMap.put(index++, log.getIp()); paramsMapList.add(paramsMap); } String script = SqlConfig.getScript("saveAccessLog", 1); SQLExcutor.excuteBatch(script, paramsMapList, DMConstants.LOCAL_CONN_POOL); }
// TODO非同期 public File executeExport(PersistenceManager pm, Collection<Long> accessLogsIds) throws IOException { File exportFile = File.createTempFile("export", ".zip", config.getTmpDir()); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(exportFile)); Set<String> traceDigests = new HashSet<String>(); for (Long id : accessLogsIds) { AccessLog accessLog = null; try { accessLog = (AccessLog) pm.detachCopy(pm.getObjectById(AccessLog.class, id)); } catch (JDOObjectNotFoundException e) { continue; } ZipEntry ze = new ZipEntry("/accessLog/" + accessLog.getId()); addDigest(traceDigests, accessLog.getRequestHeaderDigest()); addDigest(traceDigests, accessLog.getRequestBodyDigest()); addDigest(traceDigests, accessLog.getResponseHeaderDigest()); addDigest(traceDigests, accessLog.getResponseBodyDigest()); String json = accessLog.toJson().toString(); byte[] jsonBytes = json.getBytes("utf-8"); int length = jsonBytes.length; ze.setSize(length); zos.putNextEntry(ze); zos.write(jsonBytes); zos.closeEntry(); } for (String digest : traceDigests) { long length = StoreManager.getStoreLength(digest); long storeId = StoreManager.getStoreId(digest); if (length < 0 || storeId == Store.FREE_ID) { logger.warn("illegal digest:" + digest); continue; } ZipEntry ze = new ZipEntry("/store/" + Long.toString(storeId)); ze.setSize(length); zos.putNextEntry(ze); StoreStream.storeToStream(storeId, zos); zos.closeEntry(); } zos.close(); // 必要 return exportFile; }
/* (non-Javadoc) * @see naru.queuelet.Queuelet#service(java.lang.Object) */ public boolean service(Object req) { HttpContext httpContext = null; if (req instanceof Socket) { Socket socket = (Socket) req; // 指定IPからのリクエストかどうかをチェック if (checkIp(socket) == false) { return false; } httpContext = (HttpContext) context.deque(Config.QUEUE_HTTPCONTEXT_POOL); httpContext.setSocket(socket); } else { // KeepAliveを実装する時はこちら httpContext = (HttpContext) req; } // 処理の起点がaccessLogの中に採られる AccessLog accessLog = (AccessLog) context.deque(Config.QUEUE_ACCESSLOG_POOL); httpContext.setupAccessLog(accessLog); boolean parseHeader = false; try { parseHeader = httpContext.parseRequestHeader(); } catch (IOException e) { // 不当リクエスト logger.error("fail to parse Request.", e); } catch (Throwable t) { // java.nio.channels.ClosedSelectorExceptionが発生したりする logger.error("fail to parse Request.!!", t); } if (!parseHeader) { context.enque(httpContext, Config.QUEUE_CLOSE); return false; } MappingEntry entry = mappingQueue(httpContext); if (entry == null) { logger.warn("fail to mapping.URL:" + httpContext.getRequestUri()); responseDirect(httpContext, "404", "failt to mapping." + httpContext.getRequestUri()); return false; } String queue = entry.getQueue(); // 一連streamの初期化 try { setupTraceLog(httpContext, queue, accessLog); } catch (IOException e) { // streamの初期化に失敗、レスポンスもできない logger.error("fail to setupAccessTrace", e); responseDirect(httpContext, "404", "failt to accesslog." + httpContext.getRequestUri()); return false; } // 以降普通にレスポンスできる // response予約 context.enque(httpContext, Config.QUEUE_RESPONSE); entry = authentication(httpContext, entry); if (entry == null) { return false; } accessLog.setMappingSource(entry.getSourcePath()); accessLog.setMappingDestination(entry.getDestination()); // response作成依頼(認証時にqueueが変更されている可能性がある) context.enque(httpContext, entry.getQueue()); return true; }