Skip to content

Instantly share code, notes, and snippets.

@fjunior87
Created August 13, 2016 21:18
Show Gist options
  • Select an option

  • Save fjunior87/02df3325d80037c957791c39f2a7110f to your computer and use it in GitHub Desktop.

Select an option

Save fjunior87/02df3325d80037c957791c39f2a7110f to your computer and use it in GitHub Desktop.
@Component
@Service
public class ContactAdapterFactory implements AdapterFactory{
@Property(name="adapters")
public static final String[] ADAPTER_CLASSES = {
Contact.class.getName()
};
@Property(name="adaptables")
public static final String[] ADAPTABLE_CLASSES = {
Resource.class.getName()
};
@Override
@SuppressWarnings("unchecked")
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if(adaptable instanceof Resource &&
type.equals(Contact.class)) {
Contact contact = new Contact();
Resource resource = (Resource)adaptable;
ValueMap valueMap = ResourceUtil.getValueMap((Resource)adaptable);
contact.setName(valueMap.get("name", String.class));
contact.setEmail(valueMap.get("email", String.class));
contact.setPhone(valueMap.get("phone", String.class));
contact.setAddress(valueMap.get("address", String.class));
Resource contactImage = resource.getChild("contactImage");
if(contactImage != null) {
contact.setContactImage(contactImage.getPath());
}
System.out.println(contact);
return (AdapterType)contact;
} else {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment