Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed 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 com.facebook.presto.connector.jmx;
 
 
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import static com.facebook.presto.util.Types.checkType;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static java.util.stream.Collectors.toMap;
 
 public class JmxRecordSetProvider
         implements ConnectorRecordSetProvider
 {
     private final MBeanServer mbeanServer;
     private final String nodeId;
 
     @Inject
     public JmxRecordSetProvider(MBeanServer mbeanServerNodeManager nodeManager)
     {
         this. = checkNotNull(mbeanServer"mbeanServer is null");
         this. = checkNotNull(nodeManager"nodeManager is null").getCurrentNode().getNodeIdentifier();
     }
 
     @Override
     public RecordSet getRecordSet(ConnectorSplit splitList<? extends ColumnHandlecolumns)
     {
         JmxTableHandle tableHandle = checkType(splitJmxSplit.class"split").getTableHandle();
 
         checkNotNull(columns"columns is null");
         checkArgument(!columns.isEmpty(), "must provide at least one column");
 
         ImmutableMap.Builder<StringTypebuilder = ImmutableMap.builder();
         for (ColumnHandle column : columns) {
             JmxColumnHandle jmxColumnHandle = checkType(columnJmxColumnHandle.class"column");
             builder.put(jmxColumnHandle.getColumnName(), jmxColumnHandle.getColumnType());
         }
         ImmutableMap<StringTypecolumnTypes = builder.build();
 
         List<List<Object>> rows;
         try {
             Map<StringObjectattributes = getAttributes(columnTypes.keySet(), tableHandle);
             List<Objectrow = new ArrayList<>();
             // NOTE: data must be produced in the order of the columns parameter.  This code relies on the
             // fact that columnTypes is an ImmutableMap which is an order preserving LinkedHashMap under
             // the covers.
             for (Entry<StringTypeentry : columnTypes.entrySet()) {
                 if (entry.getKey().equals("node")) {
                     row.add();
                 }
                 else {
                     Object value = attributes.get(entry.getKey());
                     if (value == null) {
                         row.add(null);
                     }
                     else {
                         Class<?> javaType = entry.getValue().getJavaType();
                         if (javaType == boolean.class) {
                             if (value instanceof Boolean) {
                                 row.add(value);
                             }
                             else {
                                 // mbeans can lie about types
                                 row.add(null);
                             }
                         }
                        else if (javaType == long.class) {
                            if (value instanceof Number) {
                                row.add(((Numbervalue).longValue());
                            }
                            else {
                                // mbeans can lie about types
                                row.add(null);
                            }
                        }
                        else if (javaType == double.class) {
                            if (value instanceof Number) {
                                row.add(((Numbervalue).doubleValue());
                            }
                            else {
                                // mbeans can lie about types
                                row.add(null);
                            }
                        }
                        else if (javaType == Slice.class) {
                            if (value.getClass().isArray()) {
                                // return a string representation of the array
                                if (value.getClass().getComponentType() == String.class) {
                                    row.add(Arrays.toString((String[]) value));
                                }
                                else if (value.getClass().getComponentType() == boolean.class) {
                                    row.add(Arrays.toString((boolean[]) value));
                                }
                                else if (value.getClass().getComponentType() == byte.class) {
                                    row.add(Arrays.toString((byte[]) value));
                                }
                                else if (value.getClass().getComponentType() == char.class) {
                                    row.add(Arrays.toString((char[]) value));
                                }
                                else if (value.getClass().getComponentType() == double.class) {
                                    row.add(Arrays.toString((double[]) value));
                                }
                                else if (value.getClass().getComponentType() == float.class) {
                                    row.add(Arrays.toString((float[]) value));
                                }
                                else if (value.getClass().getComponentType() == int.class) {
                                    row.add(Arrays.toString((int[]) value));
                                }
                                else if (value.getClass().getComponentType() == long.class) {
                                    row.add(Arrays.toString((long[]) value));
                                }
                                else if (value.getClass().getComponentType() == short.class) {
                                    row.add(Arrays.toString((short[]) value));
                                }
                            }
                            else {
                                row.add(value.toString());
                            }
                        }
                    }
                }
            }
            rows = ImmutableList.of(row);
        }
        catch (JMException e) {
            rows = ImmutableList.of();
        }
        return new InMemoryRecordSet(columnTypes.values(), rows);
    }
    private Map<StringObjectgetAttributes(Set<StringuniqueColumnNamesJmxTableHandle tableHandle)
            throws JMException
    {
        ObjectName objectName = new ObjectName(tableHandle.getObjectName());
        String[] columnNamesArray = uniqueColumnNames.toArray(new String[uniqueColumnNames.size()]);
        return .getAttributes(objectNamecolumnNamesArray)
                .asList().stream()
                .collect(toMap(Attribute::getName, Attribute::getValue));
    }
New to GrepCode? Check out our FAQ X