Search in sources :

Example 1 with ModelClass

use of org.platformlayer.xaas.services.ModelClass in project platformlayer by platformlayer.

the class ProviderHelper method findModelsProviding.

private List<ModelClass<?>> findModelsProviding(Class<?> serviceClass) throws OpsException {
    List<ModelClass<?>> models = Lists.newArrayList();
    for (ServiceInfo service : serviceProviderDictionary.getAllServices()) {
        ServiceType serviceType = new ServiceType(service.getServiceType());
        ServiceProvider serviceProvider = serviceProviderDictionary.getServiceProvider(serviceType);
        for (ModelClass<?> model : serviceProvider.getModels().all()) {
            Class<?> controllerClass = serviceProvider.getControllerClass(model.getJavaClass());
            if (serviceClass.isAssignableFrom(controllerClass)) {
                models.add(model);
            }
        }
    }
    return models;
}
Also used : ServiceInfo(org.platformlayer.core.model.ServiceInfo) ModelClass(org.platformlayer.xaas.services.ModelClass) ServiceType(org.platformlayer.ids.ServiceType) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider)

Example 2 with ModelClass

use of org.platformlayer.xaas.services.ModelClass in project platformlayer by platformlayer.

the class ProviderHelper method listChildrenProviding.

public <T> List<ProviderOf<T>> listChildrenProviding(PlatformLayerKey parent, Class<T> serviceClass) throws OpsException {
    List<ProviderOf<T>> providers = Lists.newArrayList();
    Map<Class<?>, ModelClass<?>> modelClasses = Maps.newHashMap();
    for (ModelClass<?> model : findModelsProviding(serviceClass)) {
        modelClasses.put(model.getJavaClass(), model);
    }
    {
        Object parentItem = platformLayer.getItem(parent);
        ModelClass<?> modelClass = modelClasses.get(parentItem.getClass());
        if (modelClass != null) {
            providers.add(new ProviderOf<T>(modelClass, (ItemBase) parentItem));
        }
    }
    boolean includeDeleted = false;
    for (ItemBase item : platformLayer.listChildrenTyped(parent, includeDeleted)) {
        // Object item = platformLayer.promoteToTyped(untypedItem);
        ModelClass<?> modelClass = modelClasses.get(item.getClass());
        if (modelClass != null) {
            providers.add(new ProviderOf<T>(modelClass, item));
        }
    }
    return providers;
}
Also used : ModelClass(org.platformlayer.xaas.services.ModelClass) ItemBase(org.platformlayer.core.model.ItemBase) ModelClass(org.platformlayer.xaas.services.ModelClass)

Example 3 with ModelClass

use of org.platformlayer.xaas.services.ModelClass in project platformlayer by platformlayer.

the class ServiceResource method getSchema.

@GET
@Path("schema")
@Produces({ XML })
public String getSchema() throws IOException, JAXBException {
    ServiceProvider serviceProvider = getServiceProvider();
    String namespace = null;
    List<Class<?>> javaClasses = Lists.newArrayList();
    for (ModelClass<?> modelClass : serviceProvider.getModels().all()) {
        javaClasses.add(modelClass.getJavaClass());
        String modelNamespace = modelClass.getPrimaryNamespace();
        if (namespace == null) {
            namespace = modelNamespace;
        } else if (!namespace.equals(modelNamespace)) {
            throw new IllegalStateException();
        }
    }
    JAXBContext jaxbContext = JAXBContext.newInstance(javaClasses.toArray(new Class<?>[javaClasses.size()]));
    MemorySchemaOutputResolver schemaOutputResolver = new MemorySchemaOutputResolver();
    jaxbContext.generateSchema(schemaOutputResolver);
    Map<String, StringWriter> writers = schemaOutputResolver.getWriters();
    StringWriter writer = writers.get(namespace);
    if (writer == null) {
        throw new IllegalArgumentException();
    }
    return writer.getBuffer().toString();
}
Also used : MemorySchemaOutputResolver(org.platformlayer.xml.MemorySchemaOutputResolver) StringWriter(java.io.StringWriter) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) ModelClass(org.platformlayer.xaas.services.ModelClass) JAXBContext(javax.xml.bind.JAXBContext) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with ModelClass

use of org.platformlayer.xaas.services.ModelClass in project platformlayer by platformlayer.

the class JaxbContextHelper method getJaxbContext.

