Add the eclipse java compiler as an option for the whole project

Using a user provided compiler could lead to unexpected results (i.e. bugs, 
changing behaviour) so as an alternative add the option to compile the project 
with the eclipse compiler. It's much like the approach so far of using the 
eclipse-groovy-compiler, but not set as the default.

To compile successfully I had to rename a package, matching (case insensitive) 
to an existing class. Looks like OS X's case insensitive file system is at the 
root (probably an issue under windows as well). 
https://jira.codehaus.org/browse/PLX-458.

To use add -Peclipse-compiler to the maven command.


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/bfb6452e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/bfb6452e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/bfb6452e

Branch: refs/heads/master
Commit: bfb6452e36f4131b8dfa38ca7cbfc78d6af67bcb
Parents: 4c1d54b
Author: Svetoslav Neykov <[email protected]>
Authored: Mon Jan 26 14:31:39 2015 +0200
Committer: Svetoslav Neykov <[email protected]>
Committed: Mon Jan 26 16:19:10 2015 +0200

----------------------------------------------------------------------
 pom.xml                                         |  22 +++
 .../src/main/java/brooklyn/cli/ItemLister.java  |   4 +-
 .../brooklyn/cli/itemlister/ClassFinder.java    | 148 -----------------
 .../cli/itemlister/ItemDescriptors.java         | 158 -------------------
 .../java/brooklyn/cli/lister/ClassFinder.java   | 148 +++++++++++++++++
 .../brooklyn/cli/lister/ItemDescriptors.java    | 158 +++++++++++++++++++
 6 files changed, 330 insertions(+), 308 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/bfb6452e/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 3e64ccf..84c97f6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1955,6 +1955,28 @@
                 </plugins>
             </build>
         </profile>
+        <profile>
+            <id>eclipse-compiler</id>
+            <build>
+                <plugins>
+                    <plugin>
+                       <groupId>org.apache.maven.plugins</groupId>
+                       <artifactId>maven-compiler-plugin</artifactId>
+                       <configuration>
+                           <compilerId>eclipse</compilerId>
+                           <optimize>true</optimize>
+                       </configuration>
+                       <dependencies>
+                               <dependency>
+                                   <groupId>org.codehaus.plexus</groupId>
+                                   
<artifactId>plexus-compiler-eclipse</artifactId>
+                                   <version>2.4</version>
+                               </dependency>
+                       </dependencies>
+                    </plugin>
+                </plugins>
+            </build>
+        </profile>
     </profiles>
 
 </project>

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/bfb6452e/usage/cli/src/main/java/brooklyn/cli/ItemLister.java
----------------------------------------------------------------------
diff --git a/usage/cli/src/main/java/brooklyn/cli/ItemLister.java 
b/usage/cli/src/main/java/brooklyn/cli/ItemLister.java
index 9ff69e1..3c567dc 100644
--- a/usage/cli/src/main/java/brooklyn/cli/ItemLister.java
+++ b/usage/cli/src/main/java/brooklyn/cli/ItemLister.java
@@ -33,8 +33,8 @@ import org.slf4j.LoggerFactory;
 
 import brooklyn.basic.BrooklynObject;
 import brooklyn.catalog.Catalog;
