Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLING-11380: Upgrade to sling 47 #1

Merged
merged 7 commits into from Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 22 additions & 35 deletions pom.xml
Expand Up @@ -22,44 +22,28 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.sling</groupId>
<artifactId>sling</artifactId>
<version>26</version>
<artifactId>sling-bundle-parent</artifactId>
<version>47</version>
apelluru marked this conversation as resolved.
Show resolved Hide resolved
<relativePath/>
</parent>

<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.jmx.provider</artifactId>
<version>1.0.3-SNAPSHOT</version>
<packaging>bundle</packaging>

<name>Apache Sling JMX Resource Provider</name>

<scm>
<connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jmx-provider.git</connection>
<developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jmx-provider.git</developerConnection>
<url>https://gitbox.apache.org/repos/asf?p=sling-org-apache-sling-jmx-provider.git</url>
<tag>HEAD</tag>
apelluru marked this conversation as resolved.
Show resolved Hide resolved
</scm>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Private-Package>
org.apache.sling.jmx.provider.impl
</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<sling.java.version>8</sling.java.version>
<project.build.outputTimestamp>1</project.build.outputTimestamp>
</properties>

<dependencies>
<dependency>
Expand All @@ -68,27 +52,30 @@
<version>2.7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.osgi</artifactId>
<version>2.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<artifactId>osgi.cmpn</artifactId>
apelluru marked this conversation as resolved.
Show resolved Hide resolved
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<artifactId>javax.servlet-api</artifactId>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.osgi-mock.junit4</artifactId>
<version>3.2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -45,29 +45,17 @@
import javax.management.ReflectionException;
import javax.servlet.http.HttpServletRequest;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceProvider;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.commons.osgi.PropertiesUtil;

@Component(metatype=true,
label="Apache Sling JMX Resource Provider",
description="This provider mounts JMX mbeans into the resource tree.")
@Service(value = ResourceProvider.class)
@Properties({
@Property(name = ResourceProvider.ROOTS, value="/system/sling/monitoring/mbeans",
label="Root",
description="The mount point of the JMX beans"),
@Property(name = ResourceProvider.USE_RESOURCE_ACCESS_SECURITY, boolValue=true, propertyPrivate=true),
@Property(name = ResourceProvider.OWNS_ROOTS, boolValue=true, propertyPrivate=true)
})
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.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

/**
* Brief summary of a "good" object name:
*
Expand All @@ -85,6 +73,8 @@
* {name property} : is the value of the name property or "{noname}" if no name property is set
* {all other props} : name/value pairs containing all additional props
*/
@Component(service = {ResourceProvider.class})
@Designate(ocd = JMXResourceProvider.Config.class)
public class JMXResourceProvider implements ResourceProvider {

/** Configured root paths, ending with a slash */
Expand All @@ -96,11 +86,26 @@ public class JMXResourceProvider implements ResourceProvider {
/** The mbean server. */
private MBeanServer mbeanServer;

@SuppressWarnings("java:S100")
@ObjectClassDefinition(name = "Apache Sling JMX Resource Provider",
description = "This provider mounts JMX mbeans into the resource tree.")
public @interface Config {

@AttributeDefinition(name = "Root", description = "The mount point of the JMX beans")
String[] provider_roots() default {"/system/sling/monitoring/mbeans"};

@AttributeDefinition
boolean provider_useResourceAccessSecurity() default true;

@AttributeDefinition
boolean provider_ownsRoots() default true;
}

@Activate
protected void activate(final Map<String, Object> props) {
final String paths[] = PropertiesUtil.toStringArray(props.get(ResourceProvider.ROOTS));
final List<String> rootsList = new ArrayList<String>();
final List<String> rootsWithSlashList = new ArrayList<String>();
protected void activate(final Config config) {
final String[] paths = config.provider_roots();
final List<String> rootsList = new ArrayList<>();
final List<String> rootsWithSlashList = new ArrayList<>();
if ( paths != null ) {
for(final String p : paths) {
if ( p.length() > 0 ) {
Expand Down Expand Up @@ -215,7 +220,7 @@ private Set<ObjectName> queryObjectNames(final String prefix) {
Set<ObjectName> names = allNames;
if ( prefix != null ) {
final String pathPrefix = prefix + '/';
names = new HashSet<ObjectName>();
names = new HashSet<>();
for(final ObjectName name : allNames) {
final String path = this.convertObjectNameToResourcePath(name);
if ( path.startsWith(pathPrefix) ) {
Expand All @@ -234,7 +239,7 @@ public Iterator<Resource> listChildren(final Resource parent) {
if ( info.isRoot || info.mbeanInfo == null ) {
// list all MBeans
final Set<ObjectName> names = this.queryObjectNames(info.isRoot ? null : info.pathInfo);
final Set<String> filteredNames = new HashSet<String>();
final Set<String> filteredNames = new HashSet<>();
final String prefix = (info.isRoot ? null : info.pathInfo + "/");
for(final ObjectName name : names) {
final String path = this.convertObjectNameToResourcePath(name);
Expand Down Expand Up @@ -309,7 +314,7 @@ public void remove() {
} else {
parentResource = (MBeanResource)this.getResource(parent.getResourceResolver(), parent.getPath());
}
final List<Resource> list = new ArrayList<Resource>();
final List<Resource> list = new ArrayList<>();
list.add(new AttributesResource(parent.getResourceResolver(), parent.getPath() + "/mbean:attributes", parentResource));
return list.iterator();
} else if ( info.pathInfo.equals("mbean:attributes") ) {
Expand All @@ -323,7 +328,7 @@ public void remove() {
final AttributeList result = parentMBeanResource.getAttributes();

final MBeanAttributeInfo[] infos = info.mbeanInfo.getAttributes();
final Map<String, MBeanAttributeInfo> infoMap = new HashMap<String, MBeanAttributeInfo>();
final Map<String, MBeanAttributeInfo> infoMap = new HashMap<>();
for(final MBeanAttributeInfo i : infos) {
infoMap.put(i.getName(), i);
}
Expand Down
@@ -0,0 +1,47 @@
/*
* 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.jmx.provider.impl;

import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.Dictionary;
import java.util.Hashtable;

import static org.junit.Assert.assertNotNull;

public class JMXResourceProviderTest {

@Rule
public final OsgiContext context = new OsgiContext();
private JMXResourceProvider jmxResourceProvider;

@Before
public void setUp() throws Exception {
Dictionary<String, Object> properties = new Hashtable<>();
jmxResourceProvider = context.registerInjectActivateService(JMXResourceProvider.class, new JMXResourceProvider(), properties);
}

@Test
public void testIfServiceActive() {
assertNotNull(jmxResourceProvider);
}
}