SLING-5940 : Register filter using http whiteboard
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/i18n@1755296 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 21c62c4..ee5b56b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.sling</groupId>
<artifactId>sling</artifactId>
- <version>26</version>
+ <version>27</version>
<relativePath />
</parent>
@@ -73,7 +73,9 @@
<Embed-Dependency>
jackrabbit-jcr-commons;inline="org/apache/jackrabbit/util/ISO9075.*|org/apache/jackrabbit/util/XMLChar.*|org/apache/jackrabbit/util/Text.*|org/apache/jackrabbit/commons/json/Json*"
</Embed-Dependency>
-
+ <Require-Capability>
+ osgi.implementation;filter:="(&(osgi.implementation=osgi.http)(version=1.0))"
+ </Require-Capability>
</instructions>
</configuration>
</plugin>
@@ -89,6 +91,15 @@
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ <configuration>
+ <excludePackageNames>
+ org.apache.sling.i18n.impl
+ </excludePackageNames>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
@@ -132,6 +143,26 @@
</build>
<dependencies>
<dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>osgi.core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.service.http.whiteboard</artifactId>
+ <version>1.0.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.service.event</artifactId>
+ <version>1.3.1</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>javax.servlet-api</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-jcr-commons</artifactId>
<version>2.2.4</version>
@@ -154,28 +185,11 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.compendium</artifactId>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- </dependency>
- <dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.scheduler</artifactId>
<version>2.4.0</version>
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.scr.annotations</artifactId>
- </dependency>
-
<!-- Testing -->
<dependency>
<groupId>org.apache.sling</groupId>
@@ -188,13 +202,11 @@
<dependency>
<groupId>javax.jcr</groupId>
<artifactId>jcr</artifactId>
- <version>2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
- <scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
@@ -237,13 +249,13 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
- <version>1.5.0</version>
+ <version>1.7.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
- <version>1.2.13</version>
+ <version>1.2.17</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -273,7 +285,7 @@
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
- <version>4.2.1</version>
+ <version>5.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
diff --git a/src/main/java/org/apache/sling/i18n/ResourceBundleProvider.java b/src/main/java/org/apache/sling/i18n/ResourceBundleProvider.java
index d9aabb8..bbadacf 100644
--- a/src/main/java/org/apache/sling/i18n/ResourceBundleProvider.java
+++ b/src/main/java/org/apache/sling/i18n/ResourceBundleProvider.java
@@ -41,6 +41,7 @@
/**
* Returns the default <code>Locale</code> assumed by this instance.
+ * @return The default locale
*/
Locale getDefaultLocale();
diff --git a/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java b/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java
index d8d7fbf..6013f9a 100644
--- a/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java
+++ b/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java
@@ -53,6 +53,7 @@
import org.apache.sling.i18n.RequestLocaleResolver;
import org.apache.sling.i18n.ResourceBundleProvider;
import org.osgi.framework.Constants;
+import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -63,7 +64,9 @@
@SlingFilter(generateService = true,
order = 700, scope = { SlingFilterScope.REQUEST, SlingFilterScope.ERROR })
@Properties({
- @Property(name = "pattern", value="/.*"),
+ @Property(name = HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN, value="/"),
+ @Property(name = HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
+ value = "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=org.apache.sling)"),
@Property(name = Constants.SERVICE_DESCRIPTION, value = "Internationalization Support Filter"),
@Property(name = Constants.SERVICE_VENDOR, value = "The Apache Software Foundation") })
public class I18NFilter implements Filter {
@@ -256,7 +259,7 @@
}
@Override
- public Enumeration<?> getLocales() {
+ public Enumeration<Locale> getLocales() {
return Collections.enumeration(getLocaleList());
}
@@ -357,7 +360,7 @@
}
@Override
- public Enumeration<?> getLocales() {
+ public Enumeration<Locale> getLocales() {
return Collections.enumeration(getLocaleList());
}
diff --git a/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java b/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java
index 009000b..8065e22 100644
--- a/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java
+++ b/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java
@@ -48,7 +48,7 @@
/** default primary type (=resource type) for message entry dictionaries */
static final String RT_MESSAGE_ENTRY = "sling:MessageEntry";
-
+
static final String MIXIN_MESSAGE = "sling:Message";
static final String MIXIN_LANGUAGE = "mix:language";
@@ -102,7 +102,7 @@
protected void setParent(ResourceBundle parent) {
super.setParent(parent);
}
-
+
public ResourceBundle getParent() {
return parent;
}
@@ -164,7 +164,6 @@
*
* @throws NullPointerException if either of the parameters is {@code null}.
*/
- @SuppressWarnings("deprecation")
private Map<String, Object> loadFully(final ResourceResolver resolver, Set<String> roots, Set<String> languageRoots) {
final String[] searchPath = resolver.getSearchPath();
diff --git a/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java b/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
index a034313..cd30d42 100644
--- a/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
+++ b/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
@@ -41,8 +41,6 @@
import org.apache.felix.scr.annotations.Component;
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.api.SlingConstants;
import org.apache.sling.api.resource.LoginException;
@@ -56,7 +54,6 @@
import org.apache.sling.i18n.ResourceBundleProvider;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
@@ -119,7 +116,7 @@
private ResourceResolver resourceResolver;
/**
- * Map of cached resource bundles indexed by a key combined of the base name
+ * Map of cached resource bundles indexed by a key combined of the base name
* and <code>Locale</code> used to load and identify the <code>ResourceBundle</code>.
*/
private final ConcurrentHashMap<Key, JcrResourceBundle> resourceBundleCache = new ConcurrentHashMap<Key, JcrResourceBundle>();
@@ -142,7 +139,7 @@
/**
* Each ResourceBundle is registered as a service. Each registration is stored in this map with the locale & base name used as a key.
*/
- private Map<Key, ServiceRegistration> bundleServiceRegistrations;
+ private Map<Key, ServiceRegistration<ResourceBundle>> bundleServiceRegistrations;
private boolean preloadBundles;
@@ -228,7 +225,7 @@
private boolean isDictionaryResource(final String path, final org.osgi.service.event.Event event) {
// language node changes happen quite frequently (https://issues.apache.org/jira/browse/SLING-2881)
- // therefore only consider changes either for sling:MessageEntry's
+ // therefore only consider changes either for sling:MessageEntry's
// or for JSON dictionaries
String resourceType = (String) event.getProperty(SlingConstants.PROPERTY_RESOURCE_TYPE);
if (resourceType == null) {
@@ -307,7 +304,7 @@
String baseName = bundle.getBaseName();
Locale locale = bundle.getLocale();
final Key key = new Key(baseName, locale);
-
+
// defer this job
ScheduleOptions options = scheduler.AT(new Date(System.currentTimeMillis() + invalidationDelay));
final String jobName = "JcrResourceBundleProvider: reload bundle with key " + key.toString();
@@ -327,7 +324,7 @@
resourceBundleCache.remove(key);
log.info("Reloading resource bundle for {}", key);
// unregister bundle
- ServiceRegistration serviceRegistration = null;
+ ServiceRegistration<ResourceBundle> serviceRegistration = null;
synchronized (this) {
serviceRegistration = bundleServiceRegistrations.remove(key);
}
@@ -364,11 +361,9 @@
/**
* Activates and configures this component with the repository access
* details and the default locale to use
- * @throws LoginException
+ * @throws LoginException
*/
- protected void activate(ComponentContext context) throws LoginException {
- Dictionary<?, ?> props = context.getProperties();
-
+ protected void activate(BundleContext context, Map<String, Object> props) throws LoginException {
Map<String, Object> repoCredentials;
String user = PropertiesUtil.toString(props.get(PROP_USER), null);
if (user == null || user.length() == 0) {
@@ -386,8 +381,8 @@
this.defaultLocale = toLocale(localeString);
this.preloadBundles = PropertiesUtil.toBoolean(props.get(PROP_PRELOAD_BUNDLES), DEFAULT_PRELOAD_BUNDLES);
- this.bundleContext = context.getBundleContext();
- this.bundleServiceRegistrations = new HashMap<Key, ServiceRegistration>();
+ this.bundleContext = context;
+ this.bundleServiceRegistrations = new HashMap<Key, ServiceRegistration<ResourceBundle>>();
invalidationDelay = PropertiesUtil.toLong(props.get(PROP_INVALIDATION_DELAY), DEFAULT_INVALIDATION_DELAY);
if (this.resourceResolverFactory != null) { // this is only null during test execution!
if (repoCredentials == null) {
@@ -448,12 +443,12 @@
}
private void registerResourceBundle(Key key, JcrResourceBundle resourceBundle) {
- Dictionary<Object, Object> serviceProps = new Hashtable<Object, Object>();
+ Dictionary<String, Object> serviceProps = new Hashtable<String, Object>();
if (key.baseName != null) {
serviceProps.put("baseName", key.baseName);
}
serviceProps.put("locale", key.locale.toString());
- ServiceRegistration serviceReg = bundleContext.registerService(ResourceBundle.class.getName(),
+ ServiceRegistration<ResourceBundle> serviceReg = bundleContext.registerService(ResourceBundle.class,
resourceBundle, serviceProps);
synchronized (this) {
bundleServiceRegistrations.put(key, serviceReg);
@@ -537,7 +532,7 @@
languageRootPaths.clear();
synchronized (this) {
- for (ServiceRegistration serviceReg : bundleServiceRegistrations.values()) {
+ for (ServiceRegistration<ResourceBundle> serviceReg : bundleServiceRegistrations.values()) {
serviceReg.unregister();
}
bundleServiceRegistrations.clear();
diff --git a/src/main/java/org/apache/sling/i18n/package-info.java b/src/main/java/org/apache/sling/i18n/package-info.java
index 654c184..a24a548 100644
--- a/src/main/java/org/apache/sling/i18n/package-info.java
+++ b/src/main/java/org/apache/sling/i18n/package-info.java
@@ -17,8 +17,7 @@
* under the License.
*/
-@Version("2.2.0")
+@org.osgi.annotation.versioning.Version("2.2.0")
package org.apache.sling.i18n;
-import aQute.bnd.annotation.Version;
diff --git a/src/test/java/org/apache/sling/i18n/impl/ConcurrentJcrResourceBundleLoadingTest.java b/src/test/java/org/apache/sling/i18n/impl/ConcurrentJcrResourceBundleLoadingTest.java
index 2c7e476..90f862c 100644
--- a/src/test/java/org/apache/sling/i18n/impl/ConcurrentJcrResourceBundleLoadingTest.java
+++ b/src/test/java/org/apache/sling/i18n/impl/ConcurrentJcrResourceBundleLoadingTest.java
@@ -26,9 +26,9 @@
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
-import java.util.Hashtable;
+import java.util.HashMap;
import java.util.Locale;
-import java.util.ResourceBundle;
+import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@@ -40,7 +40,6 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.osgi.framework.BundleContext;
-import org.osgi.service.component.ComponentContext;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@@ -55,15 +54,15 @@
@Mock JcrResourceBundle english;
@Mock JcrResourceBundle german;
-
+
private JcrResourceBundleProvider provider;
-
+
@Before
public void setup() throws Exception {
provider = spy(new JcrResourceBundleProvider());
- Hashtable<String, Object> properties = new Hashtable<String, Object>();
+ Map<String, Object> properties = new HashMap<String, Object>();
properties.put("locale.default", "en");
- provider.activate(createComponentContext(properties));
+ provider.activate(PowerMockito.mock(BundleContext.class), properties);
doReturn(english).when(provider, "createResourceBundle", eq(null), eq(Locale.ENGLISH));
doReturn(german).when(provider, "createResourceBundle", eq(null), eq(Locale.GERMAN));
Mockito.when(german.getLocale()).thenReturn(Locale.GERMAN);
@@ -100,12 +99,12 @@
verifyPrivate(provider, times(1)).invoke("createResourceBundle", eq(null), eq(Locale.ENGLISH));
verifyPrivate(provider, times(1)).invoke("createResourceBundle", eq(null), eq(Locale.GERMAN));
}
-
+
@Test
public void newBundleUsedAfterReload() throws Exception {
provider.getResourceBundle(Locale.ENGLISH);
provider.getResourceBundle(Locale.GERMAN);
-
+
// reloading german should not reload any other bundle
provider.reloadBundle(new Key(null, Locale.GERMAN));
provider.getResourceBundle(Locale.ENGLISH);
@@ -118,12 +117,12 @@
verifyPrivate(provider, times(1)).invoke("createResourceBundle", eq(null), eq(Locale.ENGLISH));
verifyPrivate(provider, times(2)).invoke("createResourceBundle", eq(null), eq(Locale.GERMAN));
}
-
+
@Test
public void newBundleUsedAsParentAfterReload() throws Exception {
provider.getResourceBundle(Locale.ENGLISH);
provider.getResourceBundle(Locale.GERMAN);
-
+
// reloading english should also reload german (because it has english as a parent)
provider.reloadBundle(new Key(null, Locale.ENGLISH));
provider.getResourceBundle(Locale.ENGLISH);
@@ -136,11 +135,4 @@
verifyPrivate(provider, times(2)).invoke("createResourceBundle", eq(null), eq(Locale.ENGLISH));
verifyPrivate(provider, times(2)).invoke("createResourceBundle", eq(null), eq(Locale.GERMAN));
}
-
- private ComponentContext createComponentContext(Hashtable<String, Object> config) {
- final ComponentContext componentContext = PowerMockito.mock(ComponentContext.class);
- Mockito.when(componentContext.getBundleContext()).thenReturn(PowerMockito.mock(BundleContext.class));
- Mockito.when(componentContext.getProperties()).thenReturn(config);
- return componentContext;
- }
}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/i18n/it/ResourceBundleProviderIT.java b/src/test/java/org/apache/sling/i18n/it/ResourceBundleProviderIT.java
index d2f7364..1b3b623 100644
--- a/src/test/java/org/apache/sling/i18n/it/ResourceBundleProviderIT.java
+++ b/src/test/java/org/apache/sling/i18n/it/ResourceBundleProviderIT.java
@@ -79,6 +79,7 @@
return new DefaultCompositeOption(
SlingPaxOptions.defaultLaunchpadOptions(launchpadVersion),
CoreOptions.provision(CoreOptions.bundle(thisProjectsBundle.toURI().toString())),
+ mavenBundle("org.apache.felix", "org.apache.felix.http.servlet-api", "1.1.2"),
mavenBundle("org.apache.sling", "org.apache.sling.commons.osgi", "2.4.0")
).getOptions();
}