-import brooklyn.cli.itemlister.ClassFinder;
-import brooklyn.cli.itemlister.ItemDescriptors;
+import brooklyn.cli.lister.ClassFinder;
+import brooklyn.cli.lister.ItemDescriptors;
 import brooklyn.entity.Entity;
 import brooklyn.entity.proxying.ImplementedBy;
 import brooklyn.location.Location;

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/bfb6452e/usage/cli/src/main/java/brooklyn/cli/itemlister/ClassFinder.java
----------------------------------------------------------------------
diff --git a/usage/cli/src/main/java/brooklyn/cli/itemlister/ClassFinder.java 
b/usage/cli/src/main/java/brooklyn/cli/itemlister/ClassFinder.java
deleted file mode 100644
index 92cf1ca..0000000
--- a/usage/cli/src/main/java/brooklyn/cli/itemlister/ClassFinder.java
+++ /dev/null
@@ -1,148 +0,0 @@
-/*
- * 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 brooklyn.cli.itemlister;
-
-import java.io.File;
-import java.lang.annotation.Annotation;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.Collection;
-import java.util.List;
-import java.util.Set;
-
-import org.reflections.Reflections;
-import org.reflections.scanners.FieldAnnotationsScanner;
-import org.reflections.scanners.SubTypesScanner;
-import org.reflections.scanners.TypeAnnotationsScanner;
-import org.reflections.util.ConfigurationBuilder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import brooklyn.basic.BrooklynObject;
-import brooklyn.enricher.basic.AbstractEnricher;
-import brooklyn.entity.Application;
-import brooklyn.entity.Entity;
-import brooklyn.entity.basic.AbstractApplication;
-import brooklyn.entity.basic.AbstractEntity;
-import brooklyn.entity.basic.SoftwareProcessImpl;
-import brooklyn.policy.Enricher;
-import brooklyn.policy.Policy;
-import brooklyn.policy.basic.AbstractPolicy;
-import brooklyn.util.ResourceUtils;
-import brooklyn.util.javalang.UrlClassLoader;
-import brooklyn.util.net.Urls;
-import brooklyn.util.os.Os;
-
-import com.google.common.base.Predicate;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
-
-public class ClassFinder {
-
-    private static final Logger log = 
LoggerFactory.getLogger(ClassFinder.class);
-
-    private static final Collection<Class<?>> BORING = 
ImmutableList.<Class<?>>of(
-            Entity.class,
-            AbstractEntity.class,
-            SoftwareProcessImpl.class,
-            Application.class,
-            AbstractApplication.class,
-            Policy.class,
-            Enricher.class,
-            AbstractPolicy.class,
-            AbstractEnricher.class);
-
-    public static Predicate<Class<?>> notBoring() {
-        return new Predicate<Class<?>>() {
-            public boolean apply(Class<?> input) {
-                return (input != null && !BORING.contains(input));
-            }
-        };
-    }
-    
-    public static Predicate<Class<?>> withAnnotation(final Class<? extends 
Annotation> annotation) {
-        return new Predicate<Class<?>>() {
-            public boolean apply(Class<?> input) {
-                return (input != null && input.getAnnotation(annotation) != 
null);
-            }
-        };
-    }
-    
-    public static Predicate<? super Class<? extends BrooklynObject>> 
withClassNameMatching(final String typeRegex) {
-        return new Predicate<Class<?>>() {
-            public boolean apply(Class<?> input) {
-                return (input != null && input.getName() != null && 
input.getName().matches(typeRegex));
-            }
-        };
-    }
-    
-    public static List<URL> toJarUrls(String url) throws MalformedURLException 
{
-        if (url==null) throw new NullPointerException("Cannot read from null");
-        if (url=="") throw new NullPointerException("Cannot read from empty 
string");
-        
-        List<URL> result = Lists.newArrayList();
-        
-        String protocol = Urls.getProtocol(url);
-        if (protocol!=null) {
-            // it's a URL - easy
-            if ("file".equals(protocol)) {
-                url = ResourceUtils.tidyFileUrl(url);
-            }
-            result.add(new URL(url));
-        } else {
-            // treat as file
-            String tidiedPath = Os.tidyPath(url);
-            File tidiedFile = new File(tidiedPath);
-            if (tidiedFile.isDirectory()) {
-                List<File> toscan = Lists.newLinkedList();
-                toscan.add(tidiedFile);
-                while (toscan.size() > 0) {
-                    File file = toscan.remove(0);
-                    if (file.isFile()) {
-                        if (file.getName().toLowerCase().endsWith(".jar")) {
-                            result.add(new 
URL("file://"+file.getAbsolutePath()));
-                        }
-                    } else if (file.isDirectory()) {
-                        for (File subfile : file.listFiles()) {
-                            toscan.add(subfile);
-                        }
-                    } else {
-                        log.info("Cannot read "+file+"; not a file or 
directory");
-                    }
-                }
-            }
-        }
-        
-        return result;
-    }
-
-    public static <T extends BrooklynObject> Set<Class<? extends T>> 
findClasses(Collection<URL> urls, Class<T> clazz) {
-        ClassLoader classLoader = new UrlClassLoader(urls.toArray(new 
URL[urls.size()]));
-        
-        Reflections reflections = new ConfigurationBuilder()
-                .addClassLoader(classLoader)
-                .addScanners(new SubTypesScanner(), new 
TypeAnnotationsScanner(), new FieldAnnotationsScanner())
-                .addUrls(urls)
-                .build();
-        
-        Set<Class<? extends T>> types = reflections.getSubTypesOf(clazz);
-        
-        return types;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/bfb6452e/usage/cli/src/main/java/brooklyn/cli/itemlister/ItemDescriptors.java
----------------------------------------------------------------------
diff --git 
a/usage/cli/src/main/java/brooklyn/cli/itemlister/ItemDescriptors.java 
b/usage/cli/src/main/java/brooklyn/cli/itemlister/ItemDescriptors.java
deleted file mode 100644
index f909524..0000000
--- a/usage/cli/src/main/java/brooklyn/cli/itemlister/ItemDescriptors.java
+++ /dev/null
@@ -1,158 +0,0 @@
-/*
- * 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 brooklyn.cli.itemlister;
-
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import brooklyn.basic.BrooklynDynamicType;
-import brooklyn.basic.BrooklynObject;
-import brooklyn.basic.BrooklynType;
-import brooklyn.basic.BrooklynTypes;
-import brooklyn.catalog.Catalog;
-import brooklyn.config.ConfigKey;
-import brooklyn.entity.Effector;
-import brooklyn.entity.EntityType;
-import brooklyn.entity.basic.BrooklynConfigKeys;
-import brooklyn.event.Sensor;
-import brooklyn.location.LocationResolver;
-import brooklyn.rest.domain.EffectorSummary;
-import brooklyn.rest.domain.EntityConfigSummary;
-import brooklyn.rest.domain.SensorSummary;
-import brooklyn.rest.domain.SummaryComparators;
-import brooklyn.rest.transform.EffectorTransformer;
-import brooklyn.rest.transform.EntityTransformer;
-import brooklyn.rest.transform.SensorTransformer;
-
-import com.google.common.base.Strings;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
-
-public class ItemDescriptors {
-
-    public static List<Map<String, Object>> toItemDescriptors(Iterable<? 
extends Class<? extends BrooklynObject>> types, boolean headingsOnly) {
-        return toItemDescriptors(types, headingsOnly, null);
-    }
-    
-    public static List<Map<String, Object>> toItemDescriptors(Iterable<? 
extends Class<? extends BrooklynObject>> types, boolean headingsOnly, final 
String sortField) {
-        List<Map<String, Object>> itemDescriptors = Lists.newArrayList();
-        
-        for (Class<? extends BrooklynObject> type : types) {
-            Map<String, Object> itemDescriptor = toItemDescriptor(type, 
headingsOnly);
-            itemDescriptors.add(itemDescriptor);
-        }
-        
-        if (!Strings.isNullOrEmpty(sortField)) {
-            Collections.sort(itemDescriptors, new Comparator<Map<String, 
Object>>() {
-                @Override public int compare(Map<String, Object> id1, 
Map<String, Object> id2) {
-                    Object o1 = id1.get(sortField);
-                    Object o2 = id2.get(sortField);
-                    if (o1 == null) {
-                        return o2 == null ? 0 : 1;
-                    }
-                    if (o2 == null) {
-                        return -1;
-                    }
-                    return o1.toString().compareTo(o2.toString());
-                }
-            });
-        }
-        
-        return itemDescriptors;
-    }
-    
-    public static Map<String,Object> toItemDescriptor(Class<? extends 
BrooklynObject> clazz, boolean headingsOnly) {
-        BrooklynDynamicType<?, ?> dynamicType = 
BrooklynTypes.getDefinedBrooklynType(clazz);
-        BrooklynType type = dynamicType.getSnapshot();
-        ConfigKey<?> version = 
dynamicType.getConfigKey(BrooklynConfigKeys.SUGGESTED_VERSION.getName());
-        
-        Map<String,Object> result = Maps.newLinkedHashMap();
-        
-        result.put("type", clazz.getName());
-        if (version != null) {
-            result.put("defaultVersion", version.getDefaultValue());
-        }
-        
-        Catalog catalogAnnotation = clazz.getAnnotation(Catalog.class);
-        if (catalogAnnotation != null) {
-            result.put("name", catalogAnnotation.name());
-            result.put("description", catalogAnnotation.description());
-            result.put("iconUrl", catalogAnnotation.iconUrl());
-        }
-        
-        Deprecated deprecatedAnnotation = 
clazz.getAnnotation(Deprecated.class);
-        if (deprecatedAnnotation != null) {
-            result.put("deprecated", true);
-        }
-        
-        if (!headingsOnly) {
-            Set<EntityConfigSummary> config = 
Sets.newTreeSet(SummaryComparators.nameComparator());
-            Set<SensorSummary> sensors = 
Sets.newTreeSet(SummaryComparators.nameComparator());
-            Set<EffectorSummary> effectors = 
Sets.newTreeSet(SummaryComparators.nameComparator());
-
-            for (ConfigKey<?> x: type.getConfigKeys()) {
-                config.add(EntityTransformer.entityConfigSummary(x, 
dynamicType.getConfigKeyField(x.getName())));
-            }
-            result.put("config", config);
-            
-            if (type instanceof EntityType) {
-                for (Sensor<?> x: ((EntityType)type).getSensors())
-                    sensors.add(SensorTransformer.sensorSummaryForCatalog(x));
-                result.put("sensors", sensors);
-                
-                for (Effector<?> x: ((EntityType)type).getEffectors())
-                    
effectors.add(EffectorTransformer.effectorSummaryForCatalog(x));
-                result.put("effectors", effectors);
-            }
-        }
-        
-        return result;
-    }
-    
-    public static Object toItemDescriptors(List<LocationResolver> resolvers) {
-        return toItemDescriptors(resolvers, false);
-    }
-    
-    public static Object toItemDescriptors(List<LocationResolver> resolvers, 
Boolean sort) {
-        List<Object> result = Lists.newArrayList();
-        for (LocationResolver resolver : resolvers) {
-            result.add(toItemDescriptor(resolver));
-        }
-        if (sort) {
-            Collections.sort(result, new Comparator<Object>() {
-                @Override public int compare(Object o1, Object o2) {
-                    String s1 = o1 == null ? "" : o1.toString();
-                    String s2 = o2 == null ? "" : o2.toString();
-                    return s1.compareTo(s2);
-                }
-            });
-        }
-        return result;
-    }
-
-    public static Object toItemDescriptor(LocationResolver resolver) {
-        // TODO Get javadoc of LocationResolver? Could use docklet? But that 
would give dependency here
-        // on com.sun.javadoc.*
-        return resolver.getPrefix();
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/bfb6452e/usage/cli/src/main/java/brooklyn/cli/lister/ClassFinder.java
----------------------------------------------------------------------
diff --git a/usage/cli/src/main/java/brooklyn/cli/lister/ClassFinder.java 
b/usage/cli/src/main/java/brooklyn/cli/lister/ClassFinder.java
new file mode 100644
index 0000000..101e019
--- /dev/null
+++ b/usage/cli/src/main/java/brooklyn/cli/lister/ClassFinder.java
@@ -0,0 +1,148 @@
+/*
+ * 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 brooklyn.cli.lister;
+
+import java.io.File;
+import java.lang.annotation.Annotation;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+import org.reflections.Reflections;
+import org.reflections.scanners.FieldAnnotationsScanner;
+import org.reflections.scanners.SubTypesScanner;
+import org.reflections.scanners.TypeAnnotationsScanner;
+import org.reflections.util.ConfigurationBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import brooklyn.basic.BrooklynObject;
+import brooklyn.enricher.basic.AbstractEnricher;
+import brooklyn.entity.Application;
+import brooklyn.entity.Entity;
+import brooklyn.entity.basic.AbstractApplication;
+import brooklyn.entity.basic.AbstractEntity;
+import brooklyn.entity.basic.SoftwareProcessImpl;
+import brooklyn.policy.Enricher;
+import brooklyn.policy.Policy;
+import brooklyn.policy.basic.AbstractPolicy;
+import brooklyn.util.ResourceUtils;
+import brooklyn.util.javalang.UrlClassLoader;
+import brooklyn.util.net.Urls;
+import brooklyn.util.os.Os;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+
+public class ClassFinder {
+
+    private static final Logger log = 
LoggerFactory.getLogger(ClassFinder.class);
+
+    private static final Collection<Class<?>> BORING = 
ImmutableList.<Class<?>>of(
+            Entity.class,
+            AbstractEntity.class,
+            SoftwareProcessImpl.class,
+            Application.class,
+            AbstractApplication.class,
+            Policy.class,
+            Enricher.class,
+            AbstractPolicy.class,
+            AbstractEnricher.class);
+
+    public static Predicate<Class<?>> notBoring() {
+        return new Predicate<Class<?>>() {
+            public boolean apply(Class<?> input) {
+                return (input != null && !BORING.contains(input));
+            }
+        };
+    }
+    
+    public static Predicate<Class<?>> withAnnotation(final Class<? extends 
Annotation> annotation) {
+        return new Predicate<Class<?>>() {
+            public boolean apply(Class<?> input) {
+                return (input != null && input.getAnnotation(annotation) != 
null);
+            }
+        };
+    }
+    
+    public static Predicate<? super Class<? extends BrooklynObject>> 
withClassNameMatching(final String typeRegex) {
+        return new Predicate<Class<?>>() {
+            public boolean apply(Class<?> input) {
+                return (input != null && input.getName() != null && 
input.getName().matches(typeRegex));
+            }
+        };
+    }
+    
+    public static List<URL> toJarUrls(String url) throws MalformedURLException 
{
+        if (url==null) throw new NullPointerException("Cannot read from null");
+        if (url=="") throw new NullPointerException("Cannot read from empty 
string");
+        
+        List<URL> result = Lists.newArrayList();
+        
+        String protocol = Urls.getProtocol(url);
+        if (protocol!=null) {
+            // it's a URL - easy
+            if ("file".equals(protocol)) {
+                url = ResourceUtils.tidyFileUrl(url);
+            }
+            result.add(new URL(url));
+        } else {
+            // treat as file
+            String tidiedPath = Os.tidyPath(url);
+            File tidiedFile = new File(tidiedPath);
+            if (tidiedFile.isDirectory()) {
+                List<File> toscan = Lists.newLinkedList();
+                toscan.add(tidiedFile);
+                while (toscan.size() > 0) {
+                    File file = toscan.remove(0);
+                    if (file.isFile()) {
+                        if (file.getName().toLowerCase().endsWith(".jar")) {
+                            result.add(new 
URL("file://"+file.getAbsolutePath()));
+                        }
+                    } else if (file.isDirectory()) {
+                        for (File subfile : file.listFiles()) {
+                            toscan.add(subfile);
+                        }
+                    } else {
+                        log.info("Cannot read "+file+"; not a file or 
directory");
+                    }
+                }
+            }
+        }
+        
+        return result;
+    }
+
+    public static <T extends BrooklynObject> Set<Class<? extends T>> 
findClasses(Collection<URL> urls, Class<T> clazz) {
+        ClassLoader classLoader = new UrlClassLoader(urls.toArray(new 
URL[urls.size()]));
+        
+        Reflections reflections = new ConfigurationBuilder()
+                .addClassLoader(classLoader)
+                .addScanners(new SubTypesScanner(), new 
TypeAnnotationsScanner(), new FieldAnnotationsScanner())
+                .addUrls(urls)
+                .build();
+        
+        Set<Class<? extends T>> types = reflections.getSubTypesOf(clazz);
+        
+        return types;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/bfb6452e/usage/cli/src/main/java/brooklyn/cli/lister/ItemDescriptors.java
----------------------------------------------------------------------
diff --git a/usage/cli/src/main/java/brooklyn/cli/lister/ItemDescriptors.java 
b/usage/cli/src/main/java/brooklyn/cli/lister/ItemDescriptors.java
new file mode 100644
index 0000000..3039154
--- /dev/null
+++ b/usage/cli/src/main/java/brooklyn/cli/lister/ItemDescriptors.java
@@ -0,0 +1,158 @@
+/*
+ * 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 brooklyn.cli.lister;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import brooklyn.basic.BrooklynDynamicType;
+import brooklyn.basic.BrooklynObject;
+import brooklyn.basic.BrooklynType;
+import brooklyn.basic.BrooklynTypes;
+import brooklyn.catalog.Catalog;
+import brooklyn.config.ConfigKey;
+import brooklyn.entity.Effector;
+import brooklyn.entity.EntityType;
+import brooklyn.entity.basic.BrooklynConfigKeys;
+import brooklyn.event.Sensor;
+import brooklyn.location.LocationResolver;
+import brooklyn.rest.domain.EffectorSummary;
+import brooklyn.rest.domain.EntityConfigSummary;
+import brooklyn.rest.domain.SensorSummary;
+import brooklyn.rest.domain.SummaryComparators;
+import brooklyn.rest.transform.EffectorTransformer;
+import brooklyn.rest.transform.EntityTransformer;
+import brooklyn.rest.transform.SensorTransformer;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+public class ItemDescriptors {
+
+    public static List<Map<String, Object>> toItemDescriptors(Iterable<? 
extends Class<? extends BrooklynObject>> types, boolean headingsOnly) {
+        return toItemDescriptors(types, headingsOnly, null);
+    }
+    
+    public static List<Map<String, Object>> toItemDescriptors(Iterable<? 
extends Class<? extends BrooklynObject>> types, boolean headingsOnly, final 
String sortField) {
+        List<Map<String, Object>> itemDescriptors = Lists.newArrayList();
+        
+        for (Class<? extends BrooklynObject> type : types) {
+            Map<String, Object> itemDescriptor = toItemDescriptor(type, 
headingsOnly);
+            itemDescriptors.add(itemDescriptor);
+        }
+        
+        if (!Strings.isNullOrEmpty(sortField)) {
+            Collections.sort(itemDescriptors, new Comparator<Map<String, 
Object>>() {
+                @Override public int compare(Map<String, Object> id1, 
Map<String, Object> id2) {
+                    Object o1 = id1.get(sortField);
+                    Object o2 = id2.get(sortField);
+                    if (o1 == null) {
+                        return o2 == null ? 0 : 1;
+                    }
+                    if (o2 == null) {
+                        return -1;
+                    }
+                    return o1.toString().compareTo(o2.toString());
+                }
+            });
+        }
+        
+        return itemDescriptors;
+    }
+    
+    public static Map<String,Object> toItemDescriptor(Class<? extends 
BrooklynObject> clazz, boolean headingsOnly) {
+        BrooklynDynamicType<?, ?> dynamicType = 
BrooklynTypes.getDefinedBrooklynType(clazz);
+        BrooklynType type = dynamicType.getSnapshot();
+        ConfigKey<?> version = 
dynamicType.getConfigKey(BrooklynConfigKeys.SUGGESTED_VERSION.getName());
+        
+        Map<String,Object> result = Maps.newLinkedHashMap();
+        
+        result.put("type", clazz.getName());
+        if (version != null) {
+            result.put("defaultVersion", version.getDefaultValue());
+        }
+        
+        Catalog catalogAnnotation = clazz.getAnnotation(Catalog.class);
+        if (catalogAnnotation != null) {
+            result.put("name", catalogAnnotation.name());
+            result.put("description", catalogAnnotation.description());
+            result.put("iconUrl", catalogAnnotation.iconUrl());
+        }
+        
+        Deprecated deprecatedAnnotation = 
clazz.getAnnotation(Deprecated.class);
+        if (deprecatedAnnotation != null) {
+            result.put("deprecated", true);
+        }
+        
+        if (!headingsOnly) {
+            Set<EntityConfigSummary> config = 
Sets.newTreeSet(SummaryComparators.nameComparator());
+            Set<SensorSummary> sensors = 
Sets.newTreeSet(SummaryComparators.nameComparator());
+            Set<EffectorSummary> effectors = 
Sets.newTreeSet(SummaryComparators.nameComparator());
+
+            for (ConfigKey<?> x: type.getConfigKeys()) {
+                config.add(EntityTransformer.entityConfigSummary(x, 
dynamicType.getConfigKeyField(x.getName())));
+            }
+            result.put("config", config);
+            
+            if (type instanceof EntityType) {
+                for (Sensor<?> x: ((EntityType)type).getSensors())
+                    sensors.add(SensorTransformer.sensorSummaryForCatalog(x));
+                result.put("sensors", sensors);
+                
+                for (Effector<?> x: ((EntityType)type).getEffectors())
+                    
effectors.add(EffectorTransformer.effectorSummaryForCatalog(x));
+                result.put("effectors", effectors);
+            }
+        }
+        
+        return result;
+    }
+    
+    public static Object toItemDescriptors(List<LocationResolver> resolvers) {
+        return toItemDescriptors(resolvers, false);
+    }
+    
+    public static Object toItemDescriptors(List<LocationResolver> resolvers, 
Boolean sort) {
+        List<Object> result = Lists.newArrayList();
+        for (LocationResolver resolver : resolvers) {
+            result.add(toItemDescriptor(resolver));
+        }
+        if (sort) {
+            Collections.sort(result, new Comparator<Object>() {
+                @Override public int compare(Object o1, Object o2) {
+                    String s1 = o1 == null ? "" : o1.toString();
+                    String s2 = o2 == null ? "" : o2.toString();
+                    return s1.compareTo(s2);
+                }
+            });
+        }
+        return result;
+    }
+
+    public static Object toItemDescriptor(LocationResolver resolver) {
+        // TODO Get javadoc of LocationResolver? Could use docklet? But that 
would give dependency here
+        // on com.sun.javadoc.*
+        return resolver.getPrefix();
+    }
+}

Reply via email to