<commit_before>/*
 * Copyright 2011-2022 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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 com.b2international.snowowl.core.internal.locks;

import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.IDisposableService;
import com.b2international.snowowl.core.locks.IOperationLockManager;
import com.b2international.snowowl.eventbus.IEventBus;
import com.b2international.snowowl.eventbus.IHandler;
import com.b2international.snowowl.eventbus.IMessage;
import com.b2international.snowowl.eventbus.events.ClientConnectionNotification;
import com.b2international.snowowl.eventbus.events.SystemNotification;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

public class RemoteLockTargetListener implements IHandler<IMessage>, IDisposableService {

	private static final Logger LOGGER = LoggerFactory.getLogger(RemoteLockTargetListener.class);

	private final Map<String, Multimap<DatastoreLockTarget, DatastoreLockContext>> remotelyLockedContexts = Maps.newHashMap();

	private final AtomicBoolean active = new AtomicBoolean(false);
	
	public void register(final IEventBus eventBus) {
		if (active.compareAndExchange(false, true)) {
			eventBus.registerHandler(SystemNotification.ADDRESS, this);
		}
	}
	
	public void targetAcquired(final String clientId, final Iterable<DatastoreLockTarget> targets, final DatastoreLockContext context) {
		final Multimap<DatastoreLockTarget, DatastoreLockContext> targetsForClient = remotelyLockedContexts.computeIfAbsent(clientId, key -> HashMultimap.create());
		targets.forEach(target -> targetsForClient.put(target, context));
	}

	public void targetRemoved(final String clientId, final Iterable<DatastoreLockTarget> targets, final DatastoreLockContext context) {
		final Multimap<DatastoreLockTarget, DatastoreLockContext> targetsForClient = remotelyLockedContexts.computeIfAbsent(clientId, key -> HashMultimap.create());
		targets.forEach(target -> targetsForClient.remove(target, context));
	}
	
	private void clientLogout(final String clientId) {
		final Multimap<DatastoreLockTarget, DatastoreLockContext> targetsForClient = remotelyLockedContexts.remove(clientId);
		if (targetsForClient == null || targetsForClient.isEmpty()) {
			return;
		}

		LOGGER.warn("Disconnected client had locks granted, unlocking.");

		final IOperationLockManager lockManager = ApplicationContext.getInstance().getServiceChecked(IOperationLockManager.class);
		for (final Map.Entry<DatastoreLockTarget, DatastoreLockContext> targetContextPair : targetsForClient.entries()) {
			try {
				lockManager.unlock(targetContextPair.getValue(), targetContextPair.getKey());
			} catch (final IllegalArgumentException e) {
				LOGGER.error("Failed to unlock targets left after closed session.", e);
			}
		}
	}
	
	@Override
	public void handle(final IMessage message) {
		final Object body = message.body();
		if (!(body instanceof ClientConnectionNotification)) {
			return;
		}
		
		final ClientConnectionNotification notification = (ClientConnectionNotification) body;
		if (!notification.isJoining()) {
			clientLogout(notification.getClientId());
		}
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isDisposed() {
		return false;
	}
}<commit_msg>fix(core): Fix service registration/disposal of RemoteLockTargetListener<commit_after>/*
 * Copyright 2011-2022 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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 com.b2international.snowowl.core.internal.locks;

import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.IDisposableService;
import com.b2international.snowowl.core.locks.IOperationLockManager;
import com.b2international.snowowl.eventbus.IEventBus;
import com.b2international.snowowl.eventbus.IHandler;
import com.b2international.snowowl.eventbus.IMessage;
import com.b2international.snowowl.eventbus.events.ClientConnectionNotification;
import com.b2international.snowowl.eventbus.events.SystemNotification;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

public class RemoteLockTargetListener implements IHandler<IMessage>, IDisposableService {

	private static final Logger LOGGER = LoggerFactory.getLogger(RemoteLockTargetListener.class);

	private final Map<String, Multimap<DatastoreLockTarget, DatastoreLockContext>> remotelyLockedContexts = Maps.newHashMap();

	private final AtomicBoolean active = new AtomicBoolean(false);

	private IEventBus bus;
	
	public void register(final IEventBus bus) {
		if (!active.compareAndExchange(false, true)) {
			this.bus = bus;
			bus.registerHandler(SystemNotification.ADDRESS, this);
		}
	}
	
	public void targetAcquired(final String clientId, final Iterable<DatastoreLockTarget> targets, final DatastoreLockContext context) {
		final Multimap<DatastoreLockTarget, DatastoreLockContext> targetsForClient = remotelyLockedContexts.computeIfAbsent(clientId, key -> HashMultimap.create());
		targets.forEach(target -> targetsForClient.put(target, context));
	}

	public void targetRemoved(final String clientId, final Iterable<DatastoreLockTarget> targets, final DatastoreLockContext context) {
		final Multimap<DatastoreLockTarget, DatastoreLockContext> targetsForClient = remotelyLockedContexts.computeIfAbsent(clientId, key -> HashMultimap.create());
		targets.forEach(target -> targetsForClient.remove(target, context));
	}
	
	private void clientLogout(final String clientId) {
		final Multimap<DatastoreLockTarget, DatastoreLockContext> targetsForClient = remotelyLockedContexts.remove(clientId);
		if (targetsForClient == null || targetsForClient.isEmpty()) {
			return;
		}

		LOGGER.warn("Disconnected client had locks granted, unlocking.");

		final IOperationLockManager lockManager = ApplicationContext.getInstance().getServiceChecked(IOperationLockManager.class);
		for (final Map.Entry<DatastoreLockTarget, DatastoreLockContext> targetContextPair : targetsForClient.entries()) {
			try {
				lockManager.unlock(targetContextPair.getValue(), targetContextPair.getKey());
			} catch (final IllegalArgumentException e) {
				LOGGER.error("Failed to unlock targets left after closed session.", e);
			}
		}
	}
	
	@Override
	public void handle(final IMessage message) {
		final Object body = message.body();
		if (!(body instanceof ClientConnectionNotification)) {
			return;
		}
		
		final ClientConnectionNotification notification = (ClientConnectionNotification) body;
		if (!notification.isJoining()) {
			clientLogout(notification.getClientId());
		}
	}

	@Override
	public void dispose() {
		if (active.compareAndExchange(true, false)) {
			this.bus.unregisterHandler(SystemNotification.ADDRESS, this);
			this.bus = null;
		}
	}

	@Override
	public boolean isDisposed() {
		return active.get();
	}
}
{'fraction_non_alphanumeric': 0.09122764620589657, 'fraction_numerical': 0.006041565973900435, 'mean_word_length': 5.784426229508196, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1470596', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2298, 'n_words': 571}