001package org.nuxeo.ecm.core.api;
002
003import java.io.InvalidObjectException;
004import java.io.ObjectStreamException;
005import java.security.Principal;
006
007/**
008 * Document repository reference including the principal owner of the session.
009 *
010 * @since 7.10
011 * @author Stephane Lacoin at Nuxeo (aka matic)
012 */
013public class InstanceRef implements DocumentRef {
014
015    private static final long serialVersionUID = 1L;
016
017    final String repositoryName;
018
019    final Principal principal;
020
021    final DocumentRef ref;
022
023    transient DocumentModel referent;
024
025    public InstanceRef(DocumentModel doc, Principal principal) {
026        if (doc.getRef() == null) {
027            throw new NullPointerException("document as no reference yet");
028        }
029        referent = doc;
030        repositoryName = doc.getRepositoryName();
031        this.principal = principal;
032        ref = doc.getRef();
033    }
034
035    @Override
036    public int type() {
037        return -1;
038    }
039
040    @Override
041    public Object reference() {
042        return referent;
043    }
044
045    private Object readResolve() throws ObjectStreamException {
046        try {
047            try (CoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
048                referent = session.getDocument(ref);
049                referent.detach(true);
050                return referent;
051            }
052        } catch (RuntimeException cause) {
053            InvalidObjectException error = new InvalidObjectException(
054                    "Cannot refetch " + ref + " from " + repositoryName);
055            error.initCause(cause);
056            throw error;
057        }
058    }
059
060    @Override
061    public int hashCode() {
062        final int prime = 31;
063        int result = 1;
064        result = prime * result + ((repositoryName == null) ? 0 : repositoryName.hashCode());
065        result = prime * result + ((ref == null) ? 0 : ref.hashCode());
066        result = prime * result + ((principal == null) ? 0 : principal.hashCode());
067        return result;
068    }
069
070    @Override
071    public boolean equals(Object obj) {
072        if (this == obj) {
073            return true;
074        }
075        if (obj == null) {
076            return false;
077        }
078        if (getClass() != obj.getClass()) {
079            return false;
080        }
081        InstanceRef other = (InstanceRef) obj;
082        if (repositoryName == null) {
083            if (other.repositoryName != null) {
084                return false;
085            }
086        } else if (!repositoryName.equals(other.repositoryName)) {
087            return false;
088        }
089        if (ref == null) {
090            if (other.ref != null) {
091                return false;
092            }
093        } else if (!ref.equals(other.ref)) {
094            return false;
095        }
096        if (principal == null) {
097            if (other.principal != null) {
098                return false;
099            }
100        } else if (!principal.equals(other.principal)) {
101            return false;
102        }
103        return true;
104    }
105}