From 0a3cd3bb2d9786f6d89a4bdfb17f906e3c8222a9 Mon Sep 17 00:00:00 2001 From: Pan-YuJie <646836760@qq.com> Date: Thu, 20 Jul 2023 21:57:55 +0800 Subject: [PATCH 1/6] Feature: server add Ldap user authentication --- threadpool/server/auth/pom.xml | 4 + .../auth/config/GlobalSecurityConfig.java | 39 ++++- .../auth/config/LdapConfiguration.java | 72 ++++++++ .../auth/filter/JWTAuthenticationFilter.java | 18 +- .../auth/filter/LdapAuthenticationFilter.java | 155 ++++++++++++++++++ .../cn/hippo4j/auth/model/LdapUserInfo.java | 39 +++++ .../cn/hippo4j/auth/service/LdapService.java | 28 ++++ .../auth/service/impl/LdapServiceImpl.java | 72 ++++++++ .../impl/LdapUserDetailsServiceImpl.java | 115 +++++++++++++ .../service/impl/UserDetailsServiceImpl.java | 15 +- .../toolkit/BCryptPasswordEncoderTest.java | 20 +++ .../src/main/resources/ldap-back.properties | 14 ++ 12 files changed, 584 insertions(+), 7 deletions(-) create mode 100644 threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/LdapConfiguration.java create mode 100644 threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/LdapAuthenticationFilter.java create mode 100644 threadpool/server/auth/src/main/java/cn/hippo4j/auth/model/LdapUserInfo.java create mode 100644 threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/LdapService.java create mode 100644 threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/LdapServiceImpl.java create mode 100644 threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/LdapUserDetailsServiceImpl.java create mode 100644 threadpool/server/auth/src/test/java/cn/hippo4j/auth/toolkit/BCryptPasswordEncoderTest.java create mode 100644 threadpool/server/bootstrap/src/main/resources/ldap-back.properties diff --git a/threadpool/server/auth/pom.xml b/threadpool/server/auth/pom.xml index ce8d817594..aff6a5d5eb 100644 --- a/threadpool/server/auth/pom.xml +++ b/threadpool/server/auth/pom.xml @@ -62,5 +62,9 @@ hippo4j-threadpool-server-common ${project.version} + + org.springframework.boot + spring-boot-starter-data-ldap + diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/GlobalSecurityConfig.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/GlobalSecurityConfig.java index b239b42c10..83b3d1cacc 100644 --- a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/GlobalSecurityConfig.java +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/GlobalSecurityConfig.java @@ -20,6 +20,7 @@ import cn.hippo4j.auth.constant.Constants; import cn.hippo4j.auth.filter.JWTAuthenticationFilter; import cn.hippo4j.auth.filter.JWTAuthorizationFilter; +import cn.hippo4j.auth.filter.LdapAuthenticationFilter; import cn.hippo4j.auth.security.JwtTokenManager; import cn.hippo4j.auth.service.impl.UserDetailsServiceImpl; import org.springframework.beans.factory.annotation.Value; @@ -28,6 +29,7 @@ import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.config.BeanIds; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; @@ -54,9 +56,12 @@ public class GlobalSecurityConfig extends WebSecurityConfigurerAdapter { @Value("${hippo4j.core.auth.enabled:true}") private Boolean enableAuthentication; - @Resource + @Resource(name = "userDetailsServiceImpl") private UserDetailsService userDetailsService; + @Resource(name = "ldapUserDetailsServiceImpl") + private UserDetailsService ldapUserDetailsService; + @Resource private JwtTokenManager tokenManager; @@ -93,7 +98,9 @@ protected void configure(HttpSecurity http) throws Exception { .antMatchers("/static/**", "/index.html", "/favicon.ico", "/avatar.jpg").permitAll() .antMatchers("/doc.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs").anonymous() .and() - .addFilter(new JWTAuthenticationFilter(authenticationManager())) + // .addFilter(new JWTAuthenticationFilter(authenticationManager())).authenticationProvider(authenticationProvider()) + .addFilter(JWTAuthenticationFilter()).authenticationProvider(ldapAuthenticationProvider()) + .addFilter(LdapAuthenticationFilter()).authenticationProvider(ldapAuthenticationProvider()) .addFilter(new JWTAuthorizationFilter(tokenManager, authenticationManager())) .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); disableAuthenticationIfNeeded(http); @@ -106,6 +113,20 @@ public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers(ignores); } + private LdapAuthenticationFilter LdapAuthenticationFilter() throws Exception { + LdapAuthenticationFilter filter = new LdapAuthenticationFilter(authenticationManager()); + filter.setLdapUserDetailsService(ldapUserDetailsService); + filter.setAuthenticationManager(authenticationManagerBean()); + return filter; + } + + private JWTAuthenticationFilter JWTAuthenticationFilter() throws Exception { + JWTAuthenticationFilter filter = new JWTAuthenticationFilter(authenticationManager()); + filter.setLdapUserDetailsService(userDetailsService); + filter.setAuthenticationManager(authenticationManagerBean()); + return filter; + } + /** * Injection DaoAuthenticationProvider * Modify hideUserNotFoundExceptions initial value to false @@ -120,6 +141,20 @@ public DaoAuthenticationProvider authenticationProvider() { return provider; } + @Bean + public DaoAuthenticationProvider ldapAuthenticationProvider() { + DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); + authProvider.setUserDetailsService(ldapUserDetailsService); + authProvider.setPasswordEncoder(bCryptPasswordEncoder()); + return authProvider; + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.authenticationProvider(authenticationProvider()) + .authenticationProvider(ldapAuthenticationProvider()); + } + private void disableAuthenticationIfNeeded(HttpSecurity http) throws Exception { if (Boolean.FALSE.equals(enableAuthentication)) { http.authorizeRequests().antMatchers("/hippo4j/v1/cs/**").permitAll(); diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/LdapConfiguration.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/LdapConfiguration.java new file mode 100644 index 0000000000..1932878db2 --- /dev/null +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/LdapConfiguration.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.auth.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.core.support.LdapContextSource; + +import java.util.HashMap; +import java.util.Map; + +/** + * Ldap config. + */ +@Configuration +public class LdapConfiguration { + + private LdapTemplate ldapTemplate; + + @Value("${spring.ldap.urls:}") + private String url; + + @Value("${spring.ldap.base:}") + private String base; + + @Value("${spring.ldap.embedded.credential.username:}") + private String username; + + @Value("${spring.ldap.embedded.credential.password:}") + private String password; + + @Bean + public LdapContextSource contextSource() { + LdapContextSource contextSource = new LdapContextSource(); + Map config = new HashMap<>(10); + contextSource.setUrl(url); + contextSource.setBase(base); + contextSource.setUserDn(username); + contextSource.setPassword(password); + // fix garbled characters + config.put("java.naming.ldap.attributes.binary", "objectGUID"); + + contextSource.setPooled(true); + contextSource.setBaseEnvironmentProperties(config); + return contextSource; + } + + @Bean + public LdapTemplate ldapTemplate() { + if (null == ldapTemplate) { + ldapTemplate = new LdapTemplate(contextSource()); + } + return ldapTemplate; + } +} diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthenticationFilter.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthenticationFilter.java index 408aa3d426..d9f270b5ae 100644 --- a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthenticationFilter.java +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthenticationFilter.java @@ -25,23 +25,25 @@ import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.server.common.base.Results; import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.core.codec.DecodingException; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.BadCredentialsException; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; +import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken; import javax.servlet.FilterChain; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.security.GeneralSecurityException; -import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -60,11 +62,18 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte private final ThreadLocal rememberMe = new ThreadLocal(); + private UserDetailsService userDetailsService; + public JWTAuthenticationFilter(AuthenticationManager authenticationManager) { this.authenticationManager = authenticationManager; super.setFilterProcessesUrl(BASE_PATH + "/auth/login"); } + public void setLdapUserDetailsService(UserDetailsService userDetailsServiceImpl) { + this.userDetailsService = userDetailsServiceImpl; + } + + @SneakyThrows @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { @@ -78,8 +87,9 @@ public Authentication attemptAuthentication(HttpServletRequest request, request.setAttribute("loginUser", loginUser); rememberMe.set(loginUser.getRememberMe()); - authenticate = authenticationManager.authenticate( - new UsernamePasswordAuthenticationToken(loginUser.getUsername(), loginUser.getPassword(), new ArrayList())); + UserDetails userDetails = userDetailsService.loadUserByUsername(loginUser.getUsername()); + authenticate = new PreAuthenticatedAuthenticationToken(userDetails, null, userDetails.getAuthorities()); + } catch (GeneralSecurityException e) { log.warn("Password decode exception: {}", e.getMessage()); throw new DecodingException(e.getMessage()); diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/LdapAuthenticationFilter.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/LdapAuthenticationFilter.java new file mode 100644 index 0000000000..9ce982a397 --- /dev/null +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/LdapAuthenticationFilter.java @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.auth.filter; + +import cn.hippo4j.auth.model.biz.user.JwtUser; +import cn.hippo4j.auth.model.biz.user.LoginUser; +import cn.hippo4j.auth.toolkit.AESUtil; +import cn.hippo4j.auth.toolkit.JwtTokenUtil; +import cn.hippo4j.auth.toolkit.ReturnT; +import cn.hippo4j.common.toolkit.JSONUtil; +import cn.hippo4j.server.common.base.Results; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.AuthenticationServiceException; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; +import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken; + +import javax.servlet.FilterChain; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import static cn.hippo4j.auth.constant.Constants.SPLIT_COMMA; +import static cn.hippo4j.common.constant.Constants.BASE_PATH; +import static cn.hippo4j.common.constant.Constants.MAP_INITIAL_CAPACITY; + +/** + * Ldap Filter + */ +@Slf4j +public class LdapAuthenticationFilter extends UsernamePasswordAuthenticationFilter { + + private final ThreadLocal rememberMe = new ThreadLocal<>(); + + private UserDetailsService ldapUserDetailsService; + + public void setLdapUserDetailsService(UserDetailsService ldapUserDetailsServiceImpl) { + this.ldapUserDetailsService = ldapUserDetailsServiceImpl; + } + + public LdapAuthenticationFilter(AuthenticationManager authenticationManager) { + super.setFilterProcessesUrl(BASE_PATH + "/auth/ldap/login"); + } + + /** + * Whether it's just the post way + */ + private boolean postOnly = true; + + + /** + * filter obtains the username and password of LDAP and assembles it on the token. + * Then give the token for authorization + */ + @Override + public Authentication attemptAuthentication(HttpServletRequest request + , HttpServletResponse response) throws AuthenticationException { + if (postOnly && !"POST".equals(request.getMethod())) { + throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); + } else { + // Get logged in information from the input stream. + Authentication authenticate = null; + try { + LoginUser loginUser = new ObjectMapper().readValue(request.getInputStream(), LoginUser.class); + String key = new StringBuffer(loginUser.getTag()).reverse().toString(); + String password = AESUtil.decrypt(loginUser.getPassword(), key); + loginUser.setPassword(password); + request.setAttribute("loginUser", loginUser); + rememberMe.set(loginUser.getRememberMe()); + // ldap validated + UserDetails userDetails = ldapUserDetailsService.loadUserByUsername(loginUser.getUsername()); + authenticate = new PreAuthenticatedAuthenticationToken(userDetails, null, userDetails.getAuthorities()); + } catch (UsernameNotFoundException e) { + log.debug("User {} not found", e.getMessage()); + throw e; + } catch (BadCredentialsException e) { + log.debug("Bad credentials exception: {}", e.getMessage()); + throw e; + } catch (Exception e) { + log.debug("Attempt authentication error", e); + } + return authenticate; + } + } + + @Override + protected void successfulAuthentication(HttpServletRequest request, + HttpServletResponse response, + FilterChain chain, + Authentication authResult) throws IOException { + try { + JwtUser jwtUser = (JwtUser) authResult.getPrincipal(); + boolean isRemember = rememberMe.get() == 1; + String role = ""; + Collection authorities = jwtUser.getAuthorities(); + for (GrantedAuthority authority : authorities) { + role = authority.getAuthority(); + } + String token = JwtTokenUtil.createToken(jwtUser.getId(), jwtUser.getUsername(), role, isRemember); + response.setHeader("token", JwtTokenUtil.TOKEN_PREFIX + token); + response.setCharacterEncoding("UTF-8"); + Map maps = new HashMap<>(MAP_INITIAL_CAPACITY); + maps.put("data", JwtTokenUtil.TOKEN_PREFIX + token); + maps.put("roles", role.split(SPLIT_COMMA)); + response.getWriter().write(JSONUtil.toJSONString(Results.success(maps))); + } finally { + rememberMe.remove(); + } + } + + @Override + protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException { + response.setCharacterEncoding("UTF-8"); + response.getWriter().write(JSONUtil.toJSONString(new ReturnT<>(ReturnT.JWT_FAIL_CODE, getMessage(failed)))); + } + + /** + * Return different echo information to the front end according to different exception types + */ + private String getMessage(AuthenticationException failed) { + String message = "Server Error"; + if (failed instanceof UsernameNotFoundException) { + message = "用户不存在"; + } else if (failed instanceof BadCredentialsException) { + message = "密码错误"; + } + return message; + } +} diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/model/LdapUserInfo.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/model/LdapUserInfo.java new file mode 100644 index 0000000000..9a09c0b815 --- /dev/null +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/model/LdapUserInfo.java @@ -0,0 +1,39 @@ +package cn.hippo4j.auth.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Data; +import org.springframework.ldap.odm.annotations.Attribute; +import org.springframework.ldap.odm.annotations.DnAttribute; +import org.springframework.ldap.odm.annotations.Entry; +import org.springframework.ldap.odm.annotations.Id; + +import javax.naming.Name; + +@Data +@Entry(objectClasses = {"inetOrgPerson", "top"}) +public class LdapUserInfo { + + @JsonIgnore + @Id + private Name dn; + + @Attribute(name = "cn") + @DnAttribute(value = "cn") + private String userName; + + @Attribute(name = "sn") + private String lastName; + + @Attribute(name = "description") + private String description; + + @Attribute(name = "telephoneNumber") + private String telephoneNumber; + + @Attribute(name = "userPassword") + private String password; + + @Attribute(name = "ou") + private String organizational; + +} diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/LdapService.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/LdapService.java new file mode 100644 index 0000000000..823286d36c --- /dev/null +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/LdapService.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.auth.service; + +/** + * Ldap service. + */ +public interface LdapService { + /** + * Login ldap + */ + void login(String username, String password); +} diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/LdapServiceImpl.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/LdapServiceImpl.java new file mode 100644 index 0000000000..bb43be0253 --- /dev/null +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/LdapServiceImpl.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.auth.service.impl; + +import cn.hippo4j.auth.service.LdapService; +import cn.hippo4j.server.common.base.exception.ServiceException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.ldap.AuthenticationException; +import org.springframework.ldap.UncategorizedLdapException; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.query.LdapQueryBuilder; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +import static org.springframework.ldap.query.LdapQueryBuilder.query; + +@Service +@Slf4j +public class LdapServiceImpl implements LdapService { + + private final LdapTemplate ldapTemplate; + + @Value("${spring.ldap.object-class:}") + private String objectClassName; + + @Value("${spring.ldap.account-attribute:}") + private String accountAttribute; + + public LdapServiceImpl(LdapTemplate ldapTemplate) { + this.ldapTemplate = ldapTemplate; + } + + @Override + public void login(String username, String password) { + try { + ldapTemplate.authenticate(LdapQueryBuilder.query() + .where(accountAttribute).is(username) + .and(query().where("objectClass").is(objectClassName)) + , password); + log.debug("{} ldap Login successful", username); + } catch (EmptyResultDataAccessException e) { + throw new UsernameNotFoundException("ldap Can't find the user information "); + } catch (AuthenticationException e) { + log.debug("The user name or account error"); + throw new BadCredentialsException("The username or password error"); + } catch (UncategorizedLdapException e) { + log.debug("Please check whether the user name password input"); + throw new BadCredentialsException("Please check whether the username password input"); + } catch (Exception e) { + throw new ServiceException("Abnormal server"); + } + } + +} diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/LdapUserDetailsServiceImpl.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/LdapUserDetailsServiceImpl.java new file mode 100644 index 0000000000..ee5411f186 --- /dev/null +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/LdapUserDetailsServiceImpl.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.auth.service.impl; + +import cn.hippo4j.auth.mapper.UserMapper; +import cn.hippo4j.auth.model.UserInfo; +import cn.hippo4j.auth.model.biz.user.JwtUser; +import cn.hippo4j.auth.model.biz.user.LoginUser; +import cn.hippo4j.auth.service.LdapService; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.stereotype.Service; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.util.Collections; +import java.util.Objects; +import java.util.Set; + +/** + * User details service impl. + */ +@Slf4j +@Service +public class LdapUserDetailsServiceImpl implements UserDetailsService { + + @Value("${hippo4j.core.auth.enabled:true}") + private Boolean enableAuthentication; + + @Resource + private UserMapper userMapper; + + @Resource + private LdapService ldapService; + + @Override + public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException { + JwtUser anonymous = dealWithAnonymous(); + if (!Objects.isNull(anonymous)) { + return anonymous; + } + RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(requestAttributes)).getRequest(); + LoginUser loginUser = (LoginUser) request.getAttribute("loginUser"); + // ldap authentication + ldapService.login(userName, loginUser.getPassword()); + // By querying the data inventory this user does not exist + UserInfo userInfo = userMapper.selectOne(Wrappers.lambdaQuery(UserInfo.class) + .eq(UserInfo::getUserName, userName) + ); + // the database does not, create a ROLE_USER permission to the default user, password is empty + if (Objects.isNull(userInfo)) { + userInfo = new UserInfo(); + userInfo.setPassword(""); + userInfo.setUserName(loginUser.getUsername()); + userInfo.setRole("ROLE_USER"); + userMapper.insert(userInfo); + } + // structure jwtUser + JwtUser jwtUser = new JwtUser(); + jwtUser.setId(userInfo.getId()); + jwtUser.setUsername(userName); + jwtUser.setPassword(""); + Set authorities = Collections.singleton(new SimpleGrantedAuthority(userInfo.getRole() + "")); + jwtUser.setAuthorities(authorities); + return jwtUser; + } + + private JwtUser dealWithAnonymous() { + RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); + if (requestAttributes == null) { + return null; + } + HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); + LoginUser loginUser = (LoginUser) request.getAttribute("loginUser"); + if (Objects.isNull(loginUser)) { + return null; + } + if (Boolean.FALSE.equals(enableAuthentication)) { + JwtUser jwtUser = new JwtUser(); + BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); + jwtUser.setId(1L); + jwtUser.setUsername("anonymous"); + jwtUser.setPassword(bCryptPasswordEncoder.encode(loginUser.getPassword())); + Set authorities = Collections.singleton(new SimpleGrantedAuthority("ROLE_ADMIN")); + jwtUser.setAuthorities(authorities); + return jwtUser; + } + return null; + } +} diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/UserDetailsServiceImpl.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/UserDetailsServiceImpl.java index dc62a1701f..960c25aba7 100644 --- a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/UserDetailsServiceImpl.java +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/UserDetailsServiceImpl.java @@ -24,11 +24,13 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.stereotype.Service; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -43,6 +45,7 @@ * User details service impl. */ @Slf4j +@Service public class UserDetailsServiceImpl implements UserDetailsService { @Value("${hippo4j.core.auth.enabled:true}") @@ -57,10 +60,20 @@ public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundEx if (!Objects.isNull(anonymous)) { return anonymous; } - UserInfo userInfo = userMapper.selectOne(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUserName, userName)); + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + LoginUser loginUser = (LoginUser) request.getAttribute("loginUser"); + String loginPassword = loginUser.getPassword(); + UserInfo userInfo = userMapper.selectOne(Wrappers.lambdaQuery(UserInfo.class) + .eq(UserInfo::getUserName, userName) + ); if (Objects.isNull(userInfo)) { throw new UsernameNotFoundException(userName); } + // Validation password + BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); + if (!bCryptPasswordEncoder.matches(loginPassword, userInfo.getPassword())) { + throw new BadCredentialsException(userName + "密码错误,请重新输入"); + } JwtUser jwtUser = new JwtUser(); jwtUser.setId(userInfo.getId()); jwtUser.setUsername(userName); diff --git a/threadpool/server/auth/src/test/java/cn/hippo4j/auth/toolkit/BCryptPasswordEncoderTest.java b/threadpool/server/auth/src/test/java/cn/hippo4j/auth/toolkit/BCryptPasswordEncoderTest.java new file mode 100644 index 0000000000..f1d8a24bfe --- /dev/null +++ b/threadpool/server/auth/src/test/java/cn/hippo4j/auth/toolkit/BCryptPasswordEncoderTest.java @@ -0,0 +1,20 @@ +package cn.hippo4j.auth.toolkit; + +import cn.hippo4j.common.toolkit.Assert; +import org.junit.Test; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +public class BCryptPasswordEncoderTest { + + @Test + public void bCryptPasswordEncoderTest() { + + String password = "12345abc"; + + BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); + String encode = bCryptPasswordEncoder.encode(password); + boolean matches = bCryptPasswordEncoder.matches(password, encode); + Assert.isTrue(matches); + } + +} diff --git a/threadpool/server/bootstrap/src/main/resources/ldap-back.properties b/threadpool/server/bootstrap/src/main/resources/ldap-back.properties new file mode 100644 index 0000000000..5b950ede21 --- /dev/null +++ b/threadpool/server/bootstrap/src/main/resources/ldap-back.properties @@ -0,0 +1,14 @@ +#*************** Ldap Sample Configurations ***************# +### This configuration file does not take effect +### Change the LDAP server information to yourself +### Configure the following configuration file into application.properties + +# Ldap Config +spring.ldap.urls=ldap://127.0.0.1:389 +spring.ldap.base=dc=xxx,dc=com +spring.ldap.embedded.credential.username=cn=xxxx,dc=xxx,dc=com +spring.ldap.embedded.credential.password=password +# Ldap Entry object-class +spring.ldap.object-class=person +# Ldap account-attribute CommonName ( cn / uid / username / ... ) +spring.ldap.account-attribute=cn From bb870d243a7258f84aca9da87c7e68e71fe34b5c Mon Sep 17 00:00:00 2001 From: Pan-YuJie <646836760@qq.com> Date: Mon, 4 Sep 2023 20:47:18 +0800 Subject: [PATCH 2/6] Fix Server-example Unknown user --- .../core/enable/BeforeCheckConfiguration.java | 7 ++- .../cn/hippo4j/auth/config/RFC7230Config.java | 45 +++++++++++++++++++ .../cn/hippo4j/auth/security/AuthManager.java | 18 ++++++-- .../service/impl/UserDetailsServiceImpl.java | 2 + 4 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/RFC7230Config.java diff --git a/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java b/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java index 65f0cb5da2..cf56f4ae81 100644 --- a/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java +++ b/threadpool/core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java @@ -28,7 +28,6 @@ import org.springframework.core.env.ConfigurableEnvironment; import java.util.Map; -import java.util.Objects; /** * Before check configuration. @@ -92,15 +91,15 @@ public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolBeforeCheckBean(@Au } } Map etcd = properties.getEtcd(); - if (MapUtil.isNotEmpty(etcd)){ + if (MapUtil.isNotEmpty(etcd)) { String endpoints = etcd.get("endpoints"); - if ((StringUtil.isBlank(endpoints))){ + if ((StringUtil.isBlank(endpoints))) { throw new ConfigEmptyException( "Web server failed to start. The dynamic thread pool etcd endpoints is empty.", "Please check whether the [spring.dynamic.thread-pool.etcd.endpoints] configuration is empty or an empty string."); } String key = etcd.get("key"); - if ((StringUtil.isBlank(key))){ + if ((StringUtil.isBlank(key))) { throw new ConfigEmptyException( "Web server failed to start. The dynamic thread pool etcd key is empty.", "Please check whether the [spring.dynamic.thread-pool.etcd.key] configuration is empty or an empty string."); diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/RFC7230Config.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/RFC7230Config.java new file mode 100644 index 0000000000..cce59b8833 --- /dev/null +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/RFC7230Config.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.auth.config; + +import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class RFC7230Config { + + /** + * Solve the Tomcat RFC7230 problem + * + * @return + */ + @Bean + public ConfigurableServletWebServerFactory webServerFactory() { + TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); + factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> { + connector.setProperty("relaxedQueryChars", "|{}[](),/:;<=>?@[\\]{}\\"); + connector.setProperty("relaxedPathChars", "|{}[](),/:;<=>?@[\\]{}\\"); + connector.setProperty("rejectIllegalHeader", "false"); + }); + + return factory; + } +} diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/security/AuthManager.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/security/AuthManager.java index 765104d481..c2ac9be0ef 100644 --- a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/security/AuthManager.java +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/security/AuthManager.java @@ -17,13 +17,19 @@ package cn.hippo4j.auth.security; +import cn.hippo4j.auth.model.biz.user.LoginUser; import lombok.AllArgsConstructor; import lombok.SneakyThrows; import org.springframework.expression.AccessException; import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; /** * Auth manager. @@ -36,6 +42,8 @@ public class AuthManager { private final AuthenticationManager authenticationManager; + private final UserDetailsService userDetailsService; + /** * Resolve token from user. * @@ -47,9 +55,11 @@ public class AuthManager { @SneakyThrows public String resolveTokenFromUser(String userName, String rawPassword) { try { - UsernamePasswordAuthenticationToken authenticationToken = - new UsernamePasswordAuthenticationToken(userName, rawPassword); - authenticationManager.authenticate(authenticationToken); + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + LoginUser loginUser = new LoginUser(); + loginUser.setPassword(rawPassword); + request.setAttribute("loginUser", loginUser); + UserDetails userDetails = userDetailsService.loadUserByUsername(userName); } catch (AuthenticationException e) { throw new AccessException("Unknown user."); } diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/UserDetailsServiceImpl.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/UserDetailsServiceImpl.java index 5029024ca5..9a8e7ccd2d 100644 --- a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/UserDetailsServiceImpl.java +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/service/impl/UserDetailsServiceImpl.java @@ -24,6 +24,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Primary; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; @@ -46,6 +47,7 @@ */ @Slf4j @Service +@Primary public class UserDetailsServiceImpl implements UserDetailsService { @Value("${hippo4j.core.auth.enabled:true}") From 4a374aed050b2751d9916e8cd8b40b1a70d7598a Mon Sep 17 00:00:00 2001 From: Pan-YuJie <646836760@qq.com> Date: Mon, 4 Sep 2023 21:11:36 +0800 Subject: [PATCH 3/6] Remove RFC7230Config.class --- .../cn/hippo4j/auth/config/RFC7230Config.java | 45 ------------------- .../auth/filter/LdapAuthenticationFilter.java | 1 - 2 files changed, 46 deletions(-) delete mode 100644 threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/RFC7230Config.java diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/RFC7230Config.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/RFC7230Config.java deleted file mode 100644 index cce59b8833..0000000000 --- a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/config/RFC7230Config.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package cn.hippo4j.auth.config; - -import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; -import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; -import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class RFC7230Config { - - /** - * Solve the Tomcat RFC7230 problem - * - * @return - */ - @Bean - public ConfigurableServletWebServerFactory webServerFactory() { - TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); - factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> { - connector.setProperty("relaxedQueryChars", "|{}[](),/:;<=>?@[\\]{}\\"); - connector.setProperty("relaxedPathChars", "|{}[](),/:;<=>?@[\\]{}\\"); - connector.setProperty("rejectIllegalHeader", "false"); - }); - - return factory; - } -} diff --git a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/LdapAuthenticationFilter.java b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/LdapAuthenticationFilter.java index 1d88fecb9f..1b86b34fe7 100644 --- a/threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/LdapAuthenticationFilter.java +++ b/threadpool/server/auth/src/main/java/cn/hippo4j/auth/filter/LdapAuthenticationFilter.java @@ -73,7 +73,6 @@ public LdapAuthenticationFilter(AuthenticationManager authenticationManager) { */ private boolean postOnly = true; - /** * filter obtains the username and password of LDAP and assembles it on the token. * Then give the token for authorization From aafd87079a5d604cad0f43d504eff81bd811f992 Mon Sep 17 00:00:00 2001 From: Pan-YuJie <646836760@qq.com> Date: Sun, 12 Nov 2023 23:11:22 +0800 Subject: [PATCH 4/6] Fix Log printing failure problem in agent mode --- .../hippo4j/agent/bootstrap/Hippo4jAgent.java | 19 +- .../agent/core/boot/AgentPackagePath.java | 5 +- .../agent/core/boot/ServiceManager.java | 4 +- .../boot/SpringBootConfigInitializer.java | 4 +- .../core/conf/SnifferConfigInitializer.java | 24 +- .../core/jvm/LoadedLibraryCollector.java | 12 +- .../AbstractClassEnhancePluginDefine.java | 4 +- .../core/plugin/InstrumentDebuggingClass.java | 8 +- .../agent/core/plugin/PluginBootstrap.java | 6 +- .../hippo4j/agent/core/plugin/PluginCfg.java | 4 +- .../core/plugin/PluginResourcesResolver.java | 4 +- .../agent/core/plugin/PluginSelector.java | 3 +- .../bootstrap/BootstrapInstrumentBoost.java | 4 +- .../bootstrap/BootstrapPluginLogBridge.java | 4 +- .../CacheableTransformerDecorator.java | 13 +- .../enhance/ClassEnhancePluginDefine.java | 10 +- .../interceptor/enhance/ConstructorInter.java | 4 +- .../interceptor/enhance/InstMethodsInter.java | 4 +- .../InstMethodsInterWithOverrideArgs.java | 4 +- .../enhance/StaticMethodsInter.java | 4 +- .../StaticMethodsInterWithOverrideArgs.java | 4 +- .../enhance/v2/InstMethodsInterV2.java | 4 +- .../InstMethodsInterV2WithOverrideArgs.java | 4 +- .../enhance/v2/StaticMethodsInterV2.java | 4 +- .../StaticMethodsInterV2WithOverrideArgs.java | 4 +- .../plugin/jdk9module/JDK9ModuleExporter.java | 4 +- .../core/plugin/loader/AgentClassLoader.java | 13 +- .../loader/InterceptorInstanceLoader.java | 2 +- .../plugin/match/ProtectiveShieldMatcher.java | 4 +- .../agent/core/util/CustomizeExpression.java | 4 +- .../agent/core/util/ExecutorNameUtil.java | 4 +- .../hippo4j/agent/core/util/ReflectUtil.java | 4 +- .../EventPublishingFinishedInterceptor.java | 4 +- ...ynamicThreadPoolChangeHandlerSpring2x.java | 4 +- .../boot/SpringBootV2PluginBootService.java | 4 +- .../EventPublishingStartedInterceptor.java | 4 +- .../support/SpringPropertiesLoader.java | 4 +- ...lExecutorConstructorMethodInterceptor.java | 9 +- .../src/main/resources/bootstrap.properties | 15 ++ infra/common/pom.xml | 5 + .../boot/AgentPackageNotFoundException.java | 2 +- .../hippo4j/common/boot/AgentPackagePath.java | 85 +++++++ .../hippo4j/common/boot}/ClassCacheMode.java | 7 +- .../boot/DefaultNamedThreadFactory.java | 2 +- .../java/cn/hippo4j/common}/conf/Config.java | 15 +- .../common}/conf/ConfigNotFoundException.java | 2 +- .../cn/hippo4j/common/conf/Constants.java | 39 ++++ .../conf/RuntimeContextConfiguration.java | 2 +- .../cn/hippo4j/common}/logging/api/ILog.java | 2 +- .../common}/logging/api/LogManager.java | 4 +- .../common}/logging/api/LogResolver.java | 2 +- .../common}/logging/api/NoopLogger.java | 2 +- .../common}/logging/core/AbstractLogger.java | 16 +- .../common}/logging/core/Converter.java | 2 +- .../common}/logging/core/FileWriter.java | 20 +- .../hippo4j/common}/logging/core/IWriter.java | 2 +- .../common}/logging/core/JsonLogResolver.java | 6 +- .../common}/logging/core/JsonLogger.java | 4 +- .../common}/logging/core/LogEvent.java | 2 +- .../common}/logging/core/LogLevel.java | 2 +- .../logging/core/LogMessageHolder.java | 2 +- .../common}/logging/core/LogOutput.java | 2 +- .../hippo4j/common}/logging/core/Parser.java | 4 +- .../logging/core/PatternLogResolver.java | 8 +- .../common}/logging/core/PatternLogger.java | 5 +- .../common}/logging/core/ResolverType.java | 2 +- .../common}/logging/core/SystemOutWriter.java | 2 +- .../common}/logging/core/WriterFactory.java | 16 +- .../core/converters/AgentNameConverter.java | 8 +- .../core/converters/ClassConverter.java | 6 +- .../core/converters/DateConverter.java | 6 +- .../core/converters/LevelConverter.java | 6 +- .../core/converters/LiteralConverter.java | 6 +- .../core/converters/MessageConverter.java | 6 +- .../core/converters/ThreadConverter.java | 6 +- .../core/converters/ThrowableConverter.java | 8 +- .../cn/hippo4j/common/toolkit/StringUtil.java | 65 ++++++ .../toolkit/agent/ConfigInitializer.java | 210 ++++++++++++++++++ .../hippo4j/common/toolkit/agent/Length.java | 33 +++ .../agent/PlaceholderConfigurerSupport.java | 37 +++ .../agent/PropertyPlaceholderHelper.java | 206 +++++++++++++++++ .../RunnableWithExceptionProtection.java | 43 ++++ .../DynamicThreadPoolRefreshListener.java | 11 +- pom.xml | 1 + 84 files changed, 934 insertions(+), 231 deletions(-) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/boot/AgentPackageNotFoundException.java (96%) create mode 100644 infra/common/src/main/java/cn/hippo4j/common/boot/AgentPackagePath.java rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy => infra/common/src/main/java/cn/hippo4j/common/boot}/ClassCacheMode.java (90%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/boot/DefaultNamedThreadFactory.java (97%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/conf/Config.java (95%) mode change 100755 => 100644 rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/conf/ConfigNotFoundException.java (96%) create mode 100644 infra/common/src/main/java/cn/hippo4j/common/conf/Constants.java rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/conf/RuntimeContextConfiguration.java (96%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/api/ILog.java (97%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/api/LogManager.java (95%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/api/LogResolver.java (96%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/api/NoopLogger.java (98%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/AbstractLogger.java (91%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/Converter.java (95%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/FileWriter.java (94%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/IWriter.java (95%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/JsonLogResolver.java (88%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/JsonLogger.java (96%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/LogEvent.java (97%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/LogLevel.java (95%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/LogMessageHolder.java (96%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/LogOutput.java (95%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/Parser.java (98%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/PatternLogResolver.java (85%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/PatternLogger.java (94%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/ResolverType.java (95%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/SystemOutWriter.java (96%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/WriterFactory.java (78%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/converters/AgentNameConverter.java (83%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/converters/ClassConverter.java (87%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/converters/DateConverter.java (88%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/converters/LevelConverter.java (87%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/converters/LiteralConverter.java (88%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/converters/MessageConverter.java (87%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/converters/ThreadConverter.java (87%) rename {agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core => infra/common/src/main/java/cn/hippo4j/common}/logging/core/converters/ThrowableConverter.java (88%) create mode 100644 infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/ConfigInitializer.java create mode 100644 infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/Length.java create mode 100644 infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PlaceholderConfigurerSupport.java create mode 100644 infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PropertyPlaceholderHelper.java create mode 100644 infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/RunnableWithExceptionProtection.java diff --git a/agent/hippo4j-agent-bootstrap/src/main/java/cn/hippo4j/agent/bootstrap/Hippo4jAgent.java b/agent/hippo4j-agent-bootstrap/src/main/java/cn/hippo4j/agent/bootstrap/Hippo4jAgent.java index 64f6ce9c69..a4de6591a8 100644 --- a/agent/hippo4j-agent-bootstrap/src/main/java/cn/hippo4j/agent/bootstrap/Hippo4jAgent.java +++ b/agent/hippo4j-agent-bootstrap/src/main/java/cn/hippo4j/agent/bootstrap/Hippo4jAgent.java @@ -17,22 +17,17 @@ package cn.hippo4j.agent.bootstrap; -import cn.hippo4j.agent.core.boot.AgentPackageNotFoundException; import cn.hippo4j.agent.core.boot.ServiceManager; -import cn.hippo4j.agent.core.conf.Config; import cn.hippo4j.agent.core.conf.SnifferConfigInitializer; import cn.hippo4j.agent.core.jvm.LoadedLibraryCollector; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; -import cn.hippo4j.agent.core.plugin.AbstractClassEnhancePluginDefine; -import cn.hippo4j.agent.core.plugin.EnhanceContext; -import cn.hippo4j.agent.core.plugin.InstrumentDebuggingClass; -import cn.hippo4j.agent.core.plugin.PluginBootstrap; -import cn.hippo4j.agent.core.plugin.PluginException; -import cn.hippo4j.agent.core.plugin.PluginFinder; +import cn.hippo4j.agent.core.plugin.*; import cn.hippo4j.agent.core.plugin.bootstrap.BootstrapInstrumentBoost; import cn.hippo4j.agent.core.plugin.bytebuddy.CacheableTransformerDecorator; import cn.hippo4j.agent.core.plugin.jdk9module.JDK9ModuleExporter; +import cn.hippo4j.common.boot.AgentPackageNotFoundException; +import cn.hippo4j.common.conf.Config; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import net.bytebuddy.ByteBuddy; import net.bytebuddy.agent.builder.AgentBuilder; import net.bytebuddy.description.NamedElement; @@ -49,9 +44,7 @@ import java.util.List; import java.util.Map; -import static net.bytebuddy.matcher.ElementMatchers.nameContains; -import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; -import static net.bytebuddy.matcher.ElementMatchers.not; +import static net.bytebuddy.matcher.ElementMatchers.*; /** * Hippo4j Agent diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/AgentPackagePath.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/AgentPackagePath.java index 029a4bfbcf..02b3c6657a 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/AgentPackagePath.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/AgentPackagePath.java @@ -17,8 +17,9 @@ package cn.hippo4j.agent.core.boot; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; +import cn.hippo4j.common.boot.AgentPackageNotFoundException; import java.io.File; import java.net.MalformedURLException; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/ServiceManager.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/ServiceManager.java index 2026ea9f94..7391f1c261 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/ServiceManager.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/ServiceManager.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.boot; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.loader.AgentClassLoader; import java.util.Collections; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/SpringBootConfigInitializer.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/SpringBootConfigInitializer.java index eb2eafef7c..5d12c1e6eb 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/SpringBootConfigInitializer.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/SpringBootConfigInitializer.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.boot; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.util.ConfigInitializer; import java.util.Collections; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/SnifferConfigInitializer.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/SnifferConfigInitializer.java index 570a68c881..b1c0a56cce 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/SnifferConfigInitializer.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/SnifferConfigInitializer.java @@ -17,15 +17,17 @@ package cn.hippo4j.agent.core.conf; -import cn.hippo4j.agent.core.boot.AgentPackageNotFoundException; -import cn.hippo4j.agent.core.boot.AgentPackagePath; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; -import cn.hippo4j.agent.core.logging.core.JsonLogResolver; -import cn.hippo4j.agent.core.logging.core.PatternLogResolver; -import cn.hippo4j.agent.core.util.ConfigInitializer; -import cn.hippo4j.agent.core.util.PropertyPlaceholderHelper; -import cn.hippo4j.agent.core.util.StringUtil; +import cn.hippo4j.common.boot.AgentPackageNotFoundException; +import cn.hippo4j.common.boot.AgentPackagePath; +import cn.hippo4j.common.conf.Config; +import cn.hippo4j.common.conf.ConfigNotFoundException; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; +import cn.hippo4j.common.logging.core.JsonLogResolver; +import cn.hippo4j.common.logging.core.PatternLogResolver; +import cn.hippo4j.common.toolkit.StringUtil; +import cn.hippo4j.common.toolkit.agent.ConfigInitializer; +import cn.hippo4j.common.toolkit.agent.PropertyPlaceholderHelper; import java.io.File; import java.io.FileInputStream; @@ -37,7 +39,7 @@ import java.util.Map; import java.util.Properties; -import static cn.hippo4j.agent.core.conf.Constants.SERVICE_NAME_PART_CONNECTOR; +import static cn.hippo4j.common.conf.Constants.SERVICE_NAME_PART_CONNECTOR; /** * The SnifferConfigInitializer initializes all configs in several way. @@ -204,7 +206,7 @@ private static void overrideConfigBySystemProp() { * * @return the config file {@link InputStream}, or null if not needEnhance. */ - private static InputStreamReader loadConfig() throws AgentPackageNotFoundException, ConfigNotFoundException { + private static InputStreamReader loadConfig() throws AgentPackageNotFoundException, cn.hippo4j.common.conf.ConfigNotFoundException { String specifiedConfigPath = System.getProperty(SPECIFIED_CONFIG_PATH); File configFile = StringUtil.isEmpty(specifiedConfigPath) ? new File( AgentPackagePath.getPath(), DEFAULT_CONFIG_FILE_NAME) : new File(specifiedConfigPath); diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/jvm/LoadedLibraryCollector.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/jvm/LoadedLibraryCollector.java index f8a466a6c4..6bae20aa4e 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/jvm/LoadedLibraryCollector.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/jvm/LoadedLibraryCollector.java @@ -17,9 +17,9 @@ package cn.hippo4j.agent.core.jvm; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; import cn.hippo4j.agent.core.util.CollectionUtil; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -28,13 +28,7 @@ import java.net.URL; import java.net.URLClassLoader; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; public class LoadedLibraryCollector { diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/AbstractClassEnhancePluginDefine.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/AbstractClassEnhancePluginDefine.java index 41384b1ab6..82278a6a5f 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/AbstractClassEnhancePluginDefine.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/AbstractClassEnhancePluginDefine.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.interceptor.ConstructorInterceptPoint; import cn.hippo4j.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; import cn.hippo4j.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/InstrumentDebuggingClass.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/InstrumentDebuggingClass.java index e82258fb18..02a900ccd2 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/InstrumentDebuggingClass.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/InstrumentDebuggingClass.java @@ -17,11 +17,11 @@ package cn.hippo4j.agent.core.plugin; -import cn.hippo4j.agent.core.boot.AgentPackageNotFoundException; import cn.hippo4j.agent.core.boot.AgentPackagePath; -import cn.hippo4j.agent.core.conf.Config; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; +import cn.hippo4j.common.boot.AgentPackageNotFoundException; +import cn.hippo4j.common.conf.Config; import net.bytebuddy.dynamic.DynamicType; import java.io.File; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginBootstrap.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginBootstrap.java index 12dffe3914..f76fe19d83 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginBootstrap.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginBootstrap.java @@ -17,10 +17,10 @@ package cn.hippo4j.agent.core.plugin; -import cn.hippo4j.agent.core.boot.AgentPackageNotFoundException; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.loader.AgentClassLoader; +import cn.hippo4j.common.boot.AgentPackageNotFoundException; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import java.net.URL; import java.util.ArrayList; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginCfg.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginCfg.java index 81e5027b8f..fe707c9ca0 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginCfg.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginCfg.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.exception.IllegalPluginDefineException; import java.io.BufferedReader; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginResourcesResolver.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginResourcesResolver.java index 06058a0b52..861995eeaf 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginResourcesResolver.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginResourcesResolver.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.loader.AgentClassLoader; import java.io.IOException; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginSelector.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginSelector.java index 8f336b36e2..4859909ee1 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginSelector.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/PluginSelector.java @@ -20,9 +20,8 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; -import cn.hippo4j.agent.core.conf.Config; -import static cn.hippo4j.agent.core.conf.Config.Plugin.EXCLUDE_PLUGINS; +import static cn.hippo4j.common.conf.Config.Plugin.EXCLUDE_PLUGINS; /** * Select some plugins in activated plugins diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java index 3260e439b5..7138ea8574 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.bootstrap; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.AbstractClassEnhancePluginDefine; import cn.hippo4j.agent.core.plugin.ByteBuddyCoreClasses; import cn.hippo4j.agent.core.plugin.InstrumentDebuggingClass; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bootstrap/BootstrapPluginLogBridge.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bootstrap/BootstrapPluginLogBridge.java index 868b1c2c86..bdcbcfdc0f 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bootstrap/BootstrapPluginLogBridge.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bootstrap/BootstrapPluginLogBridge.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.bootstrap; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; /** * The log bridge makes the ILog accessible inside bootstrap classloader, especially for internal interceptor. diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy/CacheableTransformerDecorator.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy/CacheableTransformerDecorator.java index 8341772c23..f0ed68c6b7 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy/CacheableTransformerDecorator.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy/CacheableTransformerDecorator.java @@ -17,21 +17,18 @@ package cn.hippo4j.agent.core.plugin.bytebuddy; -import cn.hippo4j.agent.core.boot.AgentPackageNotFoundException; import cn.hippo4j.agent.core.boot.AgentPackagePath; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; import cn.hippo4j.agent.core.util.FileUtils; import cn.hippo4j.agent.core.util.IOUtils; +import cn.hippo4j.common.boot.AgentPackageNotFoundException; +import cn.hippo4j.common.boot.ClassCacheMode; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import net.bytebuddy.agent.builder.AgentBuilder; import net.bytebuddy.agent.builder.ResettableClassFileTransformer; import net.bytebuddy.utility.RandomString; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; +import java.io.*; import java.lang.instrument.IllegalClassFormatException; import java.security.ProtectionDomain; import java.util.Map; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java index 70dddbadb2..8378aa35b1 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java @@ -17,20 +17,16 @@ package cn.hippo4j.agent.core.plugin.interceptor.enhance; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.AbstractClassEnhancePluginDefine; import cn.hippo4j.agent.core.plugin.EnhanceContext; import cn.hippo4j.agent.core.plugin.PluginException; import cn.hippo4j.agent.core.plugin.bootstrap.BootstrapInstrumentBoost; -import cn.hippo4j.agent.core.plugin.interceptor.ConstructorInterceptPoint; -import cn.hippo4j.agent.core.plugin.interceptor.DeclaredInstanceMethodsInterceptPoint; -import cn.hippo4j.agent.core.plugin.interceptor.EnhanceException; -import cn.hippo4j.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; -import cn.hippo4j.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; +import cn.hippo4j.agent.core.plugin.interceptor.*; import cn.hippo4j.agent.core.plugin.interceptor.v2.InstanceMethodsInterceptV2Point; import cn.hippo4j.agent.core.plugin.interceptor.v2.StaticMethodsInterceptV2Point; import cn.hippo4j.agent.core.util.StringUtil; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.dynamic.DynamicType; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ConstructorInter.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ConstructorInter.java index d5b24c1d6c..7896eb22e0 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ConstructorInter.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ConstructorInter.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.interceptor.enhance; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.PluginException; import cn.hippo4j.agent.core.plugin.loader.InterceptorInstanceLoader; import net.bytebuddy.implementation.bind.annotation.AllArguments; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/InstMethodsInter.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/InstMethodsInter.java index 489c54d3e2..1d5dd6c1c0 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/InstMethodsInter.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/InstMethodsInter.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.interceptor.enhance; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.PluginException; import cn.hippo4j.agent.core.plugin.loader.InterceptorInstanceLoader; import net.bytebuddy.implementation.bind.annotation.AllArguments; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/InstMethodsInterWithOverrideArgs.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/InstMethodsInterWithOverrideArgs.java index 5febdb8310..57bc744442 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/InstMethodsInterWithOverrideArgs.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/InstMethodsInterWithOverrideArgs.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.interceptor.enhance; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.PluginException; import cn.hippo4j.agent.core.plugin.loader.InterceptorInstanceLoader; import net.bytebuddy.implementation.bind.annotation.AllArguments; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/StaticMethodsInter.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/StaticMethodsInter.java index 3be9ccbab1..0a98c7de57 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/StaticMethodsInter.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/StaticMethodsInter.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.interceptor.enhance; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.loader.InterceptorInstanceLoader; import net.bytebuddy.implementation.bind.annotation.AllArguments; import net.bytebuddy.implementation.bind.annotation.Origin; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/StaticMethodsInterWithOverrideArgs.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/StaticMethodsInterWithOverrideArgs.java index 99f83a17b3..e7c11a7fb5 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/StaticMethodsInterWithOverrideArgs.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/StaticMethodsInterWithOverrideArgs.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.interceptor.enhance; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.loader.InterceptorInstanceLoader; import net.bytebuddy.implementation.bind.annotation.AllArguments; import net.bytebuddy.implementation.bind.annotation.Morph; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/InstMethodsInterV2.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/InstMethodsInterV2.java index 3967736a4f..eea316d00b 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/InstMethodsInterV2.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/InstMethodsInterV2.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.interceptor.enhance.v2; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.PluginException; import cn.hippo4j.agent.core.plugin.interceptor.enhance.EnhancedInstance; import cn.hippo4j.agent.core.plugin.loader.InterceptorInstanceLoader; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/InstMethodsInterV2WithOverrideArgs.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/InstMethodsInterV2WithOverrideArgs.java index f9cd51d609..9aaa9888ac 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/InstMethodsInterV2WithOverrideArgs.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/InstMethodsInterV2WithOverrideArgs.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.interceptor.enhance.v2; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.PluginException; import cn.hippo4j.agent.core.plugin.interceptor.enhance.EnhancedInstance; import cn.hippo4j.agent.core.plugin.interceptor.enhance.OverrideCallable; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/StaticMethodsInterV2.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/StaticMethodsInterV2.java index 9778577098..7bd4c2f184 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/StaticMethodsInterV2.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/StaticMethodsInterV2.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.interceptor.enhance.v2; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.loader.InterceptorInstanceLoader; import net.bytebuddy.implementation.bind.annotation.AllArguments; import net.bytebuddy.implementation.bind.annotation.Origin; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/StaticMethodsInterV2WithOverrideArgs.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/StaticMethodsInterV2WithOverrideArgs.java index 467af26a05..ca86bf2ec1 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/StaticMethodsInterV2WithOverrideArgs.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/v2/StaticMethodsInterV2WithOverrideArgs.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.interceptor.enhance.v2; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.interceptor.enhance.OverrideCallable; import cn.hippo4j.agent.core.plugin.loader.InterceptorInstanceLoader; import net.bytebuddy.implementation.bind.annotation.AllArguments; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/jdk9module/JDK9ModuleExporter.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/jdk9module/JDK9ModuleExporter.java index 1ff3514ee4..07c69b5485 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/jdk9module/JDK9ModuleExporter.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/jdk9module/JDK9ModuleExporter.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.jdk9module; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.ByteBuddyCoreClasses; import net.bytebuddy.agent.builder.AgentBuilder; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/loader/AgentClassLoader.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/loader/AgentClassLoader.java index 3fec67cc41..d227ec7e17 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/loader/AgentClassLoader.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/loader/AgentClassLoader.java @@ -17,16 +17,16 @@ package cn.hippo4j.agent.core.plugin.loader; -import cn.hippo4j.agent.core.boot.AgentPackageNotFoundException; import cn.hippo4j.agent.core.boot.AgentPackagePath; import cn.hippo4j.agent.core.boot.PluginConfig; -import cn.hippo4j.agent.core.boot.SpringBootConfigNode; import cn.hippo4j.agent.core.boot.SpringBootConfigInitializer; -import cn.hippo4j.agent.core.conf.Config; +import cn.hippo4j.agent.core.boot.SpringBootConfigNode; import cn.hippo4j.agent.core.conf.SnifferConfigInitializer; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.PluginBootstrap; +import cn.hippo4j.common.boot.AgentPackageNotFoundException; +import cn.hippo4j.common.conf.Config; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import lombok.RequiredArgsConstructor; import java.io.BufferedInputStream; @@ -62,7 +62,7 @@ public class AgentClassLoader extends ClassLoader { private static AgentClassLoader DEFAULT_LOADER; private List classpath; - private List allJars; + private static List allJars; private ReentrantLock jarScanLock = new ReentrantLock(); public static AgentClassLoader getDefault() { @@ -192,7 +192,6 @@ private List getAllJars() { jarScanLock.unlock(); } } - return allJars; } diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/loader/InterceptorInstanceLoader.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/loader/InterceptorInstanceLoader.java index cd1e03520d..e5c55ad46a 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/loader/InterceptorInstanceLoader.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/loader/InterceptorInstanceLoader.java @@ -17,7 +17,7 @@ package cn.hippo4j.agent.core.plugin.loader; -import cn.hippo4j.agent.core.boot.AgentPackageNotFoundException; +import cn.hippo4j.common.boot.AgentPackageNotFoundException; import java.util.HashMap; import java.util.Map; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/match/ProtectiveShieldMatcher.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/match/ProtectiveShieldMatcher.java index d0ec36cb55..7a02b30027 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/match/ProtectiveShieldMatcher.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/match/ProtectiveShieldMatcher.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.plugin.match; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import net.bytebuddy.matcher.ElementMatcher; /** diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/CustomizeExpression.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/CustomizeExpression.java index 4912936bfe..f4aebf8a01 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/CustomizeExpression.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/CustomizeExpression.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.util; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import java.lang.reflect.Array; import java.lang.reflect.Field; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/ExecutorNameUtil.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/ExecutorNameUtil.java index 7a597187fd..e605e1ad15 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/ExecutorNameUtil.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/ExecutorNameUtil.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.util; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import java.lang.reflect.Field; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/ReflectUtil.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/ReflectUtil.java index e6fed965c6..105a5274c6 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/ReflectUtil.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/util/ReflectUtil.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.core.util; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import java.lang.reflect.Field; import java.lang.reflect.Modifier; diff --git a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java index 777194f0f4..dcee872618 100644 --- a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java +++ b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-1x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v1/interceptor/EventPublishingFinishedInterceptor.java @@ -18,8 +18,8 @@ package cn.hippo4j.agent.plugin.spring.boot.v1.interceptor; import cn.hippo4j.agent.adapter.dubbo.DubboThreadPoolAdapter; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.interceptor.enhance.EnhancedInstance; import cn.hippo4j.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import cn.hippo4j.agent.core.plugin.interceptor.enhance.MethodInterceptResult; diff --git a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/DynamicThreadPoolChangeHandlerSpring2x.java b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/DynamicThreadPoolChangeHandlerSpring2x.java index bcda15e4b5..6f70bf237c 100644 --- a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/DynamicThreadPoolChangeHandlerSpring2x.java +++ b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/DynamicThreadPoolChangeHandlerSpring2x.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.plugin.spring.boot.v2; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.plugin.spring.common.conf.SpringBootConfig; import cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties; import cn.hippo4j.threadpool.dynamic.mode.config.refresher.AbstractConfigThreadPoolDynamicRefresh; diff --git a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/boot/SpringBootV2PluginBootService.java b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/boot/SpringBootV2PluginBootService.java index c9b8c85bb1..d4763704cd 100644 --- a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/boot/SpringBootV2PluginBootService.java +++ b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/boot/SpringBootV2PluginBootService.java @@ -19,8 +19,8 @@ import cn.hippo4j.agent.core.boot.BootService; import cn.hippo4j.agent.core.boot.DefaultImplementor; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; /** * SpringBoot v1 plugin boot service diff --git a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/interceptor/EventPublishingStartedInterceptor.java b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/interceptor/EventPublishingStartedInterceptor.java index 0718469249..d9df47722e 100644 --- a/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/interceptor/EventPublishingStartedInterceptor.java +++ b/agent/hippo4j-agent-plugin/spring-plugins/spring-boot-2x-plugin/src/main/java/cn/hippo4j/agent/plugin/spring/boot/v2/interceptor/EventPublishingStartedInterceptor.java @@ -17,8 +17,8 @@ package cn.hippo4j.agent.plugin.spring.boot.v2.interceptor; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.interceptor.enhance.EnhancedInstance; import cn.hippo4j.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import cn.hippo4j.agent.core.plugin.interceptor.enhance.MethodInterceptResult; diff --git a/agent/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringPropertiesLoader.java b/agent/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringPropertiesLoader.java index bebfb843a8..8b215ec0bc 100644 --- a/agent/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringPropertiesLoader.java +++ b/agent/hippo4j-agent-plugin/spring-plugins/spring-plugin-common/src/main/java/cn/hippo4j/agent/plugin/spring/common/support/SpringPropertiesLoader.java @@ -18,8 +18,8 @@ package cn.hippo4j.agent.plugin.spring.common.support; import cn.hippo4j.agent.core.boot.SpringBootConfigInitializer; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.PropertySource; diff --git a/agent/hippo4j-agent-plugin/threadpool-plugin/src/main/java/cn/hippo4j/agent/plugin/thread/pool/interceptor/ThreadPoolExecutorConstructorMethodInterceptor.java b/agent/hippo4j-agent-plugin/threadpool-plugin/src/main/java/cn/hippo4j/agent/plugin/thread/pool/interceptor/ThreadPoolExecutorConstructorMethodInterceptor.java index fb4194c852..fa4424a899 100644 --- a/agent/hippo4j-agent-plugin/threadpool-plugin/src/main/java/cn/hippo4j/agent/plugin/thread/pool/interceptor/ThreadPoolExecutorConstructorMethodInterceptor.java +++ b/agent/hippo4j-agent-plugin/threadpool-plugin/src/main/java/cn/hippo4j/agent/plugin/thread/pool/interceptor/ThreadPoolExecutorConstructorMethodInterceptor.java @@ -17,14 +17,15 @@ package cn.hippo4j.agent.plugin.thread.pool.interceptor; -import cn.hippo4j.agent.core.conf.Config; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogManager; import cn.hippo4j.agent.core.plugin.interceptor.enhance.EnhancedInstance; import cn.hippo4j.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; -import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; import cn.hippo4j.agent.core.util.CollectionUtil; import cn.hippo4j.agent.core.util.StringUtil; +import cn.hippo4j.common.conf.Config; +import cn.hippo4j.common.executor.ThreadPoolExecutorRegistry; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; diff --git a/examples/threadpool-example/agent/config-apollo/src/main/resources/bootstrap.properties b/examples/threadpool-example/agent/config-apollo/src/main/resources/bootstrap.properties index 88187c9c8a..d467930f4b 100644 --- a/examples/threadpool-example/agent/config-apollo/src/main/resources/bootstrap.properties +++ b/examples/threadpool-example/agent/config-apollo/src/main/resources/bootstrap.properties @@ -31,3 +31,18 @@ spring.dynamic.thread-pool.check-state-interval=3 spring.dynamic.thread-pool.apollo.namespace=application spring.dynamic.thread-pool.config-file-type=properties +spring.dynamic.thread-pool.executors[0].thread-name-prefix = DynamicThreadPoolConfig#FIELD1 +spring.dynamic.thread-pool.executors[0].core-pool-size = 2 +spring.dynamic.thread-pool.executors[0].thread-pool-id = cn.hippo4j.example.agent.config.apollo.ThreadPoolConfiguration#RUN_MESSAGE_SEND_TASK_EXECUTOR +spring.dynamic.thread-pool.executors[0].maximum-pool-size = 20 +spring.dynamic.thread-pool.executors[0].queue-capacity = 1024 +spring.dynamic.thread-pool.executors[0].blocking-queue = ResizableCapacityLinkedBlockingQueue +spring.dynamic.thread-pool.executors[0].execute-time-out = 800 +spring.dynamic.thread-pool.executors[0].rejected-handler = AbortPolicy +spring.dynamic.thread-pool.executors[0].keep-alive-time = 6691 +spring.dynamic.thread-pool.executors[0].allow-core-thread-time-out = true +spring.dynamic.thread-pool.executors[0].alarm = true +spring.dynamic.thread-pool.executors[0].active-alarm = 80 +spring.dynamic.thread-pool.executors[0].capacity-alarm = 80 +spring.dynamic.thread-pool.executors[0].notify.interval = 8 +spring.dynamic.thread-pool.executors[0].notify.receives = nobodyiam diff --git a/infra/common/pom.xml b/infra/common/pom.xml index 20f67cbb08..ae48d1afa4 100644 --- a/infra/common/pom.xml +++ b/infra/common/pom.xml @@ -10,6 +10,11 @@ hippo4j-threadpool-infra-common + + com.google.code.gson + gson + ${gson.version} + com.fasterxml.jackson.core jackson-databind diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/AgentPackageNotFoundException.java b/infra/common/src/main/java/cn/hippo4j/common/boot/AgentPackageNotFoundException.java similarity index 96% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/AgentPackageNotFoundException.java rename to infra/common/src/main/java/cn/hippo4j/common/boot/AgentPackageNotFoundException.java index e26aae8a72..273d47e6ae 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/AgentPackageNotFoundException.java +++ b/infra/common/src/main/java/cn/hippo4j/common/boot/AgentPackageNotFoundException.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.boot; +package cn.hippo4j.common.boot; public class AgentPackageNotFoundException extends Exception { diff --git a/infra/common/src/main/java/cn/hippo4j/common/boot/AgentPackagePath.java b/infra/common/src/main/java/cn/hippo4j/common/boot/AgentPackagePath.java new file mode 100644 index 0000000000..1afbdfb750 --- /dev/null +++ b/infra/common/src/main/java/cn/hippo4j/common/boot/AgentPackagePath.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.boot; + +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; + +/** + * AgentPackagePath is a flag and finder to locate the Hippo4j agent.jar. It gets the absolute path of the agent jar. + * The path is the required metadata for agent core looking up the plugins and toolkit activations. If the lookup + * mechanism fails, the agent will exit directly. + */ +public class AgentPackagePath { + + private static final ILog LOGGER = LogManager.getLogger(AgentPackagePath.class); + + private static File AGENT_PACKAGE_PATH; + + public static File getPath() throws AgentPackageNotFoundException { + if (AGENT_PACKAGE_PATH == null) { + AGENT_PACKAGE_PATH = findPath(); + } + return AGENT_PACKAGE_PATH; + } + + public static boolean isPathFound() { + return AGENT_PACKAGE_PATH != null; + } + + private static File findPath() throws AgentPackageNotFoundException { + String classResourcePath = AgentPackagePath.class.getName().replaceAll("\\.", "/") + ".class"; + + URL resource = ClassLoader.getSystemClassLoader().getResource(classResourcePath); + if (resource != null) { + String urlString = resource.toString(); + + LOGGER.debug("The beacon class location is {}.", urlString); + + int insidePathIndex = urlString.indexOf('!'); + boolean isInJar = insidePathIndex > -1; + + if (isInJar) { + urlString = urlString.substring(urlString.indexOf("file:"), insidePathIndex); + File agentJarFile = null; + try { + agentJarFile = new File(new URL(urlString).toURI()); + } catch (MalformedURLException | URISyntaxException e) { + LOGGER.error(e, "Can not locate agent jar file by url:" + urlString); + } + if (agentJarFile.exists()) { + return agentJarFile.getParentFile(); + } + } else { + int prefixLength = "file:".length(); + String classLocation = urlString.substring( + prefixLength, urlString.length() - classResourcePath.length()); + return new File(classLocation); + } + } + + LOGGER.error("Can not locate agent jar file."); + throw new AgentPackageNotFoundException("Can not locate agent jar file."); + } + +} diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy/ClassCacheMode.java b/infra/common/src/main/java/cn/hippo4j/common/boot/ClassCacheMode.java similarity index 90% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy/ClassCacheMode.java rename to infra/common/src/main/java/cn/hippo4j/common/boot/ClassCacheMode.java index d5baf606ca..5db729f50e 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy/ClassCacheMode.java +++ b/infra/common/src/main/java/cn/hippo4j/common/boot/ClassCacheMode.java @@ -15,11 +15,8 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.plugin.bytebuddy; +package cn.hippo4j.common.boot; -/** - * ByteBuddy class cache mode - */ public enum ClassCacheMode { FILE, MEMORY -} +} \ No newline at end of file diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/DefaultNamedThreadFactory.java b/infra/common/src/main/java/cn/hippo4j/common/boot/DefaultNamedThreadFactory.java similarity index 97% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/DefaultNamedThreadFactory.java rename to infra/common/src/main/java/cn/hippo4j/common/boot/DefaultNamedThreadFactory.java index 41c41204bc..1bbe203ce6 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/boot/DefaultNamedThreadFactory.java +++ b/infra/common/src/main/java/cn/hippo4j/common/boot/DefaultNamedThreadFactory.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.boot; +package cn.hippo4j.common.boot; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/Config.java b/infra/common/src/main/java/cn/hippo4j/common/conf/Config.java old mode 100755 new mode 100644 similarity index 95% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/Config.java rename to infra/common/src/main/java/cn/hippo4j/common/conf/Config.java index 89e1c57794..8503055444 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/Config.java +++ b/infra/common/src/main/java/cn/hippo4j/common/conf/Config.java @@ -15,14 +15,13 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.conf; - -import cn.hippo4j.agent.core.logging.core.LogLevel; -import cn.hippo4j.agent.core.logging.core.LogOutput; -import cn.hippo4j.agent.core.logging.core.ResolverType; -import cn.hippo4j.agent.core.logging.core.WriterFactory; -import cn.hippo4j.agent.core.plugin.bytebuddy.ClassCacheMode; -import cn.hippo4j.agent.core.util.Length; +package cn.hippo4j.common.conf; + +import cn.hippo4j.common.boot.ClassCacheMode; +import cn.hippo4j.common.logging.core.LogLevel; +import cn.hippo4j.common.logging.core.LogOutput; +import cn.hippo4j.common.logging.core.ResolverType; +import cn.hippo4j.common.toolkit.agent.Length; import java.util.Arrays; import java.util.List; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/ConfigNotFoundException.java b/infra/common/src/main/java/cn/hippo4j/common/conf/ConfigNotFoundException.java similarity index 96% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/ConfigNotFoundException.java rename to infra/common/src/main/java/cn/hippo4j/common/conf/ConfigNotFoundException.java index 9d7d387c59..b32bd864c0 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/ConfigNotFoundException.java +++ b/infra/common/src/main/java/cn/hippo4j/common/conf/ConfigNotFoundException.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.conf; +package cn.hippo4j.common.conf; public class ConfigNotFoundException extends Exception { diff --git a/infra/common/src/main/java/cn/hippo4j/common/conf/Constants.java b/infra/common/src/main/java/cn/hippo4j/common/conf/Constants.java new file mode 100644 index 0000000000..1973cebe9b --- /dev/null +++ b/infra/common/src/main/java/cn/hippo4j/common/conf/Constants.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.conf; + +public class Constants { + + public static String PATH_SEPARATOR = System.getProperty("file.separator", "/"); + + public static String LINE_SEPARATOR = System.getProperty("line.separator", "\n"); + + public static String EMPTY_STRING = ""; + + public static char SERVICE_NAME_PART_CONNECTOR = '|'; + + // The name of the layer that represents agent-installed services, + // which is defined at + // https://github.com/apache/skywalking/blob/85ce1645be53e46286f36c0ea206c60db2d1a716/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/Layer.java#L30 + public static String EVENT_LAYER_NAME = "GENERAL"; + + public static int NULL_VALUE = 0; + + public static boolean IS_INIT_COMPLETED = false; + +} diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/RuntimeContextConfiguration.java b/infra/common/src/main/java/cn/hippo4j/common/conf/RuntimeContextConfiguration.java similarity index 96% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/RuntimeContextConfiguration.java rename to infra/common/src/main/java/cn/hippo4j/common/conf/RuntimeContextConfiguration.java index 0864baa0e2..372492eb92 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/conf/RuntimeContextConfiguration.java +++ b/infra/common/src/main/java/cn/hippo4j/common/conf/RuntimeContextConfiguration.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.conf; +package cn.hippo4j.common.conf; public class RuntimeContextConfiguration { diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/ILog.java b/infra/common/src/main/java/cn/hippo4j/common/logging/api/ILog.java similarity index 97% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/ILog.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/api/ILog.java index 89f1031f97..3bb9cba0ec 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/ILog.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/api/ILog.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.api; +package cn.hippo4j.common.logging.api; /** * The Log interface. It's very easy to understand, like any other log-component. Do just like log4j or log4j2 does. diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/LogManager.java b/infra/common/src/main/java/cn/hippo4j/common/logging/api/LogManager.java similarity index 95% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/LogManager.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/api/LogManager.java index cb6514bc68..3c72d7ef4b 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/LogManager.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/api/LogManager.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.api; +package cn.hippo4j.common.logging.api; -import cn.hippo4j.agent.core.logging.core.PatternLogResolver; +import cn.hippo4j.common.logging.core.PatternLogResolver; /** * LogManager is the {@link LogResolver} implementation manager. By using {@link LogResolver}, {@link diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/LogResolver.java b/infra/common/src/main/java/cn/hippo4j/common/logging/api/LogResolver.java similarity index 96% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/LogResolver.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/api/LogResolver.java index 4f145dbb7a..a6cf32a03b 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/LogResolver.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/api/LogResolver.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.api; +package cn.hippo4j.common.logging.api; /** * {@link LogResolver} just do only one thing: return the {@link ILog} implementation. diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/NoopLogger.java b/infra/common/src/main/java/cn/hippo4j/common/logging/api/NoopLogger.java similarity index 98% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/NoopLogger.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/api/NoopLogger.java index 9f544b63a4..99bc3db7f7 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/api/NoopLogger.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/api/NoopLogger.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.api; +package cn.hippo4j.common.logging.api; /** * No operation logger implementation. Just implement {@link ILog} interface, but do nothing. diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/AbstractLogger.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/AbstractLogger.java similarity index 91% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/AbstractLogger.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/AbstractLogger.java index 05a53ff08b..c67121c776 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/AbstractLogger.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/AbstractLogger.java @@ -15,17 +15,11 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; - -import cn.hippo4j.agent.core.conf.Config; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.core.converters.AgentNameConverter; -import cn.hippo4j.agent.core.logging.core.converters.ClassConverter; -import cn.hippo4j.agent.core.logging.core.converters.DateConverter; -import cn.hippo4j.agent.core.logging.core.converters.LevelConverter; -import cn.hippo4j.agent.core.logging.core.converters.MessageConverter; -import cn.hippo4j.agent.core.logging.core.converters.ThreadConverter; -import cn.hippo4j.agent.core.logging.core.converters.ThrowableConverter; +package cn.hippo4j.common.logging.core; + +import cn.hippo4j.common.conf.Config; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.core.converters.*; import java.util.ArrayList; import java.util.HashMap; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/Converter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/Converter.java similarity index 95% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/Converter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/Converter.java index 0bf8d9795e..27dd8981b5 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/Converter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/Converter.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; /** * The Converter, it is used to convert the LogEvent to the String. diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/FileWriter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/FileWriter.java similarity index 94% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/FileWriter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/FileWriter.java index e2fde3087e..6a26349526 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/FileWriter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/FileWriter.java @@ -15,18 +15,14 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; - -import cn.hippo4j.agent.core.boot.DefaultNamedThreadFactory; -import cn.hippo4j.agent.core.conf.Config; -import cn.hippo4j.agent.core.conf.Constants; -import cn.hippo4j.agent.core.util.RunnableWithExceptionProtection; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.FilenameFilter; -import java.io.IOException; +package cn.hippo4j.common.logging.core; + +import cn.hippo4j.common.boot.DefaultNamedThreadFactory; +import cn.hippo4j.common.conf.Config; +import cn.hippo4j.common.constant.Constants; +import cn.hippo4j.common.toolkit.agent.RunnableWithExceptionProtection; + +import java.io.*; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/IWriter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/IWriter.java similarity index 95% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/IWriter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/IWriter.java index 1dae6b82d1..d0acc94f9f 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/IWriter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/IWriter.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; public interface IWriter { diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/JsonLogResolver.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/JsonLogResolver.java similarity index 88% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/JsonLogResolver.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/JsonLogResolver.java index 913bc770c4..befbdc9a1d 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/JsonLogResolver.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/JsonLogResolver.java @@ -15,10 +15,10 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogResolver; +import cn.hippo4j.common.logging.api.LogResolver; +import cn.hippo4j.common.logging.api.ILog; import com.google.gson.Gson; public class JsonLogResolver implements LogResolver { diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/JsonLogger.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/JsonLogger.java similarity index 96% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/JsonLogger.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/JsonLogger.java index fc657ef1ea..001684dffe 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/JsonLogger.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/JsonLogger.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; -import cn.hippo4j.agent.core.logging.core.converters.LiteralConverter; +import cn.hippo4j.common.logging.core.converters.LiteralConverter; import com.google.gson.Gson; import java.util.HashMap; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogEvent.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/LogEvent.java similarity index 97% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogEvent.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/LogEvent.java index 1246232d1b..a0aa11ab68 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogEvent.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/LogEvent.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; /** * The representation of logging events. This instance is pass around to the List of Converter. diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogLevel.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/LogLevel.java similarity index 95% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogLevel.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/LogLevel.java index facc35b317..73791af9a2 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogLevel.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/LogLevel.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; public enum LogLevel { TRACE, DEBUG, INFO, WARN, ERROR, OFF diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogMessageHolder.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/LogMessageHolder.java similarity index 96% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogMessageHolder.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/LogMessageHolder.java index 244733b57d..005d2d4d87 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogMessageHolder.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/LogMessageHolder.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; /** * The LogMessageHolder is a {@link String} holder, in order to in-process propagation String across the diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogOutput.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/LogOutput.java similarity index 95% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogOutput.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/LogOutput.java index 2bf279a841..c8ba7bc269 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/LogOutput.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/LogOutput.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; public enum LogOutput { FILE, CONSOLE diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/Parser.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/Parser.java similarity index 98% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/Parser.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/Parser.java index 999b78f7ca..24cabc4a0c 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/Parser.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/Parser.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; -import cn.hippo4j.agent.core.logging.core.converters.LiteralConverter; +import cn.hippo4j.common.logging.core.converters.LiteralConverter; import java.util.ArrayList; import java.util.List; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/PatternLogResolver.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/PatternLogResolver.java similarity index 85% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/PatternLogResolver.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/PatternLogResolver.java index 075c2e01a2..0a761683a5 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/PatternLogResolver.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/PatternLogResolver.java @@ -15,11 +15,11 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; -import cn.hippo4j.agent.core.conf.Config; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.logging.api.LogResolver; +import cn.hippo4j.common.conf.Config; +import cn.hippo4j.common.logging.api.LogResolver; +import cn.hippo4j.common.logging.api.ILog; public class PatternLogResolver implements LogResolver { diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/PatternLogger.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/PatternLogger.java similarity index 94% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/PatternLogger.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/PatternLogger.java index b9e4ef67d0..c713bb5abd 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/PatternLogger.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/PatternLogger.java @@ -15,10 +15,9 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; -import cn.hippo4j.agent.core.logging.api.ILog; -import cn.hippo4j.agent.core.util.StringUtil; +import cn.hippo4j.common.toolkit.StringUtil; /** * A flexible Logger configurable with pattern string. This is default implementation of {@link ILog} This can parse a diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/ResolverType.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/ResolverType.java similarity index 95% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/ResolverType.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/ResolverType.java index 734d9294fc..774cb310ab 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/ResolverType.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/ResolverType.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; public enum ResolverType { JSON, PATTERN diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/SystemOutWriter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/SystemOutWriter.java similarity index 96% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/SystemOutWriter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/SystemOutWriter.java index e4cce11990..312d8fd2e0 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/SystemOutWriter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/SystemOutWriter.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; import java.io.PrintStream; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/WriterFactory.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/WriterFactory.java similarity index 78% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/WriterFactory.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/WriterFactory.java index 72f2ddeee4..4bf5331b1a 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/WriterFactory.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/WriterFactory.java @@ -15,14 +15,13 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core; +package cn.hippo4j.common.logging.core; -import cn.hippo4j.agent.core.boot.AgentPackageNotFoundException; -import cn.hippo4j.agent.core.boot.AgentPackagePath; -import cn.hippo4j.agent.core.conf.Config; -import cn.hippo4j.agent.core.conf.SnifferConfigInitializer; -import cn.hippo4j.agent.core.plugin.PluginFinder; -import cn.hippo4j.agent.core.util.StringUtil; +import cn.hippo4j.common.boot.AgentPackageNotFoundException; +import cn.hippo4j.common.boot.AgentPackagePath; +import cn.hippo4j.common.conf.Config; +import cn.hippo4j.common.conf.Constants; +import cn.hippo4j.common.toolkit.StringUtil; public class WriterFactory { @@ -35,8 +34,7 @@ public static IWriter getLogWriter() { if (WRITER != null) { return WRITER; } - if (SnifferConfigInitializer.isInitCompleted() - && PluginFinder.isPluginInitCompleted() + if (Constants.IS_INIT_COMPLETED && AgentPackagePath.isPathFound()) { if (StringUtil.isEmpty(Config.Logging.DIR)) { try { diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/AgentNameConverter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/AgentNameConverter.java similarity index 83% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/AgentNameConverter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/AgentNameConverter.java index 21090d2574..d6015b2179 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/AgentNameConverter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/AgentNameConverter.java @@ -15,11 +15,11 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core.converters; +package cn.hippo4j.common.logging.core.converters; -import cn.hippo4j.agent.core.conf.Config; -import cn.hippo4j.agent.core.logging.core.Converter; -import cn.hippo4j.agent.core.logging.core.LogEvent; +import cn.hippo4j.common.conf.Config; +import cn.hippo4j.common.logging.core.Converter; +import cn.hippo4j.common.logging.core.LogEvent; public class AgentNameConverter implements Converter { diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/ClassConverter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/ClassConverter.java similarity index 87% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/ClassConverter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/ClassConverter.java index 5dc9951dbf..96dbcdc294 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/ClassConverter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/ClassConverter.java @@ -15,10 +15,10 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core.converters; +package cn.hippo4j.common.logging.core.converters; -import cn.hippo4j.agent.core.logging.core.Converter; -import cn.hippo4j.agent.core.logging.core.LogEvent; +import cn.hippo4j.common.logging.core.Converter; +import cn.hippo4j.common.logging.core.LogEvent; /** * Just return logEvent.getTargetClass(). diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/DateConverter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/DateConverter.java similarity index 88% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/DateConverter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/DateConverter.java index e3a68067fb..f18da36b24 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/DateConverter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/DateConverter.java @@ -15,10 +15,10 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core.converters; +package cn.hippo4j.common.logging.core.converters; -import cn.hippo4j.agent.core.logging.core.Converter; -import cn.hippo4j.agent.core.logging.core.LogEvent; +import cn.hippo4j.common.logging.core.Converter; +import cn.hippo4j.common.logging.core.LogEvent; import java.text.SimpleDateFormat; import java.util.Date; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/LevelConverter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/LevelConverter.java similarity index 87% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/LevelConverter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/LevelConverter.java index 775c97ce78..ff818c18a7 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/LevelConverter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/LevelConverter.java @@ -15,10 +15,10 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core.converters; +package cn.hippo4j.common.logging.core.converters; -import cn.hippo4j.agent.core.logging.core.Converter; -import cn.hippo4j.agent.core.logging.core.LogEvent; +import cn.hippo4j.common.logging.core.Converter; +import cn.hippo4j.common.logging.core.LogEvent; /** * Just return logEvent.getLevel().name() diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/LiteralConverter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/LiteralConverter.java similarity index 88% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/LiteralConverter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/LiteralConverter.java index bd334955a9..58bb473442 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/LiteralConverter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/LiteralConverter.java @@ -15,10 +15,10 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core.converters; +package cn.hippo4j.common.logging.core.converters; -import cn.hippo4j.agent.core.logging.core.Converter; -import cn.hippo4j.agent.core.logging.core.LogEvent; +import cn.hippo4j.common.logging.core.Converter; +import cn.hippo4j.common.logging.core.LogEvent; /** * This Converter is used to return the literal. diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/MessageConverter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/MessageConverter.java similarity index 87% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/MessageConverter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/MessageConverter.java index a38cc3c7c7..fc15990fd9 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/MessageConverter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/MessageConverter.java @@ -15,10 +15,10 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core.converters; +package cn.hippo4j.common.logging.core.converters; -import cn.hippo4j.agent.core.logging.core.Converter; -import cn.hippo4j.agent.core.logging.core.LogEvent; +import cn.hippo4j.common.logging.core.Converter; +import cn.hippo4j.common.logging.core.LogEvent; /** * Just return the logEvent.getMessage() diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/ThreadConverter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/ThreadConverter.java similarity index 87% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/ThreadConverter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/ThreadConverter.java index 7bd6b16fdd..a22e16dfd1 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/ThreadConverter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/ThreadConverter.java @@ -15,10 +15,10 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core.converters; +package cn.hippo4j.common.logging.core.converters; -import cn.hippo4j.agent.core.logging.core.Converter; -import cn.hippo4j.agent.core.logging.core.LogEvent; +import cn.hippo4j.common.logging.core.Converter; +import cn.hippo4j.common.logging.core.LogEvent; /** * Just return the Thread.currentThread().getName() diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/ThrowableConverter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/ThrowableConverter.java similarity index 88% rename from agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/ThrowableConverter.java rename to infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/ThrowableConverter.java index 8d307f0ad8..3bab6e2b9c 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/logging/core/converters/ThrowableConverter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/converters/ThrowableConverter.java @@ -15,11 +15,11 @@ * limitations under the License. */ -package cn.hippo4j.agent.core.logging.core.converters; +package cn.hippo4j.common.logging.core.converters; -import cn.hippo4j.agent.core.conf.Constants; -import cn.hippo4j.agent.core.logging.core.Converter; -import cn.hippo4j.agent.core.logging.core.LogEvent; +import cn.hippo4j.common.constant.Constants; +import cn.hippo4j.common.logging.core.Converter; +import cn.hippo4j.common.logging.core.LogEvent; import java.io.ByteArrayOutputStream; import java.io.IOException; diff --git a/infra/common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java b/infra/common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java index 7c74e2cc3a..d85065068e 100644 --- a/infra/common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java +++ b/infra/common/src/main/java/cn/hippo4j/common/toolkit/StringUtil.java @@ -390,4 +390,69 @@ private static boolean containsText(CharSequence str) { } return false; } + + public static String join(final char delimiter, final String... strings) { + if (strings.length == 0) { + return null; + } + if (strings.length == 1) { + return strings[0]; + } + int length = strings.length - 1; + for (final String s : strings) { + if (s == null) { + continue; + } + length += s.length(); + } + final StringBuilder sb = new StringBuilder(length); + if (strings[0] != null) { + sb.append(strings[0]); + } + for (int i = 1; i < strings.length; ++i) { + if (!isEmpty(strings[i])) { + sb.append(delimiter).append(strings[i]); + } else { + sb.append(delimiter); + } + } + return sb.toString(); + } + + public static boolean substringMatch(CharSequence str, int index, CharSequence substring) { + if (index + substring.length() > str.length()) { + return false; + } + for (int i = 0; i < substring.length(); i++) { + if (str.charAt(index + i) != substring.charAt(i)) { + return false; + } + } + return true; + } + + public static String cut(String str, int threshold) { + if (isEmpty(str) || str.length() <= threshold) { + return str; + } + return str.substring(0, threshold); + } + + public static String trim(final String str, final char ch) { + if (isEmpty(str)) { + return null; + } + + final char[] chars = str.toCharArray(); + + int i = 0, j = chars.length - 1; + // noinspection StatementWithEmptyBody + for (; i < chars.length && chars[i] == ch; i++) { + } + // noinspection StatementWithEmptyBody + for (; j > 0 && chars[j] == ch; j--) { + } + + return new String(chars, i, j - i + 1); + } } diff --git a/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/ConfigInitializer.java b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/ConfigInitializer.java new file mode 100644 index 0000000000..d1b250e9e9 --- /dev/null +++ b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/ConfigInitializer.java @@ -0,0 +1,210 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.toolkit.agent; + +import cn.hippo4j.common.toolkit.StringUtil; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.*; + +/** + * Init a class's static fields by a {@link Properties}, including static fields and static inner classes. + *

+ */ +public class ConfigInitializer { + + public static void initialize(Properties properties, Class rootConfigType) throws IllegalAccessException { + initNextLevel(properties, rootConfigType, new ConfigDesc(), false); + } + + public static void initialize(Properties properties, Class rootConfigType, boolean isSpringProperties) throws IllegalAccessException { + initNextLevel(properties, rootConfigType, new ConfigDesc(), isSpringProperties); + } + + private static void initNextLevel(Properties properties, Class recentConfigType, + ConfigDesc parentDesc, boolean isSpringProperties) throws IllegalArgumentException, IllegalAccessException { + for (Field field : recentConfigType.getFields()) { + if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) { + String configKey = (parentDesc + "." + (isSpringProperties ? field.getName().replace("_", "-") : field.getName())).toLowerCase(); + Class type = field.getType(); + + if (type.equals(Map.class)) { + /* + * Map config format is, config_key[map_key]=map_value Such as plugin.opgroup.resttemplate.rule[abc]=/url/path + */ + // Deduct two generic types of the map + ParameterizedType genericType = (ParameterizedType) field.getGenericType(); + Type[] argumentTypes = genericType.getActualTypeArguments(); + + Type keyType = null; + Type valueType = null; + if (argumentTypes != null && argumentTypes.length == 2) { + // Get key type and value type of the map + keyType = argumentTypes[0]; + valueType = argumentTypes[1]; + } + Map map = (Map) field.get(null); + // Set the map from config key and properties + setForMapType(configKey, map, properties, keyType, valueType); + } else { + /* + * Others typical field type + */ + String value = properties.getProperty(configKey); + // Convert the value into real type + final Length lengthDefine = field.getAnnotation(Length.class); + if (lengthDefine != null) { + if (value != null && value.length() > lengthDefine.value()) { + value = value.substring(0, lengthDefine.value()); + } + } + Object convertedValue = convertToTypicalType(type, value); + if (convertedValue != null) { + field.set(null, convertedValue); + } + } + } + } + for (Class innerConfiguration : recentConfigType.getClasses()) { + String simpleName = innerConfiguration.getSimpleName(); + String description = isSpringProperties ? simpleName.replace("_", "-") : simpleName; + parentDesc.append(description); + initNextLevel(properties, innerConfiguration, parentDesc, isSpringProperties); + parentDesc.removeLastDesc(); + } + } + + /** + * Convert string value to typical type. + * + * @param type type to convert + * @param value string value to be converted + * @return converted value or null + */ + private static Object convertToTypicalType(Type type, String value) { + if (value == null || type == null) { + return null; + } + + Object result = null; + if (String.class.equals(type)) { + result = value; + } else if (int.class.equals(type) || Integer.class.equals(type)) { + result = Integer.valueOf(value); + } else if (long.class.equals(type) || Long.class.equals(type)) { + result = Long.valueOf(value); + } else if (boolean.class.equals(type) || Boolean.class.equals(type)) { + result = Boolean.valueOf(value); + } else if (float.class.equals(type) || Float.class.equals(type)) { + result = Float.valueOf(value); + } else if (double.class.equals(type) || Double.class.equals(type)) { + result = Double.valueOf(value); + } else if (List.class.equals(type)) { + result = convert2List(value); + } else if (type instanceof Class) { + Class clazz = (Class) type; + if (clazz.isEnum()) { + result = Enum.valueOf((Class) type, value.toUpperCase()); + } + } + return result; + } + + /** + * Set map items. + * + * @param configKey config key must not be null + * @param map map to set must not be null + * @param properties properties must not be null + * @param keyType key type of the map + * @param valueType value type of the map + */ + private static void setForMapType(String configKey, Map map, Properties properties, + final Type keyType, final Type valueType) { + + Objects.requireNonNull(configKey); + Objects.requireNonNull(map); + Objects.requireNonNull(properties); + + String prefix = configKey + "["; + String suffix = "]"; + + properties.forEach((propertyKey, propertyValue) -> { + String propertyStringKey = propertyKey.toString(); + if (propertyStringKey.startsWith(prefix) && propertyStringKey.endsWith(suffix)) { + String itemKey = propertyStringKey.substring( + prefix.length(), propertyStringKey.length() - suffix.length()); + Object keyObj; + Object valueObj; + + keyObj = convertToTypicalType(keyType, itemKey); + valueObj = convertToTypicalType(valueType, propertyValue.toString()); + + if (keyObj == null) { + keyObj = itemKey; + } + + if (valueObj == null) { + valueObj = propertyValue; + } + + map.put(keyObj, valueObj); + } + }); + } + + private static List convert2List(String value) { + if (StringUtil.isEmpty(value)) { + return Collections.emptyList(); + } + List result = new LinkedList<>(); + + String[] segments = value.split(","); + for (String segment : segments) { + String trimmedSegment = segment.trim(); + if (StringUtil.isNotEmpty(trimmedSegment)) { + result.add(trimmedSegment); + } + } + return result; + } + +} + +class ConfigDesc { + + private LinkedList descs = new LinkedList<>(); + + void append(String currentDesc) { + if (StringUtil.isNotEmpty(currentDesc)) { + descs.addLast(currentDesc); + } + } + + void removeLastDesc() { + descs.removeLast(); + } + + @Override + public String toString() { + return String.join(".", descs); + } +} diff --git a/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/Length.java b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/Length.java new file mode 100644 index 0000000000..f926a9da7f --- /dev/null +++ b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/Length.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.toolkit.agent; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The length rule of the target field. + */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Length { + + int value(); +} diff --git a/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PlaceholderConfigurerSupport.java b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PlaceholderConfigurerSupport.java new file mode 100644 index 0000000000..df6d59ec9a --- /dev/null +++ b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PlaceholderConfigurerSupport.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.toolkit.agent; + +public class PlaceholderConfigurerSupport { + + /** + * Default placeholder prefix: {@value} + */ + public static final String DEFAULT_PLACEHOLDER_PREFIX = "${"; + + /** + * Default placeholder suffix: {@value} + */ + public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}"; + + /** + * Default value separator: {@value} + */ + public static final String DEFAULT_VALUE_SEPARATOR = ":"; + +} diff --git a/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PropertyPlaceholderHelper.java b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PropertyPlaceholderHelper.java new file mode 100644 index 0000000000..1add1039b9 --- /dev/null +++ b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PropertyPlaceholderHelper.java @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.toolkit.agent; + +import cn.hippo4j.common.toolkit.StringUtil; + +import java.util.*; + +/** + * Utility class for working with Strings that have placeholder values in them. A placeholder takes the form {@code + * ${name}}. Using {@code PropertyPlaceholderHelper} these placeholders can be substituted for user-supplied values.

+ * Values for substitution can be supplied using a {@link Properties} instance or using a {@link PlaceholderResolver}. + */ +public enum PropertyPlaceholderHelper { + + INSTANCE( + PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX, + PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX, PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, + true); + + private final String placeholderPrefix; + + private final String placeholderSuffix; + + private final String simplePrefix; + + private final String valueSeparator; + + private final boolean ignoreUnresolvablePlaceholders; + + /** + * Creates a new {@code PropertyPlaceholderHelper} that uses the supplied prefix and suffix. + * + * @param placeholderPrefix the prefix that denotes the start of a placeholder + * @param placeholderSuffix the suffix that denotes the end of a placeholder + * @param valueSeparator the separating character between the placeholder variable and the + * associated default value, if any + * @param ignoreUnresolvablePlaceholders indicates whether unresolvable placeholders should be ignored ({@code + * true}) or cause an exception ({@code false}) + */ + PropertyPlaceholderHelper(String placeholderPrefix, String placeholderSuffix, String valueSeparator, + boolean ignoreUnresolvablePlaceholders) { + if (StringUtil.isEmpty(placeholderPrefix) || StringUtil.isEmpty(placeholderSuffix)) { + throw new UnsupportedOperationException("'placeholderPrefix or placeholderSuffix' must not be null"); + } + + final Map wellKnownSimplePrefixes = new HashMap(4); + + wellKnownSimplePrefixes.put("}", "{"); + wellKnownSimplePrefixes.put("]", "["); + wellKnownSimplePrefixes.put(")", "("); + + this.placeholderPrefix = placeholderPrefix; + this.placeholderSuffix = placeholderSuffix; + String simplePrefixForSuffix = wellKnownSimplePrefixes.get(this.placeholderSuffix); + if (simplePrefixForSuffix != null && this.placeholderPrefix.endsWith(simplePrefixForSuffix)) { + this.simplePrefix = simplePrefixForSuffix; + } else { + this.simplePrefix = this.placeholderPrefix; + } + this.valueSeparator = valueSeparator; + this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders; + } + + /** + * Replaces all placeholders of format {@code ${name}} with the corresponding property from the supplied {@link + * Properties}. + * + * @param value the value containing the placeholders to be replaced + * @param properties the {@code Properties} to use for replacement + * @return the supplied value with placeholders replaced inline + */ + public String replacePlaceholders(String value, final Properties properties) { + return replacePlaceholders(value, new PlaceholderResolver() { + + @Override + public String resolvePlaceholder(String placeholderName) { + return getConfigValue(placeholderName, properties); + } + }); + } + + private String getConfigValue(String key, final Properties properties) { + String value = System.getProperty(key); + if (value == null) { + value = System.getenv(key); + } + if (value == null) { + value = properties.getProperty(key); + } + return value; + } + + /** + * Replaces all placeholders of format {@code ${name}} with the value returned from the supplied {@link + * PlaceholderResolver}. + * + * @param value the value containing the placeholders to be replaced + * @param placeholderResolver the {@code PlaceholderResolver} to use for replacement + * @return the supplied value with placeholders replaced inline + */ + public String replacePlaceholders(String value, PlaceholderResolver placeholderResolver) { + return parseStringValue(value, placeholderResolver, new HashSet()); + } + + protected String parseStringValue(String value, PlaceholderResolver placeholderResolver, + Set visitedPlaceholders) { + + StringBuilder result = new StringBuilder(value); + + int startIndex = value.indexOf(this.placeholderPrefix); + while (startIndex != -1) { + int endIndex = findPlaceholderEndIndex(result, startIndex); + if (endIndex != -1) { + String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex); + String originalPlaceholder = placeholder; + if (!visitedPlaceholders.add(originalPlaceholder)) { + throw new IllegalArgumentException( + "Circular placeholder reference '" + originalPlaceholder + "' in property definitions"); + } + // Recursive invocation, parsing placeholders contained in the placeholder key. + placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders); + // Now obtain the value for the fully resolved key... + String propVal = placeholderResolver.resolvePlaceholder(placeholder); + if (propVal == null && this.valueSeparator != null) { + int separatorIndex = placeholder.indexOf(this.valueSeparator); + if (separatorIndex != -1) { + String actualPlaceholder = placeholder.substring(0, separatorIndex); + String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length()); + propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder); + if (propVal == null) { + propVal = defaultValue; + } + } + } + if (propVal != null) { + // Recursive invocation, parsing placeholders contained in the + // previously resolved placeholder value. + propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders); + result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal); + startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length()); + } else if (this.ignoreUnresolvablePlaceholders) { + // Proceed with unprocessed value. + startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length()); + } else { + throw new IllegalArgumentException( + "Could not resolve placeholder '" + placeholder + "'" + " in value \"" + value + "\""); + } + visitedPlaceholders.remove(originalPlaceholder); + } else { + startIndex = -1; + } + } + return result.toString(); + } + + private int findPlaceholderEndIndex(CharSequence buf, int startIndex) { + int index = startIndex + this.placeholderPrefix.length(); + int withinNestedPlaceholder = 0; + while (index < buf.length()) { + if (StringUtil.substringMatch(buf, index, this.placeholderSuffix)) { + if (withinNestedPlaceholder > 0) { + withinNestedPlaceholder--; + index = index + this.placeholderSuffix.length(); + } else { + return index; + } + } else if (StringUtil.substringMatch(buf, index, this.simplePrefix)) { + withinNestedPlaceholder++; + index = index + this.simplePrefix.length(); + } else { + index++; + } + } + return -1; + } + + /** + * Strategy interface used to resolve replacement values for placeholders contained in Strings. + */ + public interface PlaceholderResolver { + + /** + * Resolve the supplied placeholder name to the replacement value. + * + * @param placeholderName the name of the placeholder to resolve + * @return the replacement value, or {@code null} if no replacement is to be made + */ + String resolvePlaceholder(String placeholderName); + } +} diff --git a/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/RunnableWithExceptionProtection.java b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/RunnableWithExceptionProtection.java new file mode 100644 index 0000000000..f4179cf1a7 --- /dev/null +++ b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/RunnableWithExceptionProtection.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.toolkit.agent; + +public class RunnableWithExceptionProtection implements Runnable { + + private Runnable run; + private CallbackWhenException callback; + + public RunnableWithExceptionProtection(Runnable run, CallbackWhenException callback) { + this.run = run; + this.callback = callback; + } + + @Override + public void run() { + try { + run.run(); + } catch (Throwable t) { + callback.handle(t); + } + } + + public interface CallbackWhenException { + + void handle(Throwable t); + } +} diff --git a/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/refresher/event/DynamicThreadPoolRefreshListener.java b/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/refresher/event/DynamicThreadPoolRefreshListener.java index 7a49151ddc..252e6d0b61 100644 --- a/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/refresher/event/DynamicThreadPoolRefreshListener.java +++ b/kernel/dynamic/mode/config/src/main/java/cn/hippo4j/threadpool/dynamic/mode/config/refresher/event/DynamicThreadPoolRefreshListener.java @@ -24,12 +24,12 @@ import cn.hippo4j.common.executor.support.ResizableCapacityLinkedBlockingQueue; import cn.hippo4j.common.extension.design.Observer; import cn.hippo4j.common.extension.design.ObserverMessage; +import cn.hippo4j.common.logging.api.ILog; +import cn.hippo4j.common.logging.api.LogManager; import cn.hippo4j.common.model.executor.ExecutorProperties; import cn.hippo4j.common.toolkit.ThreadPoolExecutorUtil; import cn.hippo4j.threadpool.dynamic.mode.config.properties.BootstrapConfigProperties; import lombok.RequiredArgsConstructor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.List; import java.util.Objects; @@ -46,7 +46,7 @@ @RequiredArgsConstructor public class DynamicThreadPoolRefreshListener implements Observer { - private static final Logger LOGGER = LoggerFactory.getLogger(DynamicThreadPoolRefreshListener.class); + private static final ILog LOG = LogManager.getLogger(DynamicThreadPoolRefreshListener.class); @Override public void accept(ObserverMessage observerMessage) { @@ -127,15 +127,14 @@ private void dynamicRefreshThreadPool(ThreadPoolExecutorHolder executorHolder, E ResizableCapacityLinkedBlockingQueue queue = (ResizableCapacityLinkedBlockingQueue) executor.getQueue(); queue.setCapacity(properties.getQueueCapacity()); } else { - LOGGER.warn("The queue length cannot be modified. Queue type mismatch. Current queue type: {}", executor.getQueue().getClass().getSimpleName()); + LOG.warn("The queue length cannot be modified. Queue type mismatch. Current queue type: {}", executor.getQueue().getClass().getSimpleName()); } } } private void sendChangeNotificationMessage(ThreadPoolExecutorHolder executorHolder, ExecutorProperties properties) { ExecutorProperties executorProperties = executorHolder.getExecutorProperties(); - // TODO log cannot be printed - LOGGER.info(CHANGE_THREAD_POOL_TEXT, + LOG.info(CHANGE_THREAD_POOL_TEXT, executorHolder.getThreadPoolId(), String.format(CHANGE_DELIMITER, executorProperties.getCorePoolSize(), properties.getCorePoolSize()), String.format(CHANGE_DELIMITER, executorProperties.getMaximumPoolSize(), properties.getMaximumPoolSize()), diff --git a/pom.xml b/pom.xml index d63fbf73ce..6237e82f19 100644 --- a/pom.xml +++ b/pom.xml @@ -78,6 +78,7 @@ 2.22.1 3.1.0 2.3.2.RELEASE + 2.8.9 From 59e89ef2d686bc80f181dbd97273e18af6eee1d7 Mon Sep 17 00:00:00 2001 From: Pan-YuJie <646836760@qq.com> Date: Mon, 13 Nov 2023 13:47:02 +0800 Subject: [PATCH 5/6] Remove import * --- .../cn/hippo4j/agent/bootstrap/Hippo4jAgent.java | 11 +++++++++-- .../agent/core/jvm/LoadedLibraryCollector.java | 8 +++++++- .../bytebuddy/CacheableTransformerDecorator.java | 6 +++++- .../enhance/ClassEnhancePluginDefine.java | 6 +++++- .../cn/hippo4j/common/logging/core/FileWriter.java | 6 +++++- .../toolkit/agent/PropertyPlaceholderHelper.java | 6 +++++- .../hippo4j/common/toolkit/MessageConvertTest.java | 12 ++++++++++-- 7 files changed, 46 insertions(+), 9 deletions(-) diff --git a/agent/hippo4j-agent-bootstrap/src/main/java/cn/hippo4j/agent/bootstrap/Hippo4jAgent.java b/agent/hippo4j-agent-bootstrap/src/main/java/cn/hippo4j/agent/bootstrap/Hippo4jAgent.java index a4de6591a8..4da18b4a94 100644 --- a/agent/hippo4j-agent-bootstrap/src/main/java/cn/hippo4j/agent/bootstrap/Hippo4jAgent.java +++ b/agent/hippo4j-agent-bootstrap/src/main/java/cn/hippo4j/agent/bootstrap/Hippo4jAgent.java @@ -20,7 +20,12 @@ import cn.hippo4j.agent.core.boot.ServiceManager; import cn.hippo4j.agent.core.conf.SnifferConfigInitializer; import cn.hippo4j.agent.core.jvm.LoadedLibraryCollector; -import cn.hippo4j.agent.core.plugin.*; +import cn.hippo4j.agent.core.plugin.AbstractClassEnhancePluginDefine; +import cn.hippo4j.agent.core.plugin.EnhanceContext; +import cn.hippo4j.agent.core.plugin.InstrumentDebuggingClass; +import cn.hippo4j.agent.core.plugin.PluginBootstrap; +import cn.hippo4j.agent.core.plugin.PluginException; +import cn.hippo4j.agent.core.plugin.PluginFinder; import cn.hippo4j.agent.core.plugin.bootstrap.BootstrapInstrumentBoost; import cn.hippo4j.agent.core.plugin.bytebuddy.CacheableTransformerDecorator; import cn.hippo4j.agent.core.plugin.jdk9module.JDK9ModuleExporter; @@ -44,7 +49,9 @@ import java.util.List; import java.util.Map; -import static net.bytebuddy.matcher.ElementMatchers.*; +import static net.bytebuddy.matcher.ElementMatchers.nameContains; +import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; +import static net.bytebuddy.matcher.ElementMatchers.not; /** * Hippo4j Agent diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/jvm/LoadedLibraryCollector.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/jvm/LoadedLibraryCollector.java index 6bae20aa4e..aebec1f038 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/jvm/LoadedLibraryCollector.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/jvm/LoadedLibraryCollector.java @@ -28,7 +28,13 @@ import java.net.URL; import java.net.URLClassLoader; import java.text.SimpleDateFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Set; public class LoadedLibraryCollector { diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy/CacheableTransformerDecorator.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy/CacheableTransformerDecorator.java index f0ed68c6b7..fd653f6091 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy/CacheableTransformerDecorator.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/bytebuddy/CacheableTransformerDecorator.java @@ -28,7 +28,11 @@ import net.bytebuddy.agent.builder.ResettableClassFileTransformer; import net.bytebuddy.utility.RandomString; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; import java.lang.instrument.IllegalClassFormatException; import java.security.ProtectionDomain; import java.util.Map; diff --git a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java index 8378aa35b1..5790f80ce2 100644 --- a/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java +++ b/agent/hippo4j-agent-core/src/main/java/cn/hippo4j/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java @@ -21,7 +21,11 @@ import cn.hippo4j.agent.core.plugin.EnhanceContext; import cn.hippo4j.agent.core.plugin.PluginException; import cn.hippo4j.agent.core.plugin.bootstrap.BootstrapInstrumentBoost; -import cn.hippo4j.agent.core.plugin.interceptor.*; +import cn.hippo4j.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import cn.hippo4j.agent.core.plugin.interceptor.DeclaredInstanceMethodsInterceptPoint; +import cn.hippo4j.agent.core.plugin.interceptor.EnhanceException; +import cn.hippo4j.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import cn.hippo4j.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; import cn.hippo4j.agent.core.plugin.interceptor.v2.InstanceMethodsInterceptV2Point; import cn.hippo4j.agent.core.plugin.interceptor.v2.StaticMethodsInterceptV2Point; import cn.hippo4j.agent.core.util.StringUtil; diff --git a/infra/common/src/main/java/cn/hippo4j/common/logging/core/FileWriter.java b/infra/common/src/main/java/cn/hippo4j/common/logging/core/FileWriter.java index 6a26349526..72397d7170 100644 --- a/infra/common/src/main/java/cn/hippo4j/common/logging/core/FileWriter.java +++ b/infra/common/src/main/java/cn/hippo4j/common/logging/core/FileWriter.java @@ -22,7 +22,11 @@ import cn.hippo4j.common.constant.Constants; import cn.hippo4j.common.toolkit.agent.RunnableWithExceptionProtection; -import java.io.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FilenameFilter; +import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; diff --git a/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PropertyPlaceholderHelper.java b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PropertyPlaceholderHelper.java index 1add1039b9..8d945584e0 100644 --- a/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PropertyPlaceholderHelper.java +++ b/infra/common/src/main/java/cn/hippo4j/common/toolkit/agent/PropertyPlaceholderHelper.java @@ -19,7 +19,11 @@ import cn.hippo4j.common.toolkit.StringUtil; -import java.util.*; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Properties; +import java.util.Set; /** * Utility class for working with Strings that have placeholder values in them. A placeholder takes the form {@code diff --git a/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java b/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java index 52470aadb7..4d834dcf1f 100644 --- a/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java +++ b/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java @@ -18,13 +18,21 @@ package cn.hippo4j.common.toolkit; import cn.hippo4j.common.model.ThreadPoolRunStateInfo; -import cn.hippo4j.common.monitor.*; +import cn.hippo4j.common.monitor.AbstractMessage; +import cn.hippo4j.common.monitor.Message; +import cn.hippo4j.common.monitor.MessageTypeEnum; +import cn.hippo4j.common.monitor.MessageWrapper; +import cn.hippo4j.common.monitor.RuntimeMessage; import org.junit.Test; import org.junit.jupiter.api.Assertions; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /*** * @description : Todo From eaf5f7d2e12a11989acb499fee5026fa6f3cfa0b Mon Sep 17 00:00:00 2001 From: Pan-YuJie <646836760@qq.com> Date: Fri, 9 Aug 2024 22:53:38 +0800 Subject: [PATCH 6/6] fix: The Gson version dependency in pom under the infra package is provided --- infra/common/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/infra/common/pom.xml b/infra/common/pom.xml index ae48d1afa4..607b5cafa0 100644 --- a/infra/common/pom.xml +++ b/infra/common/pom.xml @@ -14,6 +14,7 @@ com.google.code.gson gson ${gson.version} + provided com.fasterxml.jackson.core