Java tutorial
/* * Copyright (C) 2004-2014 Polarion Software * All rights reserved. * Email: dev@polarion.com * * * Copyright (C) 2004-2014 Polarion Software * All Rights Reserved. No use, copying or distribution of this * work may be made except in accordance with a valid license * agreement from Polarion Software. This notice must be * included on all copies, modifications and derivatives of this * work. * * POLARION SOFTWARE MAKES NO REPRESENTATIONS OR WARRANTIES * ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESSED OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. POLARION SOFTWARE * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * */ package com.polarion.pso.license; import java.util.Collection; import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.polarion.alm.tracker.ITrackerService; import com.polarion.platform.core.PlatformContext; import com.polarion.platform.security.ISecurityService; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.IOFileFilter; import org.apache.commons.io.filefilter.WildcardFileFilter; import org.apache.commons.io.filefilter.AgeFileFilter; /** * This is the servlet which displays the login license of the current user * * @author David Merrill */ public class FetchUserLicenseTypeServlet extends HttpServlet { private static final long serialVersionUID = 1L; /* (non-Javadoc) * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { ISecurityService securityService = (ISecurityService) PlatformContext.getPlatform() .lookupService(ISecurityService.class); ITrackerService trackerService = (ITrackerService) PlatformContext.getPlatform() .lookupService(ITrackerService.class); String userId = securityService.getCurrentUser(); String userName = trackerService.getTrackerUser(userId).getName(); long cutoff = System.currentTimeMillis() - (1000); File directory = new File("./logs/main"); FileFilter wcFilter = new WildcardFileFilter("log4j-licensing-*.log"); AgeFileFilter ageFilter = new AgeFileFilter(cutoff); FileFilter andFilter = new org.apache.commons.io.filefilter.AndFileFilter((IOFileFilter) ageFilter, (IOFileFilter) wcFilter); Collection<File> matches = FileUtils.listFiles(directory, (IOFileFilter) andFilter, null); if (!matches.isEmpty()) { File myFile = matches.iterator().next(); RandomAccessFile licFile = new RandomAccessFile(myFile, "r"); // Read the last 1024 bytes Long fileLength = licFile.length(); Long offSet = fileLength - 1024; byte[] bStr = new byte[1024]; licFile.seek(offSet); licFile.read(bStr); licFile.close(); String logString = new java.lang.String(bStr); String[] lineArray = logString.split("\n"); String searchString = "INFO PolarionLicensing - User \'" + userId + "\' logged in"; Boolean found = false; Integer size = lineArray.length - 1; String licType = directory.toString(); for (int i = size; i >= 0; i--) { String line = lineArray[i]; if (line.contains(searchString) && found == false) { found = true; i = -1; Integer startIndex = line.indexOf(searchString) + searchString.length() + 6; licType = line.substring(startIndex); licType = licType.replace("\r", ""); licType = licType.trim(); } } req.setAttribute("userId", userName); req.setAttribute("licType", licType); } else { req.setAttribute("userId", userName); req.setAttribute("licType", "Not Found"); } getServletContext().getRequestDispatcher("/currentUserLicenseType.jsp").forward(req, resp); } /* (non-Javadoc) * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }