Skip to content

Commit

Permalink
Add OSGi condition which is satisfied once the default Ehcache services
Browse files Browse the repository at this point in the history
are registered

This allows to defer starting of custom OSGi DS component which rely on
the EhcacheManager in their activate() method until the Ehcache services
are registered

This closes ehcache#3232
  • Loading branch information
kwin committed Jun 21, 2024
1 parent 561af43 commit d4679c9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/src/docs/asciidoc/user/osgi.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Additional features can then be added by including one or more of:
* `org.ehcache:ehcache-clustered`
* `org.ehcache:ehcache-transactions`

In order to defer starting OSGi DS components which leverage `org.ehcache.core.CacheManager` (for example via `CacheManagerBuilder`) until all relevant services for Ehcache are available one can leverage the https://docs.osgi.org/specification/osgi.core/8.0.0/service.condition.html[OSGi condition] with id `ehcache`.

=== Reverting to JDK Service Lookup

If the `org.ehcache.core.osgi` property is set to `"false"` as either a framework or system property then Ehcache will
Expand Down
2 changes: 2 additions & 0 deletions ehcache-impl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ dependencies {
implementation group: 'org.ehcache', name: 'sizeof', version: parent.sizeofVersion
implementation group: 'org.terracotta', name: 'terracotta-utilities-tools', version: parent.terracottaUtilitiesVersion
compileOnly 'org.osgi:org.osgi.service.component.annotations:1.3.0'
compileOnly 'org.osgi:org.osgi.service.component:1.3.0'
implementation 'org.osgi:org.osgi.service.condition:1.0.0'
testImplementation project(':core-spi-test')
testImplementation 'org.ow2.asm:asm:6.2'
testImplementation 'org.ow2.asm:asm-commons:6.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Terracotta, Inc.
*
* Licensed 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.ehcache.impl.osgi;

import org.ehcache.core.EhcacheManager;
import org.ehcache.core.events.CacheEventDispatcherFactory;
import org.ehcache.core.events.CacheEventListenerProvider;
import org.ehcache.core.spi.service.ServiceFactory;
import org.ehcache.core.spi.store.Store;
import org.ehcache.impl.internal.spi.resilience.DefaultResilienceStrategyProvider;
import org.ehcache.impl.internal.store.tiering.TieredStore;
import org.ehcache.impl.internal.store.tiering.TieredStoreProviderFactory;
import org.ehcache.spi.loaderwriter.CacheLoaderWriterProvider;
import org.ehcache.spi.loaderwriter.WriteBehindProvider;
import org.ehcache.spi.resilience.ResilienceStrategyProvider;
import org.osgi.service.component.ComponentConstants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.condition.Condition;

/**
* The condition is satisfied once all default {@link ServiceFactory} objects mentioned in {@link EhcacheManager} are registered.
* Those are
* <ol>
* <li>{@link Store.Provider} provided through DS component {@link TieredStoreProviderFactory}</li>
* <li>{@link CacheLoaderWriterProvider} provided through DS component {@link DefaultCacheLoaderWriterProviderFactory}</li>
* <li>{@link WriteBehindProvider} provided through DS component {@link WriteBehindProviderFactory}</li>
* <li>{@link CacheEventDispatcherFactory} provided through DS component {@link CacheEventNotificationListenerServiceProviderFactory}</li>
* <li>{@link CacheEventListenerProvider} provided through DS component {@link DefaultCacheEventListenerProviderFactory}</li>
* <li>{@link ResilienceStrategyProvider} provided through DS component {@link DefaultResilienceStrategyProvider}</li>
* </ol>
* Because those services are not provided directly but through {@link ServiceFactory} this condition references directly the responsible default factories (via their component name)
*/
@Component(property = Condition.CONDITION_ID + "=ehcache")
public class EhcacheReadyCondition implements Condition {

@Reference(target = "("+ComponentConstants.COMPONENT_NAME+"=org.ehcache.impl.internal.store.tiering.TieredStoreProviderFactory)")
private ServiceFactory<TieredStore.Provider> storeProviderFactory;

@Reference(target = "("+ComponentConstants.COMPONENT_NAME+"=org.ehcache.impl.internal.spi.loaderwriter.DefaultCacheLoaderWriterProviderFactory)")
private ServiceFactory<CacheLoaderWriterProvider> cacheLoaderWriterProviderFactory;

@Reference(target = "("+ComponentConstants.COMPONENT_NAME+"=org.ehcache.impl.internal.loaderwriter.writebehind.WriteBehindProviderFactory)")
private ServiceFactory<WriteBehindProvider> writeBehindProviderFactory;

@Reference(target = "("+ComponentConstants.COMPONENT_NAME+"=org.ehcache.impl.internal.events.CacheEventNotificationListenerServiceProviderFactory)")
private ServiceFactory<CacheEventDispatcherFactory> cacheEventNotificationListenerServiceProviderFactory;

@Reference(target = "("+ComponentConstants.COMPONENT_NAME+"=org.ehcache.impl.internal.spi.event.DefaultCacheEventListenerProviderFactory)")
private ServiceFactory<CacheEventListenerProvider> cacheEventListenerProviderFactory;

@Reference(target = "("+ComponentConstants.COMPONENT_NAME+"=org.ehcache.impl.internal.spi.resilience.DefaultResilienceStrategyProviderFactory)")
private ServiceFactory<ResilienceStrategyProvider> resilienceStrategyProviderFactory;
}

0 comments on commit d4679c9

Please sign in to comment.