From cdd884de6d2d103b33b525414fce50d9b261d189 Mon Sep 17 00:00:00 2001 From: lasanthaS Date: Wed, 18 Aug 2021 01:47:46 +0530 Subject: [PATCH 01/11] Add correlation logging component --- .../pom.xml | 106 ++++++++++++++++++ .../correlation/CorrelationLogService.java | 27 +++++ .../logging/correlation/Notifiable.java | 24 ++++ .../internal/CorrelationLogManager.java | 93 +++++++++++++++ .../correlation/mgt/CorrelationLogConfig.java | 67 +++++++++++ .../mgt/CorrelationLogConfigMBean.java | 34 ++++++ .../utils/CorrelationLogConstants.java | 27 +++++ .../correlation/utils/CorrelationLogUtil.java | 33 ++++++ core/pom.xml | 1 + .../pom.xml | 2 + parent/pom.xml | 5 + 11 files changed, 419 insertions(+) create mode 100644 core/org.wso2.carbon.logging.correlation/pom.xml create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/Notifiable.java create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java diff --git a/core/org.wso2.carbon.logging.correlation/pom.xml b/core/org.wso2.carbon.logging.correlation/pom.xml new file mode 100644 index 00000000000..e6c64e71a52 --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/pom.xml @@ -0,0 +1,106 @@ + + + + + + + org.wso2.carbon + carbon-kernel + 4.6.2 + ../pom.xml + + + 4.0.0 + org.wso2.carbon.logging.correlation + bundle + WSO2 Carbon - Correlation Logging + Correlation logging component for Carbon + http://wso2.org + + + + org.eclipse.platform + org.eclipse.osgi + + + org.osgi + org.osgi.service.component.annotations + + + org.osgi + org.osgi.annotation + + + org.osgi + org.osgi.service.metatype.annotations + + + org.osgi + org.osgi.compendium + provided + + + org.wso2.carbon + org.wso2.carbon.utils + + + org.apache.geronimo.specs + geronimo-stax-api_1.0_spec + + + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.artifactId} + ${project.artifactId} + + + + org.wso2.carbon.logging.correlation.*, + + + *;resolution:=optional, + + + + + + + + + + + + + + * + + + + + + + + diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java new file mode 100644 index 00000000000..b8190665fdc --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation; + +import java.util.Map; + +public interface CorrelationLogService { + String getName(); + + void reconfigure(Map properties); +} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/Notifiable.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/Notifiable.java new file mode 100644 index 00000000000..572ad821ad3 --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/Notifiable.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation; + +public interface Notifiable { + + void notify(String key, Object value); +} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java new file mode 100644 index 00000000000..6e00d7ad55f --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.internal; + +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; +import org.osgi.service.component.annotations.ReferencePolicy; +import org.wso2.carbon.logging.correlation.mgt.CorrelationLogConfig; +import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; +import org.wso2.carbon.logging.correlation.CorrelationLogService; +import org.wso2.carbon.logging.correlation.Notifiable; +import org.wso2.carbon.utils.MBeanRegistrar; + +import java.util.HashMap; +import java.util.Map; + +@Component(immediate = true) +public class CorrelationLogManager implements Notifiable { + + Map serviceMap; + Map logProperties; + org.wso2.carbon.logging.correlation.mgt.CorrelationLogConfig logConfig; + + @Activate + protected void activate(ComponentContext context) { + serviceMap = new HashMap<>(); + logProperties = initProperties(); + + logConfig = new CorrelationLogConfig(); + logConfig.registerNotifier(this); + MBeanRegistrar.registerMBean(logConfig); + } + + @Deactivate + protected void deactivate(ComponentContext context) { + serviceMap = null; + } + + @Reference( + policy = ReferencePolicy.DYNAMIC, + cardinality = ReferenceCardinality.MULTIPLE, + unbind = "unsetCorrelationLogService" + ) + protected void setCorrelationLogService(CorrelationLogService service) { + serviceMap.put(service.getName(), service); + service.reconfigure(logProperties); + } + + protected void unsetCorrelationLogService(CorrelationLogService service) { + serviceMap.remove(service.getName()); + } + + @Override + public void notify(String key, Object value) { + logProperties.put(key, value); + reconfigure(); + } + + private void reconfigure() { + for (Map.Entry entry : serviceMap.entrySet()) { + entry.getValue().reconfigure(logProperties); + } + } + + private Map initProperties() { + // TODO: Read default properties from the deployment.toml file + Map properties = new HashMap<>(); + properties.put(CorrelationLogConstants.ENABLE, false); + properties.put(CorrelationLogConstants.COMPONENTS, ""); + properties.put(CorrelationLogConstants.BLACKLISTED_THREADS, ""); + return properties; + } +} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java new file mode 100644 index 00000000000..2de2768176a --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.mgt; + + +import org.wso2.carbon.logging.correlation.Notifiable; +import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; + +public class CorrelationLogConfig implements CorrelationLogConfigMBean { + private boolean enable; + private String components; + private String blacklistedThreads; + private Notifiable notifiable; + + @Override + public boolean isEnable() { + return enable; + } + + @Override + public void setEnable(boolean enable) { + this.enable = enable; + this.notifiable.notify(CorrelationLogConstants.ENABLE, this.enable); + } + + @Override + public String getComponents() { + return components; + } + + @Override + public void setComponents(String components) { + this.components = components; + this.notifiable.notify(CorrelationLogConstants.COMPONENTS, this.components); + } + + @Override + public String getBlacklistedThreads() { + return blacklistedThreads; + } + + @Override + public void setBlacklistedThreads(String blacklistedThreads) { + this.blacklistedThreads = blacklistedThreads; + this.notifiable.notify(CorrelationLogConstants.BLACKLISTED_THREADS, this.blacklistedThreads); + } + + public void registerNotifier(Notifiable notifiable) { + this.notifiable = notifiable; + } +} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java new file mode 100644 index 00000000000..62b87121a33 --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.mgt; + +public interface CorrelationLogConfigMBean { + + boolean isEnable(); + + void setEnable(boolean enable); + + String getComponents(); + + void setComponents(String components); + + String getBlacklistedThreads(); + + void setBlacklistedThreads(String blacklistedThreads); +} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java new file mode 100644 index 00000000000..fdfd5ff9a0e --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.utils; + +public class CorrelationLogConstants { + + public static final String ENABLE = "ENABLE"; + public static final String COMPONENTS = "COMPONENTS"; + public static final String BLACKLISTED_THREADS = "BLACKLISTED_THREADS"; + +} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java new file mode 100644 index 00000000000..ec53b3ea26f --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.utils; + +import java.util.Arrays; + +public class CorrelationLogUtil { + + public static String[] toComponentArray(String componentList) { + return componentList.split("\\s*,\\s*"); + } + + public static boolean isComponentWhitelisted(String component, String componentList) { + return componentList.equals("") || + Arrays.stream(toComponentArray(componentList)).anyMatch(s -> s.equals(component)); + } +} diff --git a/core/pom.xml b/core/pom.xml index 058f24b40e3..60dc5058964 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -149,5 +149,6 @@ org.wso2.carbon.ndatasource.rdbms org.wso2.carbon.framework.exporter org.wso2.carbon.pax-logging-log4j2-plugins + org.wso2.carbon.logging.correlation diff --git a/features/org.wso2.carbon.core.common.feature/pom.xml b/features/org.wso2.carbon.core.common.feature/pom.xml index 630f04e5605..e0766dac01d 100644 --- a/features/org.wso2.carbon.core.common.feature/pom.xml +++ b/features/org.wso2.carbon.core.common.feature/pom.xml @@ -158,6 +158,8 @@ org.wso2.carbon:org.wso2.carbon.ndatasource.rdbms:${carbon.kernel.version} + org.wso2.carbon:org.wso2.carbon.logging.correlation:${carbon.kernel.version} + org.wso2.carbon:org.wso2.carbon.user.api:${carbon.kernel.version} org.wso2.carbon:org.wso2.carbon.framework.exporter:${carbon.kernel.version} diff --git a/parent/pom.xml b/parent/pom.xml index 8364d84fc0e..c92e201dcff 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -1637,6 +1637,11 @@ org.wso2.carbon.osgi.security ${carbon.kernel.version} + + org.wso2.carbon + org.wso2.carbon.logging.correlation + ${carbon.kernel.version} + org.wso2.carbon SecVerifier From 6b41b49c547728fdb5beb1d4d29206e44ced2427 Mon Sep 17 00:00:00 2001 From: lasanthaS Date: Wed, 18 Aug 2021 01:50:41 +0530 Subject: [PATCH 02/11] Modify JDBC interceptor for correlation logs --- .../org.wso2.carbon.ndatasource.rdbms/pom.xml | 4 ++ .../rdbms/CorrelationLogInterceptor.java | 66 +++++++++++++------ 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/core/org.wso2.carbon.ndatasource.rdbms/pom.xml b/core/org.wso2.carbon.ndatasource.rdbms/pom.xml index 4918c799b07..81acdb682b0 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/pom.xml +++ b/core/org.wso2.carbon.ndatasource.rdbms/pom.xml @@ -65,6 +65,10 @@ istack-commons-runtime test + + org.wso2.carbon + org.wso2.carbon.logging.correlation + diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java index 56d115aceb5..7a099d661e6 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java @@ -21,6 +21,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.wso2.carbon.logging.correlation.CorrelationLogService; +import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; +import org.wso2.carbon.logging.correlation.utils.CorrelationLogUtil; import org.wso2.carbon.utils.xml.StringUtils; import java.lang.reflect.Constructor; @@ -32,39 +39,26 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; /** * Time-Logging interceptor for JDBC pool. * Logs the time taken to execute the query in each pool-ed connection. */ -public class CorrelationLogInterceptor extends AbstractQueryReport { +@Component( + immediate = true, + service = CorrelationLogService.class +) +public class CorrelationLogInterceptor extends AbstractQueryReport implements CorrelationLogService { private static final Log correlationLog = LogFactory.getLog("correlation"); private static final Log log = LogFactory.getLog(CorrelationLogInterceptor.class); - private static final String CORRELATION_LOG_CALL_TYPE_VALUE = "jdbc"; private static final String CORRELATION_LOG_SEPARATOR = "|"; - private static final String CORRELATION_LOG_SYSTEM_PROPERTY = "enableCorrelationLogs"; - private static final String BLACKLISTED_THREADS_SYSTEM_PROPERTY = - "org.wso2.CorrelationLogInterceptor.BlacklistedThreads"; private static final String[] DEFAULT_BLACKLISTED_THREADS = {"MessageDeliveryTaskThreadPool", "HumanTaskServer" , "BPELServer", "CarbonDeploymentSchedulerThread"}; - private List blacklistedThreadList = new ArrayList<>(); - private boolean isEnableCorrelationLogs; - - public CorrelationLogInterceptor() { - String blacklistedThreadNames = System.getProperty(BLACKLISTED_THREADS_SYSTEM_PROPERTY); - - if (blacklistedThreadNames == null) { - blacklistedThreadList.addAll(Arrays.asList(DEFAULT_BLACKLISTED_THREADS)); - } - - if (!StringUtils.isEmpty(blacklistedThreadNames)) { - blacklistedThreadList.addAll(Arrays.asList(StringUtils.split(blacklistedThreadNames, ','))); - } - - isEnableCorrelationLogs = Boolean.parseBoolean(System.getProperty(CORRELATION_LOG_SYSTEM_PROPERTY)); - } + private static List blacklistedThreadList = new ArrayList<>(); + private static boolean isEnableCorrelationLogs; @Override public void closeInvoked() { @@ -118,7 +112,37 @@ private Object invokeProxy(Method method, Object[] args, Object statement, long } } + @Activate + protected void activate(ComponentContext context) { + } + + @Deactivate + protected void deactivate(ComponentContext context) { + } + + @Override + public String getName() { + return "jdbc"; + } + + @Override + public void reconfigure(Map properties) { + String blacklistedThreadNames = (String) properties.get(CorrelationLogConstants.BLACKLISTED_THREADS); + if (blacklistedThreadNames == null) { + blacklistedThreadList.addAll(Arrays.asList(DEFAULT_BLACKLISTED_THREADS)); + } + + if (!StringUtils.isEmpty(blacklistedThreadNames)) { + blacklistedThreadList.addAll(Arrays.asList(StringUtils.split(blacklistedThreadNames, ','))); + } + + isEnableCorrelationLogs = (Boolean) properties.get(CorrelationLogConstants.ENABLE) && + CorrelationLogUtil.isComponentWhitelisted(this.getName(), + (String) properties.get(CorrelationLogConstants.COMPONENTS)); + } + /** + * * Proxy Class that is used to calculate and log the time taken for queries */ protected class StatementProxy implements InvocationHandler { From d9314ac86b0118849f0c3a703af91de5080cba5b Mon Sep 17 00:00:00 2001 From: lasanthaS Date: Wed, 18 Aug 2021 16:54:58 +0530 Subject: [PATCH 03/11] Modify Tomcat valve for correlation log improvement --- core/org.wso2.carbon.tomcat.ext/pom.xml | 4 ++ .../ext/valves/RequestCorrelationIdValve.java | 38 ++++++++++++++++--- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/core/org.wso2.carbon.tomcat.ext/pom.xml b/core/org.wso2.carbon.tomcat.ext/pom.xml index d940649c5ff..680ed52e7ef 100644 --- a/core/org.wso2.carbon.tomcat.ext/pom.xml +++ b/core/org.wso2.carbon.tomcat.ext/pom.xml @@ -118,6 +118,10 @@ org.osgi.compendium provided + + org.wso2.carbon + org.wso2.carbon.logging.correlation + diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java index 16af67c2565..31eb91f4334 100644 --- a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java @@ -28,6 +28,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.log4j.MDC; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.wso2.carbon.logging.correlation.CorrelationLogService; +import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; +import org.wso2.carbon.logging.correlation.utils.CorrelationLogUtil; import java.io.IOException; import java.net.URLDecoder; @@ -53,7 +60,11 @@ * queryToCorrelationIdMapping={'RelayState':'Correlation-ID'} * */ -public class RequestCorrelationIdValve extends ValveBase { +@Component( + immediate = true, + service = CorrelationLogService.class +) +public class RequestCorrelationIdValve extends ValveBase implements CorrelationLogService { private static final Log correlationLog = LogFactory.getLog("correlation"); private static final String CORRELATION_ID_MDC = "Correlation-ID"; @@ -69,10 +80,9 @@ public class RequestCorrelationIdValve extends ValveBase { private static final String CORRELATION_LOG_REQUEST_START = "HTTP-In-Request"; private static final String CORRELATION_LOG_SEPARATOR = "|"; private static final String CORRELATION_LOG_REQUEST_END = "HTTP-In-Response"; - private static final String CORRELATION_LOG_SYSTEM_PROPERTY = "enableCorrelationLogs"; private static final String PADDING_CHAR = "="; private static final String SPLITTING_CHAR = "&"; - private boolean isEnableCorrelationLogs; + private static boolean isEnableCorrelationLogs; @Override protected void initInternal() throws LifecycleException { @@ -91,8 +101,6 @@ protected void initInternal() throws LifecycleException { if (StringUtils.isNotEmpty(configuredCorrelationIdMdc)) { correlationIdMdc = configuredCorrelationIdMdc; } - - isEnableCorrelationLogs = Boolean.parseBoolean(System.getProperty(CORRELATION_LOG_SYSTEM_PROPERTY)); } @Override @@ -136,6 +144,14 @@ public void invoke(Request request, Response response) throws IOException, Servl } } + @Activate + protected void activate(ComponentContext context) { + } + + @Deactivate + protected void deactivate(ComponentContext context) { + } + /** * Logs the details from request * @@ -351,4 +367,16 @@ public void setQueryToCorrelationIdMapping(String queryToCorrelationIdMapping) { public void setConfiguredCorrelationIdMdc(String configuredCorrelationIdMdc) { this.configuredCorrelationIdMdc = configuredCorrelationIdMdc; } + + @Override + public String getName() { + return "http"; + } + + @Override + public void reconfigure(Map properties) { + isEnableCorrelationLogs = (boolean) properties.get(CorrelationLogConstants.ENABLE) && + CorrelationLogUtil.isComponentWhitelisted(this.getName(), + (String) properties.get(CorrelationLogConstants.COMPONENTS)); + } } From d26e1207c614729253c8df32038ebdb5a1263ebb Mon Sep 17 00:00:00 2001 From: lasanthaS Date: Sun, 22 Aug 2021 17:00:25 +0530 Subject: [PATCH 04/11] Improve correlation logging --- .../pom.xml | 1 + .../internal/CorrelationLogManager.java | 84 ++++++++++++++----- .../correlation/mgt/CorrelationLogConfig.java | 10 +++ .../mgt/CorrelationLogConfigMBean.java | 4 + .../utils/CorrelationLogConstants.java | 6 ++ .../templates/repository/conf/carbon.xml.j2 | 15 ++++ 6 files changed, 98 insertions(+), 22 deletions(-) diff --git a/core/org.wso2.carbon.logging.correlation/pom.xml b/core/org.wso2.carbon.logging.correlation/pom.xml index e6c64e71a52..9a723f08048 100644 --- a/core/org.wso2.carbon.logging.correlation/pom.xml +++ b/core/org.wso2.carbon.logging.correlation/pom.xml @@ -78,6 +78,7 @@ + !org.wso2.carbon.logging.correlation.internal.*, org.wso2.carbon.logging.correlation.*, diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java index 6e00d7ad55f..fbbe7e6d14f 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java @@ -25,11 +25,13 @@ import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.ReferenceCardinality; import org.osgi.service.component.annotations.ReferencePolicy; +import org.wso2.carbon.base.api.ServerConfigurationService; import org.wso2.carbon.logging.correlation.mgt.CorrelationLogConfig; import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; import org.wso2.carbon.logging.correlation.CorrelationLogService; import org.wso2.carbon.logging.correlation.Notifiable; import org.wso2.carbon.utils.MBeanRegistrar; +import org.wso2.carbon.utils.xml.StringUtils; import java.util.HashMap; import java.util.Map; @@ -37,18 +39,22 @@ @Component(immediate = true) public class CorrelationLogManager implements Notifiable { - Map serviceMap; - Map logProperties; - org.wso2.carbon.logging.correlation.mgt.CorrelationLogConfig logConfig; + private Map serviceMap = new HashMap<>(); + private Map properties; + private CorrelationLogConfig mbeanConfig; + private boolean isDefaultLoaded = false; + + private ServerConfigurationService serverConfigurationService; + + public CorrelationLogManager() { + // Register the MBean required for JMX. The notifier will be invoked once each field updated via JMX. + mbeanConfig = new CorrelationLogConfig(); + mbeanConfig.registerNotifier(this); + MBeanRegistrar.registerMBean(mbeanConfig); + } @Activate protected void activate(ComponentContext context) { - serviceMap = new HashMap<>(); - logProperties = initProperties(); - - logConfig = new CorrelationLogConfig(); - logConfig.registerNotifier(this); - MBeanRegistrar.registerMBean(logConfig); } @Deactivate @@ -63,31 +69,65 @@ protected void deactivate(ComponentContext context) { ) protected void setCorrelationLogService(CorrelationLogService service) { serviceMap.put(service.getName(), service); - service.reconfigure(logProperties); + // If the defaults are already loaded, configure the current implementation with configs. If not they will be + // configured once the defaults are loaded. + if (isDefaultLoaded) { + service.reconfigure(properties); + } } protected void unsetCorrelationLogService(CorrelationLogService service) { serviceMap.remove(service.getName()); } + @Reference( + policy = ReferencePolicy.DYNAMIC, + cardinality = ReferenceCardinality.MANDATORY, + unbind = "unsetServerConfigurationService" + ) + protected void setServerConfigurationService(ServerConfigurationService serverConfigurationService) { + this.serverConfigurationService = serverConfigurationService; + loadDefaults(); + // Configure all referenced service implementations with the defaults. + configureServiceImpls(); + } + + protected void unsetServerConfigurationService(ServerConfigurationService serverConfigurationService) { + this.serverConfigurationService = null; + } + @Override public void notify(String key, Object value) { - logProperties.put(key, value); - reconfigure(); + // Update the properties map and configure each service implementation with updated values. + properties.put(key, value); + configureServiceImpls(); } - private void reconfigure() { - for (Map.Entry entry : serviceMap.entrySet()) { - entry.getValue().reconfigure(logProperties); + private void configureServiceImpls() { + // Notify each service implementation with configurations. + for (Map.Entry serviceEntry : serviceMap.entrySet()) { + serviceEntry.getValue().reconfigure(properties); } } - private Map initProperties() { - // TODO: Read default properties from the deployment.toml file - Map properties = new HashMap<>(); - properties.put(CorrelationLogConstants.ENABLE, false); - properties.put(CorrelationLogConstants.COMPONENTS, ""); - properties.put(CorrelationLogConstants.BLACKLISTED_THREADS, ""); - return properties; + private void loadDefaults() { + properties = new HashMap<>(); + // Enable + String enabled = this.serverConfigurationService.getFirstProperty(CorrelationLogConstants.CONFIG_ENABLE); + properties.put(CorrelationLogConstants.ENABLE, !StringUtils.isEmpty(enabled) && Boolean.parseBoolean(enabled)); + // Components + String components = this.serverConfigurationService.getFirstProperty(CorrelationLogConstants.CONFIG_COMPONENTS); + properties.put(CorrelationLogConstants.COMPONENTS, !StringUtils.isEmpty(components) ? components : ""); + // Blacklisted threads + String blacklistedThreads = this.serverConfigurationService + .getFirstProperty(CorrelationLogConstants.CONFIG_BLACKLISTED_THREADS); + properties.put(CorrelationLogConstants.BLACKLISTED_THREADS, + !StringUtils.isEmpty(blacklistedThreads) ? blacklistedThreads : ""); + // Log all methods + String logAllMethods = this.serverConfigurationService + .getFirstProperty(CorrelationLogConstants.CONFIG_LOG_ALL_METHODS); + properties.put(CorrelationLogConstants.LOG_ALL_METHODS, + !StringUtils.isEmpty(logAllMethods) && Boolean.parseBoolean(logAllMethods)); + isDefaultLoaded = true; } } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java index 2de2768176a..517cbea443c 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java @@ -26,6 +26,7 @@ public class CorrelationLogConfig implements CorrelationLogConfigMBean { private boolean enable; private String components; private String blacklistedThreads; + private String logAllMethods; private Notifiable notifiable; @Override @@ -61,6 +62,15 @@ public void setBlacklistedThreads(String blacklistedThreads) { this.notifiable.notify(CorrelationLogConstants.BLACKLISTED_THREADS, this.blacklistedThreads); } + public String getLogAllMethods() { + return logAllMethods; + } + + public void setLogAllMethods(String logAllMethods) { + this.logAllMethods = logAllMethods; + this.notifiable.notify(CorrelationLogConstants.LOG_ALL_METHODS, this.logAllMethods); + } + public void registerNotifier(Notifiable notifiable) { this.notifiable = notifiable; } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java index 62b87121a33..38a92a792f6 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java @@ -31,4 +31,8 @@ public interface CorrelationLogConfigMBean { String getBlacklistedThreads(); void setBlacklistedThreads(String blacklistedThreads); + + String getLogAllMethods(); + + void setLogAllMethods(String logAllMethods); } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java index fdfd5ff9a0e..9dcbd28f038 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java @@ -23,5 +23,11 @@ public class CorrelationLogConstants { public static final String ENABLE = "ENABLE"; public static final String COMPONENTS = "COMPONENTS"; public static final String BLACKLISTED_THREADS = "BLACKLISTED_THREADS"; + public static final String LOG_ALL_METHODS = "LOG_ALL_METHODS"; + + public static final String CONFIG_ENABLE = "CorrelationLogs.enable"; + public static final String CONFIG_COMPONENTS = "CorrelationLogs.components"; + public static final String CONFIG_BLACKLISTED_THREADS = "CorrelationLogs.blacklistedThreads"; + public static final String CONFIG_LOG_ALL_METHODS = "CorrelationLogs.logAllMethods"; } diff --git a/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 b/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 index c1d820699f2..84fdb892096 100644 --- a/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 +++ b/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 @@ -828,4 +828,19 @@ {{server.disable_restart_from_ui | default('false')}} + {% if correlation_logs is defined %} + + + + {{correlation_logs.enable}} + + {{correlation_logs.components}} + + {{correlation_logs.blacklistedThreads}} + + {{correlation_logs.logAllMethods}} + + {% endif %} + From f8f5bedf85ee605a20b62436301c1f9705b9a319 Mon Sep 17 00:00:00 2001 From: lasanthaS Date: Sun, 22 Aug 2021 21:27:58 +0530 Subject: [PATCH 05/11] Add javadoc for correlation logs --- .../{Notifiable.java => ConfigObserver.java} | 12 +++++- .../correlation/CorrelationLogService.java | 13 ++++++ .../internal/CorrelationLogManager.java | 39 ++++++++++++++---- .../correlation/mgt/CorrelationLogConfig.java | 26 ++++++++---- .../mgt/CorrelationLogConfigMBean.java | 41 +++++++++++++++++++ .../utils/CorrelationLogConstants.java | 5 +++ .../correlation/utils/CorrelationLogUtil.java | 18 +++++++- .../rdbms/CorrelationLogInterceptor.java | 10 +++++ .../ext/valves/RequestCorrelationIdValve.java | 10 +++++ 9 files changed, 156 insertions(+), 18 deletions(-) rename core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/{Notifiable.java => ConfigObserver.java} (71%) diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/Notifiable.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/ConfigObserver.java similarity index 71% rename from core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/Notifiable.java rename to core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/ConfigObserver.java index 572ad821ad3..437321f1bb5 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/Notifiable.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/ConfigObserver.java @@ -18,7 +18,15 @@ package org.wso2.carbon.logging.correlation; -public interface Notifiable { - +/** + * The config observer interface. This needs to be implemented to retrieve config changes done via the JMX endpoint. + */ +public interface ConfigObserver { + /** + * Returns the modified field name with the new value. + * + * @param key Field name + * @param value New value + */ void notify(String key, Object value); } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java index b8190665fdc..52914aa39e4 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java @@ -20,8 +20,21 @@ import java.util.Map; +/** + * The correlation log service class definition. + */ public interface CorrelationLogService { + /** + * Returns the name of the service implementation. + * + * @return + */ String getName(); + /** + * Accepts the modified configurations via the properties map. + * + * @param properties Map of configurations + */ void reconfigure(Map properties); } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java index fbbe7e6d14f..03435836ba4 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java @@ -29,28 +29,31 @@ import org.wso2.carbon.logging.correlation.mgt.CorrelationLogConfig; import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; import org.wso2.carbon.logging.correlation.CorrelationLogService; -import org.wso2.carbon.logging.correlation.Notifiable; +import org.wso2.carbon.logging.correlation.ConfigObserver; import org.wso2.carbon.utils.MBeanRegistrar; import org.wso2.carbon.utils.xml.StringUtils; import java.util.HashMap; import java.util.Map; +/** + * The correlation log manager class. This class reads configs from the Carbon.xml and the JMX endpoint and manage all + * service implementation with current configurations. + */ @Component(immediate = true) -public class CorrelationLogManager implements Notifiable { - +public class CorrelationLogManager implements ConfigObserver { private Map serviceMap = new HashMap<>(); private Map properties; - private CorrelationLogConfig mbeanConfig; + private CorrelationLogConfig config; private boolean isDefaultLoaded = false; private ServerConfigurationService serverConfigurationService; public CorrelationLogManager() { // Register the MBean required for JMX. The notifier will be invoked once each field updated via JMX. - mbeanConfig = new CorrelationLogConfig(); - mbeanConfig.registerNotifier(this); - MBeanRegistrar.registerMBean(mbeanConfig); + config = new CorrelationLogConfig(); + config.registerObserver(this); + MBeanRegistrar.registerMBean(config); } @Activate @@ -62,6 +65,11 @@ protected void deactivate(ComponentContext context) { serviceMap = null; } + /** + * Get references of the correlation log server implementations. + * + * @param service + */ @Reference( policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE, @@ -80,6 +88,11 @@ protected void unsetCorrelationLogService(CorrelationLogService service) { serviceMap.remove(service.getName()); } + /** + * Get the reference of the server configuration service. + * + * @param serverConfigurationService + */ @Reference( policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MANDATORY, @@ -96,6 +109,12 @@ protected void unsetServerConfigurationService(ServerConfigurationService server this.serverConfigurationService = null; } + /** + * Accepts the field name and the new value on all the configuration changes via the JMX endpoint. + * + * @param key Field name + * @param value New value + */ @Override public void notify(String key, Object value) { // Update the properties map and configure each service implementation with updated values. @@ -103,6 +122,9 @@ public void notify(String key, Object value) { configureServiceImpls(); } + /** + * Iterate and configure all the referenced service implementations. + */ private void configureServiceImpls() { // Notify each service implementation with configurations. for (Map.Entry serviceEntry : serviceMap.entrySet()) { @@ -110,6 +132,9 @@ private void configureServiceImpls() { } } + /** + * Load default configurations from the Carbon.xml file. + */ private void loadDefaults() { properties = new HashMap<>(); // Enable diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java index 517cbea443c..b22f16c2393 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java @@ -19,15 +19,18 @@ package org.wso2.carbon.logging.correlation.mgt; -import org.wso2.carbon.logging.correlation.Notifiable; +import org.wso2.carbon.logging.correlation.ConfigObserver; import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; +/** + * The implementation of the Correlation log MBean. + */ public class CorrelationLogConfig implements CorrelationLogConfigMBean { private boolean enable; private String components; private String blacklistedThreads; private String logAllMethods; - private Notifiable notifiable; + private ConfigObserver configObserver; @Override public boolean isEnable() { @@ -37,7 +40,7 @@ public boolean isEnable() { @Override public void setEnable(boolean enable) { this.enable = enable; - this.notifiable.notify(CorrelationLogConstants.ENABLE, this.enable); + this.configObserver.notify(CorrelationLogConstants.ENABLE, this.enable); } @Override @@ -48,7 +51,7 @@ public String getComponents() { @Override public void setComponents(String components) { this.components = components; - this.notifiable.notify(CorrelationLogConstants.COMPONENTS, this.components); + this.configObserver.notify(CorrelationLogConstants.COMPONENTS, this.components); } @Override @@ -59,19 +62,26 @@ public String getBlacklistedThreads() { @Override public void setBlacklistedThreads(String blacklistedThreads) { this.blacklistedThreads = blacklistedThreads; - this.notifiable.notify(CorrelationLogConstants.BLACKLISTED_THREADS, this.blacklistedThreads); + this.configObserver.notify(CorrelationLogConstants.BLACKLISTED_THREADS, this.blacklistedThreads); } + @Override public String getLogAllMethods() { return logAllMethods; } + @Override public void setLogAllMethods(String logAllMethods) { this.logAllMethods = logAllMethods; - this.notifiable.notify(CorrelationLogConstants.LOG_ALL_METHODS, this.logAllMethods); + this.configObserver.notify(CorrelationLogConstants.LOG_ALL_METHODS, this.logAllMethods); } - public void registerNotifier(Notifiable notifiable) { - this.notifiable = notifiable; + /** + * Register observer to be notified when all the field changes, + * + * @param configObserver + */ + public void registerObserver(ConfigObserver configObserver) { + this.configObserver = configObserver; } } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java index 38a92a792f6..e962b3ce8fc 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java @@ -18,21 +18,62 @@ package org.wso2.carbon.logging.correlation.mgt; +/** + * The MBean used for the JMX endpoint. + */ public interface CorrelationLogConfigMBean { + /** + * Returns a flag indicating whether the correlation logs are enabled or disabled. + * + * @return + */ boolean isEnable(); + /** + * Enable/disable correlation logs. + * + * @param enable + */ void setEnable(boolean enable); + /** + * Returns a comma separated list of components to enable correlation logs. + * + * @return + */ String getComponents(); + /** + * Set comma separated list of components to enable correlation logs. + * @param components + */ void setComponents(String components); + /** + * Returns a comma separated list of threads which are ignored while logging. + * + * @return + */ String getBlacklistedThreads(); + /** + * Set a comma separated list of threads which are ignored while logging. + * @param blacklistedThreads + */ void setBlacklistedThreads(String blacklistedThreads); + /** + * Returns a flag indicating logging all methods. + * + * @return + */ String getLogAllMethods(); + /** + * Enable/disable logging all methods. + * + * @param logAllMethods + */ void setLogAllMethods(String logAllMethods); } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java index 9dcbd28f038..25255d369e8 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java @@ -18,13 +18,18 @@ package org.wso2.carbon.logging.correlation.utils; +/** + * Correlation log constants class. + */ public class CorrelationLogConstants { + // Configuration field names public static final String ENABLE = "ENABLE"; public static final String COMPONENTS = "COMPONENTS"; public static final String BLACKLISTED_THREADS = "BLACKLISTED_THREADS"; public static final String LOG_ALL_METHODS = "LOG_ALL_METHODS"; + // Config paths in Carbon.xml file public static final String CONFIG_ENABLE = "CorrelationLogs.enable"; public static final String CONFIG_COMPONENTS = "CorrelationLogs.components"; public static final String CONFIG_BLACKLISTED_THREADS = "CorrelationLogs.blacklistedThreads"; diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java index ec53b3ea26f..39085680052 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java @@ -20,14 +20,30 @@ import java.util.Arrays; +/** + * Correlation log utility class. + */ public class CorrelationLogUtil { + /** + * Convert comma separated component list into an array. + * + * @param componentList Comma separated list + * @return Array of components + */ public static String[] toComponentArray(String componentList) { return componentList.split("\\s*,\\s*"); } + /** + * Check whether the given component exists in the provided component list. + * + * @param component Component name + * @param componentList Comma separated list of components + * @return + */ public static boolean isComponentWhitelisted(String component, String componentList) { - return componentList.equals("") || + return "".equals(componentList) || Arrays.stream(toComponentArray(componentList)).anyMatch(s -> s.equals(component)); } } diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java index 7a099d661e6..ea452cd8d66 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java @@ -120,11 +120,21 @@ protected void activate(ComponentContext context) { protected void deactivate(ComponentContext context) { } + /** + * Returns the name of the service component as "jdbc". + * + * @return + */ @Override public String getName() { return "jdbc"; } + /** + * Reconfigure the interceptor based on the configurations received. + * + * @param properties Map of configurations + */ @Override public void reconfigure(Map properties) { String blacklistedThreadNames = (String) properties.get(CorrelationLogConstants.BLACKLISTED_THREADS); diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java index 31eb91f4334..83891713f93 100644 --- a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java @@ -368,11 +368,21 @@ public void setConfiguredCorrelationIdMdc(String configuredCorrelationIdMdc) { this.configuredCorrelationIdMdc = configuredCorrelationIdMdc; } + /** + * Returns the name of the service component as "http". + * + * @return + */ @Override public String getName() { return "http"; } + /** + * Reconfigure the interceptor based on the configurations received. + * + * @param properties Map of configurations + */ @Override public void reconfigure(Map properties) { isEnableCorrelationLogs = (boolean) properties.get(CorrelationLogConstants.ENABLE) && From c27adbce16a62309a699d9e06d4a2365250cae88 Mon Sep 17 00:00:00 2001 From: lasanthaS Date: Sun, 29 Aug 2021 02:53:57 +0530 Subject: [PATCH 06/11] Refactor correlation logs component --- .../logging/correlation/ConfigObserver.java | 4 +- .../CorrelationLogConfigAttribute.java | 71 +++++++ .../correlation/CorrelationLogService.java | 7 + .../internal/CorrelationLogManager.java | 177 +++++++++++------- .../correlation/mgt/ConfigMapHolder.java | 114 +++++++++++ .../correlation/mgt/CorrelationLogConfig.java | 120 ++++++++---- .../mgt/CorrelationLogConfigMBean.java | 79 -------- .../utils/CorrelationLogConstants.java | 20 +- .../correlation/utils/CorrelationLogUtil.java | 2 +- .../rdbms/CorrelationLogInterceptor.java | 26 +-- .../ext/valves/RequestCorrelationIdValve.java | 8 +- .../templates/repository/conf/carbon.xml.j2 | 30 ++- 12 files changed, 444 insertions(+), 214 deletions(-) create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigAttribute.java create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/ConfigMapHolder.java delete mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/ConfigObserver.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/ConfigObserver.java index 437321f1bb5..20b887b7acd 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/ConfigObserver.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/ConfigObserver.java @@ -28,5 +28,7 @@ public interface ConfigObserver { * @param key Field name * @param value New value */ - void notify(String key, Object value); +// void configUpdated(String key, Object value); + + void configUpdated(String component, String key, Object value); } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigAttribute.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigAttribute.java new file mode 100644 index 00000000000..0e25658830d --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigAttribute.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation; + +/** + * This class is used to describe each configuration. + */ +public class CorrelationLogConfigAttribute { + private String name; + private String description; + private String type; + private Object defaultValue; + + public CorrelationLogConfigAttribute() { + } + + public CorrelationLogConfigAttribute(String name, String description, String type, Object defaultValue) { + this.name = name; + this.description = description; + this.type = type; + this.defaultValue = defaultValue; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public Object getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(Object defaultValue) { + this.defaultValue = defaultValue; + } +} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java index 52914aa39e4..067c5709bbe 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java @@ -37,4 +37,11 @@ public interface CorrelationLogService { * @param properties Map of configurations */ void reconfigure(Map properties); + + /** + * Returns the descriptor for the MBean. This contains all the properties solely required for the component. + * + * @return Array + */ + CorrelationLogConfigAttribute[] getConfigDescriptor(); } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java index 03435836ba4..f9991eec6ca 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java @@ -18,6 +18,8 @@ package org.wso2.carbon.logging.correlation.internal; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; @@ -25,35 +27,44 @@ import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.ReferenceCardinality; import org.osgi.service.component.annotations.ReferencePolicy; -import org.wso2.carbon.base.api.ServerConfigurationService; +import org.wso2.carbon.base.ServerConfiguration; +import org.wso2.carbon.logging.correlation.ConfigObserver; +import org.wso2.carbon.logging.correlation.CorrelationLogConfigAttribute; +import org.wso2.carbon.logging.correlation.CorrelationLogService; +import org.wso2.carbon.logging.correlation.mgt.ConfigMapHolder; import org.wso2.carbon.logging.correlation.mgt.CorrelationLogConfig; import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; -import org.wso2.carbon.logging.correlation.CorrelationLogService; -import org.wso2.carbon.logging.correlation.ConfigObserver; import org.wso2.carbon.utils.MBeanRegistrar; -import org.wso2.carbon.utils.xml.StringUtils; import java.util.HashMap; import java.util.Map; /** - * The correlation log manager class. This class reads configs from the Carbon.xml and the JMX endpoint and manage all - * service implementation with current configurations. + * The correlation log manager class. This class reads correlation log configurations from the Carbon.xml file and the + * JMX endpoint and manage all service implementation by dispatching the configurations.. */ @Component(immediate = true) public class CorrelationLogManager implements ConfigObserver { - private Map serviceMap = new HashMap<>(); - private Map properties; - private CorrelationLogConfig config; - private boolean isDefaultLoaded = false; + private static Log log = LogFactory.getLog(CorrelationLogManager.class); - private ServerConfigurationService serverConfigurationService; + private Map serviceMap = new HashMap<>(); + private String objectName = null; public CorrelationLogManager() { - // Register the MBean required for JMX. The notifier will be invoked once each field updated via JMX. - config = new CorrelationLogConfig(); - config.registerObserver(this); - MBeanRegistrar.registerMBean(config); + // Initializes the root configurations. These are shared/common for all the service implementations. + CorrelationLogConfigAttribute[] attributes = new CorrelationLogConfigAttribute[] { + new CorrelationLogConfigAttribute( + CorrelationLogConstants.ENABLE, + "Enable correlation logs", Boolean.class.getName(), false), + new CorrelationLogConfigAttribute(CorrelationLogConstants.COMPONENTS, + "Components to enable logs", String.class.getName(), ""), + new CorrelationLogConfigAttribute(CorrelationLogConstants.DENIED_THREADS, + "Threads which are ignored from correlation logs", String.class.getName(), + String.join(",", CorrelationLogConstants.DEFAULT_DENIED_THREADS)) + }; + + ConfigMapHolder.getInstance().onConfigUpdated(this); + createAndRegisterMBean(null, attributes); } @Activate @@ -76,12 +87,13 @@ protected void deactivate(ComponentContext context) { unbind = "unsetCorrelationLogService" ) protected void setCorrelationLogService(CorrelationLogService service) { + log.debug("Correlation log service '" + service + "' loaded."); serviceMap.put(service.getName(), service); - // If the defaults are already loaded, configure the current implementation with configs. If not they will be - // configured once the defaults are loaded. - if (isDefaultLoaded) { - service.reconfigure(properties); + if (service.getConfigDescriptor() != null) { + createAndRegisterMBean(service.getName(), service.getConfigDescriptor()); } + // Assumption: The each service component only based on the root configs and its own configs. + updateComponentConfig(service); } protected void unsetCorrelationLogService(CorrelationLogService service) { @@ -89,70 +101,103 @@ protected void unsetCorrelationLogService(CorrelationLogService service) { } /** - * Get the reference of the server configuration service. + * Notifies each service implementation once a configuration is changed in the config map. * - * @param serverConfigurationService + * @param component Component name + * @param key Field name + * @param value New value */ - @Reference( - policy = ReferencePolicy.DYNAMIC, - cardinality = ReferenceCardinality.MANDATORY, - unbind = "unsetServerConfigurationService" - ) - protected void setServerConfigurationService(ServerConfigurationService serverConfigurationService) { - this.serverConfigurationService = serverConfigurationService; - loadDefaults(); - // Configure all referenced service implementations with the defaults. - configureServiceImpls(); + @Override + public void configUpdated(String component, String key, Object value) { + for (Map.Entry entry : serviceMap.entrySet()) { + updateComponentConfig(entry.getValue()); + } } - protected void unsetServerConfigurationService(ServerConfigurationService serverConfigurationService) { - this.serverConfigurationService = null; + /** + * Merges the root config with the component specific configs and notifies the service component. + * + * @param service + */ + private void updateComponentConfig(CorrelationLogService service) { + // Get root config, append component-wise configs on top of that and invoke the components. + Map componentSpecificConfigMap = new HashMap<>(); + for (String component : new String[] { null, service.getName() }) { + Map map = ConfigMapHolder.getInstance().getConfigMap(component); + if (map != null) { + componentSpecificConfigMap.putAll(map); + } + } + service.reconfigure(componentSpecificConfigMap); } /** - * Accepts the field name and the new value on all the configuration changes via the JMX endpoint. + * Create a dynamic MBean based on the given attributes and register it. These MBeans are created for the root + * component as well as other service service components. * - * @param key Field name - * @param value New value + * @param componentName Name of the component + * @param attributes Attribute list */ - @Override - public void notify(String key, Object value) { - // Update the properties map and configure each service implementation with updated values. - properties.put(key, value); - configureServiceImpls(); + private void createAndRegisterMBean(String componentName, CorrelationLogConfigAttribute[] attributes) { + // Create dynamic MBean via the given attributes. This will copy the defaults to the config map. + CorrelationLogConfig mbean = new CorrelationLogConfig(componentName, attributes); + // Override the defaults from the configs defined in the carbon.xml file. + overrideConfigsFromFile(componentName, attributes); + + // Register MBean. + try { + String objectName = getObjectName(); + if (componentName != null) { + objectName += ",name=" + componentName; + } + MBeanRegistrar.registerMBean(mbean, objectName); + log.debug("Registered correlation log MBean for " + componentName + " component."); + } catch (Exception e) { + String msg = "Could not register Correlation Log " + componentName + " MBean"; + log.error(msg, e); + throw new RuntimeException(msg, e); + } } /** - * Iterate and configure all the referenced service implementations. + * Load default configurations from the Carbon.xml file. */ - private void configureServiceImpls() { - // Notify each service implementation with configurations. - for (Map.Entry serviceEntry : serviceMap.entrySet()) { - serviceEntry.getValue().reconfigure(properties); + private void overrideConfigsFromFile(String componentName, CorrelationLogConfigAttribute[] attributes) { + log.debug("Overriding configurations from the carbon.xml file."); + boolean isRootConfig = componentName == null; + for (CorrelationLogConfigAttribute attr : attributes) { + String path = CorrelationLogConstants.CONFIG_ROOT + + (isRootConfig ? "." : ".componentConfigs." + componentName + ".") + + attr.getName(); + String value = ServerConfiguration.getInstance().getFirstProperty(path); + if (value != null) { + // If value is null default will be effective. + if (Boolean.class.getName().equals(attr.getType())) { + ConfigMapHolder.getInstance().setConfig(componentName, attr.getName(), Boolean.parseBoolean(value)); + } else { + ConfigMapHolder.getInstance().setConfig(componentName, attr.getName(), value); + } + } } } /** - * Load default configurations from the Carbon.xml file. + * Get object name for the MBean. + * + * @return The object name */ - private void loadDefaults() { - properties = new HashMap<>(); - // Enable - String enabled = this.serverConfigurationService.getFirstProperty(CorrelationLogConstants.CONFIG_ENABLE); - properties.put(CorrelationLogConstants.ENABLE, !StringUtils.isEmpty(enabled) && Boolean.parseBoolean(enabled)); - // Components - String components = this.serverConfigurationService.getFirstProperty(CorrelationLogConstants.CONFIG_COMPONENTS); - properties.put(CorrelationLogConstants.COMPONENTS, !StringUtils.isEmpty(components) ? components : ""); - // Blacklisted threads - String blacklistedThreads = this.serverConfigurationService - .getFirstProperty(CorrelationLogConstants.CONFIG_BLACKLISTED_THREADS); - properties.put(CorrelationLogConstants.BLACKLISTED_THREADS, - !StringUtils.isEmpty(blacklistedThreads) ? blacklistedThreads : ""); - // Log all methods - String logAllMethods = this.serverConfigurationService - .getFirstProperty(CorrelationLogConstants.CONFIG_LOG_ALL_METHODS); - properties.put(CorrelationLogConstants.LOG_ALL_METHODS, - !StringUtils.isEmpty(logAllMethods) && Boolean.parseBoolean(logAllMethods)); - isDefaultLoaded = true; + private String getObjectName() { + if (objectName == null) { + String serverPackage = ServerConfiguration.getInstance().getFirstProperty("Package"); + if (serverPackage == null) { + serverPackage = "wso2"; + } + String className = CorrelationLogConfig.class.getName(); + if (className.indexOf('.') != -1) { + className = className.substring(className.lastIndexOf('.') + 1); + } + objectName = serverPackage + ":type=" + className; + } + return objectName; } } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/ConfigMapHolder.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/ConfigMapHolder.java new file mode 100644 index 00000000000..0560c79cac3 --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/ConfigMapHolder.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.mgt; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.logging.correlation.ConfigObserver; + +import java.util.HashMap; +import java.util.Map; + +/** + * Correlation log configuration holder. + */ +public class ConfigMapHolder { + private static Log log = LogFactory.getLog(ConfigMapHolder.class); + private static ConfigMapHolder instance = new ConfigMapHolder(); + + private ConfigObserver observer = null; + private Map> componentConfigMap; + + private ConfigMapHolder() { + componentConfigMap = new HashMap<>(); + } + + /** + * Get the singleton instance of the ConfigMapHolder class. + * @return + */ + public static ConfigMapHolder getInstance() { + return instance; + } + + /** + * Registers an observer to be invoked when the config is changed. + * + * @param observer + */ + public void onConfigUpdated(ConfigObserver observer) { + this.observer = observer; + } + + /** + * Get complete config map for a component. + * + * @param component Component name + * @return + */ + public Map getConfigMap(String component) { + return componentConfigMap.get(getComponentName(component)); + } + + /** + * Get component specific configuration by key. + * + * @param component Component name + * @param key Config name + * @return + */ + public Object getConfig(String component, String key) { + Map map = componentConfigMap.get(getComponentName(component)); + if (map != null) { + return map.get(key); + } + return null; + } + + /** + * Set config in the map of map. + * + * @param component Component name + * @param key Config name + * @param value Value + */ + public void setConfig(String component, String key, Object value) { + String componentName = getComponentName(component); + if (componentConfigMap.get(componentName) == null) { + componentConfigMap.put(componentName, new HashMap<>()); + } + componentConfigMap.get(componentName).put(key, value); + if (observer != null) { + observer.configUpdated(componentName, key, value); + } + } + + /** + * If the component is null, return it as "root". Else the component name will not be changed. + * + * @param componentName + * @return + */ + private String getComponentName(String componentName) { + if (componentName != null) { + return componentName; + } + return "root"; + } +} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java index b22f16c2393..100a84e1853 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java @@ -18,70 +18,118 @@ package org.wso2.carbon.logging.correlation.mgt; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.logging.correlation.CorrelationLogConfigAttribute; -import org.wso2.carbon.logging.correlation.ConfigObserver; -import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; +import javax.management.Attribute; +import javax.management.AttributeList; +import javax.management.AttributeNotFoundException; +import javax.management.DynamicMBean; +import javax.management.InvalidAttributeValueException; +import javax.management.MBeanAttributeInfo; +import javax.management.MBeanException; +import javax.management.MBeanInfo; +import javax.management.MBeanNotificationInfo; +import javax.management.ReflectionException; /** - * The implementation of the Correlation log MBean. + * Correlatin log dynamic MBean class. */ -public class CorrelationLogConfig implements CorrelationLogConfigMBean { - private boolean enable; - private String components; - private String blacklistedThreads; - private String logAllMethods; - private ConfigObserver configObserver; +public class CorrelationLogConfig implements DynamicMBean { + private static Log log = LogFactory.getLog(CorrelationLogConfig.class); - @Override - public boolean isEnable() { - return enable; + private String componentName; + private CorrelationLogConfigAttribute[] attributes; + private MBeanInfo mBeanInfo; + + public CorrelationLogConfig(String componentName, CorrelationLogConfigAttribute[] attributes) { + this.componentName = componentName; + this.attributes = attributes; + buildDynamicMBean(); } @Override - public void setEnable(boolean enable) { - this.enable = enable; - this.configObserver.notify(CorrelationLogConstants.ENABLE, this.enable); + public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException { + if (attribute == null) { + throw new RuntimeException(new IllegalArgumentException("Attribute name cannot be null.")); + } + + // If the attribute name is valid, return the value from the config map. + if (isValidAttribute(attribute)) { + return ConfigMapHolder.getInstance().getConfig(componentName, attribute); + } + throw new AttributeNotFoundException("Cannot find " + attribute + " attribute"); } @Override - public String getComponents() { - return components; + public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { + if (attribute == null) { + throw new RuntimeException(new IllegalArgumentException("Attribute name cannot be null.")); + } + + // If the attribute name is valid, set the value to the config map. + if (isValidAttribute(attribute.getName())) { + ConfigMapHolder.getInstance().setConfig(componentName, attribute.getName(), attribute.getValue()); + return; + } + throw new AttributeNotFoundException("Cannot find " + attribute + " attribute"); } @Override - public void setComponents(String components) { - this.components = components; - this.configObserver.notify(CorrelationLogConstants.COMPONENTS, this.components); + public AttributeList getAttributes(String[] attributes) { + return null; } @Override - public String getBlacklistedThreads() { - return blacklistedThreads; + public AttributeList setAttributes(AttributeList attributes) { + return null; } @Override - public void setBlacklistedThreads(String blacklistedThreads) { - this.blacklistedThreads = blacklistedThreads; - this.configObserver.notify(CorrelationLogConstants.BLACKLISTED_THREADS, this.blacklistedThreads); + public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException { + return null; } @Override - public String getLogAllMethods() { - return logAllMethods; + public MBeanInfo getMBeanInfo() { + return mBeanInfo; } - @Override - public void setLogAllMethods(String logAllMethods) { - this.logAllMethods = logAllMethods; - this.configObserver.notify(CorrelationLogConstants.LOG_ALL_METHODS, this.logAllMethods); + /** + * Iterate each + */ + private void buildDynamicMBean() { + MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[attributes.length]; + for (int i = 0; i < attributes.length; i++) { + attributeInfos[i] = new MBeanAttributeInfo( + attributes[i].getName(), + attributes[i].getType(), + attributes[i].getDescription(), + true, + true, + attributes[i].getType().equals(Boolean.class.getName())); + + // Add the default value into the config map. + ConfigMapHolder.getInstance().setConfig(componentName, attributes[i].getName(), + attributes[i].getDefaultValue()); + } + mBeanInfo = new MBeanInfo(this.getClass().getName(), "", attributeInfos, null, null, new MBeanNotificationInfo[0]); } /** - * Register observer to be notified when all the field changes, + * Check whether the provided attributes is valid by validating it against the given attribute list. * - * @param configObserver + * @param attribute + * @return */ - public void registerObserver(ConfigObserver configObserver) { - this.configObserver = configObserver; - } + private boolean isValidAttribute(String attribute) { + for (CorrelationLogConfigAttribute attr : attributes) { + if (attr.getName().equals(attribute)) { + return true; + } + } + log.debug("Invalid attribute name " + attribute + " in the component " + componentName); + return false; + } } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java deleted file mode 100644 index e962b3ce8fc..00000000000 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfigMBean.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.mgt; - -/** - * The MBean used for the JMX endpoint. - */ -public interface CorrelationLogConfigMBean { - - /** - * Returns a flag indicating whether the correlation logs are enabled or disabled. - * - * @return - */ - boolean isEnable(); - - /** - * Enable/disable correlation logs. - * - * @param enable - */ - void setEnable(boolean enable); - - /** - * Returns a comma separated list of components to enable correlation logs. - * - * @return - */ - String getComponents(); - - /** - * Set comma separated list of components to enable correlation logs. - * @param components - */ - void setComponents(String components); - - /** - * Returns a comma separated list of threads which are ignored while logging. - * - * @return - */ - String getBlacklistedThreads(); - - /** - * Set a comma separated list of threads which are ignored while logging. - * @param blacklistedThreads - */ - void setBlacklistedThreads(String blacklistedThreads); - - /** - * Returns a flag indicating logging all methods. - * - * @return - */ - String getLogAllMethods(); - - /** - * Enable/disable logging all methods. - * - * @param logAllMethods - */ - void setLogAllMethods(String logAllMethods); -} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java index 25255d369e8..97dc1d2a414 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java @@ -24,15 +24,19 @@ public class CorrelationLogConstants { // Configuration field names - public static final String ENABLE = "ENABLE"; - public static final String COMPONENTS = "COMPONENTS"; - public static final String BLACKLISTED_THREADS = "BLACKLISTED_THREADS"; - public static final String LOG_ALL_METHODS = "LOG_ALL_METHODS"; + public static final String ENABLE = "enable"; + public static final String COMPONENTS = "components"; + public static final String LOG_ALL_METHODS = "logAllMethods"; + public static final String DENIED_THREADS = "deniedThreads"; // Config paths in Carbon.xml file - public static final String CONFIG_ENABLE = "CorrelationLogs.enable"; - public static final String CONFIG_COMPONENTS = "CorrelationLogs.components"; - public static final String CONFIG_BLACKLISTED_THREADS = "CorrelationLogs.blacklistedThreads"; - public static final String CONFIG_LOG_ALL_METHODS = "CorrelationLogs.logAllMethods"; + public static final String CONFIG_ROOT = "CorrelationLogs"; + // Defaults + public static final String[] DEFAULT_DENIED_THREADS = { + "MessageDeliveryTaskThreadPool", + "HumanTaskServer" , + "BPELServer", + "CarbonDeploymentSchedulerThread" + }; } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java index 39085680052..b0db0b142e8 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java @@ -42,7 +42,7 @@ public static String[] toComponentArray(String componentList) { * @param componentList Comma separated list of components * @return */ - public static boolean isComponentWhitelisted(String component, String componentList) { + public static boolean isComponentAllowed(String component, String componentList) { return "".equals(componentList) || Arrays.stream(toComponentArray(componentList)).anyMatch(s -> s.equals(component)); } diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java index ea452cd8d66..0d8618bded2 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java @@ -25,6 +25,7 @@ import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; +import org.wso2.carbon.logging.correlation.CorrelationLogConfigAttribute; import org.wso2.carbon.logging.correlation.CorrelationLogService; import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; import org.wso2.carbon.logging.correlation.utils.CorrelationLogUtil; @@ -55,9 +56,8 @@ public class CorrelationLogInterceptor extends AbstractQueryReport implements Co private static final Log log = LogFactory.getLog(CorrelationLogInterceptor.class); private static final String CORRELATION_LOG_CALL_TYPE_VALUE = "jdbc"; private static final String CORRELATION_LOG_SEPARATOR = "|"; - private static final String[] DEFAULT_BLACKLISTED_THREADS = {"MessageDeliveryTaskThreadPool", "HumanTaskServer" , - "BPELServer", "CarbonDeploymentSchedulerThread"}; - private static List blacklistedThreadList = new ArrayList<>(); + public static final String DENIED_THREADS = "deniedThreads"; + private static List deniedThreadList = new ArrayList<>(); private static boolean isEnableCorrelationLogs; @Override @@ -137,20 +137,20 @@ public String getName() { */ @Override public void reconfigure(Map properties) { - String blacklistedThreadNames = (String) properties.get(CorrelationLogConstants.BLACKLISTED_THREADS); - if (blacklistedThreadNames == null) { - blacklistedThreadList.addAll(Arrays.asList(DEFAULT_BLACKLISTED_THREADS)); - } - - if (!StringUtils.isEmpty(blacklistedThreadNames)) { - blacklistedThreadList.addAll(Arrays.asList(StringUtils.split(blacklistedThreadNames, ','))); - } + String deniedThreadNames = (String) properties.get(DENIED_THREADS); + deniedThreadList.clear(); + deniedThreadList.addAll(Arrays.asList(StringUtils.split(deniedThreadNames, ','))); isEnableCorrelationLogs = (Boolean) properties.get(CorrelationLogConstants.ENABLE) && - CorrelationLogUtil.isComponentWhitelisted(this.getName(), + CorrelationLogUtil.isComponentAllowed(this.getName(), (String) properties.get(CorrelationLogConstants.COMPONENTS)); } + @Override + public CorrelationLogConfigAttribute[] getConfigDescriptor() { + return null; + } + /** * * Proxy Class that is used to calculate and log the time taken for queries @@ -241,7 +241,7 @@ private void logQueryDetails(long start, long delta, String methodName) throws S private boolean isCurrentThreadBlacklisted() { String threadName = Thread.currentThread().getName(); - for (String thread : blacklistedThreadList) { + for (String thread : deniedThreadList) { if (threadName.startsWith(thread)) { return true; } diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java index 83891713f93..0226c3f7076 100644 --- a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java @@ -32,6 +32,7 @@ import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; +import org.wso2.carbon.logging.correlation.CorrelationLogConfigAttribute; import org.wso2.carbon.logging.correlation.CorrelationLogService; import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; import org.wso2.carbon.logging.correlation.utils.CorrelationLogUtil; @@ -386,7 +387,12 @@ public String getName() { @Override public void reconfigure(Map properties) { isEnableCorrelationLogs = (boolean) properties.get(CorrelationLogConstants.ENABLE) && - CorrelationLogUtil.isComponentWhitelisted(this.getName(), + CorrelationLogUtil.isComponentAllowed(this.getName(), (String) properties.get(CorrelationLogConstants.COMPONENTS)); } + + @Override + public CorrelationLogConfigAttribute[] getConfigDescriptor() { + return null; + } } diff --git a/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 b/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 index 84fdb892096..5723fea3a8f 100644 --- a/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 +++ b/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 @@ -829,17 +829,29 @@ {% if correlation_logs is defined %} - - {{correlation_logs.enable}} - - {{correlation_logs.components}} - - {{correlation_logs.blacklistedThreads}} - - {{correlation_logs.logAllMethods}} + {% for component in correlation_logs.components %}{{component}}{{"," if not loop.last}}{% endfor%} + {% if correlation_logs.denied_threads is defined %} + {% for thread in correlation_logs.denied_threads %}{{thread}}{{"," if not loop.last}}{% endfor %} + {% endif %} + {% if correlation_logs.component_configs is defined %} + + {% for component in correlation_logs.component_configs %} + <{{component.type}}> + {% for k, v in component.items() %} + {% if k != "type" %} + {% if v is sequence and v is not string and v is not mapping %} + <{{k}}>{% for p in v %}{{p}}{{"," if not loop.last}}{% endfor %} + {% else %} + <{{k}}>{{v}} + {% endif %} + {% endif %} + {% endfor %} + + {% endfor %} + + {% endif %} {% endif %} From da0c7f66f4e339c069e10557d9bca3c394c8a55c Mon Sep 17 00:00:00 2001 From: lasanthaS Date: Thu, 2 Sep 2021 09:06:11 +0530 Subject: [PATCH 07/11] Remove JMX EP and refactored --- .../CorrelationLogConfigAttribute.java | 71 ------- ...va => CorrelationLogConfigModifiable.java} | 24 ++- ...e.java => CorrelationLogConfigurable.java} | 23 +-- .../bean/CorrelationLogComponentConfig.java | 46 +++++ .../bean/CorrelationLogConfig.java | 103 ++++++++++ .../bean/ImmutableCorrelationLogConfig.java | 50 +++++ .../internal/CorrelationLogManager.java | 186 +++++++----------- .../correlation/mgt/ConfigMapHolder.java | 114 ----------- .../correlation/mgt/CorrelationLogConfig.java | 135 ------------- .../utils/CorrelationLogConstants.java | 11 +- .../correlation/utils/CorrelationLogUtil.java | 28 ++- .../ConfigurableCorrelationLogService.java | 51 +++++ .../rdbms/CorrelationLogInterceptor.java | 63 +----- .../ConfigurableCorrelationLogService.java | 45 +++++ .../ext/valves/RequestCorrelationIdValve.java | 50 +---- core/org.wso2.carbon.user.core/pom.xml | 4 + .../ConfigurableCorrelationLogService.java | 46 +++++ .../user/core/ldap/LDAPConnectionContext.java | 7 +- 18 files changed, 475 insertions(+), 582 deletions(-) delete mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigAttribute.java rename core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/{ConfigObserver.java => CorrelationLogConfigModifiable.java} (52%) rename core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/{CorrelationLogService.java => CorrelationLogConfigurable.java} (59%) create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/CorrelationLogComponentConfig.java create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/CorrelationLogConfig.java create mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/ImmutableCorrelationLogConfig.java delete mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/ConfigMapHolder.java delete mode 100644 core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java create mode 100644 core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java create mode 100644 core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java create mode 100644 core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigAttribute.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigAttribute.java deleted file mode 100644 index 0e25658830d..00000000000 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigAttribute.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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 org.wso2.carbon.logging.correlation; - -/** - * This class is used to describe each configuration. - */ -public class CorrelationLogConfigAttribute { - private String name; - private String description; - private String type; - private Object defaultValue; - - public CorrelationLogConfigAttribute() { - } - - public CorrelationLogConfigAttribute(String name, String description, String type, Object defaultValue) { - this.name = name; - this.description = description; - this.type = type; - this.defaultValue = defaultValue; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public Object getDefaultValue() { - return defaultValue; - } - - public void setDefaultValue(Object defaultValue) { - this.defaultValue = defaultValue; - } -} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/ConfigObserver.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigModifiable.java similarity index 52% rename from core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/ConfigObserver.java rename to core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigModifiable.java index 20b887b7acd..a1ff7be9072 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/ConfigObserver.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigModifiable.java @@ -1,4 +1,5 @@ /* + * * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, @@ -18,17 +19,26 @@ package org.wso2.carbon.logging.correlation; +import org.wso2.carbon.logging.correlation.bean.CorrelationLogConfig; + /** - * The config observer interface. This needs to be implemented to retrieve config changes done via the JMX endpoint. + * The class definition which needs to be implemented to override correlation log configurations dynamically in the + * runtime. */ -public interface ConfigObserver { +public interface CorrelationLogConfigModifiable { + /** - * Returns the modified field name with the new value. + * Returns an instance of the CorrelationLogConfig class which represents effective entire + * configuration related to correlation logs.. * - * @param key Field name - * @param value New value + * @return */ -// void configUpdated(String key, Object value); + CorrelationLogConfig getConfiguration(); - void configUpdated(String component, String key, Object value); + /** + * Set the entire configuration related to correlation logs. + * + * @param correlationLogConfig + */ + void updateConfiguration(CorrelationLogConfig correlationLogConfig); } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigurable.java similarity index 59% rename from core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java rename to core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigurable.java index 067c5709bbe..cc1d023d66e 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogService.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigurable.java @@ -18,30 +18,25 @@ package org.wso2.carbon.logging.correlation; -import java.util.Map; +import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; /** - * The correlation log service class definition. + * The class definition of the correlation log service class which needs to be implemented in each component. */ -public interface CorrelationLogService { +public interface CorrelationLogConfigurable { + /** - * Returns the name of the service implementation. + * Returns name of the implementation. * * @return */ String getName(); /** - * Accepts the modified configurations via the properties map. - * - * @param properties Map of configurations - */ - void reconfigure(Map properties); - - /** - * Returns the descriptor for the MBean. This contains all the properties solely required for the component. + * Receives an instance of ImmutableCorrelationLogConfig class which holds the component-specific + * configuration. * - * @return Array + * @param config */ - CorrelationLogConfigAttribute[] getConfigDescriptor(); + void configurationReceived(ImmutableCorrelationLogConfig config); } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/CorrelationLogComponentConfig.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/CorrelationLogComponentConfig.java new file mode 100644 index 00000000000..d32e04a40c5 --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/CorrelationLogComponentConfig.java @@ -0,0 +1,46 @@ +/* + * + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.bean; + +/** + * Component-specific configuration bean class. + */ +public class CorrelationLogComponentConfig { + private boolean logAllMethods; + + public CorrelationLogComponentConfig() { + } + + public CorrelationLogComponentConfig(boolean logAllMethods) { + this.logAllMethods = logAllMethods; + } + + public boolean isLogAllMethods() { + return logAllMethods; + } + + public void setLogAllMethods(boolean logAllMethods) { + this.logAllMethods = logAllMethods; + } + + public CorrelationLogComponentConfig clone() { + return new CorrelationLogComponentConfig(this.logAllMethods); + } +} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/CorrelationLogConfig.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/CorrelationLogConfig.java new file mode 100644 index 00000000000..cfe745c09d9 --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/CorrelationLogConfig.java @@ -0,0 +1,103 @@ +/* + * + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.bean; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +/** + * Configuration bean class. + */ +public class CorrelationLogConfig { + private boolean enable; + private String[] components; + private String[] deniedThreads; + private Map componentConfigs; + + public CorrelationLogConfig() { + this(false, null, null); + } + + public CorrelationLogConfig(boolean enable, String[] components, String[] deniedThreads) { + this(enable, components, deniedThreads, new HashMap<>()); + } + + public CorrelationLogConfig(boolean enable, String[] components, String[] deniedThreads, + Map componentConfigs) { + this.enable = enable; + this.components = components; + this.deniedThreads = deniedThreads; + this.componentConfigs = componentConfigs; + } + + public boolean isEnable() { + return enable; + } + + public void setEnable(boolean enable) { + this.enable = enable; + } + + public String[] getComponents() { + return components; + } + + public void setComponents(String[] components) { + this.components = components; + } + + public String[] getDeniedThreads() { + return deniedThreads; + } + + public void setDeniedThreads(String[] deniedThreads) { + this.deniedThreads = deniedThreads; + } + + public Map getComponentConfigs() { + return componentConfigs; + } + + public void setComponentConfigs(Map componentConfigs) { + this.componentConfigs = componentConfigs; + } + + /** + * Returns a clone of this instance. + * + * @return + */ + public CorrelationLogConfig clone() { + // Create a copy of the current instance. + CorrelationLogConfig clone = new CorrelationLogConfig( + this.enable, + Arrays.copyOf(this.components, this.components.length), + Arrays.copyOf(this.deniedThreads, this.deniedThreads.length)); + + // Copy component configurations. + Map clonedComponentConfigs = new HashMap<>(); + for (Map.Entry entry : this.componentConfigs.entrySet()) { + clonedComponentConfigs.put(new String(entry.getKey()), entry.getValue().clone()); + } + clone.setComponentConfigs(clonedComponentConfigs); + return clone; + } +} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/ImmutableCorrelationLogConfig.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/ImmutableCorrelationLogConfig.java new file mode 100644 index 00000000000..03e8084b02f --- /dev/null +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/bean/ImmutableCorrelationLogConfig.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.bean; + +import java.util.Arrays; + +/** + * Immutable configuration bean class for correlation log. + * An object of this class is used to dispatch configuration changes to CorrelationLogConfigurable + * service implementations. + */ +public class ImmutableCorrelationLogConfig { + private boolean enable; + private String[] deniedThreads; + private boolean logAllMethods; + + public ImmutableCorrelationLogConfig(boolean enable, String[] deniedThreads, boolean logAllMethods) { + this.enable = enable; + this.deniedThreads = deniedThreads; + this.logAllMethods = logAllMethods; + } + + public boolean isEnable() { + return enable; + } + + public String[] getDeniedThreads() { + return deniedThreads; + } + + public boolean isLogAllMethods() { + return logAllMethods; + } +} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java index f9991eec6ca..e0d21f415cd 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java @@ -28,56 +28,43 @@ import org.osgi.service.component.annotations.ReferenceCardinality; import org.osgi.service.component.annotations.ReferencePolicy; import org.wso2.carbon.base.ServerConfiguration; -import org.wso2.carbon.logging.correlation.ConfigObserver; -import org.wso2.carbon.logging.correlation.CorrelationLogConfigAttribute; -import org.wso2.carbon.logging.correlation.CorrelationLogService; -import org.wso2.carbon.logging.correlation.mgt.ConfigMapHolder; -import org.wso2.carbon.logging.correlation.mgt.CorrelationLogConfig; +import org.wso2.carbon.logging.correlation.CorrelationLogConfigModifiable; +import org.wso2.carbon.logging.correlation.CorrelationLogConfigurable; +import org.wso2.carbon.logging.correlation.bean.CorrelationLogComponentConfig; +import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; +import org.wso2.carbon.logging.correlation.bean.CorrelationLogConfig; import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; -import org.wso2.carbon.utils.MBeanRegistrar; - -import java.util.HashMap; -import java.util.Map; +import org.wso2.carbon.logging.correlation.utils.CorrelationLogUtil; /** * The correlation log manager class. This class reads correlation log configurations from the Carbon.xml file and the * JMX endpoint and manage all service implementation by dispatching the configurations.. */ -@Component(immediate = true) -public class CorrelationLogManager implements ConfigObserver { +@Component( + immediate = true, + service = CorrelationLogConfigModifiable.class) +public class CorrelationLogManager implements CorrelationLogConfigModifiable { private static Log log = LogFactory.getLog(CorrelationLogManager.class); - private Map serviceMap = new HashMap<>(); - private String objectName = null; + private CorrelationLogConfig config; public CorrelationLogManager() { - // Initializes the root configurations. These are shared/common for all the service implementations. - CorrelationLogConfigAttribute[] attributes = new CorrelationLogConfigAttribute[] { - new CorrelationLogConfigAttribute( - CorrelationLogConstants.ENABLE, - "Enable correlation logs", Boolean.class.getName(), false), - new CorrelationLogConfigAttribute(CorrelationLogConstants.COMPONENTS, - "Components to enable logs", String.class.getName(), ""), - new CorrelationLogConfigAttribute(CorrelationLogConstants.DENIED_THREADS, - "Threads which are ignored from correlation logs", String.class.getName(), - String.join(",", CorrelationLogConstants.DEFAULT_DENIED_THREADS)) - }; - - ConfigMapHolder.getInstance().onConfigUpdated(this); - createAndRegisterMBean(null, attributes); + // Load root configurations from the carbon.xml file. + this.config = loadRootConfigurations(); } @Activate protected void activate(ComponentContext context) { + log.debug("CorrelationLogManager component activated."); } @Deactivate protected void deactivate(ComponentContext context) { - serviceMap = null; + log.debug("CorrelationLogManager component deactivated."); } /** - * Get references of the correlation log server implementations. + * This invokes when a new service implementation is loaded into the runtime. * * @param service */ @@ -86,118 +73,85 @@ protected void deactivate(ComponentContext context) { cardinality = ReferenceCardinality.MULTIPLE, unbind = "unsetCorrelationLogService" ) - protected void setCorrelationLogService(CorrelationLogService service) { - log.debug("Correlation log service '" + service + "' loaded."); - serviceMap.put(service.getName(), service); - if (service.getConfigDescriptor() != null) { - createAndRegisterMBean(service.getName(), service.getConfigDescriptor()); - } - // Assumption: The each service component only based on the root configs and its own configs. - updateComponentConfig(service); + protected void setCorrelationLogService(CorrelationLogConfigurable service) { + log.debug("Get reference of service implementation '" + service.getName() + "'"); + // Load component-specific configurations from the carbon.xml file. + config.getComponentConfigs() + .put(service.getName(), loadComponentSpecificConfigs(service.getName())); + // Create an immutable configuration object and send it to the service. + service.configurationReceived(getComponentSpecificConfiguration(service.getName())); } - protected void unsetCorrelationLogService(CorrelationLogService service) { - serviceMap.remove(service.getName()); + protected void unsetCorrelationLogService(CorrelationLogConfigurable service) { + // Remove configuration of the service. + config.getComponentConfigs().remove(service.getName()); } /** - * Notifies each service implementation once a configuration is changed in the config map. + * Returns a clone of the CorrelationLogConfig object. * - * @param component Component name - * @param key Field name - * @param value New value + * @return */ @Override - public void configUpdated(String component, String key, Object value) { - for (Map.Entry entry : serviceMap.entrySet()) { - updateComponentConfig(entry.getValue()); - } + public CorrelationLogConfig getConfiguration() { + return config.clone(); } /** - * Merges the root config with the component specific configs and notifies the service component. + * Override the current CorrelationLogConfig object with new configurations. * - * @param service + * @param correlationLogConfig */ - private void updateComponentConfig(CorrelationLogService service) { - // Get root config, append component-wise configs on top of that and invoke the components. - Map componentSpecificConfigMap = new HashMap<>(); - for (String component : new String[] { null, service.getName() }) { - Map map = ConfigMapHolder.getInstance().getConfigMap(component); - if (map != null) { - componentSpecificConfigMap.putAll(map); - } - } - service.reconfigure(componentSpecificConfigMap); + @Override + public void updateConfiguration(CorrelationLogConfig correlationLogConfig) { + log.debug("Correlation log configurations are modified."); + this.config = correlationLogConfig; } /** - * Create a dynamic MBean based on the given attributes and register it. These MBeans are created for the root - * component as well as other service service components. + * Returns root configuration from carbon.xml file. * - * @param componentName Name of the component - * @param attributes Attribute list + * @return */ - private void createAndRegisterMBean(String componentName, CorrelationLogConfigAttribute[] attributes) { - // Create dynamic MBean via the given attributes. This will copy the defaults to the config map. - CorrelationLogConfig mbean = new CorrelationLogConfig(componentName, attributes); - // Override the defaults from the configs defined in the carbon.xml file. - overrideConfigsFromFile(componentName, attributes); - - // Register MBean. - try { - String objectName = getObjectName(); - if (componentName != null) { - objectName += ",name=" + componentName; - } - MBeanRegistrar.registerMBean(mbean, objectName); - log.debug("Registered correlation log MBean for " + componentName + " component."); - } catch (Exception e) { - String msg = "Could not register Correlation Log " + componentName + " MBean"; - log.error(msg, e); - throw new RuntimeException(msg, e); - } + private CorrelationLogConfig loadRootConfigurations() { + boolean enable = Boolean.parseBoolean( + ServerConfiguration.getInstance().getFirstProperty(CorrelationLogConstants.CONFIG_PATH_ENABLE)); + String[] components = CorrelationLogUtil.toArray( + ServerConfiguration.getInstance().getFirstProperty(CorrelationLogConstants.CONFIG_PATH_COMPONENTS)); + String deniedThreadsList = + ServerConfiguration.getInstance().getFirstProperty(CorrelationLogConstants.CONFIG_PATH_DENIED_THREADS); + String[] deniedThreads = deniedThreadsList != null ? + CorrelationLogUtil.toArray(deniedThreadsList) : CorrelationLogConstants.DEFAULT_DENIED_THREADS; + + log.debug("Correlation log configurations are loaded from the carbon.xml file."); + return new CorrelationLogConfig(enable, components, deniedThreads); } /** - * Load default configurations from the Carbon.xml file. + * Returns component-specific configuration from the carbon.xml file. + * + * @param componentName Component name + * @return */ - private void overrideConfigsFromFile(String componentName, CorrelationLogConfigAttribute[] attributes) { - log.debug("Overriding configurations from the carbon.xml file."); - boolean isRootConfig = componentName == null; - for (CorrelationLogConfigAttribute attr : attributes) { - String path = CorrelationLogConstants.CONFIG_ROOT - + (isRootConfig ? "." : ".componentConfigs." + componentName + ".") - + attr.getName(); - String value = ServerConfiguration.getInstance().getFirstProperty(path); - if (value != null) { - // If value is null default will be effective. - if (Boolean.class.getName().equals(attr.getType())) { - ConfigMapHolder.getInstance().setConfig(componentName, attr.getName(), Boolean.parseBoolean(value)); - } else { - ConfigMapHolder.getInstance().setConfig(componentName, attr.getName(), value); - } - } - } + private CorrelationLogComponentConfig loadComponentSpecificConfigs(String componentName) { + String path = CorrelationLogConstants.CONFIG_PATH_COMPONENT_CONFIGS + "." + componentName + ".logAllMethods"; + boolean logAllMethods = Boolean.parseBoolean(ServerConfiguration.getInstance().getFirstProperty(path)); + log.debug("Component-specific configurations for '" + componentName + "' loaded from the carbon.xml file."); + return new CorrelationLogComponentConfig(logAllMethods); } /** - * Get object name for the MBean. - * - * @return The object name + * Returns component-specific configuration from the configuration object. + * @param componentName + * @return */ - private String getObjectName() { - if (objectName == null) { - String serverPackage = ServerConfiguration.getInstance().getFirstProperty("Package"); - if (serverPackage == null) { - serverPackage = "wso2"; - } - String className = CorrelationLogConfig.class.getName(); - if (className.indexOf('.') != -1) { - className = className.substring(className.lastIndexOf('.') + 1); - } - objectName = serverPackage + ":type=" + className; - } - return objectName; + private ImmutableCorrelationLogConfig getComponentSpecificConfiguration(String componentName) { + // Build component specific immutable configuration object + return new ImmutableCorrelationLogConfig( + config.isEnable() && CorrelationLogUtil.isComponentAllowed(componentName, config.getComponents()), + config.getDeniedThreads(), + config.getComponentConfigs().get(componentName).isLogAllMethods()); } + + } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/ConfigMapHolder.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/ConfigMapHolder.java deleted file mode 100644 index 0560c79cac3..00000000000 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/ConfigMapHolder.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.mgt; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.logging.correlation.ConfigObserver; - -import java.util.HashMap; -import java.util.Map; - -/** - * Correlation log configuration holder. - */ -public class ConfigMapHolder { - private static Log log = LogFactory.getLog(ConfigMapHolder.class); - private static ConfigMapHolder instance = new ConfigMapHolder(); - - private ConfigObserver observer = null; - private Map> componentConfigMap; - - private ConfigMapHolder() { - componentConfigMap = new HashMap<>(); - } - - /** - * Get the singleton instance of the ConfigMapHolder class. - * @return - */ - public static ConfigMapHolder getInstance() { - return instance; - } - - /** - * Registers an observer to be invoked when the config is changed. - * - * @param observer - */ - public void onConfigUpdated(ConfigObserver observer) { - this.observer = observer; - } - - /** - * Get complete config map for a component. - * - * @param component Component name - * @return - */ - public Map getConfigMap(String component) { - return componentConfigMap.get(getComponentName(component)); - } - - /** - * Get component specific configuration by key. - * - * @param component Component name - * @param key Config name - * @return - */ - public Object getConfig(String component, String key) { - Map map = componentConfigMap.get(getComponentName(component)); - if (map != null) { - return map.get(key); - } - return null; - } - - /** - * Set config in the map of map. - * - * @param component Component name - * @param key Config name - * @param value Value - */ - public void setConfig(String component, String key, Object value) { - String componentName = getComponentName(component); - if (componentConfigMap.get(componentName) == null) { - componentConfigMap.put(componentName, new HashMap<>()); - } - componentConfigMap.get(componentName).put(key, value); - if (observer != null) { - observer.configUpdated(componentName, key, value); - } - } - - /** - * If the component is null, return it as "root". Else the component name will not be changed. - * - * @param componentName - * @return - */ - private String getComponentName(String componentName) { - if (componentName != null) { - return componentName; - } - return "root"; - } -} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java deleted file mode 100644 index 100a84e1853..00000000000 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/mgt/CorrelationLogConfig.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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 org.wso2.carbon.logging.correlation.mgt; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.logging.correlation.CorrelationLogConfigAttribute; - -import javax.management.Attribute; -import javax.management.AttributeList; -import javax.management.AttributeNotFoundException; -import javax.management.DynamicMBean; -import javax.management.InvalidAttributeValueException; -import javax.management.MBeanAttributeInfo; -import javax.management.MBeanException; -import javax.management.MBeanInfo; -import javax.management.MBeanNotificationInfo; -import javax.management.ReflectionException; - -/** - * Correlatin log dynamic MBean class. - */ -public class CorrelationLogConfig implements DynamicMBean { - private static Log log = LogFactory.getLog(CorrelationLogConfig.class); - - private String componentName; - private CorrelationLogConfigAttribute[] attributes; - private MBeanInfo mBeanInfo; - - public CorrelationLogConfig(String componentName, CorrelationLogConfigAttribute[] attributes) { - this.componentName = componentName; - this.attributes = attributes; - buildDynamicMBean(); - } - - @Override - public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException { - if (attribute == null) { - throw new RuntimeException(new IllegalArgumentException("Attribute name cannot be null.")); - } - - // If the attribute name is valid, return the value from the config map. - if (isValidAttribute(attribute)) { - return ConfigMapHolder.getInstance().getConfig(componentName, attribute); - } - throw new AttributeNotFoundException("Cannot find " + attribute + " attribute"); - } - - @Override - public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { - if (attribute == null) { - throw new RuntimeException(new IllegalArgumentException("Attribute name cannot be null.")); - } - - // If the attribute name is valid, set the value to the config map. - if (isValidAttribute(attribute.getName())) { - ConfigMapHolder.getInstance().setConfig(componentName, attribute.getName(), attribute.getValue()); - return; - } - throw new AttributeNotFoundException("Cannot find " + attribute + " attribute"); - } - - @Override - public AttributeList getAttributes(String[] attributes) { - return null; - } - - @Override - public AttributeList setAttributes(AttributeList attributes) { - return null; - } - - @Override - public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException { - return null; - } - - @Override - public MBeanInfo getMBeanInfo() { - return mBeanInfo; - } - - /** - * Iterate each - */ - private void buildDynamicMBean() { - MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[attributes.length]; - for (int i = 0; i < attributes.length; i++) { - attributeInfos[i] = new MBeanAttributeInfo( - attributes[i].getName(), - attributes[i].getType(), - attributes[i].getDescription(), - true, - true, - attributes[i].getType().equals(Boolean.class.getName())); - - // Add the default value into the config map. - ConfigMapHolder.getInstance().setConfig(componentName, attributes[i].getName(), - attributes[i].getDefaultValue()); - } - mBeanInfo = new MBeanInfo(this.getClass().getName(), "", attributeInfos, null, null, new MBeanNotificationInfo[0]); - } - - /** - * Check whether the provided attributes is valid by validating it against the given attribute list. - * - * @param attribute - * @return - */ - private boolean isValidAttribute(String attribute) { - for (CorrelationLogConfigAttribute attr : attributes) { - if (attr.getName().equals(attribute)) { - return true; - } - } - log.debug("Invalid attribute name " + attribute + " in the component " + componentName); - return false; - } -} diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java index 97dc1d2a414..333308f13af 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogConstants.java @@ -23,14 +23,11 @@ */ public class CorrelationLogConstants { - // Configuration field names - public static final String ENABLE = "enable"; - public static final String COMPONENTS = "components"; - public static final String LOG_ALL_METHODS = "logAllMethods"; - public static final String DENIED_THREADS = "deniedThreads"; - // Config paths in Carbon.xml file - public static final String CONFIG_ROOT = "CorrelationLogs"; + public static final String CONFIG_PATH_ENABLE = "CorrelationLogs.enable"; + public static final String CONFIG_PATH_COMPONENTS = "CorrelationLogs.components"; + public static final String CONFIG_PATH_DENIED_THREADS = "CorrelationLogs.deniedThreads"; + public static final String CONFIG_PATH_COMPONENT_CONFIGS = "CorrelationLogs.componentConfigs"; // Defaults public static final String[] DEFAULT_DENIED_THREADS = { diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java index b0db0b142e8..ebacb678eec 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/utils/CorrelationLogUtil.java @@ -18,6 +18,8 @@ package org.wso2.carbon.logging.correlation.utils; +import org.wso2.carbon.utils.xml.StringUtils; + import java.util.Arrays; /** @@ -26,24 +28,30 @@ public class CorrelationLogUtil { /** - * Convert comma separated component list into an array. + * Convert comma-separated string into an array. * - * @param componentList Comma separated list - * @return Array of components + * @param string Comma-separated string + * @return */ - public static String[] toComponentArray(String componentList) { - return componentList.split("\\s*,\\s*"); + public static String[] toArray(String string) { + if (StringUtils.isEmpty(string)) { + return new String[0]; + } + String[] arr = string.split(","); + for (int i = 0; i < arr.length; i++) { + arr[i] = arr[i].trim(); + } + return arr; } /** - * Check whether the given component exists in the provided component list. + * Check if the given component is existing is the components array. * * @param component Component name - * @param componentList Comma separated list of components + * @param components Components array * @return */ - public static boolean isComponentAllowed(String component, String componentList) { - return "".equals(componentList) || - Arrays.stream(toComponentArray(componentList)).anyMatch(s -> s.equals(component)); + public static boolean isComponentAllowed(String component, String[] components) { + return Arrays.stream(components).anyMatch(s -> s.equals(component)); } } diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java new file mode 100644 index 00000000000..8e0b61eb282 --- /dev/null +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.ndatasource.rdbms; + +import org.osgi.service.component.annotations.Component; +import org.wso2.carbon.logging.correlation.CorrelationLogConfigurable; +import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; + +@Component( + immediate = true, + service = CorrelationLogConfigurable.class +) +public class ConfigurableCorrelationLogService implements CorrelationLogConfigurable { + private static boolean enable; + private static String[] deniedThreads; + + @Override + public String getName() { + return "jdbc"; + } + + @Override + public void configurationReceived(ImmutableCorrelationLogConfig config) { + enable = config.isEnable(); + deniedThreads = config.getDeniedThreads(); + } + + public static boolean isEnable() { + return enable; + } + + public static String[] getDeniedThreads() { + return deniedThreads; + } +} diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java index 0d8618bded2..a3c1efbeda1 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java @@ -21,15 +21,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport; -import org.osgi.service.component.ComponentContext; -import org.osgi.service.component.annotations.Activate; -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.annotations.Deactivate; -import org.wso2.carbon.logging.correlation.CorrelationLogConfigAttribute; -import org.wso2.carbon.logging.correlation.CorrelationLogService; -import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; -import org.wso2.carbon.logging.correlation.utils.CorrelationLogUtil; -import org.wso2.carbon.utils.xml.StringUtils; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; @@ -38,27 +29,18 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import java.util.Map; /** * Time-Logging interceptor for JDBC pool. * Logs the time taken to execute the query in each pool-ed connection. */ -@Component( - immediate = true, - service = CorrelationLogService.class -) -public class CorrelationLogInterceptor extends AbstractQueryReport implements CorrelationLogService { +public class CorrelationLogInterceptor extends AbstractQueryReport { private static final Log correlationLog = LogFactory.getLog("correlation"); private static final Log log = LogFactory.getLog(CorrelationLogInterceptor.class); private static final String CORRELATION_LOG_CALL_TYPE_VALUE = "jdbc"; private static final String CORRELATION_LOG_SEPARATOR = "|"; - public static final String DENIED_THREADS = "deniedThreads"; - private static List deniedThreadList = new ArrayList<>(); - private static boolean isEnableCorrelationLogs; @Override public void closeInvoked() { @@ -78,7 +60,7 @@ protected void prepareCall(String s, long l) { @Override public Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time) { try { - if (isEnableCorrelationLogs) { + if (ConfigurableCorrelationLogService.isEnable()) { return invokeProxy(method, args, statement, time); } else { return statement; @@ -112,45 +94,6 @@ private Object invokeProxy(Method method, Object[] args, Object statement, long } } - @Activate - protected void activate(ComponentContext context) { - } - - @Deactivate - protected void deactivate(ComponentContext context) { - } - - /** - * Returns the name of the service component as "jdbc". - * - * @return - */ - @Override - public String getName() { - return "jdbc"; - } - - /** - * Reconfigure the interceptor based on the configurations received. - * - * @param properties Map of configurations - */ - @Override - public void reconfigure(Map properties) { - String deniedThreadNames = (String) properties.get(DENIED_THREADS); - deniedThreadList.clear(); - deniedThreadList.addAll(Arrays.asList(StringUtils.split(deniedThreadNames, ','))); - - isEnableCorrelationLogs = (Boolean) properties.get(CorrelationLogConstants.ENABLE) && - CorrelationLogUtil.isComponentAllowed(this.getName(), - (String) properties.get(CorrelationLogConstants.COMPONENTS)); - } - - @Override - public CorrelationLogConfigAttribute[] getConfigDescriptor() { - return null; - } - /** * * Proxy Class that is used to calculate and log the time taken for queries @@ -241,7 +184,7 @@ private void logQueryDetails(long start, long delta, String methodName) throws S private boolean isCurrentThreadBlacklisted() { String threadName = Thread.currentThread().getName(); - for (String thread : deniedThreadList) { + for (String thread : ConfigurableCorrelationLogService.getDeniedThreads()) { if (threadName.startsWith(thread)) { return true; } diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java new file mode 100644 index 00000000000..0f9bf06fbe5 --- /dev/null +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.tomcat.ext.service; + +import org.osgi.service.component.annotations.Component; +import org.wso2.carbon.logging.correlation.CorrelationLogConfigurable; +import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; + +@Component( + immediate = true, + service = CorrelationLogConfigurable.class +) +public class ConfigurableCorrelationLogService implements CorrelationLogConfigurable { + private static boolean enable; + + @Override + public String getName() { + return "http"; + } + + @Override + public void configurationReceived(ImmutableCorrelationLogConfig config) { + enable = config.isEnable(); + } + + public static boolean isEnable() { + return enable; + } +} diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java index 0226c3f7076..0877fb89707 100644 --- a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java @@ -30,13 +30,12 @@ import org.apache.log4j.MDC; import org.osgi.service.component.ComponentContext; import org.osgi.service.component.annotations.Activate; -import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; -import org.wso2.carbon.logging.correlation.CorrelationLogConfigAttribute; -import org.wso2.carbon.logging.correlation.CorrelationLogService; -import org.wso2.carbon.logging.correlation.utils.CorrelationLogConstants; -import org.wso2.carbon.logging.correlation.utils.CorrelationLogUtil; +import org.wso2.carbon.tomcat.ext.service.ConfigurableCorrelationLogService; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.net.URLDecoder; import java.util.ArrayList; @@ -45,9 +44,6 @@ import java.util.List; import java.util.Map; import java.util.UUID; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.http.HttpServletRequest; /** * Tomcat valve which adds MDC from a header value received. @@ -61,11 +57,7 @@ * queryToCorrelationIdMapping={'RelayState':'Correlation-ID'} * */ -@Component( - immediate = true, - service = CorrelationLogService.class -) -public class RequestCorrelationIdValve extends ValveBase implements CorrelationLogService { +public class RequestCorrelationIdValve extends ValveBase { private static final Log correlationLog = LogFactory.getLog("correlation"); private static final String CORRELATION_ID_MDC = "Correlation-ID"; @@ -83,7 +75,6 @@ public class RequestCorrelationIdValve extends ValveBase implements CorrelationL private static final String CORRELATION_LOG_REQUEST_END = "HTTP-In-Response"; private static final String PADDING_CHAR = "="; private static final String SPLITTING_CHAR = "&"; - private static boolean isEnableCorrelationLogs; @Override protected void initInternal() throws LifecycleException { @@ -125,7 +116,7 @@ public void invoke(Request request, Response response) throws IOException, Servl // Associates the generated Correlation ID Mapping to the request so that, // it will be available for any other asynchronous flows or threads spawned for this request request.setAttribute(CORRELATION_ID_MDC_REQUEST_ATTRIBUTE_NAME, associateToThreadMap); - if (isEnableCorrelationLogs) { + if (ConfigurableCorrelationLogService.isEnable()) { long currentTime = System.currentTimeMillis(); long timeTaken = currentTime - requestStartTime; logRequestDetails(currentTime, timeTaken, CORRELATION_LOG_REQUEST_START, request); @@ -135,7 +126,7 @@ public void invoke(Request request, Response response) throws IOException, Servl getNext().invoke(request, response); } } finally { - if (isEnableCorrelationLogs) { + if (ConfigurableCorrelationLogService.isEnable()) { long currentTime = System.currentTimeMillis(); long timeTaken = currentTime - requestStartTime; logRequestDetails(currentTime, timeTaken, CORRELATION_LOG_REQUEST_END, request); @@ -368,31 +359,4 @@ public void setQueryToCorrelationIdMapping(String queryToCorrelationIdMapping) { public void setConfiguredCorrelationIdMdc(String configuredCorrelationIdMdc) { this.configuredCorrelationIdMdc = configuredCorrelationIdMdc; } - - /** - * Returns the name of the service component as "http". - * - * @return - */ - @Override - public String getName() { - return "http"; - } - - /** - * Reconfigure the interceptor based on the configurations received. - * - * @param properties Map of configurations - */ - @Override - public void reconfigure(Map properties) { - isEnableCorrelationLogs = (boolean) properties.get(CorrelationLogConstants.ENABLE) && - CorrelationLogUtil.isComponentAllowed(this.getName(), - (String) properties.get(CorrelationLogConstants.COMPONENTS)); - } - - @Override - public CorrelationLogConfigAttribute[] getConfigDescriptor() { - return null; - } } diff --git a/core/org.wso2.carbon.user.core/pom.xml b/core/org.wso2.carbon.user.core/pom.xml index 23ddf48d40d..5ece8b40a97 100644 --- a/core/org.wso2.carbon.user.core/pom.xml +++ b/core/org.wso2.carbon.user.core/pom.xml @@ -142,6 +142,10 @@ org.wso2.carbon.crypto org.wso2.carbon.crypto.api + + org.wso2.carbon + org.wso2.carbon.logging.correlation + diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java new file mode 100644 index 00000000000..72bf75710eb --- /dev/null +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java @@ -0,0 +1,46 @@ +/* + * + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.user.core.ldap; + +import org.osgi.service.component.annotations.Component; +import org.wso2.carbon.logging.correlation.CorrelationLogConfigurable; +import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; + +@Component( + immediate = true, + service = CorrelationLogConfigurable.class +) +public class ConfigurableCorrelationLogService implements CorrelationLogConfigurable { + private static boolean enable; + + @Override + public String getName() { + return "ldap"; + } + + @Override + public void configurationReceived(ImmutableCorrelationLogConfig config) { + enable = config.isEnable(); + } + + public static boolean isEnable() { + return enable; + } +} diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java index f951ce6c3c6..3ca7629afcc 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java @@ -77,7 +77,6 @@ public class LDAPConnectionContext { private static final String CORRELATION_LOG_INITIALIZATION_ARGS = "empty"; private static final int CORRELATION_LOG_INITIALIZATION_ARGS_LENGTH = 0; private static final String CORRELATION_LOG_SEPARATOR = "|"; - private static final String CORRELATION_LOG_SYSTEM_PROPERTY = "enableCorrelationLogs"; public static final String CIRCUIT_STATE_OPEN = "open"; public static final String CIRCUIT_STATE_CLOSE = "close"; @@ -640,8 +639,7 @@ private LdapContext getContextForEnvironmentVariables(Hashtable environmen * @throws NamingException */ private DirContext getDirContext(Hashtable environment) throws NamingException { - - if (Boolean.parseBoolean(System.getProperty(CORRELATION_LOG_SYSTEM_PROPERTY))) { + if (ConfigurableCorrelationLogService.isEnable()) { final Class[] proxyInterfaces = new Class[]{DirContext.class}; long start = System.currentTimeMillis(); @@ -677,8 +675,7 @@ private DirContext getDirContext(Hashtable environment) throws NamingExcep */ private LdapContext getLdapContext(Hashtable environment, Control[] connectionControls) throws NamingException, UserStoreException { - - if (Boolean.parseBoolean(System.getProperty(CORRELATION_LOG_SYSTEM_PROPERTY))) { + if (ConfigurableCorrelationLogService.isEnable()) { final Class[] proxyInterfaces = new Class[]{LdapContext.class}; long start = System.currentTimeMillis(); From 1cc48996535a3849d04bd484555817255d2859a4 Mon Sep 17 00:00:00 2001 From: lasanthaS Date: Wed, 8 Sep 2021 11:50:22 +0530 Subject: [PATCH 08/11] Rename correlation log interface --- core/org.wso2.carbon.logging.correlation/pom.xml | 14 -------------- ...fiable.java => CorrelationLogConfigurator.java} | 2 +- .../internal/CorrelationLogManager.java | 8 +++----- 3 files changed, 4 insertions(+), 20 deletions(-) rename core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/{CorrelationLogConfigModifiable.java => CorrelationLogConfigurator.java} (96%) diff --git a/core/org.wso2.carbon.logging.correlation/pom.xml b/core/org.wso2.carbon.logging.correlation/pom.xml index 9a723f08048..c169cfdb7fb 100644 --- a/core/org.wso2.carbon.logging.correlation/pom.xml +++ b/core/org.wso2.carbon.logging.correlation/pom.xml @@ -75,8 +75,6 @@ ${project.artifactId} ${project.artifactId} - - !org.wso2.carbon.logging.correlation.internal.*, org.wso2.carbon.logging.correlation.*, @@ -84,18 +82,6 @@ *;resolution:=optional, - - - - - - - - - - - - * diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigModifiable.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigurator.java similarity index 96% rename from core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigModifiable.java rename to core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigurator.java index a1ff7be9072..a6d19f0ca15 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigModifiable.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigurator.java @@ -25,7 +25,7 @@ * The class definition which needs to be implemented to override correlation log configurations dynamically in the * runtime. */ -public interface CorrelationLogConfigModifiable { +public interface CorrelationLogConfigurator { /** * Returns an instance of the CorrelationLogConfig class which represents effective entire diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java index e0d21f415cd..399b74fb0c8 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java @@ -28,7 +28,7 @@ import org.osgi.service.component.annotations.ReferenceCardinality; import org.osgi.service.component.annotations.ReferencePolicy; import org.wso2.carbon.base.ServerConfiguration; -import org.wso2.carbon.logging.correlation.CorrelationLogConfigModifiable; +import org.wso2.carbon.logging.correlation.CorrelationLogConfigurator; import org.wso2.carbon.logging.correlation.CorrelationLogConfigurable; import org.wso2.carbon.logging.correlation.bean.CorrelationLogComponentConfig; import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; @@ -42,8 +42,8 @@ */ @Component( immediate = true, - service = CorrelationLogConfigModifiable.class) -public class CorrelationLogManager implements CorrelationLogConfigModifiable { + service = CorrelationLogConfigurator.class) +public class CorrelationLogManager implements CorrelationLogConfigurator { private static Log log = LogFactory.getLog(CorrelationLogManager.class); private CorrelationLogConfig config; @@ -152,6 +152,4 @@ private ImmutableCorrelationLogConfig getComponentSpecificConfiguration(String c config.getDeniedThreads(), config.getComponentConfigs().get(componentName).isLogAllMethods()); } - - } From 40122cd38af586ba7df25019ddf69ed90279d895 Mon Sep 17 00:00:00 2001 From: lasanthaS Date: Thu, 9 Sep 2021 10:34:03 +0530 Subject: [PATCH 09/11] Rename method to onConfigure --- .../carbon/logging/correlation/CorrelationLogConfigurable.java | 2 +- .../logging/correlation/internal/CorrelationLogManager.java | 2 +- .../ndatasource/rdbms/ConfigurableCorrelationLogService.java | 2 +- .../tomcat/ext/service/ConfigurableCorrelationLogService.java | 2 +- .../user/core/ldap/ConfigurableCorrelationLogService.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigurable.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigurable.java index cc1d023d66e..817a728baa1 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigurable.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/CorrelationLogConfigurable.java @@ -38,5 +38,5 @@ public interface CorrelationLogConfigurable { * * @param config */ - void configurationReceived(ImmutableCorrelationLogConfig config); + void onConfigure(ImmutableCorrelationLogConfig config); } diff --git a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java index 399b74fb0c8..ec092a4d3c3 100644 --- a/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java +++ b/core/org.wso2.carbon.logging.correlation/src/main/java/org/wso2/carbon/logging/correlation/internal/CorrelationLogManager.java @@ -79,7 +79,7 @@ protected void setCorrelationLogService(CorrelationLogConfigurable service) { config.getComponentConfigs() .put(service.getName(), loadComponentSpecificConfigs(service.getName())); // Create an immutable configuration object and send it to the service. - service.configurationReceived(getComponentSpecificConfiguration(service.getName())); + service.onConfigure(getComponentSpecificConfiguration(service.getName())); } protected void unsetCorrelationLogService(CorrelationLogConfigurable service) { diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java index 8e0b61eb282..f6869ef35cb 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java @@ -36,7 +36,7 @@ public String getName() { } @Override - public void configurationReceived(ImmutableCorrelationLogConfig config) { + public void onConfigure(ImmutableCorrelationLogConfig config) { enable = config.isEnable(); deniedThreads = config.getDeniedThreads(); } diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java index 0f9bf06fbe5..e9446d7439d 100644 --- a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java @@ -35,7 +35,7 @@ public String getName() { } @Override - public void configurationReceived(ImmutableCorrelationLogConfig config) { + public void onConfigure(ImmutableCorrelationLogConfig config) { enable = config.isEnable(); } diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java index 72bf75710eb..22c24eda38e 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java @@ -36,7 +36,7 @@ public String getName() { } @Override - public void configurationReceived(ImmutableCorrelationLogConfig config) { + public void onConfigure(ImmutableCorrelationLogConfig config) { enable = config.isEnable(); } From 98679728dd9469e2807fc7493fc1b549b2454a13 Mon Sep 17 00:00:00 2001 From: lasanthaS Date: Wed, 15 Sep 2021 13:00:25 +0530 Subject: [PATCH 10/11] Refactor config data holders --- .../org.wso2.carbon.ndatasource.rdbms/pom.xml | 1 + .../ConfigurableCorrelationLogService.java | 15 ++----- .../rdbms/CorrelationLogInterceptor.java | 5 ++- .../internal/CorrelationConfigDataHolder.java | 43 +++++++++++++++++++ .../internal/CorrelationConfigDataHolder.java | 34 +++++++++++++++ .../ConfigurableCorrelationLogService.java | 9 +--- .../ext/valves/RequestCorrelationIdValve.java | 5 ++- .../internal/CorrelationConfigDataHolder.java | 34 +++++++++++++++ .../ConfigurableCorrelationLogService.java | 9 +--- .../user/core/ldap/LDAPConnectionContext.java | 5 ++- 10 files changed, 128 insertions(+), 32 deletions(-) create mode 100644 core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/internal/CorrelationConfigDataHolder.java create mode 100644 core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/internal/CorrelationConfigDataHolder.java create mode 100644 core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/internal/CorrelationConfigDataHolder.java diff --git a/core/org.wso2.carbon.ndatasource.rdbms/pom.xml b/core/org.wso2.carbon.ndatasource.rdbms/pom.xml index 81acdb682b0..74382ca75ea 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/pom.xml +++ b/core/org.wso2.carbon.ndatasource.rdbms/pom.xml @@ -87,6 +87,7 @@ *;resolution:=optional + !org.wso2.carbon.ndatasource.rdbms.internal.*, org.wso2.carbon.ndatasource.rdbms.* * diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java index f6869ef35cb..c6c023cabcd 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java @@ -21,14 +21,13 @@ import org.osgi.service.component.annotations.Component; import org.wso2.carbon.logging.correlation.CorrelationLogConfigurable; import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; +import org.wso2.carbon.ndatasource.rdbms.internal.CorrelationConfigDataHolder; @Component( immediate = true, service = CorrelationLogConfigurable.class ) public class ConfigurableCorrelationLogService implements CorrelationLogConfigurable { - private static boolean enable; - private static String[] deniedThreads; @Override public String getName() { @@ -37,15 +36,7 @@ public String getName() { @Override public void onConfigure(ImmutableCorrelationLogConfig config) { - enable = config.isEnable(); - deniedThreads = config.getDeniedThreads(); - } - - public static boolean isEnable() { - return enable; - } - - public static String[] getDeniedThreads() { - return deniedThreads; + CorrelationConfigDataHolder.setEnable(config.isEnable()); + CorrelationConfigDataHolder.setDeniedThreads(config.getDeniedThreads()); } } diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java index a3c1efbeda1..6d7dbe10859 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport; +import org.wso2.carbon.ndatasource.rdbms.internal.CorrelationConfigDataHolder; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; @@ -60,7 +61,7 @@ protected void prepareCall(String s, long l) { @Override public Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time) { try { - if (ConfigurableCorrelationLogService.isEnable()) { + if (CorrelationConfigDataHolder.isEnable()) { return invokeProxy(method, args, statement, time); } else { return statement; @@ -184,7 +185,7 @@ private void logQueryDetails(long start, long delta, String methodName) throws S private boolean isCurrentThreadBlacklisted() { String threadName = Thread.currentThread().getName(); - for (String thread : ConfigurableCorrelationLogService.getDeniedThreads()) { + for (String thread : CorrelationConfigDataHolder.getDeniedThreads()) { if (threadName.startsWith(thread)) { return true; } diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/internal/CorrelationConfigDataHolder.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/internal/CorrelationConfigDataHolder.java new file mode 100644 index 00000000000..9006b456c56 --- /dev/null +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/internal/CorrelationConfigDataHolder.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.ndatasource.rdbms.internal; + +public class CorrelationConfigDataHolder { + private static boolean enable; + private static String[] deniedThreads; + + private CorrelationConfigDataHolder() { + } + + public static boolean isEnable() { + return enable; + } + + public static void setEnable(boolean enable) { + CorrelationConfigDataHolder.enable = enable; + } + + public static String[] getDeniedThreads() { + return deniedThreads; + } + + public static void setDeniedThreads(String[] deniedThreads) { + CorrelationConfigDataHolder.deniedThreads = deniedThreads; + } +} diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/internal/CorrelationConfigDataHolder.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/internal/CorrelationConfigDataHolder.java new file mode 100644 index 00000000000..d9bb7fbe3a9 --- /dev/null +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/internal/CorrelationConfigDataHolder.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.tomcat.ext.internal; + +public class CorrelationConfigDataHolder { + private static boolean enable; + + private CorrelationConfigDataHolder() { + } + + public static boolean isEnable() { + return enable; + } + + public static void setEnable(boolean enable) { + CorrelationConfigDataHolder.enable = enable; + } +} diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java index e9446d7439d..f0fe20ca02b 100644 --- a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java @@ -21,14 +21,13 @@ import org.osgi.service.component.annotations.Component; import org.wso2.carbon.logging.correlation.CorrelationLogConfigurable; import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; +import org.wso2.carbon.tomcat.ext.internal.CorrelationConfigDataHolder; @Component( immediate = true, service = CorrelationLogConfigurable.class ) public class ConfigurableCorrelationLogService implements CorrelationLogConfigurable { - private static boolean enable; - @Override public String getName() { return "http"; @@ -36,10 +35,6 @@ public String getName() { @Override public void onConfigure(ImmutableCorrelationLogConfig config) { - enable = config.isEnable(); - } - - public static boolean isEnable() { - return enable; + CorrelationConfigDataHolder.setEnable(config.isEnable()); } } diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java index 0877fb89707..aede20d5c9f 100644 --- a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java @@ -31,6 +31,7 @@ import org.osgi.service.component.ComponentContext; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Deactivate; +import org.wso2.carbon.tomcat.ext.internal.CorrelationConfigDataHolder; import org.wso2.carbon.tomcat.ext.service.ConfigurableCorrelationLogService; import javax.servlet.ServletException; @@ -116,7 +117,7 @@ public void invoke(Request request, Response response) throws IOException, Servl // Associates the generated Correlation ID Mapping to the request so that, // it will be available for any other asynchronous flows or threads spawned for this request request.setAttribute(CORRELATION_ID_MDC_REQUEST_ATTRIBUTE_NAME, associateToThreadMap); - if (ConfigurableCorrelationLogService.isEnable()) { + if (CorrelationConfigDataHolder.isEnable()) { long currentTime = System.currentTimeMillis(); long timeTaken = currentTime - requestStartTime; logRequestDetails(currentTime, timeTaken, CORRELATION_LOG_REQUEST_START, request); @@ -126,7 +127,7 @@ public void invoke(Request request, Response response) throws IOException, Servl getNext().invoke(request, response); } } finally { - if (ConfigurableCorrelationLogService.isEnable()) { + if (CorrelationConfigDataHolder.isEnable()) { long currentTime = System.currentTimeMillis(); long timeTaken = currentTime - requestStartTime; logRequestDetails(currentTime, timeTaken, CORRELATION_LOG_REQUEST_END, request); diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/internal/CorrelationConfigDataHolder.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/internal/CorrelationConfigDataHolder.java new file mode 100644 index 00000000000..842bf46de74 --- /dev/null +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/internal/CorrelationConfigDataHolder.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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 org.wso2.carbon.user.core.internal; + +public class CorrelationConfigDataHolder { + private static boolean enable; + + private CorrelationConfigDataHolder() { + } + + public static boolean isEnable() { + return enable; + } + + public static void setEnable(boolean enable) { + CorrelationConfigDataHolder.enable = enable; + } +} diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java index 22c24eda38e..ee64614dd4d 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java @@ -22,14 +22,13 @@ import org.osgi.service.component.annotations.Component; import org.wso2.carbon.logging.correlation.CorrelationLogConfigurable; import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; +import org.wso2.carbon.user.core.internal.CorrelationConfigDataHolder; @Component( immediate = true, service = CorrelationLogConfigurable.class ) public class ConfigurableCorrelationLogService implements CorrelationLogConfigurable { - private static boolean enable; - @Override public String getName() { return "ldap"; @@ -37,10 +36,6 @@ public String getName() { @Override public void onConfigure(ImmutableCorrelationLogConfig config) { - enable = config.isEnable(); - } - - public static boolean isEnable() { - return enable; + CorrelationConfigDataHolder.setEnable(config.isEnable()); } } diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java index 3ca7629afcc..958cc0b22be 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java @@ -26,6 +26,7 @@ import org.wso2.carbon.user.core.UserStoreConfigConstants; import org.wso2.carbon.user.core.UserStoreException; import org.wso2.carbon.user.core.dto.CorrelationLogDTO; +import org.wso2.carbon.user.core.internal.CorrelationConfigDataHolder; import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.Secret; import org.wso2.carbon.utils.UnsupportedSecretTypeException; @@ -639,7 +640,7 @@ private LdapContext getContextForEnvironmentVariables(Hashtable environmen * @throws NamingException */ private DirContext getDirContext(Hashtable environment) throws NamingException { - if (ConfigurableCorrelationLogService.isEnable()) { + if (CorrelationConfigDataHolder.isEnable()) { final Class[] proxyInterfaces = new Class[]{DirContext.class}; long start = System.currentTimeMillis(); @@ -675,7 +676,7 @@ private DirContext getDirContext(Hashtable environment) throws NamingExcep */ private LdapContext getLdapContext(Hashtable environment, Control[] connectionControls) throws NamingException, UserStoreException { - if (ConfigurableCorrelationLogService.isEnable()) { + if (CorrelationConfigDataHolder.isEnable()) { final Class[] proxyInterfaces = new Class[]{LdapContext.class}; long start = System.currentTimeMillis(); From 23eb811332d961a228ea947b84ea88cf53e1bff8 Mon Sep 17 00:00:00 2001 From: lasanthaS Date: Fri, 17 Sep 2021 12:27:50 +0530 Subject: [PATCH 11/11] Rename service classes --- .../ndatasource/rdbms/CorrelationLogInterceptor.java | 6 +++--- ...ce.java => JDBCConfigurableCorrelationLogService.java} | 8 ++++---- ...taHolder.java => JDBCCorrelationConfigDataHolder.java} | 8 ++++---- ...taHolder.java => HTTPCorrelationConfigDataHolder.java} | 6 +++--- ...ce.java => HTTPConfigurableCorrelationLogService.java} | 6 +++--- .../tomcat/ext/valves/RequestCorrelationIdValve.java | 7 +++---- ...taHolder.java => LDAPCorrelationConfigDataHolder.java} | 6 +++--- ...ce.java => LDAPConfigurableCorrelationLogService.java} | 6 +++--- .../wso2/carbon/user/core/ldap/LDAPConnectionContext.java | 6 +++--- 9 files changed, 29 insertions(+), 30 deletions(-) rename core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/{ConfigurableCorrelationLogService.java => JDBCConfigurableCorrelationLogService.java} (78%) rename core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/internal/{CorrelationConfigDataHolder.java => JDBCCorrelationConfigDataHolder.java} (83%) rename core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/internal/{CorrelationConfigDataHolder.java => HTTPCorrelationConfigDataHolder.java} (85%) rename core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/{ConfigurableCorrelationLogService.java => HTTPConfigurableCorrelationLogService.java} (83%) rename core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/internal/{CorrelationConfigDataHolder.java => LDAPCorrelationConfigDataHolder.java} (85%) rename core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/{ConfigurableCorrelationLogService.java => LDAPConfigurableCorrelationLogService.java} (83%) diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java index 6d7dbe10859..b2f7a8a3515 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/CorrelationLogInterceptor.java @@ -21,7 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport; -import org.wso2.carbon.ndatasource.rdbms.internal.CorrelationConfigDataHolder; +import org.wso2.carbon.ndatasource.rdbms.internal.JDBCCorrelationConfigDataHolder; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; @@ -61,7 +61,7 @@ protected void prepareCall(String s, long l) { @Override public Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time) { try { - if (CorrelationConfigDataHolder.isEnable()) { + if (JDBCCorrelationConfigDataHolder.isEnable()) { return invokeProxy(method, args, statement, time); } else { return statement; @@ -185,7 +185,7 @@ private void logQueryDetails(long start, long delta, String methodName) throws S private boolean isCurrentThreadBlacklisted() { String threadName = Thread.currentThread().getName(); - for (String thread : CorrelationConfigDataHolder.getDeniedThreads()) { + for (String thread : JDBCCorrelationConfigDataHolder.getDeniedThreads()) { if (threadName.startsWith(thread)) { return true; } diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/JDBCConfigurableCorrelationLogService.java similarity index 78% rename from core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java rename to core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/JDBCConfigurableCorrelationLogService.java index c6c023cabcd..5b1378c156c 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/ConfigurableCorrelationLogService.java +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/JDBCConfigurableCorrelationLogService.java @@ -21,13 +21,13 @@ import org.osgi.service.component.annotations.Component; import org.wso2.carbon.logging.correlation.CorrelationLogConfigurable; import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; -import org.wso2.carbon.ndatasource.rdbms.internal.CorrelationConfigDataHolder; +import org.wso2.carbon.ndatasource.rdbms.internal.JDBCCorrelationConfigDataHolder; @Component( immediate = true, service = CorrelationLogConfigurable.class ) -public class ConfigurableCorrelationLogService implements CorrelationLogConfigurable { +public class JDBCConfigurableCorrelationLogService implements CorrelationLogConfigurable { @Override public String getName() { @@ -36,7 +36,7 @@ public String getName() { @Override public void onConfigure(ImmutableCorrelationLogConfig config) { - CorrelationConfigDataHolder.setEnable(config.isEnable()); - CorrelationConfigDataHolder.setDeniedThreads(config.getDeniedThreads()); + JDBCCorrelationConfigDataHolder.setEnable(config.isEnable()); + JDBCCorrelationConfigDataHolder.setDeniedThreads(config.getDeniedThreads()); } } diff --git a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/internal/CorrelationConfigDataHolder.java b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/internal/JDBCCorrelationConfigDataHolder.java similarity index 83% rename from core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/internal/CorrelationConfigDataHolder.java rename to core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/internal/JDBCCorrelationConfigDataHolder.java index 9006b456c56..2cd5ebfd11f 100644 --- a/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/internal/CorrelationConfigDataHolder.java +++ b/core/org.wso2.carbon.ndatasource.rdbms/src/main/java/org/wso2/carbon/ndatasource/rdbms/internal/JDBCCorrelationConfigDataHolder.java @@ -18,11 +18,11 @@ package org.wso2.carbon.ndatasource.rdbms.internal; -public class CorrelationConfigDataHolder { +public class JDBCCorrelationConfigDataHolder { private static boolean enable; private static String[] deniedThreads; - private CorrelationConfigDataHolder() { + private JDBCCorrelationConfigDataHolder() { } public static boolean isEnable() { @@ -30,7 +30,7 @@ public static boolean isEnable() { } public static void setEnable(boolean enable) { - CorrelationConfigDataHolder.enable = enable; + JDBCCorrelationConfigDataHolder.enable = enable; } public static String[] getDeniedThreads() { @@ -38,6 +38,6 @@ public static String[] getDeniedThreads() { } public static void setDeniedThreads(String[] deniedThreads) { - CorrelationConfigDataHolder.deniedThreads = deniedThreads; + JDBCCorrelationConfigDataHolder.deniedThreads = deniedThreads; } } diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/internal/CorrelationConfigDataHolder.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/internal/HTTPCorrelationConfigDataHolder.java similarity index 85% rename from core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/internal/CorrelationConfigDataHolder.java rename to core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/internal/HTTPCorrelationConfigDataHolder.java index d9bb7fbe3a9..ba3604aaa2b 100644 --- a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/internal/CorrelationConfigDataHolder.java +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/internal/HTTPCorrelationConfigDataHolder.java @@ -18,10 +18,10 @@ package org.wso2.carbon.tomcat.ext.internal; -public class CorrelationConfigDataHolder { +public class HTTPCorrelationConfigDataHolder { private static boolean enable; - private CorrelationConfigDataHolder() { + private HTTPCorrelationConfigDataHolder() { } public static boolean isEnable() { @@ -29,6 +29,6 @@ public static boolean isEnable() { } public static void setEnable(boolean enable) { - CorrelationConfigDataHolder.enable = enable; + HTTPCorrelationConfigDataHolder.enable = enable; } } diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/HTTPConfigurableCorrelationLogService.java similarity index 83% rename from core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java rename to core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/HTTPConfigurableCorrelationLogService.java index f0fe20ca02b..bd54c8ae9a4 100644 --- a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/ConfigurableCorrelationLogService.java +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/service/HTTPConfigurableCorrelationLogService.java @@ -21,13 +21,13 @@ import org.osgi.service.component.annotations.Component; import org.wso2.carbon.logging.correlation.CorrelationLogConfigurable; import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; -import org.wso2.carbon.tomcat.ext.internal.CorrelationConfigDataHolder; +import org.wso2.carbon.tomcat.ext.internal.HTTPCorrelationConfigDataHolder; @Component( immediate = true, service = CorrelationLogConfigurable.class ) -public class ConfigurableCorrelationLogService implements CorrelationLogConfigurable { +public class HTTPConfigurableCorrelationLogService implements CorrelationLogConfigurable { @Override public String getName() { return "http"; @@ -35,6 +35,6 @@ public String getName() { @Override public void onConfigure(ImmutableCorrelationLogConfig config) { - CorrelationConfigDataHolder.setEnable(config.isEnable()); + HTTPCorrelationConfigDataHolder.setEnable(config.isEnable()); } } diff --git a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java index aede20d5c9f..ad3e4668abb 100644 --- a/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java +++ b/core/org.wso2.carbon.tomcat.ext/src/main/java/org/wso2/carbon/tomcat/ext/valves/RequestCorrelationIdValve.java @@ -31,8 +31,7 @@ import org.osgi.service.component.ComponentContext; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Deactivate; -import org.wso2.carbon.tomcat.ext.internal.CorrelationConfigDataHolder; -import org.wso2.carbon.tomcat.ext.service.ConfigurableCorrelationLogService; +import org.wso2.carbon.tomcat.ext.internal.HTTPCorrelationConfigDataHolder; import javax.servlet.ServletException; import javax.servlet.ServletRequest; @@ -117,7 +116,7 @@ public void invoke(Request request, Response response) throws IOException, Servl // Associates the generated Correlation ID Mapping to the request so that, // it will be available for any other asynchronous flows or threads spawned for this request request.setAttribute(CORRELATION_ID_MDC_REQUEST_ATTRIBUTE_NAME, associateToThreadMap); - if (CorrelationConfigDataHolder.isEnable()) { + if (HTTPCorrelationConfigDataHolder.isEnable()) { long currentTime = System.currentTimeMillis(); long timeTaken = currentTime - requestStartTime; logRequestDetails(currentTime, timeTaken, CORRELATION_LOG_REQUEST_START, request); @@ -127,7 +126,7 @@ public void invoke(Request request, Response response) throws IOException, Servl getNext().invoke(request, response); } } finally { - if (CorrelationConfigDataHolder.isEnable()) { + if (HTTPCorrelationConfigDataHolder.isEnable()) { long currentTime = System.currentTimeMillis(); long timeTaken = currentTime - requestStartTime; logRequestDetails(currentTime, timeTaken, CORRELATION_LOG_REQUEST_END, request); diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/internal/CorrelationConfigDataHolder.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/internal/LDAPCorrelationConfigDataHolder.java similarity index 85% rename from core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/internal/CorrelationConfigDataHolder.java rename to core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/internal/LDAPCorrelationConfigDataHolder.java index 842bf46de74..bd45ec69aeb 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/internal/CorrelationConfigDataHolder.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/internal/LDAPCorrelationConfigDataHolder.java @@ -18,10 +18,10 @@ package org.wso2.carbon.user.core.internal; -public class CorrelationConfigDataHolder { +public class LDAPCorrelationConfigDataHolder { private static boolean enable; - private CorrelationConfigDataHolder() { + private LDAPCorrelationConfigDataHolder() { } public static boolean isEnable() { @@ -29,6 +29,6 @@ public static boolean isEnable() { } public static void setEnable(boolean enable) { - CorrelationConfigDataHolder.enable = enable; + LDAPCorrelationConfigDataHolder.enable = enable; } } diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConfigurableCorrelationLogService.java similarity index 83% rename from core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java rename to core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConfigurableCorrelationLogService.java index ee64614dd4d..5323d4da22c 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/ConfigurableCorrelationLogService.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConfigurableCorrelationLogService.java @@ -22,13 +22,13 @@ import org.osgi.service.component.annotations.Component; import org.wso2.carbon.logging.correlation.CorrelationLogConfigurable; import org.wso2.carbon.logging.correlation.bean.ImmutableCorrelationLogConfig; -import org.wso2.carbon.user.core.internal.CorrelationConfigDataHolder; +import org.wso2.carbon.user.core.internal.LDAPCorrelationConfigDataHolder; @Component( immediate = true, service = CorrelationLogConfigurable.class ) -public class ConfigurableCorrelationLogService implements CorrelationLogConfigurable { +public class LDAPConfigurableCorrelationLogService implements CorrelationLogConfigurable { @Override public String getName() { return "ldap"; @@ -36,6 +36,6 @@ public String getName() { @Override public void onConfigure(ImmutableCorrelationLogConfig config) { - CorrelationConfigDataHolder.setEnable(config.isEnable()); + LDAPCorrelationConfigDataHolder.setEnable(config.isEnable()); } } diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java index 958cc0b22be..289ce8d0565 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/ldap/LDAPConnectionContext.java @@ -26,7 +26,7 @@ import org.wso2.carbon.user.core.UserStoreConfigConstants; import org.wso2.carbon.user.core.UserStoreException; import org.wso2.carbon.user.core.dto.CorrelationLogDTO; -import org.wso2.carbon.user.core.internal.CorrelationConfigDataHolder; +import org.wso2.carbon.user.core.internal.LDAPCorrelationConfigDataHolder; import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.Secret; import org.wso2.carbon.utils.UnsupportedSecretTypeException; @@ -640,7 +640,7 @@ private LdapContext getContextForEnvironmentVariables(Hashtable environmen * @throws NamingException */ private DirContext getDirContext(Hashtable environment) throws NamingException { - if (CorrelationConfigDataHolder.isEnable()) { + if (LDAPCorrelationConfigDataHolder.isEnable()) { final Class[] proxyInterfaces = new Class[]{DirContext.class}; long start = System.currentTimeMillis(); @@ -676,7 +676,7 @@ private DirContext getDirContext(Hashtable environment) throws NamingExcep */ private LdapContext getLdapContext(Hashtable environment, Control[] connectionControls) throws NamingException, UserStoreException { - if (CorrelationConfigDataHolder.isEnable()) { + if (LDAPCorrelationConfigDataHolder.isEnable()) { final Class[] proxyInterfaces = new Class[]{LdapContext.class}; long start = System.currentTimeMillis();