public synchronized JAXBContext getJaxbContext(Class<?> forClass) {
    if (jaxbContext == null) {
        List<Class<?>> javaClasses = Lists.newArrayList();
        javaClasses.add(Tags.class);
        javaClasses.add(Tag.class);
        javaClasses.add(ItemBase.class);
        javaClasses.add(ManagedItemCollection.class);
        javaClasses.add(JobSchedule.class);
        javaClasses.add(PlatformLayerKey.class);
        javaClasses.add(ConfigureAction.class);
        javaClasses.add(ValidateAction.class);
        javaClasses.add(DeleteAction.class);
        javaClasses.add(BackupAction.class);
        for (ServiceInfo serviceInfo : serviceProviderDictionary.getAllServices()) {
            ServiceType serviceType = new ServiceType(serviceInfo.serviceType);
            ServiceProvider serviceProvider = serviceProviderDictionary.getServiceProvider(serviceType);
            for (ModelClass<?> modelClass : serviceProvider.getModels().all()) {
                javaClasses.add(modelClass.getJavaClass());
            // String modelNamespace = modelClass.getJaxbHelper().getPrimaryNamespace();
            // if (namespace == null) {
            // namespace = modelNamespace;
            // } else if (!namespace.equals(modelNamespace)) {
            // throw new IllegalStateException();
            // }
            }
            for (Class<?> clazz : serviceProvider.getActions()) {
                javaClasses.add(clazz);
            }
        }
        try {
            jaxbContext = JAXBContext.newInstance(javaClasses.toArray(new Class<?>[javaClasses.size()]));
        } catch (JAXBException e) {
            throw new IllegalStateException("Unable to build JAXB context", e);
        }
    }
    return jaxbContext;
}
Also used : ServiceInfo(org.platformlayer.core.model.ServiceInfo) ServiceType(org.platformlayer.ids.ServiceType) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) JAXBException(javax.xml.bind.JAXBException) ModelClass(org.platformlayer.xaas.services.ModelClass)

Example 5 with ModelClass

use of org.platformlayer.xaas.services.ModelClass in project platformlayer by platformlayer.

the class ItemServiceImpl method ensureItem.

