BrooklynRestResourceUtils { public Application create(ApplicationSpec spec) { log.warn("Using deprecated functionality (as of 0.9.0), ApplicationSpec style (pre CAMP plans). " + "Transition to actively supported spec plans."); log.debug("REST creating application instance for {}", spec); if (!Entitlements.isEntitled(mgmt.getEntitlementManager(), Entitlements.DEPLOY_APPLICATION, spec)) { throw WebResourceUtils.forbidden("User '%s' is not authorized to deploy application %s", Entitlements.getEntitlementContext().user(), spec); } final String type = spec.getType(); final String name = spec.getName(); final Map<String,String> configO = spec.getConfig(); final Set<EntitySpec> entities = (spec.getEntities() == null) ? ImmutableSet.<EntitySpec>of() : spec.getEntities(); final Application instance; FindItemAndClass itemAndClass; if (Strings.isEmpty(type)) { itemAndClass = new FindItemAndClass().setAs(BasicApplication.class, null); } else { itemAndClass = new FindItemAndClass().inferFrom(type); } if (!Entitlements.isEntitled(mgmt.getEntitlementManager(), Entitlements.INVOKE_EFFECTOR, null)) { throw WebResourceUtils.forbidden("User '%s' is not authorized to create application from applicationSpec %s", Entitlements.getEntitlementContext().user(), spec); } try { if (Application.class.isAssignableFrom(itemAndClass.clazz)) { org.apache.brooklyn.api.entity.EntitySpec<?> coreSpec = toCoreEntitySpec(itemAndClass.clazz, name, configO, itemAndClass.catalogItemId); configureRenderingMetadata(spec, coreSpec); for (EntitySpec entitySpec : entities) { log.info("REST creating instance for entity {}", entitySpec.getType()); coreSpec.child(toCoreEntitySpec(entitySpec)); } log.info("REST placing '{}' under management", spec.getName() != null ? spec.getName() : spec); instance = (Application) mgmt.getEntityManager().createEntity(coreSpec); } else if (Entity.class.isAssignableFrom(itemAndClass.clazz)) { if (entities.size() > 0) log.warn("Cannot supply additional entities when using a non-application entity; ignoring in spec {}", spec); org.apache.brooklyn.api.entity.EntitySpec<?> coreSpec = toCoreEntitySpec(BasicApplication.class, name, configO, itemAndClass.catalogItemId); configureRenderingMetadata(spec, coreSpec); coreSpec.child(toCoreEntitySpec(itemAndClass.clazz, name, configO, itemAndClass.catalogItemId) .configure(BrooklynCampConstants.PLAN_ID, "soleChildId")); coreSpec.enricher(Enrichers.builder() .propagatingAllBut(Attributes.SERVICE_UP, Attributes.SERVICE_NOT_UP_INDICATORS, Attributes.SERVICE_STATE_ACTUAL, Attributes.SERVICE_STATE_EXPECTED, Attributes.SERVICE_PROBLEMS) .from(new DslComponent(Scope.CHILD, "soleChildId").newTask()) .build()); log.info("REST placing '{}' under management", spec.getName()); instance = (Application) mgmt.getEntityManager().createEntity(coreSpec); } else { throw new IllegalArgumentException("Class " + itemAndClass.clazz + " must extend one of ApplicationBuilder, Application or Entity"); } return instance; } catch (Exception e) { log.error("REST failed to create application: " + e, e); throw Exceptions.propagate(e); } } BrooklynRestResourceUtils(ManagementContext mgmt); BrooklynTypeRegistry getTypeRegistry(); BrooklynCatalog getCatalog(); ClassLoader getCatalogClassLoader(); LocationRegistry getLocationRegistry(); Policy getPolicy(String application, String entity, String policy); EntityAdjunct getAdjunct(String application, String entity, String adjunct); Policy getPolicy(Entity entity, String policy); EntityAdjunct getAdjunct(Entity entity, String adjunct); Entity getEntity(String application, String entity); Application getApplication(String application); Entity searchForEntityNamed(Entity root, String entity); Application create(ApplicationSpec spec); Task<?> start(Application app, ApplicationSpec spec); Task<?> start(Application app, List<? extends Location> locations); List<Location> getLocationsManaged(ApplicationSpec spec); Task<?> destroy(final Application application); Task<?> expunge(final Entity entity, final boolean release); @Deprecated static String fixLocation(String locationId); Object getObjectValueForDisplay(Object value); String getStringValueForDisplay(Object value); boolean isUrlServerSideAndSafe(String url); Iterable<Entity> descendantsOfAnyType(String application, String entity); Iterable<Entity> descendantsOfType(String application, String entity, final String typeRegex); void reloadBrooklynProperties();  }
@Test public void testCreateAppFromImplClass() { ApplicationSpec spec = ApplicationSpec.builder() .name("myname") .type(SampleNoOpApplication.class.getName()) .locations(ImmutableSet.of("localhost")) .build(); Application app = util.create(spec); assertEquals(ImmutableList.copyOf(managementContext.getApplications()), ImmutableList.of(app)); assertEquals(app.getDisplayName(), "myname"); assertTrue(app instanceof EntityProxy); assertTrue(app instanceof MyInterface); assertFalse(app instanceof SampleNoOpApplication); }