Expose runtime information through JMX
diff --git a/src/main/java/org/apache/sling/uca/impl/Agent.java b/src/main/java/org/apache/sling/uca/impl/Agent.java
index 3842268..d46c56c 100644
--- a/src/main/java/org/apache/sling/uca/impl/Agent.java
+++ b/src/main/java/org/apache/sling/uca/impl/Agent.java
@@ -18,8 +18,15 @@
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
+import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
+import java.util.List;
import java.util.concurrent.TimeUnit;
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.MBeanRegistrationException;
+import javax.management.NotCompliantMBeanException;
+
public class Agent {
public static void premain(String args, Instrumentation inst) {
@@ -39,15 +46,26 @@
Log.get().log("Preparing to install URL transformers. Configured timeouts - connectTimeout : %d, readTimeout: %d", connectTimeout, readTimeout);
+ AgentInfo agentInfoMBean = new AgentInfo(connectTimeout, readTimeout);
+
ClassFileTransformer[] transformers = new ClassFileTransformer[] {
- new JavaNetTimeoutTransformer(connectTimeout, readTimeout),
- new HttpClient3TimeoutTransformer(connectTimeout, readTimeout),
- new HttpClient4TimeoutTransformer(connectTimeout, readTimeout),
- new OkHttpTimeoutTransformer(connectTimeout, readTimeout)
+ new JavaNetTimeoutTransformer(connectTimeout, readTimeout, agentInfoMBean),
+ new HttpClient3TimeoutTransformer(connectTimeout, readTimeout, agentInfoMBean),
+ new HttpClient4TimeoutTransformer(connectTimeout, readTimeout, agentInfoMBean),
+ new OkHttpTimeoutTransformer(connectTimeout, readTimeout, agentInfoMBean)
};
- for ( ClassFileTransformer transformer : transformers )
+ List<String> transformerNames = new ArrayList<>();
+ for ( ClassFileTransformer transformer : transformers ) {
inst.addTransformer(transformer, true);
+ transformerNames.add(transformer.getClass().getName());
+ }
+
+ try {
+ ManagementFactory.getPlatformMBeanServer().registerMBean(agentInfoMBean, AgentInfo.NAME);
+ } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
+ Log.get().log("Failed registering MBean: %s", e.getMessage());
+ }
Log.get().log("All transformers installed");
}
diff --git a/src/main/java/org/apache/sling/uca/impl/AgentInfo.java b/src/main/java/org/apache/sling/uca/impl/AgentInfo.java
new file mode 100644
index 0000000..1a4f641
--- /dev/null
+++ b/src/main/java/org/apache/sling/uca/impl/AgentInfo.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.uca.impl;
+
+import java.lang.instrument.ClassFileTransformer;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+public class AgentInfo implements AgentInfoMBean {
+
+ static final ObjectName NAME;
+
+ static {
+ try {
+ NAME = new ObjectName(AgentInfo.class.getPackage().getName()+":type=Agent");
+ } catch (MalformedObjectNameException e) {
+ throw new ExceptionInInitializerError(e);
+ }
+ }
+
+ private final long connectTimeoutMillis;
+
+ private final long readTimeoutMillis;
+
+ private List<String> transformers = new ArrayList<>();
+
+ private List<String> transformedClasses = new ArrayList<>();
+
+
+ public AgentInfo(long connectTimeoutMillis, long readTimeoutMillis) {
+ this.connectTimeoutMillis = connectTimeoutMillis;
+ this.readTimeoutMillis = readTimeoutMillis;
+ }
+
+ @Override
+ public long getConnectTimeoutMillis() {
+ return connectTimeoutMillis;
+ }
+
+ @Override
+ public long getReadTimeoutMillis() {
+ return readTimeoutMillis;
+ }
+
+ public String[] getTransformers() {
+ return transformers.toArray(new String[0]);
+ }
+
+ public String[] getTransformedClasses() {
+ return transformedClasses.toArray(new String[0]);
+ }
+
+ public void registerTransformedClass(String transformedClassName) {
+ transformedClasses.add(transformedClassName);
+ }
+
+ public void registerTransformer(Class<? extends ClassFileTransformer> transformerClass) {
+ transformers.add(transformerClass.getName());
+ }
+}
diff --git a/src/main/java/org/apache/sling/uca/impl/AgentInfoMBean.java b/src/main/java/org/apache/sling/uca/impl/AgentInfoMBean.java
new file mode 100644
index 0000000..d0b9eb9
--- /dev/null
+++ b/src/main/java/org/apache/sling/uca/impl/AgentInfoMBean.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.uca.impl;
+
+/**
+ * Exposes runtime information about the agent using <tt>JMX</tt>.
+ *
+ */
+public interface AgentInfoMBean {
+
+ /**
+ * Returns the connect timeout
+ *
+ * @return the connect timeout as configured, in milliseconds
+ */
+ long getConnectTimeoutMillis();
+
+ /**
+ * Returns the read timeout
+ *
+ * @return the read timeout as configured, in milliseconds
+ */
+ long getReadTimeoutMillis();
+
+ /**
+ * Returns the active transformers
+ *
+ * @return the active transformers
+ */
+ String[] getTransformers();
+
+ /**
+ * Returns the classes that were transformed to enforce global timeout defaults
+ *
+ * @return the classes that were transformed
+ */
+ String[] getTransformedClasses();
+}
diff --git a/src/main/java/org/apache/sling/uca/impl/HttpClient3TimeoutTransformer.java b/src/main/java/org/apache/sling/uca/impl/HttpClient3TimeoutTransformer.java
index 9bf5d92..05e2891 100644
--- a/src/main/java/org/apache/sling/uca/impl/HttpClient3TimeoutTransformer.java
+++ b/src/main/java/org/apache/sling/uca/impl/HttpClient3TimeoutTransformer.java
@@ -37,10 +37,13 @@
private final long connectTimeoutMillis;
private final long readTimeoutMillis;
+ private final AgentInfo agentInfoMbean;
- public HttpClient3TimeoutTransformer(long connectTimeoutMillis, long readTimeoutMillis) {
+ public HttpClient3TimeoutTransformer(long connectTimeoutMillis, long readTimeoutMillis, AgentInfo agentInfoMBean) {
this.connectTimeoutMillis = connectTimeoutMillis;
this.readTimeoutMillis = readTimeoutMillis;
+ this.agentInfoMbean = agentInfoMBean;
+ this.agentInfoMbean.registerTransformer(getClass());
}
@Override
@@ -64,6 +67,8 @@
classfileBuffer = cc.toBytecode();
cc.detach();
Log.get().log("Transformation complete.");
+
+ agentInfoMbean.registerTransformedClass(className);
}
return classfileBuffer;
} catch (Exception e) {
diff --git a/src/main/java/org/apache/sling/uca/impl/HttpClient4TimeoutTransformer.java b/src/main/java/org/apache/sling/uca/impl/HttpClient4TimeoutTransformer.java
index 5b5b317..6a3990a 100644
--- a/src/main/java/org/apache/sling/uca/impl/HttpClient4TimeoutTransformer.java
+++ b/src/main/java/org/apache/sling/uca/impl/HttpClient4TimeoutTransformer.java
@@ -30,8 +30,8 @@
private static final String REQUEST_CONFIG_BUILDER_CLASS_NAME = Descriptor.toJvmName("org.apache.http.client.config.RequestConfig$Builder");
- public HttpClient4TimeoutTransformer(long connectTimeoutMillis, long readTimeoutMillis) {
+ public HttpClient4TimeoutTransformer(long connectTimeoutMillis, long readTimeoutMillis, AgentInfo agentInfoMBean) {
super(REQUEST_CONFIG_BUILDER_CLASS_NAME, "connectTimeout", "socketTimeout",
- connectTimeoutMillis, readTimeoutMillis);
+ connectTimeoutMillis, readTimeoutMillis, agentInfoMBean);
}
}
diff --git a/src/main/java/org/apache/sling/uca/impl/JavaNetTimeoutTransformer.java b/src/main/java/org/apache/sling/uca/impl/JavaNetTimeoutTransformer.java
index 121417c..02e07f3 100644
--- a/src/main/java/org/apache/sling/uca/impl/JavaNetTimeoutTransformer.java
+++ b/src/main/java/org/apache/sling/uca/impl/JavaNetTimeoutTransformer.java
@@ -49,10 +49,13 @@
private final long readTimeoutMillis;
private final long connectTimeoutMillis;
+ private final AgentInfo agentInfoMBean;
- public JavaNetTimeoutTransformer(long connectTimeout, long readTimeout) {
+ public JavaNetTimeoutTransformer(long connectTimeout, long readTimeout, AgentInfo agentInfoMBean) {
this.connectTimeoutMillis = connectTimeout;
this.readTimeoutMillis = readTimeout;
+ this.agentInfoMBean = agentInfoMBean;
+ this.agentInfoMBean.registerTransformer(getClass());
}
@Override
@@ -67,6 +70,8 @@
classfileBuffer = connectMethod.getDeclaringClass().toBytecode();
connectMethod.getDeclaringClass().detach();
Log.get().log("Transformation complete.");
+
+ this.agentInfoMBean.registerTransformedClass(className);
}
return classfileBuffer;
} catch (Exception e) {
diff --git a/src/main/java/org/apache/sling/uca/impl/OkHttpTimeoutTransformer.java b/src/main/java/org/apache/sling/uca/impl/OkHttpTimeoutTransformer.java
index bcfae7c..458996e 100644
--- a/src/main/java/org/apache/sling/uca/impl/OkHttpTimeoutTransformer.java
+++ b/src/main/java/org/apache/sling/uca/impl/OkHttpTimeoutTransformer.java
@@ -28,9 +28,9 @@
private static final String REQUEST_CONFIG_BUILDER_CLASS_NAME = Descriptor.toJvmName("okhttp3.OkHttpClient$Builder");
- public OkHttpTimeoutTransformer(long connectTimeoutMillis, long readTimeoutMillis) {
+ public OkHttpTimeoutTransformer(long connectTimeoutMillis, long readTimeoutMillis, AgentInfo agentInfoMBean) {
super(REQUEST_CONFIG_BUILDER_CLASS_NAME, "connectTimeout", "readTimeout",
- connectTimeoutMillis, readTimeoutMillis);
+ connectTimeoutMillis, readTimeoutMillis, agentInfoMBean);
}
}
diff --git a/src/main/java/org/apache/sling/uca/impl/UpdateFieldsInConstructorTimeoutTransformer.java b/src/main/java/org/apache/sling/uca/impl/UpdateFieldsInConstructorTimeoutTransformer.java
index 274bcd4..1ed23e9 100644
--- a/src/main/java/org/apache/sling/uca/impl/UpdateFieldsInConstructorTimeoutTransformer.java
+++ b/src/main/java/org/apache/sling/uca/impl/UpdateFieldsInConstructorTimeoutTransformer.java
@@ -36,15 +36,18 @@
private final String readTimeoutFieldName;
private final long connectTimeoutMillis;
private final long readTimeoutMillis;
+ private final AgentInfo agentInfoMBean;
public UpdateFieldsInConstructorTimeoutTransformer(String className, String connectTimeoutFieldName,
- String readTimeoutFieldName, long connectTimeoutMillis, long readTimeoutMillis) {
+ String readTimeoutFieldName, long connectTimeoutMillis, long readTimeoutMillis, AgentInfo agentInfoMBean) {
this.className = className;
this.connectTimeoutFieldName = connectTimeoutFieldName;
this.readTimeoutFieldName = readTimeoutFieldName;
this.connectTimeoutMillis = connectTimeoutMillis;
this.readTimeoutMillis = readTimeoutMillis;
+ this.agentInfoMBean = agentInfoMBean;
+ this.agentInfoMBean.registerTransformer(getClass());
}
@Override
@@ -66,6 +69,7 @@
classfileBuffer = cc.toBytecode();
cc.detach();
Log.get().log("Transformation complete.");
+ this.agentInfoMBean.registerTransformedClass(className);
}
return classfileBuffer;
} catch (Exception e) {
diff --git a/src/test/java/org/apache/sling/uca/impl/JaveNetTimeoutTransformerTest.java b/src/test/java/org/apache/sling/uca/impl/JaveNetTimeoutTransformerTest.java
index c0abffc..9ddffde 100644
--- a/src/test/java/org/apache/sling/uca/impl/JaveNetTimeoutTransformerTest.java
+++ b/src/test/java/org/apache/sling/uca/impl/JaveNetTimeoutTransformerTest.java
@@ -31,7 +31,7 @@
@BeforeEach
public void initFields() {
- transformer = new JavaNetTimeoutTransformer(1, 1);
+ transformer = new JavaNetTimeoutTransformer(1, 1, new AgentInfo(1, 1));
}
@Test