/*-
 * ~~~~~~licensing~~~~~~
 * artifact-management
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * 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.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.core.artifact.installer.builder.impl.cluster;

import java.util.ArrayList;
import java.util.List;

import org.apache.karaf.cellar.core.GroupManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.entaxy.platform.base.support.osgi.OSGIUtils;
import ru.entaxy.platform.core.artifact.installer.builder.typed.impl.TypedInstallerHelper;

public class ClusterTypedInstallerHelper extends TypedInstallerHelper {

	private static final Logger log = LoggerFactory.getLogger(ClusterTypedInstallerHelper.class);
	
	protected List<String> groups = new ArrayList<>();

	public void setGroups(List<String> groups) {
		this.groups = groups;
	}
	
	public List<String> getGroups() {
		return groups;
	}

	protected boolean checkGroup(String groupName) {
		GroupManager groupManager = OSGIUtils.getService(GroupManager.class);
		if (groupManager == null)
			try {
				groupManager = OSGIUtils.services().ofClass(GroupManager.class).waitService(20000).<GroupManager>get();
				if (groupManager == null)
					throw new Exception("Service not found in 20000 ms");
			} catch (Exception e) {
				log.error("ERROR getting GroupManager service: ", e);
				return false;
			}
		return (groupManager.findGroupByName(groupName) != null);
	} 
	
}
