<commit_before>/**
 * Copyright (C) 2009-2014 Dell, Inc.
 * See annotations for authorship information
 *
 * ====================================================================
 * 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.dasein.cloud.compute;

import org.dasein.cloud.AsynchronousTask;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.CloudProvider;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.OperationNotSupportedException;
import org.dasein.cloud.ProviderContext;
import org.dasein.cloud.Requirement;
import org.dasein.cloud.ResourceStatus;
import org.dasein.cloud.Tag;
import org.dasein.cloud.identity.ServiceAction;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;

/**
 * Basic implementations of deprecated methods, helper functions, and default approaches to implementing methods for
 * image support.
 * <p>Created by <NAME>: 1/29/13 1:55 PM</p>
 * @author <NAME>
 * @version 2013.04
 * @since 2013.04
 */
public abstract class AbstractImageSupport implements MachineImageSupport {
    private CloudProvider provider;

    public AbstractImageSupport(@Nonnull CloudProvider provider) {
        this.provider = provider;
    }

    @Override
    public void addImageShare(@Nonnull String providerImageId, @Nonnull String accountNumber) throws CloudException, InternalException {
        throw new OperationNotSupportedException("Support for image sharing is not currently implemented");
    }

    @Override
    public void addPublicShare(@Nonnull String providerImageId) throws CloudException, InternalException {
        throw new OperationNotSupportedException("Support for image sharing is not currently implemented");
    }

    @Override
    public @Nonnull String bundleVirtualMachine(@Nonnull String virtualMachineId, @Nonnull MachineImageFormat format, @Nonnull String bucket, @Nonnull String name) throws CloudException, InternalException {
        throw new OperationNotSupportedException("Image bundling is not currently implemented");
    }

    @Override
    public void bundleVirtualMachineAsync(@Nonnull String virtualMachineId, @Nonnull MachineImageFormat format, @Nonnull String bucket, @Nonnull String name, @Nonnull AsynchronousTask<String> trackingTask) throws CloudException, InternalException {
        throw new OperationNotSupportedException("Image bundling is not currently implemented");
    }

    /**
     * Executes an actual image capture of a virtual machine. The default implementation throws an {@link OperationNotSupportedException}
     * indicating this is not supported.
     * @param options the options used for creating the virtual machine
     * @param task the task for tracking image creation (or null if not interested/not asynchronous)
     * @return a newly created image
     * @throws CloudException an error occurred with the cloud provider while capturing the virtual machine as an image
     * @throws InternalException an error occurred locally while attempting to capture the virtual machine as an image
     */
    protected MachineImage capture(@Nonnull ImageCreateOptions options, @Nullable AsynchronousTask<MachineImage> task) throws CloudException, InternalException {
        throw new OperationNotSupportedException("Image capture is not currently implemented");
    }

    @Override
    public final @Nonnull MachineImage captureImage(@Nonnull ImageCreateOptions options) throws CloudException, InternalException {
        boolean supported = false;

        for( MachineImageType type : MachineImageType.values() ) {
            if( getCapabilities().supportsImageCapture(type) ) {
                supported = true;
            }
        }
        if( !supported ) {
            throw new OperationNotSupportedException("Image capture is not supported in " + getProvider().getCloudName());
        }
        return capture(options, null);
    }

    @Override
    public final void captureImageAsync(final @Nonnull ImageCreateOptions options, final @Nonnull AsynchronousTask<MachineImage> taskTracker) throws CloudException, InternalException {
        boolean supported = false;

        for( MachineImageType type : MachineImageType.values() ) {
            if( getCapabilities().supportsImageCapture(type) ) {
                supported = true;
            }
        }
        if( !supported ) {
            throw new OperationNotSupportedException("Image capture is not supported in " + getProvider().getCloudName());
        }
        getProvider().hold();
        Thread t = new Thread() {
            public void run() {
                try {
                    MachineImage img = capture(options, taskTracker);

                    if( !taskTracker.isComplete() ) {
                        taskTracker.completeWithResult(img);
                    }
                }
                catch( Throwable t ) {
                    taskTracker.complete(t);
                }
                finally {
                    getProvider().release();
                }
            }
        };

        t.setName("Capture of " + options.getVirtualMachineId() + " in " + getProvider().getCloudName());
        t.setDaemon(true);
        t.start();
    }

    protected @Nonnull ProviderContext getContext() throws CloudException {
        ProviderContext ctx = getProvider().getContext();

        if( ctx == null ) {
            throw new CloudException("No context has been set for this request");
        }
        return ctx;
    }

