You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2018/02/05 19:07:51 UTC
[sling-org-apache-sling-startupfilter] branch master updated:
Migrate to OSGi annotation
This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-startupfilter.git
The following commit(s) were added to refs/heads/master by this push:
new 990c9ca Migrate to OSGi annotation
990c9ca is described below
commit 990c9ca59f8ad2a3cf8bf8891c59a734f0d2b012
Author: Carsten Ziegeler <cz...@adobe.com>
AuthorDate: Mon Feb 5 11:07:44 2018 -0800
Migrate to OSGi annotation
---
pom.xml | 11 +---
.../startupfilter/impl/StartupFilterImpl.java | 67 ++++++++++++----------
.../OSGI-INF/metatype/metatype.properties | 37 ------------
.../startupfilter/impl/StartupFilterImplTest.java | 17 +++---
4 files changed, 46 insertions(+), 86 deletions(-)
diff --git a/pom.xml b/pom.xml
index 6b50c91..2fa3f1c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.sling</groupId>
<artifactId>sling</artifactId>
- <version>30</version>
+ <version>32</version>
<relativePath/>
</parent>
@@ -44,10 +44,6 @@
<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>
@@ -73,11 +69,6 @@
<scope>provided</scope>
</dependency>
<dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.scr.annotations</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
diff --git a/src/main/java/org/apache/sling/startupfilter/impl/StartupFilterImpl.java b/src/main/java/org/apache/sling/startupfilter/impl/StartupFilterImpl.java
index f6e126c..7fa3649 100644
--- a/src/main/java/org/apache/sling/startupfilter/impl/StartupFilterImpl.java
+++ b/src/main/java/org/apache/sling/startupfilter/impl/StartupFilterImpl.java
@@ -22,7 +22,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
-import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@@ -33,14 +32,6 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-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.Property;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
-import org.apache.felix.scr.annotations.ReferencePolicy;
-import org.apache.felix.scr.annotations.Service;
import org.apache.sling.startupfilter.StartupFilter;
import org.apache.sling.startupfilter.StartupFilterDisabler;
import org.apache.sling.startupfilter.StartupInfoProvider;
@@ -48,7 +39,16 @@ import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
+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.osgi.service.http.whiteboard.HttpWhiteboardConstants;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -56,35 +56,43 @@ import org.slf4j.LoggerFactory;
/** StartupFilter implementation. Initially registered
* as a StartupFilter only, the Filter registration
* is dynamic, on-demand. */
-@Component(immediate=true, metatype=true)
-@Service(value=StartupFilter.class)
+@Component(service = {StartupFilter.class})
+@Designate(ocd = StartupFilterImpl.Config.class)
public class StartupFilterImpl implements StartupFilter, Filter {
+ @ObjectClassDefinition(name = "Apache Sling Startup Filter",
+ description="Rejects Sling requests with a 503 error code during startup.")
+ public static @interface Config {
+
+ @AttributeDefinition(name = "Active by default?",
+ description = "If true, the filter is active as soon as the service starts.")
+ boolean active_by_default() default true;
+
+ @AttributeDefinition(name = "Default message",
+ description = "The default message is returned in the HHTTP response of the filter, " +
+ "followed by any messages supplied by StartupInfoProvider services.")
+ String default_message() default StartupFilterImpl.DEFAULT_MESSAGE;
+ }
+
private final Logger log = LoggerFactory.getLogger(getClass());
private ServiceRegistration<Filter> filterServiceRegistration;
private BundleContext bundleContext;
private ServiceTracker<StartupInfoProvider, StartupInfoProvider> providersTracker;
private int providersTrackerCount = -1;
- private final List<StartupInfoProvider> providers = new ArrayList<StartupInfoProvider>();
-
- @Property(boolValue=true)
- public static final String ACTIVE_BY_DEFAULT_PROP = "active.by.default";
- private boolean defaultFilterActive;
+ private final List<StartupInfoProvider> providers = new ArrayList<>();
public static final String DEFAULT_MESSAGE = "Startup in progress";
- @Property(value=DEFAULT_MESSAGE)
- public static final String DEFAULT_MESSAGE_PROP = "default.message";
- private String defaultMessage;
-
- @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policy=ReferencePolicy.DYNAMIC)
+ @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL)
private volatile StartupFilterDisabler startupFilterDisabler;
private static final String FRAMEWORK_PROP_MANAGER_ROOT = "felix.webconsole.manager.root";
static final String DEFAULT_MANAGER_ROOT = "/system/console";
private String managerRoot;
+ private Config config;
+
/** @inheritDoc */
@Override
public void doFilter(ServletRequest request, ServletResponse sr, FilterChain chain) throws IOException, ServletException {
@@ -112,7 +120,7 @@ public class StartupFilterImpl implements StartupFilter, Filter {
updateProviders();
final StringBuilder sb = new StringBuilder();
- sb.append(defaultMessage);
+ sb.append(config.default_message());
for(StartupInfoProvider p : providers) {
sb.append('\n');
sb.append(p.getProgressInfo());
@@ -163,22 +171,19 @@ public class StartupFilterImpl implements StartupFilter, Filter {
}
@Activate
- protected void activate(final BundleContext ctx, final Map<String, Object> properties) throws InterruptedException {
+ protected void activate(final BundleContext ctx,
+ final Config config) throws InterruptedException {
bundleContext = ctx;
providersTracker = new ServiceTracker(bundleContext, StartupInfoProvider.class, null);
providersTracker.open();
- Object prop = properties.get(DEFAULT_MESSAGE_PROP);
- defaultMessage = prop == null ? DEFAULT_MESSAGE : prop.toString();
-
- prop = properties.get(ACTIVE_BY_DEFAULT_PROP);
- defaultFilterActive = (prop instanceof Boolean ? (Boolean)prop : false);
+ this.config = config;
- prop = bundleContext.getProperty(FRAMEWORK_PROP_MANAGER_ROOT);
+ final String prop = bundleContext.getProperty(FRAMEWORK_PROP_MANAGER_ROOT);
managerRoot = prop == null ? DEFAULT_MANAGER_ROOT : prop.toString();
- if(defaultFilterActive) {
+ if(config.active_by_default()) {
enable();
}
log.info("Activated, enabled={}, managerRoot={}", isEnabled(), managerRoot);
@@ -197,7 +202,7 @@ public class StartupFilterImpl implements StartupFilter, Filter {
public synchronized void enable() {
if (filterServiceRegistration == null) {
final String pattern = "/";
- final Hashtable<String, Object> params = new Hashtable<String, Object>();
+ final Hashtable<String, Object> params = new Hashtable<>();
params.put(Constants.SERVICE_RANKING, 0x9000); // run before RequestLoggerFilter (0x8000)
params.put("sling.filter.scope", "REQUEST");
params.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
diff --git a/src/main/resources/OSGI-INF/metatype/metatype.properties b/src/main/resources/OSGI-INF/metatype/metatype.properties
deleted file mode 100644
index 5f9bcb2..0000000
--- a/src/main/resources/OSGI-INF/metatype/metatype.properties
+++ /dev/null
@@ -1,37 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-#
-
-
-#
-# This file contains localization strings for configuration labels and
-# descriptions as used in the metatype.xml descriptor generated by the
-# the Sling SCR plugin
-
-org.apache.sling.startupfilter.impl.StartupFilterImpl.name=Apache Sling Startup Filter
-org.apache.sling.startupfilter.impl.StartupFilterImpl.description=Rejects Sling requests \
- with a 503 error code during startup.
-
-active.by.default.name=Active by default?
-active.by.default.description=If true, the filter is active as \
- soon as the service starts.
-
-default.message.name=Default message
-default.message.description=The default message is returned in the \
- HTTP response of the filter, followed by any messages supplied \
- by StartupInfoProvider services.
diff --git a/src/test/java/org/apache/sling/startupfilter/impl/StartupFilterImplTest.java b/src/test/java/org/apache/sling/startupfilter/impl/StartupFilterImplTest.java
index 58d7f6c..4b0db14 100644
--- a/src/test/java/org/apache/sling/startupfilter/impl/StartupFilterImplTest.java
+++ b/src/test/java/org/apache/sling/startupfilter/impl/StartupFilterImplTest.java
@@ -23,8 +23,6 @@ import static org.junit.Assert.assertEquals;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Dictionary;
-import java.util.HashMap;
-import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.Filter;
@@ -35,6 +33,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.startupfilter.StartupInfoProvider;
+import org.apache.sling.startupfilter.impl.StartupFilterImpl.Config;
import org.hamcrest.Description;
import org.jmock.Expectations;
import org.jmock.Mockery;
@@ -94,8 +93,8 @@ public class StartupFilterImplTest {
}
}
static private class TestFilterImpl extends StartupFilterImpl {
- void setup(BundleContext ctx, Map<String, Object> props) throws Exception {
- activate(ctx, props);
+ void setup(BundleContext ctx, StartupFilterImpl.Config config) throws Exception {
+ activate(ctx, config);
}
};
@@ -167,11 +166,13 @@ public class StartupFilterImplTest {
messageWriter = new StringWriter();
final PrintWriter responseWriter = new PrintWriter(messageWriter);
- final Map<String, Object> props = new HashMap<String, Object>();
- props.put(StartupFilterImpl.ACTIVE_BY_DEFAULT_PROP, Boolean.TRUE);
-
+ final Config cfg = mockery.mock(StartupFilterImpl.Config.class);
final ServiceReference [] providerRefs = provider == null ? null : new ServiceReference[] { provider };
mockery.checking(new Expectations() {{
+ allowing(cfg).active_by_default();
+ will(returnValue(true));
+ allowing(cfg).default_message();
+ will(returnValue(StartupFilterImpl.DEFAULT_MESSAGE));
allowing(bundleContext).createFilter(with(any(String.class)));
allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)));
allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
@@ -211,7 +212,7 @@ public class StartupFilterImplTest {
allowing(chain).doFilter(with(any(ServletRequest.class)), with(any(ServletResponse.class)));
}});
- filter.setup(bundleContext, props);
+ filter.setup(bundleContext, cfg);
}
private String getPathInfo() {
--
To stop receiving notification emails like this one, please contact
cziegeler@apache.org.