/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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 org.wso2.carbon.identity.application.common.model;
import org.apache.axiom.om.OMElement;
import org.apache.commons.collections.CollectionUtils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class LocalAuthenticatorConfig implements Serializable {
private static final long serialVersionUID = 3363298518257599291L;
protected String name;
protected String displayName;
protected boolean enabled;
protected Property[] properties = new Property[0];
/*
* <LocalAuthenticatorConfig> <Name></Name> <DisplayName></DisplayName> <IsEnabled></IsEnabled>
* <Properties></Properties> </LocalAuthenticatorConfig>
*/
public static LocalAuthenticatorConfig build(OMElement localAuthenticatorConfigOM) {
LocalAuthenticatorConfig localAuthenticatorConfig = new LocalAuthenticatorConfig();
if (localAuthenticatorConfigOM == null) {
return new LocalAuthenticatorConfig();
}
Iterator<?> members = localAuthenticatorConfigOM.getChildElements();
while (members.hasNext()) {
OMElement member = (OMElement) members.next();
if ("Name".equals(member.getLocalName())) {
localAuthenticatorConfig.setName(member.getText());
} else if ("DisplayName".equals(member.getLocalName())) {
localAuthenticatorConfig.setDisplayName(member.getText());
} else if ("IsEnabled".equals(member.getLocalName())) {
if (member.getText() != null && member.getText().trim().length() > 0) {
localAuthenticatorConfig.setEnabled(Boolean.parseBoolean(member.getText()));
}
} else if ("Properties".equals(member.getLocalName())) {
Iterator<?> propertiesIter = member.getChildElements();
ArrayList<Property> propertiesArrList = new ArrayList<Property>();
if (propertiesIter != null) {
while (propertiesIter.hasNext()) {
OMElement propertiesElement = (OMElement) (propertiesIter.next());
Property prop = Property.build(propertiesElement);
if (prop != null) {
propertiesArrList.add(prop);
}
}
}
if (CollectionUtils.isNotEmpty(propertiesArrList)) {
Property[] propertiesArr = propertiesArrList.toArray(new Property[0]);
localAuthenticatorConfig.setProperties(propertiesArr);
}
}
}
return localAuthenticatorConfig;
}
/**
* @return
*/
public String getName() {
return name;
}
/**
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return
*/
public boolean isEnabled() {
return enabled;
}
/**
* @param enabled
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/**
* @return
*/
public boolean isValid() {
return true;
}
/**
* @return
*/
public Property[] getProperties() {
return properties;
}
/**
* @param properties
*/
public void setProperties(Property[] properties) {
if (properties == null) {
return;
}
Set<Property> propertySet = new HashSet<Property>(Arrays.asList(properties));
this.properties = propertySet.toArray(new Property[propertySet.size()]);
}
/**
* @return
*/
public String getDisplayName() {
return displayName;
}
/**
* @param displayName
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof LocalAuthenticatorConfig)) {
return false;
}
LocalAuthenticatorConfig that = (LocalAuthenticatorConfig) o;
if (name != null ? !name.equals(that.name) : that.name != null)
return false;
return true;
}
@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
}