Search in sources :

Example 1 with SingletonService

use of org.platformlayer.xaas.SingletonService in project platformlayer by platformlayer.

the class CreationValidator method validateCreateItem.

public void validateCreateItem(ItemBase item) throws OpsException {
    // Object model;
    // try {
    // model = managed.getModel(); // Throws if not valid XML
    // } catch (Exception e) {
    // throw new OpsException("Invalid model", e);
    // }
    Class<? extends ItemBase> modelClass = item.getClass();
    SingletonService singletonServiceAnnotation = modelClass.getAnnotation(SingletonService.class);
    if (singletonServiceAnnotation != null) {
        // Only one can be created per scope
        Iterable<? extends ItemBase> items = platformLayer.listItems(modelClass);
        List<PlatformLayerKey> matches = Lists.newArrayList();
        for (ItemBase peer : items) {
            switch(peer.getState()) {
                case ACTIVE:
                case CREATION_REQUESTED:
                case BUILD:
                case BUILD_ERROR:
                    matches.add(peer.getKey());
                    break;
                case DELETE_REQUESTED:
                case DELETED:
                    break;
                default:
                    throw new IllegalStateException();
            }
        }
        if (!matches.isEmpty()) {
            throw new OpsException("Cannot create multiple instances of: " + modelClass.getName() + ".  Others=" + Joiner.on(",").join(matches));
        }
    }
}
Also used : SingletonService(org.platformlayer.xaas.SingletonService) ItemBase(org.platformlayer.core.model.ItemBase) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Aggregations

ItemBase (org.platformlayer.core.model.ItemBase)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 SingletonService (org.platformlayer.xaas.SingletonService)1