Search in sources :

Example 21 with ValueMap

use of org.apache.sling.api.resource.ValueMap in project sling by apache.

the class View method getViewId.

/**
     * Returns the id of this view.
     * @return the id of this view
     */
public String getViewId() {
    final ValueMap props = getResource().adaptTo(ValueMap.class);
    if (props == null) {
        // avoid a NPE below
        logger.warn("getViewId: could not get properties of " + getResource().getPath() + ", using resource name instead: " + getResource().getName());
        return getResource().getName();
    }
    final String clusterId = props.get(VIEW_PROPERTY_CLUSTER_ID, String.class);
    if (clusterId != null && clusterId.length() > 0) {
        return clusterId;
    } else {
        return getResource().getName();
    }
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap)

Example 22 with ValueMap

use of org.apache.sling.api.resource.ValueMap in project sling by apache.

the class VotingView method getVote.

/**
     * Get the vote of the instance with the given slingId
     * @param slingId
     * @return null if that instance did not vote yet (or the structure
     * is faulty), true if the instance voted yes, false if it voted no
     */
public Boolean getVote(String slingId) {
    Resource members = getResource().getChild("members");
    if (members == null) {
        return null;
    }
    final Resource memberResource = members.getChild(slingId);
    if (memberResource == null) {
        return null;
    }
    final ValueMap properties = memberResource.adaptTo(ValueMap.class);
    if (properties == null) {
        return null;
    }
    final Boolean vote = properties.get("vote", Boolean.class);
    return vote;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource)

Example 23 with ValueMap

use of org.apache.sling.api.resource.ValueMap in project sling by apache.

the class VotingView method newVoting.

/**
     * Create a new voting with the given list of instances, the given
     * voting/view id and the given slingid of the initiator.
     * @param newViewId the new voting/view id
     * @param initiatorId the slingid of the initiator
     * @param liveInstances the list of live instances to add to the voting
     * @return a DAO object representing the voting
     */
public static VotingView newVoting(final ResourceResolver resourceResolver, final Config config, final String newViewId, String initiatorId, final Set<String> liveInstances) throws PersistenceException {
    if (!liveInstances.contains(initiatorId)) {
        // SLING-4617 : a voting, on a single instance, was created without the local instance
        // this should in no case happen - the local instance should always be part of the live
        // instances. if that's not the case, then something's fishy and we should not create
        // the new voting - and instead rely on a retry later.
        logger.warn("newVoting: liveInstances does not include initiatorId (local instance) - not creating new, invalid, voting");
        return null;
    }
    final Resource votingResource = ResourceHelper.getOrCreateResource(resourceResolver, config.getOngoingVotingsPath() + "/" + newViewId);
    final ModifiableValueMap votingMap = votingResource.adaptTo(ModifiableValueMap.class);
    votingMap.put("votingStart", Calendar.getInstance());
    String clusterId = null;
    Calendar clusterIdDefinedAt = null;
    String clusterIdDefinedBy = null;
    final View currentlyEstablishedView = ViewHelper.getEstablishedView(resourceResolver, config);
    if (currentlyEstablishedView != null) {
        final ValueMap establishedViewValueMap = currentlyEstablishedView.getResource().adaptTo(ValueMap.class);
        clusterId = establishedViewValueMap.get(VIEW_PROPERTY_CLUSTER_ID, String.class);
        if (clusterId == null || clusterId.length() == 0) {
            clusterId = currentlyEstablishedView.getResource().getName();
        }
        Date date = establishedViewValueMap.get(VIEW_PROPERTY_CLUSTER_ID_DEFINED_AT, Date.class);
        if (date != null) {
            clusterIdDefinedAt = Calendar.getInstance();
            clusterIdDefinedAt.setTime(date);
        }
        clusterIdDefinedBy = establishedViewValueMap.get(VIEW_PROPERTY_CLUSTER_ID_DEFINED_BY, String.class);
    }
    if (clusterId == null || clusterId.length() == 0) {
        clusterId = newViewId;
        clusterIdDefinedAt = Calendar.getInstance();
    }
    votingMap.put(VIEW_PROPERTY_CLUSTER_ID, clusterId);
    if (clusterIdDefinedAt != null) {
        votingMap.put(VIEW_PROPERTY_CLUSTER_ID_DEFINED_AT, clusterIdDefinedAt);
    }
    if (clusterIdDefinedBy == null || clusterIdDefinedBy.length() == 0) {
        clusterIdDefinedBy = initiatorId;
    }
    votingMap.put(VIEW_PROPERTY_CLUSTER_ID_DEFINED_BY, clusterIdDefinedBy);
    final Resource membersResource = resourceResolver.create(votingResource, "members", null);
    final Iterator<String> it = liveInstances.iterator();
    while (it.hasNext()) {
        String memberId = it.next();
        Map<String, Object> properties = new HashMap<String, Object>();
        if (memberId.equals(initiatorId)) {
            properties.put("initiator", true);
            properties.put("vote", true);
            properties.put("votedAt", Calendar.getInstance());
        }
        Resource instanceResource = ResourceHelper.getOrCreateResource(resourceResolver, config.getClusterInstancesPath() + "/" + memberId);
        String leaderElectionId = instanceResource.adaptTo(ValueMap.class).get("leaderElectionId", String.class);
        properties.put("leaderElectionId", leaderElectionId);
        resourceResolver.create(membersResource, memberId, properties);
    }
    logger.debug("newVoting: committing new voting: newViewId=" + newViewId + ", initiatorId=" + initiatorId + ", resource=" + votingResource + ", #members: " + liveInstances.size() + ", members: " + liveInstances);
    resourceResolver.commit();
    logger.info("newVoting: new voting started: newViewId=" + newViewId + ", initiatorId=" + initiatorId + ", resource=" + votingResource + ", #members: " + liveInstances.size() + ", members: " + liveInstances);
    return new VotingView(votingResource);
}
Also used : HashMap(java.util.HashMap) Calendar(java.util.Calendar) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource) View(org.apache.sling.discovery.impl.common.View) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Date(java.util.Date)

