proj_name
stringclasses
131 values
relative_path
stringlengths
30
228
class_name
stringlengths
1
68
func_name
stringlengths
1
48
masked_class
stringlengths
78
9.82k
func_body
stringlengths
46
9.61k
len_input
int64
29
2.01k
len_output
int64
14
1.94k
total
int64
55
2.05k
relevant_context
stringlengths
0
38.4k
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/common/filter/AuthFilter.java
AuthFilter
doFilter
class AuthFilter implements Filter { private static final Logger log = LoggerFactory.getLogger(AuthFilter.class); public void init(FilterConfig config) throws ServletException { } public void destroy() { } /** * doFilter determines if user is an administrator or redirect to login page * * @param req task request * @param resp task response * @param chain filter chain * @throws ServletException */ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException {<FILL_FUNCTION_BODY>} }
HttpServletRequest servletRequest = (HttpServletRequest) req; HttpServletResponse servletResponse = (HttpServletResponse) resp; boolean isAdmin = false; try { //read auth token String authToken = AuthUtil.getAuthToken(servletRequest.getSession()); //check if exists if (authToken != null && !authToken.trim().equals("")) { //check if valid admin auth token String userType = AuthDB.isAuthorized(AuthUtil.getUserId(servletRequest.getSession()), authToken); if (userType != null) { String uri = servletRequest.getRequestURI(); if (Auth.MANAGER.equals(userType)) { isAdmin = true; } else if (!uri.contains("/manage/") && Auth.ADMINISTRATOR.equals(userType)) { isAdmin = true; } AuthUtil.setUserType(servletRequest.getSession(), userType); //check to see if user has timed out String timeStr = AuthUtil.getTimeout(servletRequest.getSession()); if (timeStr != null && !timeStr.trim().equals("")) { SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyyHHmmss"); Date sessionTimeout = sdf.parse(timeStr); Date currentTime = new Date(); //if current time > timeout then redirect to login page if (sessionTimeout == null || currentTime.after(sessionTimeout)) { isAdmin = false; } else { AuthUtil.setTimeout(servletRequest.getSession()); } } else { isAdmin = false; } } } //if not admin redirect to login page if (!isAdmin) { AuthUtil.deleteAllSession(servletRequest.getSession()); servletResponse.sendRedirect(servletRequest.getContextPath() + "/"); } else { chain.doFilter(req, resp); } } catch (SQLException | ParseException | IOException | GeneralSecurityException ex) { AuthUtil.deleteAllSession(servletRequest.getSession()); log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); }
159
566
725
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/common/util/AppConfig.java
AppConfig
decryptProperty
class AppConfig { private static final Logger log = LoggerFactory.getLogger(AppConfig.class); private static PropertiesConfiguration prop; public static final String CONFIG_DIR = StringUtils.isNotEmpty(System.getProperty("CONFIG_DIR")) ? System.getProperty("CONFIG_DIR").trim() : AppConfig.class.getClassLoader().getResource(".").getPath(); static { try { //move configuration to specified dir if (StringUtils.isNotEmpty(System.getProperty("CONFIG_DIR"))) { File configFile = new File(CONFIG_DIR + "BastillionConfig.properties"); if (!configFile.exists()) { File oldConfig = new File(AppConfig.class.getClassLoader().getResource(".").getPath() + "BastillionConfig.properties"); FileUtils.moveFile(oldConfig, configFile); } configFile = new File(CONFIG_DIR + "jaas.conf"); if (!configFile.exists()) { File oldConfig = new File(AppConfig.class.getClassLoader().getResource(".").getPath() + "jaas.conf"); FileUtils.moveFile(oldConfig, configFile); } } prop = new PropertiesConfiguration(CONFIG_DIR + "BastillionConfig.properties"); } catch (IOException | ConfigurationException ex) { log.error(ex.toString(), ex); } } private AppConfig() { } /** * gets the property from config * * @param name property name * @return configuration property */ public static String getProperty(String name) { String property = null; if (StringUtils.isNotEmpty(name)) { if (StringUtils.isNotEmpty(System.getenv(name))) { property = System.getenv(name); } else if (StringUtils.isNotEmpty(System.getenv(name.toUpperCase()))) { property = System.getenv(name.toUpperCase()); } else { property = prop.getString(name); } } return property; } /** * gets the property from config * * @param name property name * @param defaultValue default value if property is empty * @return configuration property */ public static String getProperty(String name, String defaultValue) { String value = getProperty(name); if (StringUtils.isEmpty(value)) { value = defaultValue; } return value; } /** * gets the property from config and replaces placeholders * * @param name property name * @param replacementMap name value pairs of place holders to replace * @return configuration property */ public static String getProperty(String name, Map<String, String> replacementMap) { String value = getProperty(name); if (StringUtils.isNotEmpty(value)) { //iterate through map to replace text Set<String> keySet = replacementMap.keySet(); for (String key : keySet) { //replace values in string String rVal = replacementMap.get(key); value = value.replace("${" + key + "}", rVal); } } return value; } /** * removes property from the config * * @param name property name */ public static void removeProperty(String name) throws ConfigurationException { //remove property prop.clearProperty(name); prop.save(); } /** * updates the property in the config * * @param name property name * @param value property value */ public static void updateProperty(String name, String value) throws ConfigurationException { //remove property if (StringUtils.isNotEmpty(value)) { prop.setProperty(name, value); prop.save(); } } /** * checks if property is encrypted * * @param name property name * @return true if property is encrypted */ public static boolean isPropertyEncrypted(String name) { String property = prop.getString(name); if (StringUtils.isNotEmpty(property)) { return property.matches("^" + EncryptionUtil.CRYPT_ALGORITHM + "\\{.*\\}$"); } else { return false; } } /** * decrypts and returns the property from config * * @param name property name * @return configuration property */ public static String decryptProperty(String name) throws GeneralSecurityException {<FILL_FUNCTION_BODY>} /** * encrypts and updates the property in the config * * @param name property name * @param value property value */ public static void encryptProperty(String name, String value) throws ConfigurationException, GeneralSecurityException { //remove property if (StringUtils.isNotEmpty(value)) { prop.setProperty(name, EncryptionUtil.CRYPT_ALGORITHM + "{" + EncryptionUtil.encrypt(value) + "}"); prop.save(); } } }
String retVal = prop.getString(name); if (StringUtils.isNotEmpty(retVal)) { retVal = retVal.replaceAll("^" + EncryptionUtil.CRYPT_ALGORITHM + "\\{", "").replaceAll("\\}$", ""); retVal = EncryptionUtil.decrypt(retVal); } return retVal;
1,308
97
1,405
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/common/util/AuthUtil.java
AuthUtil
getAuthToken
class AuthUtil { public static final String SESSION_ID = "sessionId"; public static final String USER_ID = "userId"; public static final String USERNAME = "username"; public static final String AUTH_TOKEN = "authToken"; public static final String TIMEOUT = "timeout"; private AuthUtil() { } /** * query session for OTP shared secret * * @param session http session * @return shared secret */ public static String getOTPSecret(HttpSession session) throws GeneralSecurityException { String secret = (String) session.getAttribute("otp_secret"); secret = EncryptionUtil.decrypt(secret); return secret; } /** * set authentication type * * @param session http session * @param authType authentication type */ public static void setAuthType(HttpSession session, String authType) { if (authType != null) { session.setAttribute("authType", authType); } } /** * query authentication type * * @param session http session * @return authentication type */ public static String getAuthType(HttpSession session) { String authType = (String) session.getAttribute("authType"); return authType; } /** * set user type * * @param session http session * @param userType user type */ public static void setUserType(HttpSession session, String userType) { if (userType != null) { session.setAttribute("userType", userType); } } /** * query user type * * @param session http session * @return user type */ public static String getUserType(HttpSession session) { String userType = (String) session.getAttribute("userType"); return userType; } /** * set session id * * @param session http session * @param sessionId session id */ public static void setSessionId(HttpSession session, Long sessionId) throws GeneralSecurityException { if (sessionId != null) { session.setAttribute(SESSION_ID, EncryptionUtil.encrypt(sessionId.toString())); } } /** * query session id * * @param session http session * @return session id */ public static Long getSessionId(HttpSession session) throws GeneralSecurityException { Long sessionId = null; String sessionIdStr = EncryptionUtil.decrypt((String) session.getAttribute(SESSION_ID)); if (sessionIdStr != null && !sessionIdStr.trim().equals("")) { sessionId = Long.parseLong(sessionIdStr); } return sessionId; } /** * query session for user id * * @param session http session * @return user id */ public static Long getUserId(HttpSession session) throws GeneralSecurityException { Long userId = null; String userIdStr = EncryptionUtil.decrypt((String) session.getAttribute(USER_ID)); if (userIdStr != null && !userIdStr.trim().equals("")) { userId = Long.parseLong(userIdStr); } return userId; } /** * query session for the username * * @param session http session * @return username */ public static String getUsername(HttpSession session) { return (String) session.getAttribute(USERNAME); } /** * query session for authentication token * * @param session http session * @return authentication token */ public static String getAuthToken(HttpSession session) throws GeneralSecurityException {<FILL_FUNCTION_BODY>} /** * query session for timeout * * @param session http session * @return timeout string */ public static String getTimeout(HttpSession session) { String timeout = (String) session.getAttribute(TIMEOUT); return timeout; } /** * set session OTP shared secret * * @param session http session * @param secret shared secret */ public static void setOTPSecret(HttpSession session, String secret) throws GeneralSecurityException { if (secret != null && !secret.trim().equals("")) { session.setAttribute("otp_secret", EncryptionUtil.encrypt(secret)); } } /** * set session user id * * @param session http session * @param userId user id */ public static void setUserId(HttpSession session, Long userId) throws GeneralSecurityException { if (userId != null) { session.setAttribute(USER_ID, EncryptionUtil.encrypt(userId.toString())); } } /** * set session username * * @param session http session * @param username username */ public static void setUsername(HttpSession session, String username) { if (username != null) { session.setAttribute(USERNAME, username); } } /** * set session authentication token * * @param session http session * @param authToken authentication token */ public static void setAuthToken(HttpSession session, String authToken) throws GeneralSecurityException { if (authToken != null && !authToken.trim().equals("")) { session.setAttribute(AUTH_TOKEN, EncryptionUtil.encrypt(authToken)); } } /** * set session timeout * * @param session http session */ public static void setTimeout(HttpSession session) { //set session timeout SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyyHHmmss"); Calendar timeout = Calendar.getInstance(); timeout.add(Calendar.MINUTE, Integer.parseInt(AppConfig.getProperty("sessionTimeout", "15"))); session.setAttribute(TIMEOUT, sdf.format(timeout.getTime())); } /** * delete all session information * * @param session */ public static void deleteAllSession(HttpSession session) { session.setAttribute(TIMEOUT, null); session.setAttribute(AUTH_TOKEN, null); session.setAttribute(USER_ID, null); session.setAttribute(SESSION_ID, null); session.invalidate(); } /** * return client ip from servlet request * * @param servletRequest http servlet request * @return client ip */ public static String getClientIPAddress(HttpServletRequest servletRequest) { String clientIP = null; if (StringUtils.isNotEmpty(AppConfig.getProperty("clientIPHeader"))) { clientIP = servletRequest.getHeader(AppConfig.getProperty("clientIPHeader")); } if (StringUtils.isEmpty(clientIP)) { clientIP = servletRequest.getRemoteAddr(); } return clientIP; } }
String authToken = (String) session.getAttribute(AUTH_TOKEN); authToken = EncryptionUtil.decrypt(authToken); return authToken;
1,815
45
1,860
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/control/LoginKtrl.java
LoginKtrl
loginSubmit
class LoginKtrl extends BaseKontroller { private static final Logger log = LoggerFactory.getLogger(LoginKtrl.class); //check if otp is enabled @Model(name = "otpEnabled") static final Boolean otpEnabled = ("required".equals(AppConfig.getProperty("oneTimePassword")) || "optional".equals(AppConfig.getProperty("oneTimePassword"))); private static final Logger loginAuditLogger = LoggerFactory.getLogger("io.bastillion.manage.control.LoginAudit"); private final String AUTH_ERROR = "Authentication Failed : Login credentials are invalid"; private final String AUTH_ERROR_NO_PROFILE = "Authentication Failed : There are no profiles assigned to this account"; private final String AUTH_ERROR_EXPIRED_ACCOUNT = "Authentication Failed : Account has expired"; @Model(name = "auth") Auth auth; public LoginKtrl(HttpServletRequest request, HttpServletResponse response) { super(request, response); } @Kontrol(path = "/login", method = MethodType.GET) public String login() { return "/login.html"; } @Kontrol(path = "/loginSubmit", method = MethodType.POST) public String loginSubmit() throws ServletException {<FILL_FUNCTION_BODY>} @Kontrol(path = "/logout", method = MethodType.GET) public String logout() { AuthUtil.deleteAllSession(getRequest().getSession()); return "redirect:/"; } /** * Validates fields for auth submit */ @Validate(input = "/login.html") public void validateLoginSubmit() { if (auth.getUsername() == null || auth.getUsername().trim().equals("")) { addFieldError("auth.username", "Required"); } if (auth.getPassword() == null || auth.getPassword().trim().equals("")) { addFieldError("auth.password", "Required"); } } }
String retVal = "redirect:/admin/menu.html"; String authToken = null; try { authToken = AuthDB.login(auth); //get client IP String clientIP = AuthUtil.getClientIPAddress(getRequest()); if (authToken != null) { User user = AuthDB.getUserByAuthToken(authToken); if (user != null) { String sharedSecret = null; if (otpEnabled) { sharedSecret = AuthDB.getSharedSecret(user.getId()); if (StringUtils.isNotEmpty(sharedSecret) && (auth.getOtpToken() == null || !OTPUtil.verifyToken(sharedSecret, auth.getOtpToken()))) { loginAuditLogger.info(auth.getUsername() + " (" + clientIP + ") - " + AUTH_ERROR); addError(AUTH_ERROR); return "/login.html"; } } //check to see if admin has any assigned profiles if (!User.MANAGER.equals(user.getUserType()) && (user.getProfileList() == null || user.getProfileList().size() <= 0)) { loginAuditLogger.info(auth.getUsername() + " (" + clientIP + ") - " + AUTH_ERROR_NO_PROFILE); addError(AUTH_ERROR_NO_PROFILE); return "/login.html"; } //check to see if account has expired if (user.isExpired()) { loginAuditLogger.info(auth.getUsername() + " (" + clientIP + ") - " + AUTH_ERROR_EXPIRED_ACCOUNT); addError(AUTH_ERROR_EXPIRED_ACCOUNT); return "/login.html"; } AuthUtil.setAuthToken(getRequest().getSession(), authToken); AuthUtil.setUserId(getRequest().getSession(), user.getId()); AuthUtil.setAuthType(getRequest().getSession(), user.getAuthType()); AuthUtil.setTimeout(getRequest().getSession()); AuthUtil.setUsername(getRequest().getSession(), user.getUsername()); AuthDB.updateLastLogin(user); //for first time login redirect to set OTP if (otpEnabled && StringUtils.isEmpty(sharedSecret)) { retVal = "redirect:/admin/viewOTP.ktrl"; } else if ("changeme".equals(auth.getPassword()) && Auth.AUTH_BASIC.equals(user.getAuthType())) { retVal = "redirect:/admin/userSettings.ktrl"; } loginAuditLogger.info(auth.getUsername() + " (" + clientIP + ") - Authentication Success"); } } else { loginAuditLogger.info(auth.getUsername() + " (" + clientIP + ") - " + AUTH_ERROR); addError(AUTH_ERROR); retVal = "/login.html"; } } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return retVal;
572
868
1,440
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/control/OTPKtrl.java
OTPKtrl
qrImage
class OTPKtrl extends BaseKontroller { private static final Logger log = LoggerFactory.getLogger(OTPKtrl.class); public static final boolean requireOTP = "required".equals(AppConfig.getProperty("oneTimePassword")); //QR image size private static final int QR_IMAGE_WIDTH = 325; private static final int QR_IMAGE_HEIGHT = 325; @Model(name = "qrImage") String qrImage; @Model(name = "sharedSecret") String sharedSecret; public OTPKtrl(HttpServletRequest request, HttpServletResponse response) { super(request, response); } @Kontrol(path = "/admin/viewOTP", method = MethodType.GET) public String viewOTP() throws ServletException { sharedSecret = OTPUtil.generateSecret(); try { AuthUtil.setOTPSecret(getRequest().getSession(), sharedSecret); } catch (GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } qrImage = new Date().getTime() + ".png"; return "/admin/two-factor_otp.html"; } @Kontrol(path = "/admin/otpSubmit", method = MethodType.POST) public String otpSubmit() throws ServletException { try { AuthDB.updateSharedSecret(sharedSecret, AuthUtil.getAuthToken(getRequest().getSession())); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } if (requireOTP) { AuthUtil.deleteAllSession(getRequest().getSession()); } return "redirect:/logout.ktrl"; } @Kontrol(path = "/admin/qrImage", method = MethodType.GET) public String qrImage() throws ServletException {<FILL_FUNCTION_BODY>} }
String username; String secret; try { username = UserDB.getUser(AuthUtil.getUserId(getRequest().getSession())).getUsername(); secret = AuthUtil.getOTPSecret(getRequest().getSession()); AuthUtil.setOTPSecret(getRequest().getSession(), null); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } try { String qrCodeText = "otpauth://totp/Bastillion%20%28" + URLEncoder.encode(getRequest().getHeader("host").replaceAll("\\:.*$", ""), "utf-8") + "%29:" + username + "?secret=" + secret; QRCodeWriter qrWriter = new QRCodeWriter(); Hashtable<EncodeHintType, String> hints = new Hashtable<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix matrix = qrWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT, hints); getResponse().setContentType("image/png"); BufferedImage image = new BufferedImage(QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, QR_IMAGE_WIDTH, QR_IMAGE_HEIGHT); graphics.setColor(Color.BLACK); for (int x = 0; x < QR_IMAGE_WIDTH; x++) { for (int y = 0; y < QR_IMAGE_HEIGHT; y++) { if (matrix.get(x, y)) { graphics.fillRect(x, y, 1, 1); } } } ImageIO.write(image, "png", getResponse().getOutputStream()); getResponse().getOutputStream().flush(); getResponse().getOutputStream().close(); } catch (IOException | WriterException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return null;
592
675
1,267
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/control/ProfileKtrl.java
ProfileKtrl
saveProfile
class ProfileKtrl extends BaseKontroller { private static final Logger log = LoggerFactory.getLogger(ProfileKtrl.class); @Model(name = "sortedSet") SortedSet sortedSet = new SortedSet(); @Model(name = "profile") Profile profile = new Profile(); public ProfileKtrl(HttpServletRequest request, HttpServletResponse response) { super(request, response); } @Kontrol(path = "/manage/viewProfiles", method = MethodType.GET) public String viewSystems() throws ServletException { try { sortedSet = ProfileDB.getProfileSet(sortedSet); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "/manage/view_profiles.html"; } @Kontrol(path = "/manage/saveProfile", method = MethodType.POST) public String saveProfile() throws ServletException {<FILL_FUNCTION_BODY>} @Kontrol(path = "/manage/deleteProfile", method = MethodType.GET) public String deleteProfile() throws ServletException { if (profile.getId() != null) { try { ProfileDB.deleteProfile(profile.getId()); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } } return "redirect:/manage/viewProfiles.ktrl?sortedSet.orderByDirection=" + sortedSet.getOrderByDirection() + "&sortedSet.orderByField=" + sortedSet.getOrderByField(); } /** * validate save profile */ @Validate(input = "/manage/view_profiles.html") public void validateSaveProfile() throws ServletException { if (profile == null || profile.getNm() == null || profile.getNm().trim().equals("")) { addFieldError("profile.nm", "Required"); } if (!this.getFieldErrors().isEmpty()) { try { sortedSet = ProfileDB.getProfileSet(sortedSet); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } } } }
try { if (profile.getId() != null) { ProfileDB.updateProfile(profile); } else { ProfileDB.insertProfile(profile); } } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "redirect:/manage/viewProfiles.ktrl?sortedSet.orderByDirection=" + sortedSet.getOrderByDirection() + "&sortedSet.orderByField=" + sortedSet.getOrderByField();
693
161
854
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/control/ProfileSystemsKtrl.java
ProfileSystemsKtrl
viewProfileSystems
class ProfileSystemsKtrl extends BaseKontroller { private static final Logger log = LoggerFactory.getLogger(ProfileSystemsKtrl.class); @Model(name = "profile") Profile profile; @Model(name = "sortedSet") SortedSet sortedSet = new SortedSet(); @Model(name = "systemSelectId") List<Long> systemSelectId = new ArrayList<>(); public ProfileSystemsKtrl(HttpServletRequest request, HttpServletResponse response) { super(request, response); } @Kontrol(path = "/manage/viewProfileSystems", method = MethodType.GET) public String viewProfileSystems() throws ServletException {<FILL_FUNCTION_BODY>} @Kontrol(path = "/manage/assignSystemsToProfile", method = MethodType.POST) public String assignSystemsToProfile() throws ServletException { if (systemSelectId != null) { try { ProfileSystemsDB.setSystemsForProfile(profile.getId(), systemSelectId); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } } RefreshAuthKeyUtil.refreshProfileSystems(profile.getId()); return "redirect:/manage/viewProfiles.ktrl"; } }
if (profile != null && profile.getId() != null) { try { profile = ProfileDB.getProfile(profile.getId()); sortedSet = SystemDB.getSystemSet(sortedSet, profile.getId()); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } } return "/manage/view_profile_systems.html";
361
122
483
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/control/ProfileUsersKtrl.java
ProfileUsersKtrl
assignSystemsToProfile
class ProfileUsersKtrl extends BaseKontroller { private static final Logger log = LoggerFactory.getLogger(ProfileUsersKtrl.class); @Model(name = "profile") Profile profile; @Model(name = "sortedSet") SortedSet sortedSet = new SortedSet(); @Model(name = "userSelectId") List<Long> userSelectId = new ArrayList<>(); public ProfileUsersKtrl(HttpServletRequest request, HttpServletResponse response) { super(request, response); } @Kontrol(path = "/manage/viewProfileUsers", method = MethodType.GET) public String viewProfileUsers() throws ServletException { if (profile != null && profile.getId() != null) { try { profile = ProfileDB.getProfile(profile.getId()); sortedSet = UserDB.getAdminUserSet(sortedSet, profile.getId()); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } } return "/manage/view_profile_users.html"; } @Kontrol(path = "/manage/assignUsersToProfile", method = MethodType.POST) public String assignSystemsToProfile() throws ServletException {<FILL_FUNCTION_BODY>} }
if (userSelectId != null) { try { UserProfileDB.setUsersForProfile(profile.getId(), userSelectId); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } } RefreshAuthKeyUtil.refreshProfileSystems(profile.getId()); return "redirect:/manage/viewProfiles.ktrl";
351
120
471
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/control/ScriptKtrl.java
ScriptKtrl
validateSaveScript
class ScriptKtrl extends BaseKontroller { private static final Logger log = LoggerFactory.getLogger(ScriptKtrl.class); @Model(name = "sortedSet") SortedSet sortedSet = new SortedSet(); @Model(name = "script") Script script = new Script(); public ScriptKtrl(HttpServletRequest request, HttpServletResponse response) { super(request, response); } @Kontrol(path = "/admin/viewScripts", method = MethodType.GET) public String viewScripts() throws ServletException { try { Long userId = AuthUtil.getUserId(getRequest().getSession()); sortedSet = ScriptDB.getScriptSet(sortedSet, userId); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "/admin/view_scripts.html"; } @Kontrol(path = "/admin/saveScript", method = MethodType.POST) public String saveScript() throws ServletException { try { Long userId = AuthUtil.getUserId(getRequest().getSession()); if (script.getId() != null) { ScriptDB.updateScript(script, userId); } else { ScriptDB.insertScript(script, userId); } } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "redirect:/admin/viewScripts.ktrl?sortedSet.orderByDirection=" + sortedSet.getOrderByDirection() + "&sortedSet.orderByField=" + sortedSet.getOrderByField(); } @Kontrol(path = "/admin/deleteScript", method = MethodType.GET) public String deleteScript() throws ServletException { if (script.getId() != null) { try { Long userId = AuthUtil.getUserId(getRequest().getSession()); ScriptDB.deleteScript(script.getId(), userId); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } } return "redirect:/admin/viewScripts.ktrl?sortedSet.orderByDirection=" + sortedSet.getOrderByDirection() + "&sortedSet.orderByField=" + sortedSet.getOrderByField(); } /** * Validates all fields for adding a user */ @Validate(input = "/admin/view_scripts.html") public void validateSaveScript() throws ServletException {<FILL_FUNCTION_BODY>} }
if (script == null || script.getDisplayNm() == null || script.getDisplayNm().trim().equals("")) { addFieldError("script.displayNm", "Required"); } if (script == null || script.getScript() == null || script.getScript().trim().equals("") || (new Script()).getScript().trim().equals(script.getScript().trim())) { addFieldError("script.script", "Required"); } if (!this.getFieldErrors().isEmpty()) { try { Long userId = AuthUtil.getUserId(getRequest().getSession()); sortedSet = ScriptDB.getScriptSet(sortedSet, userId); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } }
707
227
934
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/control/SessionAuditKtrl.java
SessionAuditKtrl
getJSONTermOutputForSession
class SessionAuditKtrl extends BaseKontroller { private static final Logger log = LoggerFactory.getLogger(SessionAuditKtrl.class); @Model(name = "sortedSet") SortedSet sortedSet = new SortedSet(); @Model(name = "sessionId") Long sessionId; @Model(name = "instanceId") Integer instanceId; @Model(name = "sessionAudit") SessionAudit sessionAudit; @Model(name = "systemList") List<HostSystem> systemList; @Model(name = "userList") List<User> userList; public SessionAuditKtrl(HttpServletRequest request, HttpServletResponse response) { super(request, response); } @Kontrol(path = "/manage/viewSessions", method = MethodType.GET) public String viewSessions() throws ServletException { if (sortedSet.getOrderByField() == null || sortedSet.getOrderByField().trim().equals("")) { sortedSet.setOrderByField(SessionAuditDB.SORT_BY_SESSION_TM); sortedSet.setOrderByDirection("desc"); } try { systemList = SystemDB.getSystemSet(new SortedSet(SystemDB.SORT_BY_NAME)).getItemList(); userList = UserDB.getUserSet(new SortedSet(SessionAuditDB.SORT_BY_USERNAME)).getItemList(); sortedSet = SessionAuditDB.getSessions(sortedSet); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "/manage/view_sessions.html"; } @Kontrol(path = "/manage/getTermsForSession", method = MethodType.GET) public String getTermsForSession() throws ServletException { try { sessionAudit = SessionAuditDB.getSessionsTerminals(sessionId); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "/manage/view_terms.html"; } @Kontrol(path = "/manage/getJSONTermOutputForSession", method = MethodType.GET) public String getJSONTermOutputForSession() throws ServletException {<FILL_FUNCTION_BODY>} }
try { String json = new Gson().toJson(SessionAuditDB.getTerminalLogsForSession(sessionId, instanceId)); getResponse().getOutputStream().write(json.getBytes()); } catch (SQLException | GeneralSecurityException | IOException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return null;
644
106
750
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/control/SystemKtrl.java
SystemKtrl
validateSaveSystem
class SystemKtrl extends BaseKontroller { private static final Logger log = LoggerFactory.getLogger(SystemKtrl.class); public static final String REQUIRED = "Required"; @Model(name = "sortedSet") SortedSet sortedSet = new SortedSet(); @Model(name = "hostSystem") HostSystem hostSystem = new HostSystem(); @Model(name = "script") Script script = null; @Model(name = "password") String password; @Model(name = "passphrase") String passphrase; @Model(name = "profileList") List<Profile> profileList = new ArrayList<>(); public SystemKtrl(HttpServletRequest request, HttpServletResponse response) { super(request, response); } @Kontrol(path = "/admin/viewSystems", method = MethodType.GET) public String viewAdminSystems() throws ServletException { try { Long userId = AuthUtil.getUserId(getRequest().getSession()); if (Auth.MANAGER.equals(AuthUtil.getUserType(getRequest().getSession()))) { sortedSet = SystemDB.getSystemSet(sortedSet); profileList = ProfileDB.getAllProfiles(); } else { sortedSet = SystemDB.getUserSystemSet(sortedSet, userId); profileList = UserProfileDB.getProfilesByUser(userId); } if (script != null && script.getId() != null) { script = ScriptDB.getScript(script.getId(), userId); } } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "/admin/view_systems.html"; } @Kontrol(path = "/manage/viewSystems", method = MethodType.GET) public String viewManageSystems() throws ServletException { try { sortedSet = SystemDB.getSystemSet(sortedSet); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "/manage/view_systems.html"; } @Kontrol(path = "/manage/saveSystem", method = MethodType.POST) public String saveSystem() throws ServletException { String retVal = "redirect:/manage/viewSystems.ktrl?sortedSet.orderByDirection=" + sortedSet.getOrderByDirection() + "&sortedSet.orderByField=" + sortedSet.getOrderByField(); hostSystem = SSHUtil.authAndAddPubKey(hostSystem, passphrase, password); try { if (hostSystem.getId() != null) { SystemDB.updateSystem(hostSystem); } else { hostSystem.setId(SystemDB.insertSystem(hostSystem)); } sortedSet = SystemDB.getSystemSet(sortedSet); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } if (!HostSystem.SUCCESS_STATUS.equals(hostSystem.getStatusCd())) { retVal = "/manage/view_systems.html"; } return retVal; } @Kontrol(path = "/manage/deleteSystem", method = MethodType.GET) public String deleteSystem() throws ServletException { if (hostSystem.getId() != null) { try { SystemDB.deleteSystem(hostSystem.getId()); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } } return "redirect:/manage/viewSystems.ktrl?sortedSet.orderByDirection=" + sortedSet.getOrderByDirection() + "&sortedSet.orderByField=" + sortedSet.getOrderByField(); } /** * Validates all fields for adding a host system */ @Validate(input = "/manage/view_systems.html") public void validateSaveSystem() throws ServletException {<FILL_FUNCTION_BODY>} }
if (hostSystem == null || hostSystem.getDisplayNm() == null || hostSystem.getDisplayNm().trim().equals("")) { addFieldError("hostSystem.displayNm", REQUIRED); } if (hostSystem == null || hostSystem.getUser() == null || hostSystem.getUser().trim().equals("")) { addFieldError("hostSystem.user", REQUIRED); } if (hostSystem == null || hostSystem.getHost() == null || hostSystem.getHost().trim().equals("")) { addFieldError("hostSystem.host", REQUIRED); } if (hostSystem == null || hostSystem.getPort() == null) { addFieldError("hostSystem.port", REQUIRED); } else if (!(hostSystem.getPort() > 0)) { addFieldError("hostSystem.port", "Invalid"); } if (hostSystem == null || hostSystem.getAuthorizedKeys() == null || hostSystem.getAuthorizedKeys().trim().equals("") || hostSystem.getAuthorizedKeys().trim().equals("~")) { addFieldError("hostSystem.authorizedKeys", REQUIRED); } if (!this.getFieldErrors().isEmpty()) { try { sortedSet = SystemDB.getSystemSet(sortedSet); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } }
1,208
432
1,640
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/control/UploadAndPushKtrl.java
UploadAndPushKtrl
push
class UploadAndPushKtrl extends BaseKontroller { private static final Logger log = LoggerFactory.getLogger(UploadAndPushKtrl.class); public static final String UPLOAD_PATH = DBUtils.class.getClassLoader().getResource(".").getPath() + "../upload"; @Model(name = "upload") File upload; @Model(name = "uploadFileName") String uploadFileName; @Model(name = "idList") List<Long> idList = new ArrayList<>(); @Model(name = "pushDir") String pushDir = "~"; @Model(name = "hostSystemList") List<HostSystem> hostSystemList; @Model(name = "pendingSystemStatus") HostSystem pendingSystemStatus; @Model(name = "currentSystemStatus") HostSystem currentSystemStatus; public UploadAndPushKtrl(HttpServletRequest request, HttpServletResponse response) { super(request, response); } @Kontrol(path = "/admin/setUpload", method = MethodType.GET) public String setUpload() throws Exception { Long userId = AuthUtil.getUserId(getRequest().getSession()); SystemStatusDB.setInitialSystemStatus(idList, userId, AuthUtil.getUserType(getRequest().getSession())); return "/admin/upload.html"; } @Kontrol(path = "/admin/uploadSubmit", method = MethodType.POST) public String uploadSubmit() { String retVal = "/admin/upload_result.html"; try { Long userId = AuthUtil.getUserId(getRequest().getSession()); List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(getRequest()); for (FileItem item : multiparts) { if (!item.isFormField()) { uploadFileName = new File(item.getName()).getName(); File path = new File(UPLOAD_PATH); if (!path.exists()) { path.mkdirs(); } upload = new File(UPLOAD_PATH + File.separator + uploadFileName); item.write(upload); } else { pushDir = item.getString(); } } pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId); hostSystemList = SystemStatusDB.getAllSystemStatus(userId); } catch (Exception ex) { log.error(ex.toString(), ex); retVal = "/admin/upload.html"; } //reset csrf token back since it's already set on page load getRequest().getSession().setAttribute(SecurityFilter._CSRF, getRequest().getParameter(SecurityFilter._CSRF)); return retVal; } @Kontrol(path = "/admin/push", method = MethodType.POST) public String push() throws ServletException {<FILL_FUNCTION_BODY>} }
try { Long userId = AuthUtil.getUserId(getRequest().getSession()); Long sessionId = AuthUtil.getSessionId(getRequest().getSession()); //get next pending system pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId); if (pendingSystemStatus != null) { //get session for system SchSession session = null; for (Integer instanceId : SecureShellKtrl.getUserSchSessionMap().get(sessionId).getSchSessionMap().keySet()) { //if host system id matches pending system then upload if (pendingSystemStatus.getId().equals(SecureShellKtrl.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId).getHostSystem().getId())) { session = SecureShellKtrl.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId); } } if (session != null) { //push upload to system currentSystemStatus = SSHUtil.pushUpload(pendingSystemStatus, session.getSession(), UPLOAD_PATH + "/" + uploadFileName, pushDir + "/" + uploadFileName); //update system status SystemStatusDB.updateSystemStatus(currentSystemStatus, userId); pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId); } } //if push has finished to all servers then delete uploaded file if (pendingSystemStatus == null) { File delFile = new File(UPLOAD_PATH, uploadFileName); FileUtils.deleteQuietly(delFile); //delete all expired files in upload path File delDir = new File(UPLOAD_PATH); if (delDir.isDirectory()) { //set expire time to delete all files older than 48 hrs Calendar expireTime = Calendar.getInstance(); expireTime.add(Calendar.HOUR, -48); Iterator<File> filesToDelete = FileUtils.iterateFiles(delDir, new AgeFileFilter(expireTime.getTime()), TrueFileFilter.TRUE); while (filesToDelete.hasNext()) { delFile = filesToDelete.next(); delFile.delete(); } } } hostSystemList = SystemStatusDB.getAllSystemStatus(userId); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } //reset csrf token back since it's already set on page load getRequest().getSession().setAttribute(SecurityFilter._CSRF, getRequest().getParameter(SecurityFilter._CSRF)); return "/admin/upload_result.html";
750
689
1,439
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/control/UserSettingsKtrl.java
UserSettingsKtrl
passwordSubmit
class UserSettingsKtrl extends BaseKontroller { private static final Logger log = LoggerFactory.getLogger(UserSettingsKtrl.class); public static final String REQUIRED = "Required"; @Model(name = "themeMap") static Map<String, String> themeMap1 = new LinkedHashMap<>(Map.ofEntries( entry("Tango", "#2e3436,#cc0000,#4e9a06,#c4a000,#3465a4,#75507b,#06989a,#d3d7cf,#555753,#ef2929,#8ae234,#fce94f,#729fcf,#ad7fa8,#34e2e2,#eeeeec"), entry("XTerm", "#000000,#cd0000,#00cd00,#cdcd00,#0000ee,#cd00cd,#00cdcd,#e5e5e5,#7f7f7f,#ff0000,#00ff00,#ffff00,#5c5cff,#ff00ff,#00ffff,#ffffff") )); @Model(name = "planeMap") static Map<String, String> planeMap1 = new LinkedHashMap<>(Map.ofEntries( entry("Black on light yellow", "#FFFFDD,#000000"), entry("Black on white", "#FFFFFF,#000000"), entry("Gray on black", "#000000,#AAAAAA"), entry("Green on black", "#000000,#00FF00"), entry("White on black", "#000000,#FFFFFF") )); @Model(name = "publicKey") static String publicKey; @Model(name = "auth") Auth auth; @Model(name = "userSettings") UserSettings userSettings; static { try { publicKey = PrivateKeyDB.getApplicationKey().getPublicKey(); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); } } public UserSettingsKtrl(HttpServletRequest request, HttpServletResponse response) { super(request, response); } @Kontrol(path = "/admin/userSettings", method = MethodType.GET) public String userSettings() throws ServletException { try { userSettings = UserThemeDB.getTheme(AuthUtil.getUserId(getRequest().getSession())); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "/admin/user_settings.html"; } @Kontrol(path = "/admin/passwordSubmit", method = MethodType.POST) public String passwordSubmit() throws ServletException {<FILL_FUNCTION_BODY>} @Kontrol(path = "/admin/themeSubmit", method = MethodType.POST) public String themeSubmit() throws ServletException { userSettings.setTheme(userSettings.getTheme()); userSettings.setPlane(userSettings.getPlane()); try { UserThemeDB.saveTheme(AuthUtil.getUserId(getRequest().getSession()), userSettings); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "redirect:/admin/menu.html"; } /** * Validates fields for password submit */ @Validate(input = "/admin/user_settings.html") public void validatePasswordSubmit() { if (auth.getPassword() == null || auth.getPassword().trim().equals("")) { addFieldError("auth.password", REQUIRED); } if (auth.getPasswordConfirm() == null || auth.getPasswordConfirm().trim().equals("")) { addFieldError("auth.passwordConfirm", REQUIRED); } if (auth.getPrevPassword() == null || auth.getPrevPassword().trim().equals("")) { addFieldError("auth.prevPassword", REQUIRED); } } }
String retVal = "/admin/user_settings.html"; if (!auth.getPassword().equals(auth.getPasswordConfirm())) { addError("Passwords do not match"); } else if (!PasswordUtil.isValid(auth.getPassword())) { addError(PasswordUtil.PASSWORD_REQ_ERROR_MSG); } else { try { auth.setAuthToken(AuthUtil.getAuthToken(getRequest().getSession())); if (AuthDB.updatePassword(auth)) { retVal = "redirect:/admin/menu.html"; } else { addError("Current password is invalid"); } } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } } return retVal;
1,212
245
1,457
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/control/UsersKtrl.java
UsersKtrl
validateSaveUser
class UsersKtrl extends BaseKontroller { private static final Logger log = LoggerFactory.getLogger(UsersKtrl.class); public static final String REQUIRED = "Required"; @Model(name = "sortedSet") SortedSet sortedSet = new SortedSet(); @Model(name = "user") User user = new User(); @Model(name = "resetSharedSecret") Boolean resetSharedSecret = false; @Model(name = "userId") Long userId; public UsersKtrl(HttpServletRequest request, HttpServletResponse response) { super(request, response); } @Kontrol(path = "/manage/viewUsers", method = MethodType.GET) public String viewUsers() throws ServletException { try { userId = AuthUtil.getUserId(getRequest().getSession()); sortedSet = UserDB.getUserSet(sortedSet); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "/manage/view_users.html"; } @Kontrol(path = "/manage/saveUser", method = MethodType.POST) public String saveUser() throws ServletException { String retVal = "redirect:/manage/viewUsers.ktrl?sortedSet.orderByDirection=" + sortedSet.getOrderByDirection() + "&sortedSet.orderByField=" + sortedSet.getOrderByField(); try { if (user.getId() != null) { if (user.getPassword() == null || user.getPassword().trim().equals("")) { UserDB.updateUserNoCredentials(user); } else { UserDB.updateUserCredentials(user); } //check if reset is set if (resetSharedSecret) { UserDB.resetSharedSecret(user.getId()); } } else { UserDB.insertUser(user); } } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return retVal; } @Kontrol(path = "/manage/deleteUser", method = MethodType.GET) public String deleteUser() throws ServletException { try { if (user.getId() != null && !user.getId().equals(AuthUtil.getUserId(getRequest().getSession()))) { UserDB.deleteUser(user.getId()); PublicKeyDB.deleteUserPublicKeys(user.getId()); RefreshAuthKeyUtil.refreshAllSystems(); } } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "redirect:/manage/viewUsers.ktrl?sortedSet.orderByDirection=" + sortedSet.getOrderByDirection() + "&sortedSet.orderByField=" + sortedSet.getOrderByField(); } @Kontrol(path = "/manage/unlockUser", method = MethodType.GET) public String unlockUser() throws ServletException { try { if (user.getId() != null && !user.getId().equals(AuthUtil.getUserId(getRequest().getSession()))) { UserDB.unlockAccount(user.getId()); } } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); } return "redirect:/manage/viewUsers.ktrl?sortedSet.orderByDirection=" + sortedSet.getOrderByDirection() + "&sortedSet.orderByField=" + sortedSet.getOrderByField(); } /** * Validates all fields for adding a user */ @Validate(input = "/manage/view_users.html") public void validateSaveUser() throws ServletException {<FILL_FUNCTION_BODY>} }
if (user == null || user.getUsername() == null || user.getUsername().trim().equals("")) { addFieldError("user.username", REQUIRED); } if (user == null || user.getLastNm() == null || user.getLastNm().trim().equals("")) { addFieldError("user.lastNm", REQUIRED); } if (user == null || user.getFirstNm() == null || user.getFirstNm().trim().equals("")) { addFieldError("user.firstNm", REQUIRED); } if (user != null && user.getPassword() != null && !user.getPassword().trim().equals("")) { if (!user.getPassword().equals(user.getPasswordConfirm())) { addError("Passwords do not match"); } else if (!PasswordUtil.isValid(user.getPassword())) { addError(PasswordUtil.PASSWORD_REQ_ERROR_MSG); } } if (user != null && user.getId() == null && !Auth.AUTH_EXTERNAL.equals(user.getAuthType()) && (user.getPassword() == null || user.getPassword().trim().equals(""))) { addError("Password is required"); } try { if (user != null && !UserDB.isUnique(user.getId(), user.getUsername())) { addError("Username has been taken"); } if (!this.getFieldErrors().isEmpty() || !this.getErrors().isEmpty()) { userId = AuthUtil.getUserId(getRequest().getSession()); sortedSet = UserDB.getUserSet(sortedSet); } } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); throw new ServletException(ex.toString(), ex); }
1,133
528
1,661
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/db/PrivateKeyDB.java
PrivateKeyDB
getApplicationKey
class PrivateKeyDB { private PrivateKeyDB() { } /** * returns public private key for application * * @return app key values */ public static ApplicationKey getApplicationKey() throws SQLException, GeneralSecurityException {<FILL_FUNCTION_BODY>} }
ApplicationKey appKey = null; Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("select * from application_key"); ResultSet rs = stmt.executeQuery(); while (rs.next()) { appKey = new ApplicationKey(); appKey.setId(rs.getLong("id")); appKey.setPassphrase(EncryptionUtil.decrypt(rs.getString("passphrase"))); appKey.setPrivateKey(EncryptionUtil.decrypt(rs.getString("private_key"))); appKey.setPublicKey(rs.getString("public_key")); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); return appKey;
77
207
284
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/db/ProfileDB.java
ProfileDB
getProfileSet
class ProfileDB { public static final String FILTER_BY_SYSTEM = "system"; public static final String FILTER_BY_USER = "username"; public static final String SORT_BY_PROFILE_NM = "nm"; private ProfileDB() { } /** * method to do order by based on the sorted set object for profiles * * @return list of profiles */ public static SortedSet getProfileSet(SortedSet sortedSet) throws SQLException, GeneralSecurityException {<FILL_FUNCTION_BODY>} /** * returns all profile information * * @return list of profiles */ public static List<Profile> getAllProfiles() throws SQLException, GeneralSecurityException { ArrayList<Profile> profileList = new ArrayList<>(); Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("select * from profiles order by nm asc"); ResultSet rs = stmt.executeQuery(); while (rs.next()) { Profile profile = new Profile(); profile.setId(rs.getLong("id")); profile.setNm(rs.getString("nm")); profile.setDesc(rs.getString("desc")); profileList.add(profile); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); return profileList; } /** * returns profile based on id * * @param profileId profile id * @return profile */ public static Profile getProfile(Long profileId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); Profile profile = getProfile(con, profileId); DBUtils.closeConn(con); return profile; } /** * returns profile based on id * * @param con db connection object * @param profileId profile id * @return profile */ public static Profile getProfile(Connection con, Long profileId) throws SQLException { Profile profile = null; PreparedStatement stmt = con.prepareStatement("select * from profiles where id=?"); stmt.setLong(1, profileId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { profile = new Profile(); profile.setId(rs.getLong("id")); profile.setNm(rs.getString("nm")); profile.setDesc(rs.getString("desc")); profile.setHostSystemList(ProfileSystemsDB.getSystemsByProfile(con, profileId)); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); return profile; } /** * inserts new profile * * @param profile profile object */ public static void insertProfile(Profile profile) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("insert into profiles (nm, desc) values (?,?)"); stmt.setString(1, profile.getNm()); stmt.setString(2, profile.getDesc()); stmt.execute(); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); } /** * updates profile * * @param profile profile object */ public static void updateProfile(Profile profile) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("update profiles set nm=?, desc=? where id=?"); stmt.setString(1, profile.getNm()); stmt.setString(2, profile.getDesc()); stmt.setLong(3, profile.getId()); stmt.execute(); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); } /** * deletes profile * * @param profileId profile id */ public static void deleteProfile(Long profileId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("delete from profiles where id=?"); stmt.setLong(1, profileId); stmt.execute(); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); } }
ArrayList<Profile> profileList = new ArrayList<>(); String orderBy = ""; if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) { orderBy = " order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection(); } String sql = "select distinct p.* from profiles p "; if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_SYSTEM))) { sql = sql + ", system_map m, system s where m.profile_id = p.id and m.system_id = s.id" + " and (lower(s.display_nm) like ? or lower(s.host) like ?)"; } else if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_USER))) { sql = sql + ", user_map m, users u where m.profile_id = p.id and m.user_id = u.id" + " and (lower(u.first_nm) like ? or lower(u.last_nm) like ?" + " or lower(u.email) like ? or lower(u.username) like ?)"; } sql = sql + orderBy; Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement(sql); if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_SYSTEM))) { stmt.setString(1, "%" + sortedSet.getFilterMap().get(FILTER_BY_SYSTEM).toLowerCase() + "%"); stmt.setString(2, "%" + sortedSet.getFilterMap().get(FILTER_BY_SYSTEM).toLowerCase() + "%"); } else if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_USER))) { stmt.setString(1, "%" + sortedSet.getFilterMap().get(FILTER_BY_USER).toLowerCase() + "%"); stmt.setString(2, "%" + sortedSet.getFilterMap().get(FILTER_BY_USER).toLowerCase() + "%"); stmt.setString(3, "%" + sortedSet.getFilterMap().get(FILTER_BY_USER).toLowerCase() + "%"); stmt.setString(4, "%" + sortedSet.getFilterMap().get(FILTER_BY_USER).toLowerCase() + "%"); } ResultSet rs = stmt.executeQuery(); while (rs.next()) { Profile profile = new Profile(); profile.setId(rs.getLong("id")); profile.setNm(rs.getString("nm")); profile.setDesc(rs.getString("desc")); profileList.add(profile); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); sortedSet.setItemList(profileList); return sortedSet;
1,136
785
1,921
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/db/ProfileSystemsDB.java
ProfileSystemsDB
getSystemIdsByProfile
class ProfileSystemsDB { private ProfileSystemsDB() { } /** * sets host systems for profile * * @param profileId profile id * @param systemIdList list of host system ids */ public static void setSystemsForProfile(Long profileId, List<Long> systemIdList) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("delete from system_map where profile_id=?"); stmt.setLong(1, profileId); stmt.execute(); DBUtils.closeStmt(stmt); for (Long systemId : systemIdList) { stmt = con.prepareStatement("insert into system_map (profile_id, system_id) values (?,?)"); stmt.setLong(1, profileId); stmt.setLong(2, systemId); stmt.execute(); DBUtils.closeStmt(stmt); } DBUtils.closeConn(con); } /** * returns a list of systems for a given profile * * @param con DB connection * @param profileId profile id * @return list of host systems */ public static List<HostSystem> getSystemsByProfile(Connection con, Long profileId) throws SQLException { List<HostSystem> hostSystemList = new ArrayList<>(); PreparedStatement stmt = con.prepareStatement("select * from system s, system_map m where s.id=m.system_id and m.profile_id=? order by display_nm asc"); stmt.setLong(1, profileId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { HostSystem hostSystem = new HostSystem(); hostSystem.setId(rs.getLong("id")); hostSystem.setDisplayNm(rs.getString("display_nm")); hostSystem.setUser(rs.getString("username")); hostSystem.setHost(rs.getString("host")); hostSystem.setPort(rs.getInt("port")); hostSystem.setAuthorizedKeys(rs.getString("authorized_keys")); hostSystemList.add(hostSystem); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); return hostSystemList; } /** * returns a list of systems for a given profile * * @param profileId profile id * @return list of host systems */ public static List<HostSystem> getSystemsByProfile(Long profileId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); List<HostSystem> hostSystemList = getSystemsByProfile(con, profileId); DBUtils.closeConn(con); return hostSystemList; } /** * returns a list of system ids for a given profile * * @param con DB con * @param profileId profile id * @return list of host systems */ public static List<Long> getSystemIdsByProfile(Connection con, Long profileId) throws SQLException {<FILL_FUNCTION_BODY>} /** * returns a list of system ids for a given profile * * @param profileId profile id * @return list of host systems */ public static List<Long> getSystemIdsByProfile(Long profileId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); List<Long> systemIdList = getSystemIdsByProfile(con, profileId); DBUtils.closeConn(con); return systemIdList; } /** * returns a list of system ids for a given profile * * @param con DB con * @param profileId profile id * @param userId user id * @return list of host systems */ public static List<Long> getSystemIdsByProfile(Connection con, Long profileId, Long userId) throws SQLException { List<Long> systemIdList = new ArrayList<>(); PreparedStatement stmt = con.prepareStatement("select sm.system_id from system_map sm, user_map um where um.profile_id=sm.profile_id and sm.profile_id=? and um.user_id=?"); stmt.setLong(1, profileId); stmt.setLong(2, userId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { systemIdList.add(rs.getLong("system_id")); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); return systemIdList; } /** * returns a list of system ids for a given profile * * @param profileId profile id * @param userId user id * @return list of host systems */ public static List<Long> getSystemIdsByProfile(Long profileId, Long userId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); List<Long> systemIdList = getSystemIdsByProfile(con, profileId, userId); DBUtils.closeConn(con); return systemIdList; } }
List<Long> systemIdList = new ArrayList<>(); PreparedStatement stmt = con.prepareStatement("select * from system s, system_map m where s.id=m.system_id and m.profile_id=? order by display_nm asc"); stmt.setLong(1, profileId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { systemIdList.add(rs.getLong("id")); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); return systemIdList;
1,344
151
1,495
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/db/ScriptDB.java
ScriptDB
getScript
class ScriptDB { public static final String DISPLAY_NM = "display_nm"; public static final String SORT_BY_DISPLAY_NM = DISPLAY_NM; private ScriptDB() { } /** * returns scripts based on sort order defined * * @param sortedSet object that defines sort order * @param userId user id * @return sorted script list */ public static SortedSet getScriptSet(SortedSet sortedSet, Long userId) throws SQLException, GeneralSecurityException { ArrayList<Script> scriptList = new ArrayList<>(); String orderBy = ""; if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) { orderBy = "order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection(); } String sql = "select * from scripts where user_id=? " + orderBy; Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement(sql); stmt.setLong(1, userId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { Script script = new Script(); script.setId(rs.getLong("id")); script.setDisplayNm(rs.getString(DISPLAY_NM)); script.setScript(rs.getString("script")); scriptList.add(script); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); sortedSet.setItemList(scriptList); return sortedSet; } /** * returns script base on id * * @param scriptId script id * @param userId user id * @return script object */ public static Script getScript(Long scriptId, Long userId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); Script script = getScript(con, scriptId, userId); DBUtils.closeConn(con); return script; } /** * returns script base on id * * @param con DB connection * @param scriptId script id * @param userId user id * @return script object */ public static Script getScript(Connection con, Long scriptId, Long userId) throws SQLException {<FILL_FUNCTION_BODY>} /** * inserts new script * * @param script script object * @param userId user id */ public static void insertScript(Script script, Long userId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("insert into scripts (display_nm, script, user_id) values (?,?,?)"); stmt.setString(1, script.getDisplayNm()); stmt.setString(2, script.getScript()); stmt.setLong(3, userId); stmt.execute(); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); } /** * updates existing script * * @param script script object * @param userId user id */ public static void updateScript(Script script, Long userId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("update scripts set display_nm=?, script=? where id=? and user_id=?"); stmt.setString(1, script.getDisplayNm()); stmt.setString(2, script.getScript()); stmt.setLong(3, script.getId()); stmt.setLong(4, userId); stmt.execute(); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); } /** * deletes script * * @param scriptId script id * @param userId user id */ public static void deleteScript(Long scriptId, Long userId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("delete from scripts where id=? and user_id=?"); stmt.setLong(1, scriptId); stmt.setLong(2, userId); stmt.execute(); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); } }
Script script = null; PreparedStatement stmt = con.prepareStatement("select * from scripts where id=? and user_id=?"); stmt.setLong(1, scriptId); stmt.setLong(2, userId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { script = new Script(); script.setId(rs.getLong("id")); script.setDisplayNm(rs.getString(DISPLAY_NM)); script.setScript(rs.getString("script")); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); return script;
1,177
175
1,352
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/db/SystemStatusDB.java
SystemStatusDB
getNextPendingSystem
class SystemStatusDB { public static final String STATUS_CD = "status_cd"; private SystemStatusDB() { } /** * set the initial status for selected systems * * @param systemSelectIds systems ids to set initial status * @param userId user id * @param userType user type */ public static void setInitialSystemStatus(List<Long> systemSelectIds, Long userId, String userType) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); //checks perms if to see if in assigned profiles if (!Auth.MANAGER.equals(userType)) { systemSelectIds = SystemDB.checkSystemPerms(con, systemSelectIds, userId); } //deletes all old systems deleteAllSystemStatus(con, userId); for (Long hostSystemId : systemSelectIds) { HostSystem hostSystem = new HostSystem(); hostSystem.setId(hostSystemId); hostSystem.setStatusCd(HostSystem.INITIAL_STATUS); //insert new status insertSystemStatus(con, hostSystem, userId); } DBUtils.closeConn(con); } /** * deletes all records from status table for user * * @param con DB connection object * @param userId user id */ private static void deleteAllSystemStatus(Connection con, Long userId) throws SQLException { PreparedStatement stmt = con.prepareStatement("delete from status where user_id=?"); stmt.setLong(1, userId); stmt.execute(); DBUtils.closeStmt(stmt); } /** * inserts into the status table to keep track of key placement status * * @param con DB connection object * @param hostSystem systems for authorized_keys replacement * @param userId user id */ private static void insertSystemStatus(Connection con, HostSystem hostSystem, Long userId) throws SQLException { PreparedStatement stmt = con.prepareStatement("insert into status (id, status_cd, user_id) values (?,?,?)"); stmt.setLong(1, hostSystem.getId()); stmt.setString(2, hostSystem.getStatusCd()); stmt.setLong(3, userId); stmt.execute(); DBUtils.closeStmt(stmt); } /** * updates the status table to keep track of key placement status * * @param hostSystem systems for authorized_keys replacement * @param userId user id */ public static void updateSystemStatus(HostSystem hostSystem, Long userId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); updateSystemStatus(con, hostSystem, userId); DBUtils.closeConn(con); } /** * updates the status table to keep track of key placement status * * @param con DB connection * @param hostSystem systems for authorized_keys replacement * @param userId user id */ public static void updateSystemStatus(Connection con, HostSystem hostSystem, Long userId) throws SQLException { PreparedStatement stmt = con.prepareStatement("update status set status_cd=? where id=? and user_id=?"); stmt.setString(1, hostSystem.getStatusCd()); stmt.setLong(2, hostSystem.getId()); stmt.setLong(3, userId); stmt.execute(); DBUtils.closeStmt(stmt); } /** * returns all key placement statuses * * @param userId user id */ public static SortedSet getSortedSetStatus(Long userId) throws SQLException, GeneralSecurityException { SortedSet sortedSet = new SortedSet(); sortedSet.setItemList(getAllSystemStatus(userId)); return sortedSet; } /** * returns all key placement statuses * * @param userId user id */ public static List<HostSystem> getAllSystemStatus(Long userId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); List<HostSystem> hostSystemList = getAllSystemStatus(con, userId); DBUtils.closeConn(con); return hostSystemList; } /** * returns all key placement statuses * * @param con DB connection object * @param userId user id */ private static List<HostSystem> getAllSystemStatus(Connection con, Long userId) throws SQLException { List<HostSystem> hostSystemList = new ArrayList<>(); PreparedStatement stmt = con.prepareStatement("select * from status where user_id=? order by id asc"); stmt.setLong(1, userId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { HostSystem hostSystem = SystemDB.getSystem(con, rs.getLong("id")); hostSystem.setStatusCd(rs.getString(STATUS_CD)); hostSystemList.add(hostSystem); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); return hostSystemList; } /** * returns key placement status of system * * @param systemId system id * @param userId user id */ public static HostSystem getSystemStatus(Long systemId, Long userId) throws SQLException, GeneralSecurityException { HostSystem hostSystem = null; Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("select * from status where id=? and user_id=?"); stmt.setLong(1, systemId); stmt.setLong(2, userId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { hostSystem = SystemDB.getSystem(con, rs.getLong("id")); hostSystem.setStatusCd(rs.getString(STATUS_CD)); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); return hostSystem; } /** * returns the first system that authorized keys has not been tried * * @param userId user id * @return hostSystem systems for authorized_keys replacement */ public static HostSystem getNextPendingSystem(Long userId) throws SQLException, GeneralSecurityException {<FILL_FUNCTION_BODY>} }
HostSystem hostSystem = null; Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("select * from status where (status_cd like ? or status_cd like ? or status_cd like ?) and user_id=? order by id asc"); stmt.setString(1, HostSystem.INITIAL_STATUS); stmt.setString(2, HostSystem.AUTH_FAIL_STATUS); stmt.setString(3, HostSystem.PUBLIC_KEY_FAIL_STATUS); stmt.setLong(4, userId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { hostSystem = SystemDB.getSystem(con, rs.getLong("id")); hostSystem.setStatusCd(rs.getString(STATUS_CD)); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); return hostSystem;
1,683
255
1,938
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/db/UserProfileDB.java
UserProfileDB
checkIsUsersProfile
class UserProfileDB { private UserProfileDB() { } /** * sets users for profile * * @param profileId profile id * @param userIdList list of user ids */ public static void setUsersForProfile(Long profileId, List<Long> userIdList) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("delete from user_map where profile_id=?"); stmt.setLong(1, profileId); stmt.execute(); DBUtils.closeStmt(stmt); for (Long userId : userIdList) { stmt = con.prepareStatement("insert into user_map (profile_id, user_id) values (?,?)"); stmt.setLong(1, profileId); stmt.setLong(2, userId); stmt.execute(); DBUtils.closeStmt(stmt); } //delete all unassigned keys by profile PublicKeyDB.deleteUnassignedKeysByProfile(con, profileId); DBUtils.closeConn(con); } /** * return a list of profiles for user * * @param userId user id * @return profile list */ public static List<Profile> getProfilesByUser(Long userId) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); List<Profile> profileList = getProfilesByUser(con, userId); DBUtils.closeConn(con); return profileList; } /** * return a list of profiles for user * * @param userId user id * @return profile list */ public static List<Profile> getProfilesByUser(Connection con, Long userId) throws SQLException { ArrayList<Profile> profileList = new ArrayList<>(); PreparedStatement stmt = con.prepareStatement("select * from profiles g, user_map m where g.id=m.profile_id and m.user_id=? order by nm asc"); stmt.setLong(1, userId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { Profile profile = new Profile(); profile.setId(rs.getLong("id")); profile.setNm(rs.getString("nm")); profile.setDesc(rs.getString("desc")); profileList.add(profile); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); return profileList; } /** * checks to determine if user belongs to profile * * @param userId user id * @param profileId profile id * @return true if user belongs to profile */ public static boolean checkIsUsersProfile(Long userId, Long profileId) throws SQLException, GeneralSecurityException {<FILL_FUNCTION_BODY>} /** * assigns profiles to given user * * @param userId user id * @param allProfilesNmList list of all profiles * @param assignedProfilesNmList list of assigned profiles */ public static void assignProfilesToUser(Connection con, Long userId, List<String> allProfilesNmList, List<String> assignedProfilesNmList) throws SQLException { for (String profileNm : allProfilesNmList) { if (StringUtils.isNotEmpty(profileNm)) { Long profileId = null; PreparedStatement stmt = con.prepareStatement("select id from profiles p where lower(p.nm) like ?"); stmt.setString(1, profileNm.toLowerCase()); ResultSet rs = stmt.executeQuery(); while (rs.next()) { profileId = rs.getLong("id"); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); if (profileId != null) { stmt = con.prepareStatement("delete from user_map where profile_id=?"); stmt.setLong(1, profileId); stmt.execute(); DBUtils.closeStmt(stmt); if (assignedProfilesNmList.contains(profileNm)) { stmt = con.prepareStatement("insert into user_map (profile_id, user_id) values (?,?)"); stmt.setLong(1, profileId); stmt.setLong(2, userId); stmt.execute(); DBUtils.closeStmt(stmt); } //delete all unassigned keys by profile PublicKeyDB.deleteUnassignedKeysByProfile(con, profileId); } } } } /** * assigns profiles to given user * * @param userId user id * @param profileNm profile name */ public static void assignProfileToUser(Connection con, Long userId, String profileNm) throws SQLException { if (StringUtils.isNotEmpty(profileNm)) { Long profileId = null; PreparedStatement stmt = con.prepareStatement("select id from profiles p where lower(p.nm) like ?"); stmt.setString(1, profileNm.toLowerCase()); ResultSet rs = stmt.executeQuery(); while (rs.next()) { profileId = rs.getLong("id"); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); if (profileId != null) { stmt = con.prepareStatement("delete from user_map where profile_id=?"); stmt.setLong(1, profileId); stmt.execute(); DBUtils.closeStmt(stmt); stmt = con.prepareStatement("insert into user_map (profile_id, user_id) values (?,?)"); stmt.setLong(1, profileId); stmt.setLong(2, userId); stmt.execute(); DBUtils.closeStmt(stmt); //delete all unassigned keys by profile PublicKeyDB.deleteUnassignedKeysByProfile(con, profileId); } } } }
boolean isUsersProfile = false; Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("select * from user_map where profile_id=? and user_id=?"); stmt.setLong(1, profileId); stmt.setLong(2, userId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { isUsersProfile = true; } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); return isUsersProfile;
1,593
159
1,752
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/db/UserThemeDB.java
UserThemeDB
getTheme
class UserThemeDB { private UserThemeDB() { } /** * get user theme * * @param userId object * @return user theme object */ public static UserSettings getTheme(Long userId) throws SQLException, GeneralSecurityException {<FILL_FUNCTION_BODY>} /** * saves user theme * * @param userId object */ public static void saveTheme(Long userId, UserSettings theme) throws SQLException, GeneralSecurityException { Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("delete from user_theme where user_id=?"); stmt.setLong(1, userId); stmt.execute(); DBUtils.closeStmt(stmt); if (StringUtils.isNotEmpty(theme.getPlane()) || StringUtils.isNotEmpty(theme.getTheme())) { stmt = con.prepareStatement("insert into user_theme(user_id, bg, fg, d1, d2, d3, d4, d5, d6, d7, d8, b1, b2, b3, b4, b5, b6, b7, b8) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); stmt.setLong(1, userId); stmt.setString(2, theme.getBg()); stmt.setString(3, theme.getFg()); //if contains all 16 theme colors insert if (theme.getColors() != null && theme.getColors().length == 16) { for (int i = 0; i < 16; i++) { stmt.setString(i + 4, theme.getColors()[i]); } //else set to null } else { for (int i = 0; i < 16; i++) { stmt.setString(i + 4, null); } } stmt.execute(); DBUtils.closeStmt(stmt); } DBUtils.closeConn(con); } }
UserSettings theme = null; Connection con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement("select * from user_theme where user_id=?"); stmt.setLong(1, userId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { theme = new UserSettings(); theme.setBg(rs.getString("bg")); theme.setFg(rs.getString("fg")); if (StringUtils.isNotEmpty(rs.getString("d1"))) { String[] colors = new String[16]; colors[0] = rs.getString("d1"); colors[1] = rs.getString("d2"); colors[2] = rs.getString("d3"); colors[3] = rs.getString("d4"); colors[4] = rs.getString("d5"); colors[5] = rs.getString("d6"); colors[6] = rs.getString("d7"); colors[7] = rs.getString("d8"); colors[8] = rs.getString("b1"); colors[9] = rs.getString("b2"); colors[10] = rs.getString("b3"); colors[11] = rs.getString("b4"); colors[12] = rs.getString("b5"); colors[13] = rs.getString("b6"); colors[14] = rs.getString("b7"); colors[15] = rs.getString("b8"); theme.setColors(colors); } } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); DBUtils.closeConn(con); return theme;
541
456
997
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/model/SortedSet.java
SortedSet
getOrderByField
class SortedSet { private String orderByField = null; private String orderByDirection = "asc"; private List itemList; private Map<String, String> filterMap = new HashMap<>(); public SortedSet() { } public SortedSet(String orderByField) { this.orderByField = orderByField; } public String getOrderByField() {<FILL_FUNCTION_BODY>} public void setOrderByField(String orderByField) { this.orderByField = orderByField; } public String getOrderByDirection() { if ("asc".equalsIgnoreCase(orderByDirection)) { return "asc"; } else { return "desc"; } } public void setOrderByDirection(String orderByDirection) { this.orderByDirection = orderByDirection; } public List getItemList() { return itemList; } public void setItemList(List itemList) { this.itemList = itemList; } public Map<String, String> getFilterMap() { return filterMap; } public void setFilterMap(Map<String, String> filterMap) { this.filterMap = filterMap; } }
if (orderByField != null) { return orderByField.replaceAll("[^0-9,a-z,A-Z,\\_,\\.]", ""); } return null;
334
55
389
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/model/UserSettings.java
UserSettings
setPlane
class UserSettings { String[] colors = null; String bg; String fg; String plane; String theme; Integer ptyWidth; Integer ptyHeight; public String[] getColors() { return colors; } public void setColors(String[] colors) { this.colors = colors; } public String getBg() { return bg; } public void setBg(String bg) { this.bg = bg; } public String getFg() { return fg; } public void setFg(String fg) { this.fg = fg; } public String getPlane() { if (StringUtils.isNotEmpty(bg) && StringUtils.isNotEmpty(fg)) { plane = bg + "," + fg; } return plane; } public void setPlane(String plane) {<FILL_FUNCTION_BODY>} public String getTheme() { if (this.colors != null && this.colors.length == 16) { theme = StringUtils.join(this.colors, ","); } return theme; } public void setTheme(String theme) { if (StringUtils.isNotEmpty(theme) && theme.split(",").length == 16) { this.setColors(theme.split(",")); } this.theme = theme; } public Integer getPtyWidth() { return ptyWidth; } public void setPtyWidth(Integer ptyWidth) { this.ptyWidth = ptyWidth; } public Integer getPtyHeight() { return ptyHeight; } public void setPtyHeight(Integer ptyHeight) { this.ptyHeight = ptyHeight; } }
if (StringUtils.isNotEmpty(plane) && plane.split(",").length == 2) { this.setBg(plane.split(",")[0]); this.setFg(plane.split(",")[1]); } this.plane = plane;
494
73
567
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/task/SecureShellTask.java
SecureShellTask
run
class SecureShellTask implements Runnable { private static final Logger log = LoggerFactory.getLogger(SecureShellTask.class); InputStream outFromChannel; SessionOutput sessionOutput; public SecureShellTask(SessionOutput sessionOutput, InputStream outFromChannel) { this.sessionOutput = sessionOutput; this.outFromChannel = outFromChannel; } public void run() {<FILL_FUNCTION_BODY>} }
InputStreamReader isr = new InputStreamReader(outFromChannel); BufferedReader br = new BufferedReader(isr); SessionOutputUtil.addOutput(sessionOutput); char[] buff = new char[1024]; int read; try { while ((read = br.read(buff)) != -1) { SessionOutputUtil.addToOutput(sessionOutput.getSessionId(), sessionOutput.getInstanceId(), buff, 0, read); Thread.sleep(50); } SessionOutputUtil.removeOutput(sessionOutput.getSessionId(), sessionOutput.getInstanceId()); } catch (IOException | InterruptedException ex) { log.error(ex.toString(), ex); }
121
183
304
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/task/SentOutputTask.java
SentOutputTask
run
class SentOutputTask implements Runnable { private static final Logger log = LoggerFactory.getLogger(SentOutputTask.class); Session session; Long sessionId; User user; public SentOutputTask(Long sessionId, Session session, User user) { this.sessionId = sessionId; this.session = session; this.user = user; } public void run() {<FILL_FUNCTION_BODY>} }
Gson gson = new Gson(); while (session.isOpen()) { try { Connection con = DBUtils.getConn(); List<SessionOutput> outputList = SessionOutputUtil.getOutput(con, sessionId, user); if (!outputList.isEmpty()) { String json = gson.toJson(outputList); //send json to session this.session.getBasicRemote().sendText(json); } Thread.sleep(25); DBUtils.closeConn(con); } catch (SQLException | GeneralSecurityException | IOException | InterruptedException ex) { log.error(ex.toString(), ex); } }
120
171
291
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/util/DSPool.java
DSPool
registerDataSource
class DSPool { private static BasicDataSource dsPool = null; private static final String BASE_DIR = AppConfig.CONFIG_DIR; private static final String DB_DRIVER = AppConfig.getProperty("dbDriver"); private static final int MAX_ACTIVE = Integer.parseInt(AppConfig.getProperty("maxActive")); private static final boolean TEST_ON_BORROW = Boolean.parseBoolean(AppConfig.getProperty("testOnBorrow")); private static final int MIN_IDLE = Integer.parseInt(AppConfig.getProperty("minIdle")); private static final int MAX_WAIT = Integer.parseInt(AppConfig.getProperty("maxWait")); private DSPool() { } /** * fetches the data source for H2 db * * @return data source pool */ public static BasicDataSource getDataSource() throws GeneralSecurityException { if (dsPool == null) { dsPool = registerDataSource(); } return dsPool; } /** * register the data source for H2 DB * * @return pooling database object */ private static BasicDataSource registerDataSource() throws GeneralSecurityException {<FILL_FUNCTION_BODY>} }
System.setProperty("h2.baseDir", BASE_DIR); // create a database connection String user = AppConfig.getProperty("dbUser"); String password = AppConfig.decryptProperty("dbPassword"); String connectionURL = AppConfig.getProperty("dbConnectionURL"); if (connectionURL != null && connectionURL.contains("CIPHER=")) { password = "filepwd " + password; } String validationQuery = "select 1"; BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(DB_DRIVER); dataSource.setMaxTotal(MAX_ACTIVE); dataSource.setTestOnBorrow(TEST_ON_BORROW); dataSource.setMinIdle(MIN_IDLE); dataSource.setMaxWaitMillis(MAX_WAIT); dataSource.setValidationQuery(validationQuery); dataSource.setUsername(user); dataSource.setPassword(password); dataSource.setUrl(connectionURL); return dataSource;
320
269
589
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/util/EncryptionUtil.java
EncryptionUtil
encrypt
class EncryptionUtil { private static final Logger log = LoggerFactory.getLogger(EncryptionUtil.class); //secret key private static byte[] key = new byte[0]; static { try { key = KeyStoreUtil.getSecretBytes(KeyStoreUtil.ENCRYPTION_KEY_ALIAS); } catch (GeneralSecurityException ex) { log.error(ex.toString(), ex); } } public static final String CRYPT_ALGORITHM = "AES"; public static final String HASH_ALGORITHM = "SHA-256"; private EncryptionUtil() { } /** * generate salt for hash * * @return salt */ public static String generateSalt() { byte[] salt = new byte[32]; SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(salt); return new String(Base64.encodeBase64(salt)); } /** * return hash value of string * * @param str unhashed string * @param salt salt for hash * @return hash value of string */ public static String hash(String str, String salt) throws NoSuchAlgorithmException { String hash = null; MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM); if (StringUtils.isNotEmpty(salt)) { md.update(Base64.decodeBase64(salt.getBytes())); } md.update(str.getBytes(StandardCharsets.UTF_8)); hash = new String(Base64.encodeBase64(md.digest())); return hash; } /** * return hash value of string * * @param str unhashed string * @return hash value of string */ public static String hash(String str) throws NoSuchAlgorithmException { return hash(str, null); } /** * return encrypted value of string * * @param key secret key * @param str unencrypted string * @return encrypted string */ public static String encrypt(byte[] key, String str) throws GeneralSecurityException {<FILL_FUNCTION_BODY>} /** * return decrypted value of encrypted string * * @param key secret key * @param str encrypted string * @return decrypted string */ public static String decrypt(byte[] key, String str) throws GeneralSecurityException { String retVal = null; if (str != null && str.length() > 0) { Cipher c = Cipher.getInstance(CRYPT_ALGORITHM); c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, CRYPT_ALGORITHM)); byte[] decodedVal = Base64.decodeBase64(str.getBytes()); retVal = new String(c.doFinal(decodedVal)); } return retVal; } /** * return encrypted value of string * * @param str unencrypted string * @return encrypted string */ public static String encrypt(String str) throws GeneralSecurityException { return encrypt(key, str); } /** * return decrypted value of encrypted string * * @param str encrypted string * @return decrypted string */ public static String decrypt(String str) throws GeneralSecurityException { return decrypt(key, str); } }
String retVal = null; if (str != null && str.length() > 0) { Cipher c = Cipher.getInstance(CRYPT_ALGORITHM); c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, CRYPT_ALGORITHM)); byte[] encVal = c.doFinal(str.getBytes()); retVal = new String(Base64.encodeBase64(encVal)); } return retVal;
912
131
1,043
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/util/KeyStoreUtil.java
KeyStoreUtil
initializeKeyStore
class KeyStoreUtil { private static final Logger log = LoggerFactory.getLogger(KeyStoreUtil.class); private static KeyStore keyStore = null; private static final String keyStoreFile = AppConfig.CONFIG_DIR + "/bastillion.jceks"; private static final char[] KEYSTORE_PASS = new char[]{ 'G', '~', 'r', 'x', 'Z', 'E', 'w', 'f', 'a', '[', '!', 'f', 'Z', 'd', '*', 'L', '8', 'm', 'h', 'u', '#', 'j', '9', ':', '~', ';', 'U', '>', 'O', 'i', '8', 'r', 'C', '}', 'f', 't', '%', '[', 'H', 'h', 'M', '&', 'K', ':', 'l', '5', 'c', 'H', '6', 'r', 'A', 'E', '.', 'F', 'Y', 'W', '}', '{', '*', '8', 'd', 'E', 'C', 'A', '6', 'F', 'm', 'j', 'u', 'A', 'Q', '%', '{', '/', '@', 'm', '&', '5', 'S', 'q', '4', 'Q', '+', 'Y', '|', 'X', 'W', 'z', '8', '<', 'j', 'd', 'a', '}', '`', '0', 'N', 'B', '3', 'i', 'v', '5', 'U', ' ', '2', 'd', 'd', '(', '&', 'J', '_', '9', 'o', '(', '2', 'I', '`', ';', '>', '#', '$', 'X', 'j', '&', '&', '%', '>', '#', '7', 'q', '>', ')', 'L', 'A', 'v', 'h', 'j', 'i', '8', '~', ')', 'a', '~', 'W', '/', 'l', 'H', 'L', 'R', '+', '\\', 'i', 'R', '_', '+', 'y', 's', '0', 'n', '\'', '=', '{', 'B', ':', 'l', '1', '%', '^', 'd', 'n', 'H', 'X', 'B', '$', 'f', '"', '#', ')', '{', 'L', '/', 'q', '\'', 'O', '%', 's', 'M', 'Q', ']', 'D', 'v', ';', 'L', 'C', 'd', '?', 'D', 'l', 'h', 'd', 'i', 'N', '4', 'R', '>', 'O', ';', '$', '(', '4', '-', '0', '^', 'Y', ')', '5', 'V', 'M', '7', 'S', 'a', 'c', 'D', 'C', 'w', 'A', 'o', 'n', 's', 'r', '*', 'G', '[', 'l', 'h', '$', 'U', 's', '_', 'D', 'f', 'X', '~', '.', '7', 'B', 'A', 'E', '(', '#', ']', ':', '`', ',', 'k', 'y'}; private static final int KEYLENGTH = 256; //Alias for encryption keystore public static final String ENCRYPTION_KEY_ALIAS = "KEYBOX-ENCRYPTION_KEY"; static { File f = new File(keyStoreFile); //load or create keystore try { if (f.isFile() && f.canRead()) { keyStore = KeyStore.getInstance("JCEKS"); FileInputStream keyStoreInputStream = new FileInputStream(f); keyStore.load(keyStoreInputStream, KEYSTORE_PASS); } //create keystore else { initializeKeyStore(); } } catch (IOException | GeneralSecurityException ex) { log.error(ex.toString(), ex); } } /** * get secret entry for alias * * @param alias keystore secret alias * @return secret byte array */ public static byte[] getSecretBytes(String alias) throws GeneralSecurityException { KeyStore.SecretKeyEntry entry = (KeyStore.SecretKeyEntry) keyStore.getEntry(alias, new KeyStore.PasswordProtection(KEYSTORE_PASS)); return entry.getSecretKey().getEncoded(); } /** * get secret entry for alias * * @param alias keystore secret alias * @return secret string */ public static String getSecretString(String alias) throws GeneralSecurityException { KeyStore.SecretKeyEntry entry = (KeyStore.SecretKeyEntry) keyStore.getEntry(alias, new KeyStore.PasswordProtection(KEYSTORE_PASS)); return new String(entry.getSecretKey().getEncoded()); } /** * set secret in keystore * * @param alias keystore secret alias * @param secret keystore entry */ public static void setSecret(String alias, byte[] secret) throws KeyStoreException { KeyStore.ProtectionParameter protectionParameter = new KeyStore.PasswordProtection(KEYSTORE_PASS); SecretKeySpec secretKey = new SecretKeySpec(secret, 0, secret.length, "AES"); KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(secretKey); keyStore.setEntry(alias, secretKeyEntry, protectionParameter); } /** * set secret in keystore * * @param alias keystore secret alias * @param secret keystore entry */ public static void setSecret(String alias, String secret) throws KeyStoreException { setSecret(alias, secret.getBytes()); } /** * delete existing and create new keystore */ public static void resetKeyStore() throws IOException, GeneralSecurityException { File file = new File(keyStoreFile); if (file.exists()) { FileUtils.forceDelete(file); } //create new keystore initializeKeyStore(); } /** * create new keystore */ private static void initializeKeyStore() throws GeneralSecurityException, IOException {<FILL_FUNCTION_BODY>} }
keyStore = KeyStore.getInstance("JCEKS"); //create keystore keyStore.load(null, KEYSTORE_PASS); //set encryption key KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(KEYLENGTH); KeyStoreUtil.setSecret(KeyStoreUtil.ENCRYPTION_KEY_ALIAS, keyGenerator.generateKey().getEncoded()); //write keystore FileOutputStream fos = new FileOutputStream(keyStoreFile); keyStore.store(fos, KEYSTORE_PASS); fos.close();
1,649
152
1,801
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/util/OTPUtil.java
OTPUtil
verifyToken
class OTPUtil { private static final Logger log = LoggerFactory.getLogger(OTPUtil.class); //sizes to generate OTP secret private static final int SECRET_SIZE = 10; private static final int NUM_SCRATCH_CODES = 5; private static final int SCRATCH_CODE_SIZE = 4; //token window in near future or past private static final int TOKEN_WINDOW = 3; //interval for validation token change private static final int CHANGE_INTERVAL = 30; private OTPUtil() { } /** * generates OPT secret * * @return String shared secret */ public static String generateSecret() { byte[] buffer = new byte[(NUM_SCRATCH_CODES * SCRATCH_CODE_SIZE) + SECRET_SIZE]; new SecureRandom().nextBytes(buffer); byte[] secret = Arrays.copyOf(buffer, SECRET_SIZE); return new String(new Base32().encode(secret)); } /** * verifies code for OTP secret * * @param secret shared secret * @param token verification token * @return true if success */ public static boolean verifyToken(String secret, long token) { //check token in near future or past int window = TOKEN_WINDOW; for (int i = window; i >= -window; i--) { long time = (new Date().getTime() / TimeUnit.SECONDS.toMillis(CHANGE_INTERVAL)) + i; if (verifyToken(secret, token, time)) { return true; } } return false; } /** * verifies code for OTP secret per time interval * * @param secret shared secret * @param token verification token * @param time time representation to calculate OTP * @return true if success */ private static boolean verifyToken(String secret, long token, long time) {<FILL_FUNCTION_BODY>} }
long calculated = -1; byte[] key = new Base32().decode(secret); SecretKeySpec secretKey = new SecretKeySpec(key, "HmacSHA1"); try { Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] hash = mac.doFinal(ByteBuffer.allocate(8).putLong(time).array()); int offset = hash[hash.length - 1] & 0xF; for (int i = 0; i < 4; ++i) { calculated <<= 8; calculated |= (hash[offset + i] & 0xFF); } calculated &= 0x7FFFFFFF; calculated %= 1000000; } catch (Exception ex) { log.error(ex.toString(), ex); } return (calculated != -1 && calculated == token);
540
240
780
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/util/RefreshAuthKeyUtil.java
RefreshAllSystemsTask
run
class RefreshAllSystemsTask implements Runnable { private static final Logger log = LoggerFactory.getLogger(RefreshAllSystemsTask.class); @Override public void run() {<FILL_FUNCTION_BODY>} }
//distribute all public keys try { SSHUtil.distributePubKeysToAllSystems(); } catch (SQLException | GeneralSecurityException ex) { log.error(ex.toString(), ex); }
65
59
124
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/util/SessionOutputSerializer.java
SessionOutputSerializer
serialize
class SessionOutputSerializer implements JsonSerializer<Object> { @Override public JsonElement serialize(Object src, Type typeOfSrc, JsonSerializationContext context) {<FILL_FUNCTION_BODY>} }
JsonObject object = new JsonObject(); if (typeOfSrc.equals(AuditWrapper.class)) { AuditWrapper auditWrapper = (AuditWrapper) src; object.addProperty("user_id", auditWrapper.getUser().getId()); object.addProperty("username", auditWrapper.getUser().getUsername()); object.addProperty("user_type", auditWrapper.getUser().getUserType()); object.addProperty("first_nm", auditWrapper.getUser().getFirstNm()); object.addProperty("last_nm", auditWrapper.getUser().getLastNm()); object.addProperty("email", auditWrapper.getUser().getEmail()); object.addProperty("session_id", auditWrapper.getSessionOutput().getSessionId()); object.addProperty("instance_id", auditWrapper.getSessionOutput().getInstanceId()); object.addProperty("host_id", auditWrapper.getSessionOutput().getId()); object.addProperty("host", auditWrapper.getSessionOutput().getDisplayLabel()); object.addProperty("output", auditWrapper.getSessionOutput().getOutput().toString()); object.addProperty("timestamp", new Date().getTime()); } return object;
54
296
350
<no_super_class>
bastillion-io_Bastillion
Bastillion/src/main/java/io/bastillion/manage/util/SessionOutputUtil.java
SessionOutputUtil
getOutput
class SessionOutputUtil { private static final Map<Long, UserSessionsOutput> userSessionsOutputMap = new ConcurrentHashMap<>(); public final static boolean enableInternalAudit = "true".equals(AppConfig.getProperty("enableInternalAudit")); private static final Gson gson = new GsonBuilder().registerTypeAdapter(AuditWrapper.class, new SessionOutputSerializer()).create(); private static final Logger systemAuditLogger = LoggerFactory.getLogger("io.bastillion.manage.util.SystemAudit"); private SessionOutputUtil() { } /** * removes session for user session * * @param sessionId session id */ public static void removeUserSession(Long sessionId) { UserSessionsOutput userSessionsOutput = userSessionsOutputMap.get(sessionId); if (userSessionsOutput != null) { userSessionsOutput.getSessionOutputMap().clear(); } userSessionsOutputMap.remove(sessionId); } /** * removes session output for host system * * @param sessionId session id * @param instanceId id of host system instance */ public static void removeOutput(Long sessionId, Integer instanceId) { UserSessionsOutput userSessionsOutput = userSessionsOutputMap.get(sessionId); if (userSessionsOutput != null) { userSessionsOutput.getSessionOutputMap().remove(instanceId); } } /** * adds a new output * * @param sessionOutput session output object */ public static void addOutput(SessionOutput sessionOutput) { UserSessionsOutput userSessionsOutput = userSessionsOutputMap.get(sessionOutput.getSessionId()); if (userSessionsOutput == null) { userSessionsOutputMap.put(sessionOutput.getSessionId(), new UserSessionsOutput()); userSessionsOutput = userSessionsOutputMap.get(sessionOutput.getSessionId()); } userSessionsOutput.getSessionOutputMap().put(sessionOutput.getInstanceId(), sessionOutput); } /** * adds a new output * * @param sessionId session id * @param instanceId id of host system instance * @param value Array that is the source of characters * @param offset The initial offset * @param count The length */ public static void addToOutput(Long sessionId, Integer instanceId, char[] value, int offset, int count) { UserSessionsOutput userSessionsOutput = userSessionsOutputMap.get(sessionId); if (userSessionsOutput != null) { userSessionsOutput.getSessionOutputMap().get(instanceId).getOutput().append(value, offset, count); } } /** * returns list of output lines * * @param sessionId session id object * @param user user auth object * @return session output list */ public static List<SessionOutput> getOutput(Connection con, Long sessionId, User user) throws SQLException {<FILL_FUNCTION_BODY>} }
List<SessionOutput> outputList = new ArrayList<>(); UserSessionsOutput userSessionsOutput = userSessionsOutputMap.get(sessionId); if (userSessionsOutput != null) { for (Integer key : userSessionsOutput.getSessionOutputMap().keySet()) { //get output chars and set to output SessionOutput sessionOutput = userSessionsOutput.getSessionOutputMap().get(key); if (sessionOutput != null && sessionOutput.getOutput() != null && StringUtils.isNotEmpty(sessionOutput.getOutput())) { outputList.add(sessionOutput); //send to audit logger systemAuditLogger.info(gson.toJson(new AuditWrapper(user, sessionOutput))); if (enableInternalAudit) { SessionAuditDB.insertTerminalLog(con, sessionOutput); } userSessionsOutput.getSessionOutputMap().put(key, new SessionOutput(sessionId, sessionOutput)); } } } return outputList;
787
267
1,054
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/ChronicleHashCloseOnExitHook.java
ChronicleHashCloseOnExitHook
closeAll
class ChronicleHashCloseOnExitHook { private static WeakHashMap<VanillaChronicleHash<?, ?, ?, ?>.Identity, Long> maps = new WeakHashMap<>(); private static long order = 0; static { PriorityHook.add(80, ChronicleHashCloseOnExitHook::closeAll); } private ChronicleHashCloseOnExitHook() { } static synchronized void add(VanillaChronicleHash<?, ?, ?, ?> hash) { if (maps == null) throw new IllegalStateException("Shutdown in progress"); maps.put(hash.identity, order++); } static synchronized void remove(VanillaChronicleHash<?, ?, ?, ?> hash) { if (maps == null) return; // we are already in shutdown maps.remove(hash.identity); } private static void closeAll() {<FILL_FUNCTION_BODY>} }
try { WeakHashMap<VanillaChronicleHash<?, ?, ?, ?>.Identity, Long> maps; synchronized (ChronicleHashCloseOnExitHook.class) { maps = ChronicleHashCloseOnExitHook.maps; ChronicleHashCloseOnExitHook.maps = null; } TreeMap<Long, VanillaChronicleHash<?, ?, ?, ?>> orderedMaps = new TreeMap<>(); maps.forEach((identity, order) -> orderedMaps.put(order, identity.hash())); // close later added maps first orderedMaps.descendingMap().values().forEach(h -> { try { Runnable preShutdownAction = h.getPreShutdownAction(); if (preShutdownAction != null) { try { preShutdownAction.run(); } catch (Throwable throwable) { try { Jvm.error().on(ChronicleHashCloseOnExitHook.class, "Error running pre-shutdown action for " + h.toIdentityString() + " :", throwable); } catch (Throwable t2) { throwable.addSuppressed(t2); throwable.printStackTrace(); } } } h.close(); } catch (Throwable throwable) { try { Jvm.error().on(ChronicleHashCloseOnExitHook.class, "Error while closing " + h.toIdentityString() + " during shutdown hook:", throwable); } catch (Throwable t2) { // This may occur if the log service has already been shut down. Try to fall // back to printStackTrace(). throwable.addSuppressed(t2); throwable.printStackTrace(); } } }); } catch (Throwable throwable) { try { Jvm.error().on(ChronicleHashCloseOnExitHook.class, "Error while closing maps during shutdown hook:", throwable); } catch (Throwable t2) { throwable.addSuppressed(t2); throwable.printStackTrace(); } }
250
541
791
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/InMemoryChronicleHashResources.java
InMemoryChronicleHashResources
releaseMemoryResource
class InMemoryChronicleHashResources extends ChronicleHashResources { @Override void releaseMemoryResource(final MemoryResource allocation) {<FILL_FUNCTION_BODY>} }
assert SKIP_ASSERTIONS || assertAddress(allocation.address); assert SKIP_ASSERTIONS || assertPosition(allocation.size); OS.memory().freeMemory(allocation.address, allocation.size);
46
57
103
<methods>public non-sealed void <init>() ,public final boolean releaseManually() ,public void run() ,public final void setChronicleHashIdentityString(java.lang.String) <variables>private static final int COMPLETELY_CLOSED,private static final int OPEN,private static final int PARTIALLY_CLOSED,private java.lang.String chronicleHashIdentityString,private List<java.io.Closeable> closeables,private List<WeakReference<net.openhft.chronicle.hash.impl.ContextHolder>> contexts,private List<net.openhft.chronicle.hash.impl.MemoryResource> memoryResources,private volatile int state
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/IntCompactOffHeapLinearHashTable.java
IntCompactOffHeapLinearHashTable
writeEntryVolatile
class IntCompactOffHeapLinearHashTable extends CompactOffHeapLinearHashTable { private static final long SCALE = 4L; /** * Must not store {@code h} in a field, to avoid memory leaks. * * @see net.openhft.chronicle.hash.impl.stage.hash.Chaining#initMap */ IntCompactOffHeapLinearHashTable(VanillaChronicleHash h) { super(h); } @Override long indexToPos(long index) { return index * SCALE; } @Override public long step(long pos) { return (pos + SCALE) & capacityMask2; } @Override public long stepBack(long pos) { return (pos - SCALE) & capacityMask2; } @Override public long readEntry(final long address, final long pos) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); return OS.memory().readInt(address + pos); } @Override public long readEntryVolatile(final long address, final long pos) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); return OS.memory().readVolatileInt(address + pos); } @Override public void writeEntryVolatile(final long address, final long pos, final long key, final long value) {<FILL_FUNCTION_BODY>} @Override public void writeEntry(long address, long pos, long newEntry) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeInt(address + pos, (int) newEntry); } @Override public void clearEntry(long address, long pos) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeInt(address + pos, 0); } }
assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeVolatileInt(address + pos, (int) entry(key, value));
557
57
614
<methods>public static long capacityFor(long) ,public void checkValueForPut(long) ,public abstract void clearEntry(long, long) ,public boolean empty(long) ,public long entry(long, long) ,public static int entrySize(int, int) ,public long hlPos(long) ,public long key(long) ,public static int keyBits(long, int) ,public static long mask(int) ,public long maskUnsetKey(long) ,public abstract long readEntry(long, long) ,public abstract long readEntryVolatile(long, long) ,public long remove(long, long) ,public abstract long step(long) ,public abstract long stepBack(long) ,public long value(long) ,public static int valueBits(long) ,public abstract void writeEntry(long, long, long) ,public abstract void writeEntryVolatile(long, long, long, long) <variables>public static final double MAX_LOAD_FACTOR,public static final int MAX_TIER_CHUNKS,public static final int MAX_TIER_ENTRIES,public static final double MAX_UPPER_BOUND_LOAD_FACTOR,public static final long UNSET_ENTRY,public static final long UNSET_KEY,private final non-sealed long capacityMask,final non-sealed long capacityMask2,private final non-sealed int keyBits,private final non-sealed long keyMask,private final non-sealed long valueMask
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/LongCompactOffHeapLinearHashTable.java
LongCompactOffHeapLinearHashTable
clearEntry
class LongCompactOffHeapLinearHashTable extends CompactOffHeapLinearHashTable { private static final long SCALE = 8L; /** * Must not store {@code h} in a field, to avoid memory leaks. * * @see net.openhft.chronicle.hash.impl.stage.hash.Chaining#initMap */ LongCompactOffHeapLinearHashTable(VanillaChronicleHash h) { super(h); } @Override long indexToPos(long index) { return index * SCALE; } @Override public long step(long pos) { return (pos + SCALE) & capacityMask2; } @Override public long stepBack(long pos) { return (pos - SCALE) & capacityMask2; } @Override public long readEntry(final long address, final long pos) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); return OS.memory().readLong(address + pos); } @Override public long readEntryVolatile(final long address, final long pos) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); return OS.memory().readVolatileLong(address + pos); } @Override public void writeEntryVolatile(final long address, final long pos, final long key, final long value) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeVolatileLong(address + pos, entry(key, value)); } @Override public void writeEntry(final long address, final long pos, final long newEntry) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeLong(address + pos, newEntry); } @Override public void clearEntry(final long address, final long pos) {<FILL_FUNCTION_BODY>} }
assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeLong(address + pos, 0L);
570
50
620
<methods>public static long capacityFor(long) ,public void checkValueForPut(long) ,public abstract void clearEntry(long, long) ,public boolean empty(long) ,public long entry(long, long) ,public static int entrySize(int, int) ,public long hlPos(long) ,public long key(long) ,public static int keyBits(long, int) ,public static long mask(int) ,public long maskUnsetKey(long) ,public abstract long readEntry(long, long) ,public abstract long readEntryVolatile(long, long) ,public long remove(long, long) ,public abstract long step(long) ,public abstract long stepBack(long) ,public long value(long) ,public static int valueBits(long) ,public abstract void writeEntry(long, long, long) ,public abstract void writeEntryVolatile(long, long, long, long) <variables>public static final double MAX_LOAD_FACTOR,public static final int MAX_TIER_CHUNKS,public static final int MAX_TIER_ENTRIES,public static final double MAX_UPPER_BOUND_LOAD_FACTOR,public static final long UNSET_ENTRY,public static final long UNSET_KEY,private final non-sealed long capacityMask,final non-sealed long capacityMask2,private final non-sealed int keyBits,private final non-sealed long keyMask,private final non-sealed long valueMask
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/PersistedChronicleHashResources.java
PersistedChronicleHashResources
releaseExtraSystemResources
class PersistedChronicleHashResources extends ChronicleHashResources { private File file; public PersistedChronicleHashResources(File file) { this.file = file; OS.memory().storeFence(); // Emulate final semantics of the file field } @Override void releaseMemoryResource(MemoryResource mapping) throws IOException { OS.unmap(mapping.address, mapping.size); } @Override Throwable releaseExtraSystemResources() {<FILL_FUNCTION_BODY>} }
if (file == null) return null; Throwable thrown = null; try { CanonicalRandomAccessFiles.release(file); file = null; } catch (Throwable t) { thrown = t; } return thrown;
136
71
207
<methods>public non-sealed void <init>() ,public final boolean releaseManually() ,public void run() ,public final void setChronicleHashIdentityString(java.lang.String) <variables>private static final int COMPLETELY_CLOSED,private static final int OPEN,private static final int PARTIALLY_CLOSED,private java.lang.String chronicleHashIdentityString,private List<java.io.Closeable> closeables,private List<WeakReference<net.openhft.chronicle.hash.impl.ContextHolder>> contexts,private List<net.openhft.chronicle.hash.impl.MemoryResource> memoryResources,private volatile int state
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/data/bytes/EntryKeyBytesData.java
EntryKeyBytesData
innerGetUsing
class EntryKeyBytesData<K> extends AbstractData<K> { @StageRef VanillaChronicleHashHolder<K> hh; @StageRef KeyBytesInterop<K> ki; @StageRef SegmentStages s; @StageRef HashEntryStages<K> entry; @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @Stage("CachedEntryKey") private K cachedEntryKey; @Stage("CachedEntryKey") private boolean cachedEntryKeyRead = false; private void initCachedEntryKey() { cachedEntryKey = innerGetUsing(cachedEntryKey); cachedEntryKeyRead = true; } @Override public RandomDataInput bytes() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return s.segmentBS; } @Override public long offset() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return entry.keyOffset; } @Override public long size() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return entry.keySize; } @Override public long hash(LongHashFunction f) { checkOnEachPublicOperation.checkOnEachPublicOperation(); return super.hash(f); } @Override public K get() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return cachedEntryKey; } @Override public K getUsing(K using) { checkOnEachPublicOperation.checkOnEachPublicOperation(); return innerGetUsing(using); } private K innerGetUsing(K usingKey) {<FILL_FUNCTION_BODY>} }
Bytes bytes = s.segmentBytesForRead(); bytes.readPosition(entry.keyOffset); return ki.keyReader.read(bytes, size(), usingKey);
450
47
497
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/data/bytes/InputKeyBytesData.java
InputKeyBytesData
initInputKeyBytesStore
class InputKeyBytesData<K> extends AbstractData<K> { @Stage("InputKeyBytes") private final VanillaBytes inputKeyBytes = VanillaBytes.vanillaBytes();; @StageRef KeyBytesInterop<K> ki; @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @Stage("InputKeyBytesStore") private BytesStore inputKeyBytesStore = null; @Stage("InputKeyBytesStore") private long inputKeyBytesOffset; @Stage("InputKeyBytesStore") private long inputKeyBytesSize; @Stage("InputKeyBytes") private boolean inputKeyBytesUsed = false; @Stage("CachedInputKey") private K cachedInputKey; @Stage("CachedInputKey") private boolean cachedInputKeyRead = false; public void initInputKeyBytesStore(BytesStore bytesStore, long offset, long size) {<FILL_FUNCTION_BODY>} boolean inputKeyBytesInit() { return inputKeyBytesUsed; } void initInputKeyBytes() { inputKeyBytes.bytesStore(inputKeyBytesStore, inputKeyBytesOffset, inputKeyBytesSize); inputKeyBytesUsed = true; } void closeInputKeyBytes() { inputKeyBytes.bytesStore(BytesStore.empty(), 0, 0); inputKeyBytesUsed = false; } private void initCachedInputKey() { cachedInputKey = innerGetUsing(cachedInputKey); cachedInputKeyRead = true; } @Override public RandomDataInput bytes() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return inputKeyBytes.bytesStore(); } @Override public long offset() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return inputKeyBytesOffset; } @Override public long size() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return inputKeyBytesSize; } @Override public K get() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return cachedInputKey; } @Override public K getUsing(K using) { checkOnEachPublicOperation.checkOnEachPublicOperation(); return innerGetUsing(using); } private K innerGetUsing(K usingKey) { inputKeyBytes.readPosition(inputKeyBytesOffset); return ki.keyReader.read(inputKeyBytes, inputKeyBytesSize, usingKey); } }
inputKeyBytesStore = bytesStore; inputKeyBytesOffset = offset; inputKeyBytesSize = size;
642
31
673
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/AllocatedChunks.java
AllocatedChunks
initEntryAndKeyCopying
class AllocatedChunks { @StageRef public VanillaChronicleHashHolder<?> hh; @StageRef public SegmentStages s; @StageRef public HashEntryStages<?> entry; @StageRef public Alloc alloc; public int allocatedChunks = 0; public void initAllocatedChunks(int allocatedChunks) { this.allocatedChunks = allocatedChunks; } /** * @return {@code true} is tier has changed */ public boolean initEntryAndKeyCopying( long entrySize, long bytesToCopy, long prevPos, int prevChunks) {<FILL_FUNCTION_BODY>} }
initAllocatedChunks(hh.h().inChunks(entrySize)); long oldSegmentTierBaseAddr = s.tierBaseAddr; long oldKeySizeAddr = oldSegmentTierBaseAddr + entry.keySizeOffset; long oldKeyAddr = oldSegmentTierBaseAddr + entry.keyOffset; int tierBeforeAllocation = s.tier; long pos = alloc.alloc(allocatedChunks, prevPos, prevChunks); entry.copyExistingEntry(pos, bytesToCopy, oldKeyAddr, oldKeySizeAddr); return s.tier != tierBeforeAllocation;
188
153
341
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashEntryChecksumStrategy.java
HashEntryChecksumStrategy
computeChecksum
class HashEntryChecksumStrategy implements ChecksumStrategy { @StageRef SegmentStages s; @StageRef HashEntryStages<?> e; @StageRef KeyHashCode h; @Override public void computeAndStoreChecksum() { int checksum = computeChecksum(); s.segmentBS.writeInt(e.entryEnd(), checksum); } @Override public int computeChecksum() {<FILL_FUNCTION_BODY>} @Override public boolean innerCheckSum() { int oldChecksum = storedChecksum(); int checksum = computeChecksum(); return oldChecksum == checksum; } @Override public int storedChecksum() { return s.segmentBS.readInt(e.entryEnd()); } @Override public long extraEntryBytes() { return CHECKSUM_STORED_BYTES; } }
long keyHashCode = h.keyHashCode(); long keyEnd = e.keyEnd(); long len = e.entryEnd() - keyEnd; long checksum; if (len > 0) { long addr = s.tierBaseAddr + keyEnd; long payloadChecksum = LongHashFunction.xx_r39().hashMemory(addr, len); checksum = hash8To16Bytes(e.keySize, keyHashCode, payloadChecksum); } else { // non replicated ChronicleSet has no payload checksum = keyHashCode; } return (int) ((checksum >>> 32) ^ checksum);
257
173
430
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashEntryStages.java
HashEntryStages
checkSum
class HashEntryStages<K> implements HashEntry<K>, ChecksumEntry { @StageRef public VanillaChronicleHashHolder<?> hh; @StageRef public SegmentStages s; @StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation; @StageRef public HashLookupPos hlp; public long pos = -1; @Stage("EntryOffset") public long keySizeOffset = -1; public long keySize = -1; public long keyOffset = -1; public boolean delayedUpdateChecksum = false; @StageRef public EntryKeyBytesData<K> entryKey; @Stage("EntrySizeInChunks") public int entrySizeInChunks = 0; @StageRef HashEntryChecksumStrategy hashEntryChecksumStrategy; public final ChecksumStrategy checksumStrategy = hh.h().checksumEntries ? hashEntryChecksumStrategy : NoChecksumStrategy.INSTANCE; public void initPos(long pos) { this.pos = pos; } public abstract void closePos(); public abstract boolean entryOffsetInit(); public void initEntryOffset(long keySizeOffset) { this.keySizeOffset = keySizeOffset; } public void initEntryOffset() { keySizeOffset = s.entrySpaceOffset + pos * hh.h().chunkSize; } public abstract void closeEntryOffset(); public void initKeySize(long keySize) { this.keySize = keySize; } public abstract void closeKeySize(); public void initKeyOffset(long keyOffset) { this.keyOffset = keyOffset; } public abstract void closeKeyOffset(); public void readExistingEntry(long pos) { initPos(pos); Bytes segmentBytes = s.segmentBytesForRead(); segmentBytes.readPosition(keySizeOffset); initKeySize(hh.h().keySizeMarshaller.readSize(segmentBytes)); initKeyOffset(segmentBytes.readPosition()); } public void readFoundEntry(long pos, long keySizeOffset, long keySize, long keyOffset) { initPos(pos); initEntryOffset(keySizeOffset); initKeySize(keySize); initKeyOffset(keyOffset); } public void closeEntry() { closePos(); closeEntryOffset(); closeKeySize(); closeKeyOffset(); } public void writeNewEntry(long pos, Data<?> key) { initPos(pos); initKeySize(key.size()); Bytes segmentBytes = s.segmentBytesForWrite(); segmentBytes.writePosition(keySizeOffset); hh.h().keySizeMarshaller.writeSize(segmentBytes, keySize); initKeyOffset(segmentBytes.writePosition()); key.writeTo(s.segmentBS, keyOffset); } public void copyExistingEntry( long newPos, long bytesToCopy, long oldKeyAddr, long oldKeySizeAddr) { initPos(newPos); initKeyOffset(keySizeOffset + (oldKeyAddr - oldKeySizeAddr)); // Calling Access.copy() which is probably slower because not of abstractions, // because there is no BytesStore.write(off, addr, len) method. Alternative is // to make a final BytesStore rawMemoryStore = new PointerBytesStore().set(0, Long.MAX_V) // and here: s.segmentBS.write(keySizeOffset, rawMemoryStore, keySizeAddr, bytesToCopy) Access.copy( nativeAccess(), null, oldKeySizeAddr, checkedBytesStoreAccess(), s.segmentBS, keySizeOffset, bytesToCopy); } public long keyEnd() { return keyOffset + keySize; } public long entryEnd() { return keyEnd(); } public void initDelayedUpdateChecksum(boolean delayedUpdateChecksum) { // makes delayedUpdateChecksum dependent on keySizeOffset and Locks stages, to trigger // delayedUpdateChecksum close on these stages' close assert entryOffsetInit() && keySizeOffset >= 0; assert s.locksInit() && s.localLockState != LocalLockState.UNLOCKED; assert delayedUpdateChecksum; // doesn't make sense to init to "uninit" false value this.delayedUpdateChecksum = true; } abstract boolean delayedUpdateChecksumInit(); public void closeDelayedUpdateChecksum() { if (hh.h().checksumEntries) hashEntryChecksumStrategy.computeAndStoreChecksum(); delayedUpdateChecksum = false; } @Override public void updateChecksum() { checkOnEachPublicOperation.checkOnEachPublicOperation(); if (!hh.h().checksumEntries) { throw new UnsupportedOperationException(hh.h().toIdentityString() + ": Checksum is not stored in this Chronicle Hash"); } s.innerUpdateLock.lock(); initDelayedUpdateChecksum(true); } @Override public boolean checkSum() {<FILL_FUNCTION_BODY>} long entrySize() { return checksumStrategy.extraEntryBytes() + entryEnd() - keySizeOffset; } @NotNull @Override public Data<K> key() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return entryKey; } void initEntrySizeInChunks() { entrySizeInChunks = hh.h().inChunks(entrySize()); } public void initEntrySizeInChunks(int actuallyUsedChunks) { entrySizeInChunks = actuallyUsedChunks; } public void innerRemoveEntryExceptHashLookupUpdate() { s.free(pos, entrySizeInChunks); s.incrementModCount(); } }
checkOnEachPublicOperation.checkOnEachPublicOperation(); if (!hh.h().checksumEntries) { throw new UnsupportedOperationException(hh.h().toIdentityString() + ": Checksum is not stored in this Chronicle Hash"); } // This is needed, because a concurrent update lock holder might perform an entry update, // but not yet written a checksum (because checksum write is delayed to the update unlock). // So checkSum() on read lock level might result to false negative results. s.innerUpdateLock.lock(); return delayedUpdateChecksumInit() || checksumStrategy.innerCheckSum();
1,527
158
1,685
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupPos.java
HashLookupPos
initHashLookupPos
class HashLookupPos { public long hashLookupPos = -1; @StageRef HashLookupSearch hls; @StageRef SegmentStages s; public abstract boolean hashLookupPosInit(); public void initHashLookupPos() {<FILL_FUNCTION_BODY>} public void initHashLookupPos(long hashLookupPos) { this.hashLookupPos = hashLookupPos; } @Stage("HashLookupPos") public void setHashLookupPos(long hashLookupPos) { this.hashLookupPos = hashLookupPos; } public abstract void closeHashLookupPos(); }
// Validation + make hashLookupPos a dependant of tier. This is needed, because after // tier change should re-perform hashLookupSearch, starting from the searchStartPos. // Not an assert statement, because segmentTier stage should be initialized regardless // assertions enabled or not. if (s.tier < 0) throw new AssertionError(); s.innerReadLock.lock(); this.hashLookupPos = hls.searchStartPos;
180
121
301
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupSearch.java
HashLookupSearch
nextPos
class HashLookupSearch { @StageRef public VanillaChronicleHashHolder<?> hh; @Stage("SearchKey") public long searchStartPos; @StageRef SegmentStages s; @StageRef HashLookupPos hlp; @StageRef KeySearch<?> ks; @StageRef MapEntryStages<?, ?> e; @Stage("SearchKey") long searchKey = UNSET_KEY; public CompactOffHeapLinearHashTable hl() { return hh.h().hashLookup; } public void initSearchKey(long searchKey) { this.searchKey = searchKey; searchStartPos = hl().hlPos(searchKey); } private long addr() { return s.tierBaseAddr; } public long nextPos() {<FILL_FUNCTION_BODY>} public void found() { hlp.setHashLookupPos(hl().stepBack(hlp.hashLookupPos)); } public void remove() { hlp.setHashLookupPos(hl().remove(addr(), hlp.hashLookupPos)); } public void putNewVolatile(long entryPos) { // Correctness check + make putNewVolatile() dependant on keySearch, this, in turn, // is needed for hlp.hashLookupPos re-initialization after nextTier(). // Not an assert statement, because ks.searchStatePresent() should run regardless assertions // enabled or not. boolean keySearchReInit = !ks.keySearchInit(); if (ks.searchStatePresent()) throw new AssertionError(); if (keySearchReInit) { // if key search was re-init, entry was re-init too during the search e.readExistingEntry(entryPos); } hl().checkValueForPut(entryPos); hl().writeEntryVolatile(addr(), hlp.hashLookupPos, searchKey, entryPos); } public boolean checkSlotContainsExpectedKeyAndValue(long value) { // volatile read not needed here because this method is for verifying within-thread // invariants long entry = hl().readEntry(addr(), hlp.hashLookupPos); return hl().key(entry) == searchKey && hl().value(entry) == value; } }
long pos = hlp.hashLookupPos; CompactOffHeapLinearHashTable hl = hl(); while (true) { // read volatile to make a happens-before edge between entry insertion from concurrent // thread under update lock and this thread (reading the entry) long entry = hl.readEntryVolatile(addr(), pos); if (hl.empty(entry)) { hlp.setHashLookupPos(pos); return -1L; } pos = hl.step(pos); if (pos == searchStartPos) break; if (hl.key(entry) == searchKey) { hlp.setHashLookupPos(pos); return hl.value(entry); } } throw new IllegalStateException(hh.h().toIdentityString() + ": HashLookup overflow should never occur");
618
222
840
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/ReadLock.java
ReadLock
unlock
class ReadLock implements InterProcessLock { @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @StageRef SegmentStages s; @StageRef HashEntryStages entry; @StageRef HashLookupPos hlp; @Override public boolean isHeldByCurrentThread() { checkOnEachPublicOperation.checkOnEachLockOperation(); return s.localLockState.read; } @Override public void lock() { checkOnEachPublicOperation.checkOnEachLockOperation(); if (s.localLockState == UNLOCKED) { if (s.readZero() && s.updateZero() && s.writeZero()) { try { s.segmentHeader.readLock(s.segmentHeaderAddress); } catch (InterProcessDeadLockException e) { throw s.debugContextsAndLocks(e); } } s.incrementRead(); s.setLocalLockState(READ_LOCKED); } } @Override public void lockInterruptibly() throws InterruptedException { checkOnEachPublicOperation.checkOnEachLockOperation(); if (Thread.interrupted()) throw new InterruptedException(); if (s.localLockState == UNLOCKED) { if (s.readZero() && s.updateZero() && s.writeZero()) { try { s.segmentHeader.readLockInterruptibly(s.segmentHeaderAddress); } catch (InterProcessDeadLockException e) { throw s.debugContextsAndLocks(e); } } s.incrementRead(); s.setLocalLockState(READ_LOCKED); } } @Override public boolean tryLock() { checkOnEachPublicOperation.checkOnEachLockOperation(); if (s.localLockState == UNLOCKED) { if (!s.readZero() || !s.updateZero() || !s.writeZero() || s.segmentHeader.tryReadLock(s.segmentHeaderAddress)) { s.incrementRead(); s.setLocalLockState(READ_LOCKED); return true; } else { return false; } } else { return true; } } @Override public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException { checkOnEachPublicOperation.checkOnEachLockOperation(); if (Thread.interrupted()) throw new InterruptedException(); if (s.localLockState == UNLOCKED) { if (!s.readZero() || !s.updateZero() || !s.writeZero() || s.segmentHeader.tryReadLock(s.segmentHeaderAddress, time, unit)) { s.incrementRead(); s.setLocalLockState(READ_LOCKED); return true; } else { return false; } } else { return true; } } @Override public void unlock() {<FILL_FUNCTION_BODY>} @Override public boolean isHeld() { return s.localLockState != null && s.localLockState != UNLOCKED; } }
checkOnEachPublicOperation.checkOnEachLockOperation(); if (s.localLockState != UNLOCKED) { // TODO what should close here? hlp.closeHashLookupPos(); entry.closeEntry(); } s.readUnlockAndDecrementCount(); s.setLocalLockState(UNLOCKED);
825
89
914
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/UpdateLock.java
UpdateLock
unlock
class UpdateLock implements InterProcessLock { @StageRef VanillaChronicleHashHolder<?> hh; @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @StageRef SegmentStages s; @StageRef HashEntryStages<?> entry; @Override public boolean isHeldByCurrentThread() { checkOnEachPublicOperation.checkOnEachLockOperation(); return s.localLockState.update; } @Override public void lock() { checkOnEachPublicOperation.checkOnEachLockOperation(); switch (s.localLockState) { case UNLOCKED: s.checkIterationContextNotLockedInThisThread(); if (s.updateZero() && s.writeZero()) { if (!s.readZero()) throw forbiddenUpdateLockWhenOuterContextReadLocked(); try { s.segmentHeader.updateLock(s.segmentHeaderAddress); } catch (InterProcessDeadLockException e) { throw s.debugContextsAndLocks(e); } } s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return; case READ_LOCKED: throw forbiddenUpgrade(); case UPDATE_LOCKED: case WRITE_LOCKED: // do nothing } } /** * Non-static because after compilation it becomes inner class which forbids static methods */ @NotNull private IllegalMonitorStateException forbiddenUpgrade() { return new IllegalMonitorStateException( hh.h().toIdentityString() + ": Cannot upgrade from read to update lock"); } /** * Non-static because after compilation it becomes inner class which forbids static methods */ @NotNull private IllegalStateException forbiddenUpdateLockWhenOuterContextReadLocked() { return new IllegalStateException(hh.h().toIdentityString() + ": Cannot acquire update lock, because outer context holds read lock. " + "In this case you should acquire update lock in the outer context up front"); } @Override public void lockInterruptibly() throws InterruptedException { checkOnEachPublicOperation.checkOnEachLockOperation(); if (Thread.interrupted()) throw new InterruptedException(); switch (s.localLockState) { case UNLOCKED: s.checkIterationContextNotLockedInThisThread(); if (s.updateZero() && s.writeZero()) { if (!s.readZero()) throw forbiddenUpdateLockWhenOuterContextReadLocked(); try { s.segmentHeader.updateLockInterruptibly(s.segmentHeaderAddress); } catch (InterProcessDeadLockException e) { throw s.debugContextsAndLocks(e); } } s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return; case READ_LOCKED: throw forbiddenUpgrade(); case UPDATE_LOCKED: case WRITE_LOCKED: // do nothing } } @Override public boolean tryLock() { checkOnEachPublicOperation.checkOnEachLockOperation(); switch (s.localLockState) { case UNLOCKED: s.checkIterationContextNotLockedInThisThread(); if (s.updateZero() && s.writeZero()) { if (!s.readZero()) throw forbiddenUpdateLockWhenOuterContextReadLocked(); if (s.segmentHeader.tryUpdateLock(s.segmentHeaderAddress)) { s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return true; } else { return false; } } else { s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return true; } case READ_LOCKED: throw forbiddenUpgrade(); case UPDATE_LOCKED: case WRITE_LOCKED: return true; default: throw new IllegalStateException(hh.h().toIdentityString() + ": unexpected localLockState=" + s.localLockState); } } @Override public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException { checkOnEachPublicOperation.checkOnEachLockOperation(); if (Thread.interrupted()) throw new InterruptedException(); switch (s.localLockState) { case UNLOCKED: s.checkIterationContextNotLockedInThisThread(); if (s.updateZero() && s.writeZero()) { if (!s.readZero()) throw forbiddenUpdateLockWhenOuterContextReadLocked(); if (s.segmentHeader.tryUpdateLock(s.segmentHeaderAddress, time, unit)) { s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return true; } else { return false; } } else { s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return true; } case READ_LOCKED: throw forbiddenUpgrade(); case UPDATE_LOCKED: case WRITE_LOCKED: return true; default: throw new IllegalStateException(hh.h().toIdentityString() + ": unexpected localLockState=" + s.localLockState); } } @Override public void unlock() {<FILL_FUNCTION_BODY>} @Override public boolean isHeld() { return s.localLockState != null && s.localLockState != UNLOCKED; } }
checkOnEachPublicOperation.checkOnEachLockOperation(); switch (s.localLockState) { case UNLOCKED: case READ_LOCKED: return; case UPDATE_LOCKED: entry.closeDelayedUpdateChecksum(); if (s.decrementUpdate() == 0 && s.writeZero()) { s.segmentHeader.downgradeUpdateToReadLock(s.segmentHeaderAddress); } break; case WRITE_LOCKED: entry.closeDelayedUpdateChecksum(); if (s.decrementWrite() == 0) { if (!s.updateZero()) { s.segmentHeader.downgradeWriteToUpdateLock(s.segmentHeaderAddress); } else { s.segmentHeader.downgradeWriteToReadLock(s.segmentHeaderAddress); } } } s.incrementRead(); s.setLocalLockState(READ_LOCKED);
1,445
242
1,687
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/hash/Chaining.java
Chaining
initUsed
class Chaining extends ChainingInterface { public final List<ChainingInterface> contextChain; public final int indexInContextChain; /** * First context, ever created in this thread. rootContextInThisThread === contextChain.get(0). */ public final ChainingInterface rootContextInThisThread; @Stage("Used") public boolean used; @Stage("Used") private boolean firstContextLockedInThisThread; public Chaining(VanillaChronicleMap map) { contextChain = new ArrayList<>(); contextChain.add(this); indexInContextChain = 0; rootContextInThisThread = this; initMap(map); } public Chaining(ChainingInterface rootContextInThisThread, VanillaChronicleMap map) { contextChain = rootContextInThisThread.getContextChain(); indexInContextChain = contextChain.size(); contextChain.add(this); this.rootContextInThisThread = rootContextInThisThread; initMap(map); } private static <T extends ChainingInterface> T initUsedAndReturn( VanillaChronicleMap map, ChainingInterface context) { try { context.initUsed(true, map); //noinspection unchecked return (T) context; } catch (Throwable throwable) { try { ((AutoCloseable) context).close(); } catch (Throwable t) { throwable.addSuppressed(t); } throw throwable; } } @Override public List<ChainingInterface> getContextChain() { return contextChain; } public <T> T contextAtIndexInChain(int index) { //noinspection unchecked return (T) contextChain.get(index); } /** * This method stores a reference to the context's owner ChronicleMap into a field of the * context in the beginning of each usage of the context. Previously, this field was final and * set only once during context creation. It was preventing ChronicleMap objects from becoming * dead and collected by the GC, while any thread, from which the ChronicleMap was accessed * (hence a thread local context created), is alive. * <p> * The chain of strong references: * 1) Thread -> * 2) ThreadLocalMap -> * 3) Entry with ThreadLocal {@link net.openhft.chronicle.map.VanillaChronicleMap#cxt} as weak * referent and a context (e. g. {@link net.openhft.chronicle.map.impl.CompiledMapQueryContext}) * as value (a simple field, not a weak reference!) -> * 4) final reference to the owner {@link VanillaChronicleMap} -> * 5) ThreadLocal {@link net.openhft.chronicle.map.VanillaChronicleMap#cxt} (a strong reference * this time! note that this ThreadLocal is an instance field of VanillaChronicleMap) * <p> * So in order to break this chain at step 4), contexts store references to their owner * ChronicleMaps only when contexts are used. */ public abstract void initMap(VanillaChronicleMap map); @Override public boolean usedInit() { return used; } /** * Init method parameterized to avoid automatic initialization. {@code used} argument should * always be {@code true}. */ @Override public void initUsed(boolean used, VanillaChronicleMap map) {<FILL_FUNCTION_BODY>} @SuppressWarnings("unused") void closeUsed() { used = false; if (firstContextLockedInThisThread) rootContextInThisThread.unlockContextLocally(); } @Override public <T extends ChainingInterface> T getContext( Class<? extends T> contextClass, BiFunction<ChainingInterface, VanillaChronicleMap, T> createChaining, VanillaChronicleMap map) { for (int i = 0; i < contextChain.size(); i++) { ChainingInterface context = contextChain.get(i); if (context.getClass() == contextClass && !context.usedInit()) { return initUsedAndReturn(map, context); } } int maxNestedContexts = 1 << 10; if (contextChain.size() > maxNestedContexts) { throw new IllegalStateException(map.toIdentityString() + ": More than " + maxNestedContexts + " nested ChronicleHash contexts\n" + "are not supported. Very probable that you simply forgot to close context\n" + "somewhere (recommended to use try-with-resources statement).\n" + "Otherwise this is a bug, please report with this\n" + "stack trace on https://github.com/OpenHFT/Chronicle-Map/issues"); } //noinspection unchecked T context = createChaining.apply(this, map); return initUsedAndReturn(map, context); } }
assert used; firstContextLockedInThisThread = rootContextInThisThread.lockContextLocally(map); initMap(map); this.used = true;
1,304
46
1,350
<methods>public non-sealed void <init>() ,public abstract T getContext(Class<? extends T>, BiFunction<net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface,VanillaChronicleMap#RAW,T>, VanillaChronicleMap#RAW) ,public abstract List<net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface> getContextChain() ,public abstract void initUsed(boolean, VanillaChronicleMap#RAW) ,public abstract boolean usedInit() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/hash/OwnerThreadHolder.java
OwnerThreadHolder
checkAccessingFromOwnerThread
class OwnerThreadHolder { final Thread owner = Thread.currentThread(); @StageRef VanillaChronicleHashHolder<?> hh; public void checkAccessingFromOwnerThread() {<FILL_FUNCTION_BODY>} }
if (owner != Thread.currentThread()) { throw new ConcurrentModificationException(hh.h().toIdentityString() + ": Context shouldn't be accessed from multiple threads"); }
67
52
119
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/hash/ThreadLocalState.java
ThreadLocalState
closeContext
class ThreadLocalState { private static final Memory MEMORY = OS.memory(); private static final long CONTEXT_LOCK_OFFSET; private static final int CONTEXT_UNLOCKED = 0; private static final int CONTEXT_LOCKED_LOCALLY = 1; private static final int CONTEXT_CLOSED = 2; static { try { Field contextLockField = ThreadLocalState.class.getDeclaredField("contextLock"); contextLockField.setAccessible(true); CONTEXT_LOCK_OFFSET = MEMORY.getFieldOffset(contextLockField); } catch (NoSuchFieldException e) { throw new AssertionError(e); } } public boolean iterationContextLockedInThisThread; private volatile int contextLock = CONTEXT_UNLOCKED; /** * Returns {@code true} if this is the outer context lock in this thread, {@code false} if this * is a nested context. */ public boolean lockContextLocally(ChronicleHash<?, ?, ?, ?> hash) { // hash().isOpen() check guarantees no starvation of a thread calling chMap.close() and // trying to close this context by closeContext() method below, while the thread owning this // context frequently locks and unlocks it (e. g. in a loop). This is also the only check // for chMap openness during the whole context usage lifecycle. if (hash.isOpen() && MEMORY.compareAndSwapInt(this, CONTEXT_LOCK_OFFSET, CONTEXT_UNLOCKED, CONTEXT_LOCKED_LOCALLY)) { return true; } else { if (contextLock == CONTEXT_LOCKED_LOCALLY) return false; // Don't extract this hash().isOpen() and the one above, because they could return // different results: the first (above) could return true, the second (below) - false. if (contextLock == CONTEXT_CLOSED || !hash.isOpen()) throw new ChronicleHashClosedException(hash); throw new AssertionError("Unknown context lock state: " + contextLock); } } public void unlockContextLocally() { // Ensure all reads from mapped memory are done before thread calling chronicleMap.close() // frees resources potentially unmapping some memory from where those reads are performed. MEMORY.loadFence(); // Avoid volatile write to avoid expensive store-load barrier MEMORY.writeOrderedInt(this, CONTEXT_LOCK_OFFSET, CONTEXT_UNLOCKED); } public void closeContext(String chronicleHashIdentityString) {<FILL_FUNCTION_BODY>} private boolean tryCloseContext() { return MEMORY.compareAndSwapInt(this, CONTEXT_LOCK_OFFSET, CONTEXT_UNLOCKED, CONTEXT_CLOSED); } public abstract Thread owner(); /** * Returns if this ThreadLocalState prevents a Map from being closed (which may be an asynchronous snapshot operation). * <p> * This method returns a snapshot value from a potentially volatile source that may change at any time. * * @return if prevents closing */ public boolean preventClose() { return isLocked(); } /** * Returns if this ThreadLocalState is locked (asynchronous snapshot operation). * <p> * This method returns a snapshot value from a volatile source that may change at any time. * * @return if locked */ protected boolean isLocked() { return contextLock == CONTEXT_LOCKED_LOCALLY; } }
if (tryCloseContext()) return; // Unless there are bugs in this codebase, it could happen that // contextLock == CONTEXT_CLOSED here only if closeContext() has succeed, and the subsequent // contextHolder.clear() has failed in ChronicleHashResources.closeContext(), though this is // hardly imaginable: contextHolder.clear() couldn't fail with OutOfMemoryError (because // there are no allocations in this method) and StackOverflowError (because in this case // closeContext() would fail with StackOverflowError before). But anyway it's probably // a good idea to make this check rather than not to make. if (contextLock == CONTEXT_CLOSED) return; // If first attempt of closing a context (i. e. moving from unused to closed state) failed, // it means that the context is still in use. If this context belongs to the current thread, // this is a bug, because we cannot "wait" until context is unused in the same thread: if (owner() == Thread.currentThread()) { throw new IllegalStateException(chronicleHashIdentityString + ": Attempt to close a Chronicle Hash in the context " + "of not yet finished query or iteration"); } // If the context belongs to a different thread, wait until that thread finishes it's work // with the context: // Double the current timeout for segment locks "without timeout", that effectively // specifies maximum lock (hence context) holding time long timeoutMillis = TimeUnit.SECONDS.toMillis(LOCK_TIMEOUT_SECONDS) * 2; long lastTime = System.currentTimeMillis(); do { if (tryCloseContext()) return; // Unless there are bugs in this codebase, this should never happen. But anyway it's // probably a good idea to make this check rather than not to make. if (contextLock == CONTEXT_CLOSED) return; Thread.yield(); long now = System.currentTimeMillis(); if (now != lastTime) { lastTime = now; timeoutMillis--; } } while (timeoutMillis >= 0); throw new RuntimeException(chronicleHashIdentityString + ": Failed to close a context, belonging to the thread\n" + owner() + ", in the state: " + owner().getState() + "\n" + "Possible reasons:\n" + "- The context owner thread exited before closing this context. Ensure that you\n" + "always close opened Chronicle Map's contexts, the best way to do this is to use\n" + "try-with-resources blocks." + "- The context owner thread runs some context operation (e. g. a query) for\n" + "unexpectedly long time (at least " + LOCK_TIMEOUT_SECONDS + " seconds).\n" + "You should either redesign your logic to spend less time in Chronicle Map\n" + "contexts (recommended) or synchronize map.close() with queries externally,\n" + "so that close() is called only after all query operations finished.\n" + "- Iteration over a large Chronicle Map takes more than " + LOCK_TIMEOUT_SECONDS + " seconds.\n" + "In this case you should synchronize map.close() with iterations over the map\n" + "externally, so that close() is called only after all iterations are finished.\n" + "- This is a dead lock involving the context owner thread and this thread (from\n" + "which map.close() method is called. Make sure you always close Chronicle Map\n" + "contexts, preferably using try-with-resources blocks.");
912
923
1,835
<no_super_class>

No dataset card yet

New: Create and edit this dataset card directly on the website!

Contribute a Dataset Card
Downloads last month
16
Add dataset card