    @Override
    public final @Nullable MachineImage getMachineImage(@Nonnull String providerImageId) throws CloudException, InternalException {
        return getImage(providerImageId);
    }

    protected final @Nonnull CloudProvider getProvider() {
        return provider;
    }

    @Override
    public @Nonnull String getProviderTermForImage(@Nonnull Locale locale) {
        try {
            return getCapabilities().getProviderTermForImage(locale, ImageClass.MACHINE);
        }
        catch ( Throwable t) {
            throw new RuntimeException(t);
        }
    }

    @Override
    public @Nonnull String getProviderTermForCustomImage(@Nonnull Locale locale, @Nonnull ImageClass cls) {
        try {
            return getCapabilities().getProviderTermForImage(locale, cls);
        }
        catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }

    @Override
    public boolean hasPublicLibrary() {
        try {
            return getCapabilities().supportsPublicLibrary(ImageClass.MACHINE);
        }
        catch( Throwable t ) {
            throw new RuntimeException(t);
        }
    }

    @Override
    @Deprecated
    public @Nonnull Requirement identifyLocalBundlingRequirement() throws CloudException, InternalException {
        return getCapabilities().identifyLocalBundlingRequirement();
    }

    @Override
    public final @Nonnull AsynchronousTask<String> imageVirtualMachine(@Nonnull String vmId, @Nonnull String name, @Nonnull String description) throws CloudException, InternalException {
        ComputeServices services = getProvider().getComputeServices();

        if( services == null ) {
            throw new CloudException("No virtual machine " + vmId + " exists to image in this cloud");
        }
        VirtualMachineSupport support = services.getVirtualMachineSupport();

        if( support == null ) {
            throw new CloudException("No virtual machine " + vmId + " exists to image in this cloud");
        }
        VirtualMachine vm = support.getVirtualMachine(vmId);
<commit_msg>Added one missing deprecated override<commit_after>/**
 * Copyright (C) 2009-2014 Dell, Inc.
 * See annotations for authorship information
 *
 * ====================================================================
 * 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.dasein.cloud.compute;

import org.dasein.cloud.AsynchronousTask;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.CloudProvider;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.OperationNotSupportedException;
import org.dasein.cloud.ProviderContext;
import org.dasein.cloud.Requirement;
import org.dasein.cloud.ResourceStatus;
import org.dasein.cloud.Tag;
import org.dasein.cloud.identity.ServiceAction;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;

/**
 * Basic implementations of deprecated methods, helper functions, and default approaches to implementing methods for
 * image support.
 * <p>Created by <NAME>: 1/29/13 1:55 PM</p>
 * @author <NAME>
 * @version 2013.04
 * @since 2013.04
 */
public abstract class AbstractImageSupport implements MachineImageSupport {
    private CloudProvider provider;

    public AbstractImageSupport(@Nonnull CloudProvider provider) {
        this.provider = provider;
    }

    @Override
    public void addImageShare(@Nonnull String providerImageId, @Nonnull String accountNumber) throws CloudException, InternalException {
        throw new OperationNotSupportedException("Support for image sharing is not currently implemented");
    }

    @Override
    public void addPublicShare(@Nonnull String providerImageId) throws CloudException, InternalException {
        throw new OperationNotSupportedException("Support for image sharing is not currently implemented");
    }

    @Override
    public @Nonnull String bundleVirtualMachine(@Nonnull String virtualMachineId, @Nonnull MachineImageFormat format, @Nonnull String bucket, @Nonnull String name) throws CloudException, InternalException {
        throw new OperationNotSupportedException("Image bundling is not currently implemented");
    }

    @Override
    public void bundleVirtualMachineAsync(@Nonnull String virtualMachineId, @Nonnull MachineImageFormat format, @Nonnull String bucket, @Nonnull String name, @Nonnull AsynchronousTask<String> trackingTask) throws CloudException, InternalException {
        throw new OperationNotSupportedException("Image bundling is not currently implemented");
    }

    /**
     * Executes an actual image capture of a virtual machine. The default implementation throws an {@link OperationNotSupportedException}
     * indicating this is not supported.
     * @param options the options used for creating the virtual machine
     * @param task the task for tracking image creation (or null if not interested/not asynchronous)
     * @return a newly created image
     * @throws CloudException an error occurred with the cloud provider while capturing the virtual machine as an image
     * @throws InternalException an error occurred locally while attempting to capture the virtual machine as an image
     */
    protected MachineImage capture(@Nonnull ImageCreateOptions options, @Nullable AsynchronousTask<MachineImage> task) throws CloudException, InternalException {
        throw new OperationNotSupportedException("Image capture is not currently implemented");
    }

    @Override
    public final @Nonnull MachineImage captureImage(@Nonnull ImageCreateOptions options) throws CloudException, InternalException {
        boolean supported = false;

        for( MachineImageType type : MachineImageType.values() ) {
            if( getCapabilities().supportsImageCapture(type) ) {
                supported = true;
            }
        }
        if( !supported ) {
            throw new OperationNotSupportedException("Image capture is not supported in " + getProvider().getCloudName());
        }
        return capture(options, null);
    }

    @Override
    public final void captureImageAsync(final @Nonnull ImageCreateOptions options, final @Nonnull AsynchronousTask<MachineImage> taskTracker) throws CloudException, InternalException {
        boolean supported = false;

        for( MachineImageType type : MachineImageType.values() ) {
            if( getCapabilities().supportsImageCapture(type) ) {
                supported = true;
            }
        }
        if( !supported ) {
            throw new OperationNotSupportedException("Image capture is not supported in " + getProvider().getCloudName());
        }
        getProvider().hold();
        Thread t = new Thread() {
            public void run() {
                try {
                    MachineImage img = capture(options, taskTracker);

                    if( !taskTracker.isComplete() ) {
                        taskTracker.completeWithResult(img);
                    }
                }
                catch( Throwable t ) {
                    taskTracker.complete(t);
                }
                finally {
                    getProvider().release();
                }
            }
        };

        t.setName("Capture of " + options.getVirtualMachineId() + " in " + getProvider().getCloudName());
        t.setDaemon(true);
        t.start();
    }

    protected @Nonnull ProviderContext getContext() throws CloudException {
        ProviderContext ctx = getProvider().getContext();

        if( ctx == null ) {
            throw new CloudException("No context has been set for this request");
        }
        return ctx;
    }

    @Override
    public final @Nullable MachineImage getMachineImage(@Nonnull String providerImageId) throws CloudException, InternalException {
        return getImage(providerImageId);
    }

    protected final @Nonnull CloudProvider getProvider() {
        return provider;
    }

    @Override
    @Deprecated
    public @Nonnull String getProviderTermForImage(@Nonnull Locale locale) {
        try {
            return getCapabilities().getProviderTermForImage(locale, ImageClass.MACHINE);
        }
        catch ( Throwable t) {
            throw new RuntimeException(t);
        }
    }

    @Override
    @Deprecated
    public @Nonnull String getProviderTermForImage(@Nonnull Locale locale, @Nonnull ImageClass cls) {
        try {
            return getCapabilities().getProviderTermForImage(locale, cls);
        }
        catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }

    @Override
    @Deprecated
    public @Nonnull String getProviderTermForCustomImage(@Nonnull Locale locale, @Nonnull ImageClass cls) {
        try {
            return getCapabilities().getProviderTermForImage(locale, cls);
        }
        catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }

    @Override
    @Deprecated
    public boolean hasPublicLibrary() {
        try {
            return getCapabilities().supportsPublicLibrary(ImageClass.MACHINE);
        }
        catch( Throwable t ) {
            throw new RuntimeException(t);
        }
    }

    @Override
    @Deprecated
    public @Nonnull Requirement identifyLocalBundlingRequirement() throws CloudException, InternalException {
        return getCapabilities().identifyLocalBundlingRequirement();
    }

    @Override
    public final @Nonnull AsynchronousTask<String> imageVirtualMachine(@Nonnull String vmId, @Nonnull String name, @Nonnull String description) throws CloudException, InternalException {
        ComputeServices services = getProvider().getComputeServices();

        if( services == null ) {
            throw new CloudException("No virtual machine " + vmId + " exists to image in this cloud");
        }
        VirtualMachineSupport support = services.getVirtualMachineSupport();

        if( support == null ) {
            throw new CloudException("No virtual machine " + vmId + " exists to image in this cloud");
        }
        VirtualMachine vm = support.getVirtualMachine(vmId);

{'fraction_non_alphanumeric': 0.09193290261395294, 'fraction_numerical': 0.003793491790646672, 'mean_word_length': 3.0743781695242696, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '7320673', 'n_tokens_mistral': 4050, 'n_tokens_neox': 3901, 'n_words': 1284}