用于POJO创建的JAXB Unmarshalling拦截

时间:2013-01-23 14:33:52

标签: java jaxb unmarshalling

我正在使用JAXB编组和解组来在服务器和客户端之间传输数据。由于数据类由服务器和客户端共享,这似乎是正确的做事方式。我在网上找到的例子很适合暂时性的数据,当服务器发出新请求时,这些数据将被丢弃。

但我的一些数据不是暂时的,随后的服务器民意调查正在寻找更新。我在响应中包含了一个时间戳,因此这类请求只需要报告那些已修改过的项目。

我正在寻找的是一种拦截解组过程的方法,以便在POJO创建时我的代码被调用来做它的事情。我不想盲目地创建一个新实例,而是希望使用唯一标识符(它被编码到XML元素节点的属性中)来查找已经获取但已过时的实例,我在缓存中返回该实例并返回该实例。因此,当解组继续时,它将成为真正的更新。

顺便提一下,我的POJO数据对象支持否决权和属性更改支持,而GUI代码使用属性更改事件来更新刷新本身,并且可能会使用否决权来注意用户是否正在修改已经存在的内容在别处更新。下面列出了一个例子:

@Entity(name="Operative")
@Table(name="vOperatives")
@XmlRootElement(name="operative")
public class OperativeEntity 
    extends AbstractEntity {

    public static final String IDENT_PROPERTY = "identity";
    public static final String GIVEN_NAME_PROPERTY = "given-name";
    public static final String FAMILY_NAME_PROPERTY = "family-name";
    public static final String EMAIL_PROPERTY = "email";
    public static final String EMAIL_VERIFIED_PROPERTY = "email-verified";

    private volatile UUID uuid;
    private volatile String givenName;
    private volatile String familyName;
    private volatile String email;
    private volatile boolean emailVerified;

    public OperativeEntity() {
    }

    public OperativeEntity(UUIDFactory factory) {
        setUUID(factory.generate());
    }

    @Id
    @Column(name="uuid", length=40)
    @XmlAttribute(name="uuid")
    public String getIdent() {
        return null == uuid ? null : uuid.toString();
    }

    public void setIdent(String uuid) {
        setUUID(UUID.fromString(uuid));
    }

    @Transient
    @XmlTransient
    public UUID getUUID() {
        return uuid;
    }

    public void setUUID(UUID id) {
        if (null == id)
            throw new NullPointerException("No UUID given");
        uuid = id;
    }

    @Column(name="gvn", length=60, nullable=true)
    @XmlAttribute(name="given-name")
    public String getGivenName() {
        return givenName;
    }

    public void setGivenName(String newName)
        throws PropertyVetoException {

        if (null == givenName && null == newName)
            return;

        else if (null != givenName && givenName.equals(newName))
            return;

        final String oldName = givenName;
        fireVetoableChange(GIVEN_NAME_PROPERTY, oldName, newName);
        givenName = newName;
        firePropertyChange(GIVEN_NAME_PROPERTY, oldName, newName);
    }

    @Column(name="fmly", length=60, nullable=true)
    @XmlAttribute(name="family-name")
    public String getFamilyName() {
        return familyName;
    }

    public void setFamilyName(String newName)
        throws PropertyVetoException {

        if (null == familyName && null == newName)
            return;

        else if (null != familyName && familyName.equals(newName))
            return;

        final String oldName = familyName;
        fireVetoableChange(FAMILY_NAME_PROPERTY, oldName, newName);
        familyName = newName;
        firePropertyChange(FAMILY_NAME_PROPERTY, oldName, newName);
    }

    @Column(name="eMail", nullable=true)
    @XmlAttribute(name="e-mail")
    public String getEMail() {
        return email;
    }

    public void setEMail(String newEMail)
        throws PropertyVetoException {

        if (null == email && null == newEMail)
            return;

        else if (null != email && email.equals(newEMail))
            return;

        final String oldEMail = email;
        fireVetoableChange(EMAIL_PROPERTY, oldEMail, newEMail);
        email = newEMail;
        firePropertyChange(EMAIL_PROPERTY, oldEMail, newEMail);
    }

    @Column(name="vrfd", nullable=true)
    @XmlAttribute(name="email-verified")
    public boolean isEMailVerified() {
        return emailVerified;
    }

    public void setEMailVerified(boolean newVerified)
        throws PropertyVetoException {

        if (newVerified == emailVerified)
            return;

        final boolean oldVerified = emailVerified;
        fireVetoableChange(EMAIL_VERIFIED_PROPERTY, oldVerified, newVerified);
        emailVerified = newVerified;
        firePropertyChange(EMAIL_VERIFIED_PROPERTY, oldVerified, newVerified);
    }
}

0 个答案:

没有答案