路径: src/main/java/org/apache/ambari/server/orm/entities/自定义实体类.java
对应的数据表为 auditlog_table,添加注解@Table、@Entity、@Id、@Column;自定义属性:id和note;添加getter/setter方法。
还需要在 /src/main/resources/META-INF/persistence.xml里面添加entityTest的信息。
路径: src/main/java/org/apache/ambari/server/orm/dao/TestUserDao.java
sql语句要遵循JPA规范,如果要指定表的某一个属性,比如id,则id的表现形式为 test.id。
路径: src/main/java/org/apache/ambari/server/api/services/users/TestUserService.java
路径: org.apache.ambari.server.controller.spi.Resource.java
路径: org.apache.ambari.server.api.resources.TestUserResourceDefinition.java
路径: org.apache.ambari.server.api.resources.ResourceInstanceFactoryImpl.java
路径: org.apache.ambari.server.controller.ResourceProviderFactory.java
路径: org.apache.ambari.server.controller.internal.AbstractControllerResourceProvider.java
路径: org.apache.ambari.server.controller.internal.AuditlogResourceProvider.java
public class TestUserResourceProvider extends AbstractControllerResourceProvider
public static final Logger LOG = LoggerFactory.getLogger(TestUserResourceProvider.class);
//定义属性值
public static final String USER_RESOURCE_CATEGORY = "auditlog";
public static final String AUDIT_ID_PROPERTY_ID = "id";
public static final String AUDIT_TIME_PROPERTY_ID = "time";
public static final String AUDIT_USER_PROPERTY_ID = "user";
public static final String AUDIT_STATUS_PROPERTY_ID = "status";
public static final String AUDIT_OPERATION_PROPERTY_ID = "operation";
public static final String AUDIT_REMOTEIP_PROPERTY_ID = "remoteIp";
public static final String AUDIT_NOTE_PROPERTY_ID = "note";
public static final String USER_USERNAME_PROPERTY_ID = USER_RESOURCE_CATEGORY + "/" + AUDIT_ID_PROPERTY_ID;
public static final String USER_DISPLAY_NAME_PROPERTY_ID = USER_RESOURCE_CATEGORY + "/" + AUDIT_TIME_PROPERTY_ID;
public static final String USER_LOCAL_USERNAME_PROPERTY_ID = USER_RESOURCE_CATEGORY + "/" + AUDIT_USER_PROPERTY_ID;
public static final String USER_ACTIVE_PROPERTY_ID = USER_RESOURCE_CATEGORY + "/" + AUDIT_STATUS_PROPERTY_ID;
public static final String USER_CREATE_TIME_PROPERTY_ID = USER_RESOURCE_CATEGORY + "/" + AUDIT_OPERATION_PROPERTY_ID;
public static final String USER_CONSECUTIVE_FAILURES_PROPERTY_ID = USER_RESOURCE_CATEGORY + "/" + AUDIT_REMOTEIP_PROPERTY_ID;
public static final String USER_ADMIN_PROPERTY_ID = USER_RESOURCE_CATEGORY + "/" + AUDIT_NOTE_PROPERTY_ID;
/**
* 用户资源的键属性id。
*/
private static Map<Resource.Type, String> keyPropertyIds = ImmutableMap.<Resource.Type, String>builder()
.put(Resource.Type.TestUser, AUDIT_ID_PROPERTY_ID)
.build();
private static Set<String> propertyIds = Sets.newHashSet(
USER_USERNAME_PROPERTY_ID,
USER_DISPLAY_NAME_PROPERTY_ID,
USER_LOCAL_USERNAME_PROPERTY_ID,
USER_ACTIVE_PROPERTY_ID,
USER_CREATE_TIME_PROPERTY_ID,
USER_CONSECUTIVE_FAILURES_PROPERTY_ID,
USER_ADMIN_PROPERTY_ID
);
/**
* 为给定的管理控制器创建新的资源提供程序。
* @param managementController
*/
@AssistedInject
TestUserResourceProvider(@Assisted AmbariManagementController managementController) {
super(Resource.Type.TestUser, propertyIds, keyPropertyIds, managementController);
setRequiredCreateAuthorizations(EnumSet.of(RoleAuthorization.AMBARI_MANAGE_USERS));
setRequiredDeleteAuthorizations(EnumSet.of(RoleAuthorization.AMBARI_MANAGE_USERS));
}
// 4) getResource()
@Override
public Set<Resource> getResources(Request request, Predicate predicate)
throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
final Set<TestUserRequest> requests = new HashSet<TestUserRequest>();
if (predicate == null) {
requests.add(getRequest(null));
} else {
for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) {
requests.add(getRequest(propertyMap));
}
}
Set<TestUserResponse> responses = getResources(new Command<Set<TestUserResponse>>() {
@Override
public Set<TestUserResponse> invoke() throws AmbariException, AuthorizationException {
return getManagementController().getTestUser(requests);
}
});
if (LOG.isDebugEnabled()) {
LOG.debug("Found clusters matching getClusters request"
+ ", clusterResponseCount=" + responses.size());
}
Set<String> requestedIds = getRequestPropertyIds(request, predicate);
Set<Resource> resources = new HashSet<Resource>();
for (TestUserResponse auditlogResponse : responses) {
ResourceImpl resource = new ResourceImpl(Resource.Type.TestUser);
String note =testUserResponse.getNote();
String[] sourceNoteArray = note.split(", ");
String time = sourceNoteArray[0].replace("T", " ").replaceAll("\\..*", "");
String user = sourceNoteArray[1].replace("User(", "").replace(")", "");
String status = note.split("Status\\(")[1].split("\\)")[0];
boolean isOperation = note.contains("Operation");
String operation = null;
if (isOperation) {
operation = note.split("Operation\\(")[1].split("\\),")[0];
} else {
operation = "";
}
boolean isRemoteIp = note.contains("RemoteIp");
String remoteIp = null;
if (isRemoteIp) {
remoteIp = note.split("RemoteIp\\(")[1].split("\\)")[0];
} else {
remoteIp = "";
}
// 该方法是判断requestedIds是否包含AUDIT_ID_PROPERTY_ID,如果包含则返回true,将值存入resource中;反之则不存入。
setResourceProperty(resource, AUDIT_ID_PROPERTY_ID,
testUserResponse.getId(), requestedIds);
setResourceProperty(resource, AUDIT_TIME_PROPERTY_ID,
time, requestedIds);
setResourceProperty(resource, AUDIT_USER_PROPERTY_ID,
user, requestedIds);
setResourceProperty(resource, AUDIT_STATUS_PROPERTY_ID,
status, requestedIds);
setResourceProperty(resource, AUDIT_OPERATION_PROPERTY_ID,
operation, requestedIds);
setResourceProperty(resource, AUDIT_REMOTEIP_PROPERTY_ID,
remoteIp, requestedIds);
setResourceProperty(resource, AUDIT_NOTE_PROPERTY_ID,
note, requestedIds);
resources.add(resource);
}
return resources;
}
//5) 类内其他方法
/**
* ----- ResourceProvider ------------------------------------------------
*/
@Override
protected Set<String> getPKPropertyIds() {
return new HashSet<>(keyPropertyIds.values());
}
private TestUserRequest getRequest(Map<String, Object> properties) {
if (properties == null) {
return new TestUserRequest(null);
}
TestUserRequest request = new TestUserRequest ((String)properties.get(USER_USERNAME_PROPERTY_ID));
request.setId((String) properties.get(AUDIT_ID_PROPERTY_ID));
request.setNote((String) properties.get(AUDIT_NOTE_PROPERTY_ID));
return request;
}
路径: src/main/java/org/apache/ambari/server/controller/TestUserRequest.java
路径: src/main/java/org/apache/ambari/server/controller/TestUserResponse.java
mbariManagementController类内添加getTestUser()
路径:org.apache.ambari.server.controller.AmbariManagementController.java
在AmbariManagementControllerImpl类内实现getTestUser()