@@ -57,6 +57,7 @@ public class LiveBeansView implements LiveBeansViewMBean, ApplicationContextAwar
57
57
private static final Set <ConfigurableApplicationContext > applicationContexts =
58
58
new LinkedHashSet <ConfigurableApplicationContext >();
59
59
60
+
60
61
static void registerApplicationContext (ConfigurableApplicationContext applicationContext ) {
61
62
String mbeanDomain = applicationContext .getEnvironment ().getProperty (MBEAN_DOMAIN_PROPERTY_NAME );
62
63
if (mbeanDomain != null ) {
@@ -94,6 +95,7 @@ static void unregisterApplicationContext(ConfigurableApplicationContext applicat
94
95
95
96
private ConfigurableApplicationContext applicationContext ;
96
97
98
+
97
99
public void setApplicationContext (ApplicationContext applicationContext ) {
98
100
Assert .isTrue (applicationContext instanceof ConfigurableApplicationContext ,
99
101
"ApplicationContext does not implement ConfigurableApplicationContext" );
@@ -173,8 +175,7 @@ protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
173
175
else {
174
176
result .append ("\" type\" : null,\n " );
175
177
}
176
- String resource = StringUtils .replace (bd .getResourceDescription (), "\\ " , "/" );
177
- result .append ("\" resource\" : \" " ).append (resource ).append ("\" ,\n " );
178
+ result .append ("\" resource\" : \" " ).append (getEscapedResourceDescription (bd )).append ("\" ,\n " );
178
179
result .append ("\" dependencies\" : [" );
179
180
String [] dependencies = bf .getDependenciesForBean (beanName );
180
181
if (dependencies .length > 0 ) {
@@ -199,7 +200,7 @@ protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
199
200
}
200
201
201
202
/**
202
- * Determine whether the specified bean is eligible for inclusion in the
203
+ * Determine whether the specified bean is eligible for inclusion in the
203
204
* LiveBeansView JSON snapshot.
204
205
* @param beanName the name of the bean
205
206
* @param bd the corresponding bean definition
@@ -211,4 +212,31 @@ protected boolean isBeanEligible(String beanName, BeanDefinition bd, Configurabl
211
212
(!bd .isLazyInit () || bf .containsSingleton (beanName )));
212
213
}
213
214
215
+ /**
216
+ * Determine a resource description for the given bean definition and
217
+ * apply basic JSON escaping (backslashes, double quotes) to it.
218
+ * @param bd the bean definition to build the resource description for
219
+ * @return the JSON-escaped resource description
220
+ */
221
+ protected String getEscapedResourceDescription (BeanDefinition bd ) {
222
+ String resourceDescription = bd .getResourceDescription ();
223
+ if (resourceDescription == null ) {
224
+ return null ;
225
+ }
226
+ StringBuilder result = new StringBuilder (resourceDescription .length () + 16 );
227
+ for (int i = 0 ; i < resourceDescription .length (); i ++) {
228
+ char character = resourceDescription .charAt (i );
229
+ if (character == '\\' ) {
230
+ result .append ('/' );
231
+ }
232
+ else if (character == '"' ) {
233
+ result .append ("\\ " ).append ('"' );
234
+ }
235
+ else {
236
+ result .append (character );
237
+ }
238
+ }
239
+ return result .toString ();
240
+ }
241
+
214
242
}
0 commit comments