diff --git oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java index 5574a0a..1cfda25 100644 --- oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java +++ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java @@ -16,6 +16,7 @@ */ package org.apache.jackrabbit.oak.plugins.segment; +import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static java.util.Collections.emptyMap; import static org.apache.jackrabbit.oak.commons.PropertiesUtil.toBoolean; @@ -49,6 +50,7 @@ import org.apache.felix.scr.annotations.PropertyOption; 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.ReferencePolicyOption; import org.apache.jackrabbit.oak.api.jmx.CheckpointMBean; import org.apache.jackrabbit.oak.osgi.ObserverTracker; import org.apache.jackrabbit.oak.osgi.OsgiWhiteboard; @@ -234,11 +236,12 @@ public class SegmentNodeStoreService extends ProxyNodeStore private GCMonitorTracker gcMonitor; - private ComponentContext context; - - @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, - policy = ReferencePolicy.DYNAMIC) - private volatile BlobStore blobStore; + @Reference( + policy = ReferencePolicy.STATIC, + policyOption = ReferencePolicyOption.GREEDY, + cardinality = ReferenceCardinality.OPTIONAL_UNARY + ) + private BlobStore blobStore; private ServiceRegistration storeRegistration; private ServiceRegistration providerRegistration; @@ -264,43 +267,28 @@ public class SegmentNodeStoreService extends ProxyNodeStore public static final String PROP_BLOB_GC_MAX_AGE = "blobGcMaxAgeInSecs"; @Override - protected synchronized SegmentNodeStore getNodeStore() { - checkState(delegate != null, "service must be activated when used"); - return delegate; + protected SegmentNodeStore getNodeStore() { + return checkNotNull(delegate, "service must be activated when used"); + } + + /** + * needed for situations where you have to unwrap the + * SegmentNodeStoreService, to get the SegmentStore, like the failover + */ + @Override + public SegmentStore getSegmentStore() { + return checkNotNull(store, "Segment store not activated"); } @Activate private void activate(ComponentContext context) throws IOException { - this.context = context; this.customBlobStore = Boolean.parseBoolean(lookup(context, CUSTOM_BLOB_STORE)); if (blobStore == null && customBlobStore) { log.info("BlobStore use enabled. SegmentNodeStore would be initialized when BlobStore would be available"); - } else { - registerNodeStore(); - } - } - - public void registerNodeStore() throws IOException { - if (registerSegmentStore()) { - boolean standby = toBoolean(lookup(context, STANDBY), false); - providerRegistration = context.getBundleContext().registerService( - SegmentStoreProvider.class.getName(), this, null); - if (!standby) { - Dictionary props = new Hashtable(); - props.put(Constants.SERVICE_PID, SegmentNodeStore.class.getName()); - props.put("oak.nodestore.description", new String[]{"nodeStoreType=segment"}); - storeRegistration = context.getBundleContext().registerService( - NodeStore.class.getName(), this, props); - } + return; } - } - public synchronized boolean registerSegmentStore() throws IOException { - if (context == null) { - log.info("Component still not activated. Ignoring the initialization call"); - return false; - } Dictionary properties = context.getProperties(); name = String.valueOf(properties.get(NAME)); @@ -440,88 +428,87 @@ public class SegmentNodeStoreService extends ProxyNodeStore "Segment node store compaction strategy settings"); log.info("SegmentNodeStore initialized"); - return true; - } - private static String lookup(ComponentContext context, String property) { - if (context.getProperties().get(property) != null) { - return context.getProperties().get(property).toString(); - } - if (context.getBundleContext().getProperty(property) != null) { - return context.getBundleContext().getProperty(property); + boolean standby = toBoolean(lookup(context, STANDBY), false); + providerRegistration = context.getBundleContext().registerService( + SegmentStoreProvider.class.getName(), this, null); + if (!standby) { + Dictionary props = new Hashtable(); + props.put(Constants.SERVICE_PID, SegmentNodeStore.class.getName()); + props.put("oak.nodestore.description", new String[]{"nodeStoreType=segment"}); + storeRegistration = context.getBundleContext().registerService( + NodeStore.class.getName(), this, props); } - return null; } @Deactivate - public synchronized void deactivate() { - unregisterNodeStore(); - - if (observerTracker != null) { - observerTracker.stop(); - } - if (gcMonitor != null) { - gcMonitor.stop(); - } - delegate = null; - if (store != null) { - store.close(); - store = null; + private void deactivate() { + if (storeRegistration != null) { + storeRegistration.unregister(); + storeRegistration = null; } - } - protected void bindBlobStore(BlobStore blobStore) throws IOException { - this.blobStore = blobStore; - registerNodeStore(); - } - - protected void unbindBlobStore(BlobStore blobStore){ - this.blobStore = null; - unregisterNodeStore(); - } - - private void unregisterNodeStore() { - if(providerRegistration != null){ + if (providerRegistration != null) { providerRegistration.unregister(); providerRegistration = null; } - if(storeRegistration != null){ - storeRegistration.unregister(); - storeRegistration = null; + + if (compactionStrategyRegistration != null) { + compactionStrategyRegistration.unregister(); + compactionStrategyRegistration = null; } - if (checkpointRegistration != null) { - checkpointRegistration.unregister(); - checkpointRegistration = null; + + if (blobGCRegistration != null) { + blobGCRegistration.unregister(); + blobGCRegistration = null; } + if (revisionGCRegistration != null) { revisionGCRegistration.unregister(); revisionGCRegistration = null; } - if (blobGCRegistration != null) { - blobGCRegistration.unregister(); - blobGCRegistration = null; + + if (checkpointRegistration != null) { + checkpointRegistration.unregister(); + checkpointRegistration = null; } - if (compactionStrategyRegistration != null) { - compactionStrategyRegistration.unregister(); - compactionStrategyRegistration = null; + + if (executor != null) { + executor.stop(); + executor = null; } + + if (observerTracker != null) { + observerTracker.stop(); + observerTracker = null; + } + if (fsgcMonitorMBean != null) { fsgcMonitorMBean.unregister(); fsgcMonitorMBean = null; } - if (executor != null) { - executor.stop(); - executor = null; + + delegate = null; + + if (store != null) { + store.close(); + store = null; + } + + if (gcMonitor != null) { + gcMonitor.stop(); + gcMonitor = null; } } - /** - * needed for situations where you have to unwrap the - * SegmentNodeStoreService, to get the SegmentStore, like the failover - */ - @Override - public SegmentStore getSegmentStore() { - return store; + private static String lookup(ComponentContext context, String property) { + if (context.getProperties().get(property) != null) { + return context.getProperties().get(property).toString(); + } + if (context.getBundleContext().getProperty(property) != null) { + return context.getBundleContext().getProperty(property); + } + return null; } //------------------------------------------------------------< Observable >---