<T extends ItemBase> T ensureItem(final ProjectAuthorization auth, final T item, final boolean canExist, final boolean generateUniqueName, final String uniqueTagKey) throws OpsException {
    final ModelClass<T> modelClass = (ModelClass<T>) serviceProviderDirectory.getModelClass(item.getClass());
    if (modelClass == null) {
        throw new IllegalStateException("Unknown item type");
    }
    final Class<T> javaClass = modelClass.getJavaClass();
    // JaxbHelper jaxbHelper = JaxbHelper.get(javaClass);
    final ServiceProvider serviceProvider = modelClass.getProvider();
    String id = item.getId();
    if (Strings.isNullOrEmpty(id)) {
        if (generateUniqueName) {
            id = serviceProvider.buildItemId(modelClass, item);
        } else {
            // otherwise we end up with lots of randomly named items
            throw new OpsException("Must specify item id");
        // id = UUID.randomUUID().toString();
        // item.setId(id);
        }
    }
    ProjectId project = getProjectId(auth);
    PlatformLayerKey itemKey = new PlatformLayerKey(null, project, modelClass.getServiceType(), modelClass.getItemType(), new ManagedItemId(id));
    item.setKey(itemKey);
    item.state = ManagedItemState.CREATION_REQUESTED;
    final OpsContext opsContext = buildTemporaryOpsContext(modelClass.getServiceType(), auth);
    T created = OpsContext.runInContext(opsContext, new CheckedCallable<T, Exception>() {

        @Override
        public T call() throws Exception {
            PlatformLayerKey itemKey = item.getKey();
            T existing;
            SecretProvider secretProvider = SecretProvider.from(auth);
            if (uniqueTagKey != null) {
                boolean fetchTags = true;
                Tag uniqueTag = null;
                for (Tag tag : item.getTags()) {
                    if (Objects.equal(tag.getKey(), uniqueTagKey)) {
                        uniqueTag = tag;
                    }
                }
                if (uniqueTag == null) {
                    throw new IllegalArgumentException("Could not find unique tag");
                }
                Filter filter = TagFilter.byTag(uniqueTag);
                filter = StateFilter.excludeDeleted(filter);
                existing = null;
                List<T> existingList = repository.findAll(modelClass, itemKey.getProject(), fetchTags, secretProvider, filter);
                if (!existingList.isEmpty()) {
                    if (existingList.size() != 1) {
                        throw new IllegalArgumentException("Found multiple items with unique tag");
                    }
                    existing = existingList.get(0);
                }
                if (existing == null) {
                    itemKey = findUniqueId(item, itemKey, secretProvider);
                }
            } else {
                if (generateUniqueName) {
                    itemKey = findUniqueId(item, itemKey, secretProvider);
                }
                try {
                    boolean fetchTags = true;
                    existing = Casts.checkedCast(repository.getManagedItem(itemKey, fetchTags, secretProvider), javaClass);
                } catch (RepositoryException e) {
                    throw new OpsException("Error fetching item from database", e);
                }
            }
            if (!canExist && existing != null) {
                throw new OpsException("Item already exists");
            }
            serviceProvider.beforeCreateItem(item);
            ProjectId project = getProjectId(auth);
            T newItem;
            try {
                if (existing == null) {
                    newItem = repository.createManagedItem(project, item);
                } else {
                    item.secret = existing.secret;
                    item.setKey(existing.getKey());
                    newItem = repository.updateManagedItem(project, item);
                    TagChanges tagChanges = new TagChanges();
                    for (Tag tag : item.getTags()) {
                        if (newItem.getTags().hasTag(tag)) {
                            continue;
                        }
                        boolean uniqueTagKey = false;
                        if (tag.getKey().equals(Tag.PARENT.getKey())) {
                            uniqueTagKey = true;
                        }
                        tagChanges.addTags.add(tag);
                        if (uniqueTagKey) {
                            for (Tag oldTag : newItem.getTags().findTags(tag.getKey())) {
                                tagChanges.removeTags.add(oldTag);
                            }
                        }
                    }
                    if (!tagChanges.isEmpty()) {
                        repository.changeTags(modelClass, project, newItem.getKey().getItemId(), tagChanges, null);
                    }
                }
            } catch (RepositoryException e) {
                throw new OpsException("Error writing object to database", e);
            }
            itemKey = newItem.getKey();
            JobData jobKey = changeQueue.notifyChange(auth, itemKey, ManagedItemState.CREATION_REQUESTED);
            return newItem;
        }

        private <T extends ItemBase> PlatformLayerKey findUniqueId(final T item, final PlatformLayerKey itemKey, SecretProvider secretProvider) throws RepositoryException {
            int sequence = 0;
            while (true) {
                String tryId = item.getId();
                if (sequence != 0) {
                    tryId += sequence;
                }
                final PlatformLayerKey tryKey = itemKey.withId(new ManagedItemId(tryId));
                boolean fetchTags = false;
                ItemBase found = repository.getManagedItem(tryKey, fetchTags, secretProvider);
                if (found == null) {
                    item.setKey(tryKey);
                    return tryKey;
                }
                sequence++;
            }
        }
    });
    return created;
}
Also used : OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase) ProjectId(org.platformlayer.ids.ProjectId) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) RepositoryException(org.platformlayer.RepositoryException) OpsContext(org.platformlayer.ops.OpsContext) TagChanges(org.platformlayer.core.model.TagChanges) ManagedItemId(org.platformlayer.ids.ManagedItemId) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException) ModelClass(org.platformlayer.xaas.services.ModelClass) StateFilter(org.platformlayer.StateFilter) TagFilter(org.platformlayer.TagFilter) Filter(org.platformlayer.Filter) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) List(java.util.List) Tag(org.platformlayer.core.model.Tag) JobData(org.platformlayer.jobs.model.JobData) SecretProvider(org.platformlayer.auth.crypto.SecretProvider)

Aggregations

ModelClass (org.platformlayer.xaas.services.ModelClass)8 ServiceProvider (org.platformlayer.xaas.services.ServiceProvider)5 ItemBase (org.platformlayer.core.model.ItemBase)3 ServiceInfo (org.platformlayer.core.model.ServiceInfo)3 ServiceType (org.platformlayer.ids.ServiceType)3 RepositoryException (org.platformlayer.RepositoryException)2 SecretProvider (org.platformlayer.auth.crypto.SecretProvider)2 ManagedItemId (org.platformlayer.ids.ManagedItemId)2 AesCryptoKey (com.fathomdb.crypto.AesCryptoKey)1 CryptoKey (com.fathomdb.crypto.CryptoKey)1 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)1 StringWriter (java.io.StringWriter)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Filter (org.platformlayer.Filter)1