/*******************************************************************************
 * Copyright (c) 2008, 2018 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     EclipseSource Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.pde.internal.core.target;

import java.io.InputStream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.pde.core.target.ITargetDefinition;
import org.eclipse.pde.core.target.ITargetHandle;
import org.eclipse.pde.core.target.TargetEvents;

/**
 * Common implementation of target handles.
 *
 * @since 3.5
 */
public abstract class AbstractTargetHandle implements ITargetHandle {

	@Override
	public ITargetDefinition getTargetDefinition() throws CoreException {
		TargetDefinition definition = new TargetDefinition(this);
		if (exists()) {
			definition.setContents(getInputStream());
		}
		return definition;
	}

	/**
	 * Returns an input stream of the target definition's contents.
	 *
	 * @return stream of content
	 * @throws CoreException
	 *             if an error occurs
	 */
	protected abstract InputStream getInputStream() throws CoreException;

	/**
	 * Deletes the underlying target definition.
	 *
	 * @throws CoreException if unable to delete
	 */
	abstract void delete() throws CoreException;

	public final void save(ITargetDefinition definition) throws CoreException {
		doSave(definition);
		TargetPlatformService.scheduleEvent(TargetEvents.TOPIC_TARGET_SAVED, definition.getHandle());
	}

	/**
	 * Saves the definition to underlying storage.
	 *
	 * @param definition target to save
	 * @throws CoreException on failure
	 */
	abstract void doSave(ITargetDefinition definition) throws CoreException;
}
