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 static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static com.facebook.presto.util.Types.checkType;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static java.util.Locale.ENGLISH;
 import static javax.management.ObjectName.WILDCARD;
 
 public class JmxMetadata
         extends ReadOnlyConnectorMetadata
 {
     public static final String SCHEMA_NAME = "jmx";
 
     private final String connectorId;
     private final MBeanServer mbeanServer;
 
     @Inject
     public JmxMetadata(JmxConnectorId jmxConnectorIdMBeanServer mbeanServer)
     {
         this. = checkNotNull(jmxConnectorId"jmxConnectorId is null").toString();
         this. = checkNotNull(mbeanServer"mbeanServer is null");
     }
 
     @Override
     public List<StringlistSchemaNames(ConnectorSession session)
     {
         return ImmutableList.of();
     }
 
     @Override
     public JmxTableHandle getTableHandle(ConnectorSession sessionSchemaTableName tableName)
     {
         checkNotNull(tableName"tableName is null");
         if (!tableName.getSchemaName().equals()) {
             return null;
         }
 
         try {
             ObjectName objectName = Iterables.find(.queryNames(null), objectNameEqualsIgnoreCase(new ObjectName(tableName.getTableName())));
             MBeanInfo mbeanInfo = .getMBeanInfo(objectName);
 
             ImmutableList.Builder<JmxColumnHandlecolumns = ImmutableList.builder();
             int ordinalPosition = 0;
             columns.add(new JmxColumnHandle("node"ordinalPosition++));
             for (MBeanAttributeInfo attribute : mbeanInfo.getAttributes()) {
                 if (!attribute.isReadable()) {
                     continue;
                 }
                 columns.add(new JmxColumnHandle(attribute.getName(), getColumnType(attribute), ordinalPosition++));
             }
             return new JmxTableHandle(objectName.toString(), columns.build());
         }
         catch (NoSuchElementException | JMException e) {
             return null;
         }
     }
    @Override
    {
        return checkType(tableHandleJmxTableHandle.class"tableHandle").getTableMetadata();
    }
    @Override
    public List<SchemaTableNamelistTables(ConnectorSession sessionString schemaNameOrNull)
    {
        if (schemaNameOrNull != null && !schemaNameOrNull.equals()) {
            return ImmutableList.of();
        }
        Builder<SchemaTableNametableNames = ImmutableList.builder();
        for (ObjectName objectName : .queryNames(null)) {
            // todo remove lower case when presto supports mixed case names
            tableNames.add(new SchemaTableName(objectName.toString().toLowerCase()));
        }
        return tableNames.build();
    }
    @Override
    {
        return null;
    }
    @Override
    {
        JmxTableHandle jmxTableHandle = checkType(tableHandleJmxTableHandle.class"tableHandle");
        return ImmutableMap.copyOf(Maps.uniqueIndex(jmxTableHandle.getColumns(), column -> column.getColumnName().toLowerCase()));
    }
    @Override
    public ColumnMetadata getColumnMetadata(ConnectorTableHandle tableHandleConnectorColumnHandle columnHandle)
    {
        checkType(tableHandleJmxTableHandle.class"tableHandle");
        return checkType(columnHandleJmxColumnHandle.class"columnHandle").getColumnMetadata();
    }
    @Override
    {
        checkNotNull(prefix"prefix is null");
        if (prefix.getSchemaName() != null && !prefix.getSchemaName().equals()) {
            return ImmutableMap.of();
        }
        ImmutableMap.Builder<SchemaTableNameList<ColumnMetadata>> columns = ImmutableMap.builder();
        List<SchemaTableNametableNames;
        if (prefix.getTableName() == null) {
            tableNames = listTables(sessionprefix.getSchemaName());
        }
        else {
            tableNames = ImmutableList.of(new SchemaTableName(prefix.getSchemaName(), prefix.getTableName()));
        }
        for (SchemaTableName tableName : tableNames) {
            JmxTableHandle tableHandle = getTableHandle(sessiontableName);
            columns.put(tableNametableHandle.getTableMetadata().getColumns());
        }
        return columns.build();
    }
    private Type getColumnType(MBeanAttributeInfo attribute)
    {
        Type columnType;
        switch (attribute.getType()) {
            case "boolean":
            case "java.lang.Boolean":
                columnType = ;
                break;
            case "byte":
            case "java.lang.Byte":
            case "short":
            case "java.lang.Short":
            case "int":
            case "java.lang.Integer":
            case "long":
            case "java.lang.Long":
                columnType = ;
                break;
            case "java.lang.Number":
            case "float":
            case "java.lang.Float":
            case "double":
            case "java.lang.Double":
                columnType = ;
                break;
            default:
                columnType = ;
                break;
        }
        return columnType;
    }
    {
        final String canonicalObjectName = objectName.getCanonicalName();
        return new Predicate<ObjectName>()
        {
            @Override
            public boolean apply(ObjectName input)
            {
                return canonicalObjectName.equalsIgnoreCase(input.getCanonicalName());
            }
        };
    }
New to GrepCode? Check out our FAQ X