Example 24 with ValueMap

use of org.apache.sling.api.resource.ValueMap in project sling by apache.

the class VotingView method isInitiatedBy.

/**
     * Checks whether this voting was initiated by the given slingId
     * @return whether this voting was initiated by the given slingId
     */
public boolean isInitiatedBy(final String slingId) {
    Resource r = getResource();
    if (r == null) {
        return false;
    }
    Resource members = r.getChild("members");
    if (members == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("isInitiatedBy: slingId=" + slingId + ", members null!");
        }
        return false;
    }
    final Resource memberResource = members.getChild(slingId);
    if (memberResource == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("isInitiatedBy: slingId=" + slingId + ", memberResource null!");
        }
        return false;
    }
    final ValueMap properties = memberResource.adaptTo(ValueMap.class);
    if (properties == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("isInitiatedBy: slingId=" + slingId + ", properties null!");
        }
        return false;
    }
    final Boolean initiator = properties.get("initiator", Boolean.class);
    boolean result = initiator != null && initiator;
    if (logger.isDebugEnabled()) {
        logger.debug("isInitiatedBy: slingId=" + slingId + ", initiator=" + initiator + ", result=" + result);
    }
    return result;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource)

Example 25 with ValueMap

use of org.apache.sling.api.resource.ValueMap in project sling by apache.

the class VotingView method toString.

@Override
public String toString() {
    try {
        final Resource members = getResource().getChild("members");
        String initiatorId = null;
        final StringBuilder sb = new StringBuilder();
        if (members != null) {
            Iterator<Resource> it = members.getChildren().iterator();
            while (it.hasNext()) {
                Resource r = it.next();
                if (sb.length() != 0) {
                    sb.append(", ");
                }
                sb.append(r.getName());
                ValueMap properties = r.adaptTo(ValueMap.class);
                if (properties != null) {
                    Boolean initiator = properties.get("initiator", Boolean.class);
                    if (initiator != null && initiator) {
                        initiatorId = r.getName();
                    }
                }
            }
        }
        return "a VotingView[viewId=" + getViewId() + ", id=" + getResource().getName() + ", initiator=" + initiatorId + ", members=" + sb + "]";
    } catch (Exception e) {
        return "a VotingView[" + super.toString() + "]";
    }
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource) ValueFormatException(javax.jcr.ValueFormatException) RepositoryException(javax.jcr.RepositoryException) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Aggregations

ValueMap (org.apache.sling.api.resource.ValueMap)278 Resource (org.apache.sling.api.resource.Resource)205 Test (org.junit.Test)160 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)59 HashMap (java.util.HashMap)54 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)44 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)26 Map (java.util.Map)21 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)21 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)20 RewriterResponse (org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse)20 Expectations (org.jmock.Expectations)20 ArrayList (java.util.ArrayList)18 PersistenceException (org.apache.sling.api.resource.PersistenceException)17 Calendar (java.util.Calendar)16 Date (java.util.Date)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)12 InputStream (java.io.InputStream)11 Node (javax.jcr.Node)11