Skip to content

Commit

Permalink
SLING-5940 : Register filter using http whiteboard
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1755296 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
cziegeler committed Aug 5, 2016
1 parent f0361eb commit f770fe1
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 67 deletions.
60 changes: 36 additions & 24 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.sling</groupId>
<artifactId>sling</artifactId>
<version>26</version>
<version>27</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -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:="(&amp;(osgi.implementation=osgi.http)(version=1.0))"
</Require-Capability>
</instructions>
</configuration>
</plugin>
Expand All @@ -87,6 +89,15 @@
</excludes>
</configuration>
</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>
Expand Down Expand Up @@ -131,6 +142,26 @@
</plugins>
</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>
Expand All @@ -153,29 +184,12 @@
<groupId>org.slf4j</groupId>
<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>
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public interface ResourceBundleProvider {

/**
* Returns the default <code>Locale</code> assumed by this instance.
* @return The default locale
*/
Locale getDefaultLocale();

Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/apache/sling/i18n/impl/I18NFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -256,7 +259,7 @@ public Locale getLocale() {
}

@Override
public Enumeration<?> getLocales() {
public Enumeration<Locale> getLocales() {
return Collections.enumeration(getLocaleList());
}

Expand Down Expand Up @@ -357,7 +360,7 @@ public Locale getLocale() {
}

@Override
public Enumeration<?> getLocales() {
public Enumeration<Locale> getLocales() {
return Collections.enumeration(getLocaleList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class JcrResourceBundle extends ResourceBundle {

/** 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";
Expand Down Expand Up @@ -102,7 +102,7 @@ protected Set<String> getLanguageRootPaths() {
protected void setParent(ResourceBundle parent) {
super.setParent(parent);
}

public ResourceBundle getParent() {
return parent;
}
Expand Down Expand Up @@ -164,7 +164,6 @@ protected Object handleGetObject(String key) {
*
* @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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -119,7 +116,7 @@ public class JcrResourceBundleProvider implements ResourceBundleProvider, EventH
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>();
Expand All @@ -142,7 +139,7 @@ public class JcrResourceBundleProvider implements ResourceBundleProvider, EventH
/**
* 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;

Expand Down Expand Up @@ -228,7 +225,7 @@ public void handleEvent(final org.osgi.service.event.Event event) {

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) {
Expand Down Expand Up @@ -307,7 +304,7 @@ private void scheduleReloadBundle(JcrResourceBundle bundle) {
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();
Expand All @@ -327,7 +324,7 @@ void reloadBundle(final Key key) {
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);
}
Expand Down Expand Up @@ -364,11 +361,9 @@ void reloadBundle(final Key key) {
/**
* 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) {
Expand All @@ -386,8 +381,8 @@ protected void activate(ComponentContext context) throws LoginException {
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) {
Expand Down Expand Up @@ -448,12 +443,12 @@ private ResourceBundle getResourceBundleInternal(final String baseName, final Lo
}

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);
Expand Down Expand Up @@ -537,7 +532,7 @@ private void clearCache() {
languageRootPaths.clear();

synchronized (this) {
for (ServiceRegistration serviceReg : bundleServiceRegistrations.values()) {
for (ServiceRegistration<ResourceBundle> serviceReg : bundleServiceRegistrations.values()) {
serviceReg.unregister();
}
bundleServiceRegistrations.clear();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/apache/sling/i18n/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit f770fe1

Please sign in to comment.