@SuppressWarnings("unchecked") private Role parseRole(Element element) throws RESTLightClientException { Role role = new Role(); role.setResourceURI(element.getChildText(RESOURCE_URI_ELEMENT)); role.setId(element.getChildText(ROLE_ID_ELEMENT)); role.setName(element.getChildText(ROLE_NAME_ELEMENT)); role.setDescription(element.getChildText(ROLE_DESCRIPTION_ELEMENT)); role.setSessionTimeout(Integer.parseInt(element.getChildText(ROLE_SESSION_TIMEOUT_ELEMENT))); Element subRoles = element.getChild(ROLE_ROLES_ELEMENT); if (subRoles != null) { for (Element subRole : (List<Element>) subRoles.getChildren(ROLE_ROLE_ELEMENT)) { role.getRoles().add(subRole.getValue()); } } Element privileges = element.getChild(ROLE_PRIVILEGES_ELEMENT); if (privileges != null) { for (Element privilege : (List<Element>) privileges.getChildren(ROLE_PRIVILEGE_ELEMENT)) { role.getPrivileges().add(privilege.getValue()); } } role.setUserManaged( element.getChildText(ROLE_USER_MANAGED_ELEMENT).equals("true") ? true : false); return role; }
private Document buildRole(Role role) { Element root = new Element(ROLE_REQUEST_ELEMENT); Document doc = new Document().setRootElement(root); Element data = new Element("data"); root.addContent(data); data.addContent(new Element(ROLE_ID_ELEMENT).setText(role.getId())); data.addContent(new Element(ROLE_NAME_ELEMENT).setText(role.getName())); data.addContent(new Element(ROLE_DESCRIPTION_ELEMENT).setText(role.getDescription())); data.addContent( new Element(ROLE_SESSION_TIMEOUT_ELEMENT).setText(role.getSessionTimeout() + "")); Element subRoles = new Element(ROLE_ROLES_ELEMENT); for (String subRole : role.getRoles()) { subRoles.addContent(new Element(ROLE_ROLE_ELEMENT).setText(subRole)); } data.addContent(subRoles); Element privileges = new Element(ROLE_PRIVILEGES_ELEMENT); for (String privilege : role.getPrivileges()) { privileges.addContent(new Element(ROLE_PRIVILEGE_ELEMENT).setText(privilege)); } data.addContent(privileges); data.addContent( new Element(ROLE_USER_MANAGED_ELEMENT).setText(role.isUserManaged() ? "true" : "false")); return doc; }
/** * Returns all known objects in the Catalog (Tables, Views, Databases, and Functions). Some * metadata may be skipped for objects that have a catalog version < the specified "fromVersion". * Skips builtins. */ public TGetAllCatalogObjectsResponse getCatalogObjects(long fromVersion) { TGetAllCatalogObjectsResponse resp = new TGetAllCatalogObjectsResponse(); resp.setObjects(new ArrayList<TCatalogObject>()); resp.setMax_catalog_version(Catalog.INITIAL_CATALOG_VERSION); // Take a lock on the catalog to ensure this update contains a consistent snapshot // of all items in the catalog. catalogLock_.readLock().lock(); try { for (String dbName : getDbNames(null)) { Db db = getDb(dbName); if (db == null) { LOG.error( "Database: " + dbName + " was expected to be in the catalog " + "cache. Skipping database and all child objects for this update."); continue; } if (db.isSystemDb()) continue; TCatalogObject catalogDb = new TCatalogObject(TCatalogObjectType.DATABASE, db.getCatalogVersion()); catalogDb.setDb(db.toThrift()); resp.addToObjects(catalogDb); for (String tblName : db.getAllTableNames()) { TCatalogObject catalogTbl = new TCatalogObject(TCatalogObjectType.TABLE, Catalog.INITIAL_CATALOG_VERSION); Table tbl = db.getTable(tblName); if (tbl == null) { LOG.error( "Table: " + tblName + " was expected to be in the catalog " + "cache. Skipping table for this update."); continue; } // Only add the extended metadata if this table's version is >= // the fromVersion. if (tbl.getCatalogVersion() >= fromVersion) { try { catalogTbl.setTable(tbl.toThrift()); } catch (Exception e) { LOG.debug( String.format( "Error calling toThrift() on table %s.%s: %s", dbName, tblName, e.getMessage()), e); continue; } catalogTbl.setCatalog_version(tbl.getCatalogVersion()); } else { catalogTbl.setTable(new TTable(dbName, tblName)); } resp.addToObjects(catalogTbl); } for (Function fn : db.getFunctions(null, new PatternMatcher())) { TCatalogObject function = new TCatalogObject(TCatalogObjectType.FUNCTION, fn.getCatalogVersion()); function.setFn(fn.toThrift()); resp.addToObjects(function); } } for (DataSource dataSource : getDataSources()) { TCatalogObject catalogObj = new TCatalogObject(TCatalogObjectType.DATA_SOURCE, dataSource.getCatalogVersion()); catalogObj.setData_source(dataSource.toThrift()); resp.addToObjects(catalogObj); } for (HdfsCachePool cachePool : hdfsCachePools_) { TCatalogObject pool = new TCatalogObject(TCatalogObjectType.HDFS_CACHE_POOL, cachePool.getCatalogVersion()); pool.setCache_pool(cachePool.toThrift()); resp.addToObjects(pool); } // Get all roles for (Role role : authPolicy_.getAllRoles()) { TCatalogObject thriftRole = new TCatalogObject(); thriftRole.setRole(role.toThrift()); thriftRole.setCatalog_version(role.getCatalogVersion()); thriftRole.setType(role.getCatalogObjectType()); resp.addToObjects(thriftRole); for (RolePrivilege p : role.getPrivileges()) { TCatalogObject privilege = new TCatalogObject(); privilege.setPrivilege(p.toThrift()); privilege.setCatalog_version(p.getCatalogVersion()); privilege.setType(p.getCatalogObjectType()); resp.addToObjects(privilege); } } // Each update should contain a single "TCatalog" object which is used to // pass overall state on the catalog, such as the current version and the // catalog service id. TCatalogObject catalog = new TCatalogObject(); catalog.setType(TCatalogObjectType.CATALOG); // By setting the catalog version to the latest catalog version at this point, // it ensure impalads will always bump their versions, even in the case where // an object has been dropped. catalog.setCatalog_version(getCatalogVersion()); catalog.setCatalog(new TCatalog(catalogServiceId_)); resp.addToObjects(catalog); // The max version is the max catalog version of all items in the update. resp.setMax_catalog_version(getCatalogVersion()); return resp; } finally { catalogLock_.readLock().unlock(); } }