Return-Path: X-Original-To: apmail-brooklyn-dev-archive@minotaur.apache.org Delivered-To: apmail-brooklyn-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A8665186D5 for ; Thu, 12 Nov 2015 11:11:44 +0000 (UTC) Received: (qmail 60390 invoked by uid 500); 12 Nov 2015 11:11:44 -0000 Delivered-To: apmail-brooklyn-dev-archive@brooklyn.apache.org Received: (qmail 60356 invoked by uid 500); 12 Nov 2015 11:11:44 -0000 Mailing-List: contact dev-help@brooklyn.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.incubator.apache.org Delivered-To: mailing list dev@brooklyn.incubator.apache.org Received: (qmail 60345 invoked by uid 99); 12 Nov 2015 11:11:44 -0000 Received: from Unknown (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Nov 2015 11:11:44 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id D35CA18098E for ; Thu, 12 Nov 2015 11:11:43 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.971 X-Spam-Level: X-Spam-Status: No, score=0.971 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, T_RP_MATCHES_RCVD=-0.01, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id q-DqC5kJItty for ; Thu, 12 Nov 2015 11:11:36 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with SMTP id 14D7623055 for ; Thu, 12 Nov 2015 11:11:36 +0000 (UTC) Received: (qmail 60299 invoked by uid 99); 12 Nov 2015 11:11:35 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Nov 2015 11:11:35 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id CAF6BE0099; Thu, 12 Nov 2015 11:11:35 +0000 (UTC) From: neykov To: dev@brooklyn.incubator.apache.org Reply-To: dev@brooklyn.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-brooklyn pull request: Flesh out type creation using the... Content-Type: text/plain Message-Id: <20151112111135.CAF6BE0099@git1-us-west.apache.org> Date: Thu, 12 Nov 2015 11:11:35 +0000 (UTC) Github user neykov commented on a diff in the pull request: https://github.com/apache/incubator-brooklyn/pull/1017#discussion_r44645535 --- Diff: core/src/main/java/org/apache/brooklyn/core/typereg/TypePlanTransformers.java --- @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.core.typereg; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.TreeMap; + +import org.apache.brooklyn.api.mgmt.ManagementContext; +import org.apache.brooklyn.api.typereg.BrooklynTypeRegistry; +import org.apache.brooklyn.api.typereg.RegisteredType; +import org.apache.brooklyn.api.typereg.RegisteredTypeLoadingContext; +import org.apache.brooklyn.util.collections.MutableList; +import org.apache.brooklyn.util.exceptions.Exceptions; +import org.apache.brooklyn.util.exceptions.PropagatedRuntimeException; +import org.apache.brooklyn.util.guava.Maybe; +import org.apache.brooklyn.util.text.Strings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.annotations.Beta; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import com.google.common.collect.Multimap; + +public class TypePlanTransformers { + + private static final Logger log = LoggerFactory.getLogger(TypePlanTransformers.class); + + private static Collection getAll() { + return ImmutableList.copyOf(ServiceLoader.load(BrooklynTypePlanTransformer.class)); + } + + private static Collection> OVERRIDE; + @SafeVarargs + @VisibleForTesting + public synchronized static void forceAvailable(Class ...classes) { + OVERRIDE = Arrays.asList(classes); + } + public synchronized static void clearForced() { + OVERRIDE = null; + } + + public static Collection all(ManagementContext mgmt) { + // TODO cache these in the TypeRegistry, looking for new ones periodically or supplying a way to register them + Collection> override = OVERRIDE; + Collection result = new ArrayList(); + if (override!=null) { + for (Class o1: override) { + try { + result.add(o1.newInstance()); + } catch (Exception e) { + Exceptions.propagate(e); + } + } + } else { + result.addAll(getAll()); + } + for(BrooklynTypePlanTransformer t : result) { + t.injectManagementContext(mgmt); + } + return result; + } + + /** returns a list of {@link BrooklynTypePlanTransformer} instances for this {@link ManagementContext} + * which may be able to handle the given plan; the list is sorted with highest-score transformer first */ + @Beta + public static List forType(ManagementContext mgmt, RegisteredType type, RegisteredTypeLoadingContext constraint) { + Multimap byScoreMulti = ArrayListMultimap.create(); + Collection transformers = all(mgmt); + for (BrooklynTypePlanTransformer transformer : transformers) { + double score = transformer.scoreForType(type, constraint); + if (score>0) byScoreMulti.put(score, transformer); + } + Map> tree = new TreeMap>(byScoreMulti.asMap()); + List> highestFirst = new ArrayList>(tree.values()); + Collections.reverse(highestFirst); --- End diff -- Could pass `Collections.reverseOrder()` to the `TreeMap` instead. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---