use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class VotingView method vote.
/**
* add a vote from the given slingId to this voting
* @param slingId the slingId which is voting
* @param vote true for a yes-vote, false for a no-vote
*/
public void vote(final String slingId, final Boolean vote, final String leaderElectionId) {
if (logger.isDebugEnabled()) {
logger.debug("vote: slingId=" + slingId + ", vote=" + vote);
}
Resource r = getResource();
if (r == null) {
logger.error("vote: no resource set. slingId = " + slingId + ", vote=" + vote);
return;
}
Resource members = r.getChild("members");
if (members == null) {
logger.error("vote: no members resource available for " + r + ". slingId = " + slingId + ", vote=" + vote);
return;
}
final Resource memberResource = members.getChild(slingId);
if (memberResource == null) {
if (vote == null || !vote) {
// if I wanted to vote no or empty, then it's no big deal
// that I can't find my entry ..
logger.debug("vote: no memberResource found for slingId=" + slingId + ", vote=" + vote + ", resource=" + getResource());
} else {
// if I wanted to vote yes, then it is a big deal that I can't find myself
logger.error("vote: no memberResource found for slingId=" + slingId + ", vote=" + vote + ", resource=" + getResource());
}
return;
}
final ModifiableValueMap memberMap = memberResource.adaptTo(ModifiableValueMap.class);
if (vote == null) {
if (memberMap.containsKey("vote")) {
logger.info("vote: removing vote (vote==null) of slingId=" + slingId + " on: " + this);
} else {
logger.debug("vote: removing vote (vote==null) of slingId=" + slingId + " on: " + this);
}
memberMap.remove("vote");
} else {
boolean shouldVote = true;
try {
if (memberMap.containsKey("vote")) {
Object v = memberMap.get("vote");
if (v instanceof Property) {
Property p = (Property) v;
if (p.getBoolean() == vote) {
logger.debug("vote: already voted, with same vote (" + vote + "), not voting again");
shouldVote = false;
}
} else if (v instanceof Boolean) {
Boolean b = (Boolean) v;
if (b == vote) {
logger.debug("vote: already voted, with same vote (" + vote + "), not voting again");
shouldVote = false;
}
}
}
} catch (ValueFormatException e) {
logger.warn("vote: got a ValueFormatException: " + e, e);
} catch (RepositoryException e) {
logger.warn("vote: got a RepositoryException: " + e, e);
}
if (shouldVote) {
logger.info("vote: slingId=" + slingId + " is voting vote=" + vote + " on " + getResource());
memberMap.put("vote", vote);
memberMap.put("votedAt", Calendar.getInstance());
String currentLeaderElectionId = memberMap.get("leaderElectionId", String.class);
if (leaderElectionId != null && (currentLeaderElectionId == null || !currentLeaderElectionId.equals(leaderElectionId))) {
// SLING-5030 : to ensure leader-step-down after being
// isolated from the cluster, the leaderElectionId must
// be explicitly set upon voting.
// for 99% of the cases not be necessary,
// for the rejoin-after-isolation case however it is
logger.info("vote: changing leaderElectionId on vote to " + leaderElectionId);
memberMap.put("leaderElectionId", leaderElectionId);
memberMap.put("leaderElectionIdCreatedAt", new Date());
}
}
}
try {
getResource().getResourceResolver().commit();
} catch (PersistenceException e) {
logger.error("vote: PersistenceException while voting: " + e, e);
}
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class View method remove.
/**
* Delete this view from the repository
*/
public void remove(boolean logErrorOnFailure) {
final ResourceResolver resourceResolver = getResource().getResourceResolver();
try {
resourceResolver.delete(getResource());
resourceResolver.commit();
} catch (PersistenceException pe) {
if (logErrorOnFailure) {
logger.error("remove: Could not remove node: " + pe, pe);
} else {
logger.info("remove: Could not remove node: " + pe);
}
resourceResolver.refresh();
}
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class VotingHandler method promote.
/**
* Promote a particular voting to be the new established view
*/
private void promote(final ResourceResolver resourceResolver, final Resource winningVoteResource) throws PersistenceException {
Resource previousViewsResource = ResourceHelper.getOrCreateResource(resourceResolver, config.getPreviousViewPath());
final Resource establishedViewsResource = ResourceHelper.getOrCreateResource(resourceResolver, config.getEstablishedViewPath());
final Resource ongoingVotingsResource = ResourceHelper.getOrCreateResource(resourceResolver, config.getOngoingVotingsPath());
if (logger.isDebugEnabled()) {
logger.debug("promote: previousViewsResource=" + previousViewsResource.getPath());
logger.debug("promote: establishedViewsResource=" + establishedViewsResource.getPath());
logger.debug("promote: ongoingVotingsResource=" + ongoingVotingsResource.getPath());
logger.debug("promote: winningVoteResource=" + winningVoteResource.getPath());
}
// step 1: remove any nodes under previousViews
final Iterator<Resource> it1 = previousViewsResource.getChildren().iterator();
try {
while (it1.hasNext()) {
Resource previousView = it1.next();
resourceResolver.delete(previousView);
}
} catch (PersistenceException e) {
// if we cannot delete, apply workaround suggested in SLING-3785
logger.error("promote: Could not delete a previous view - trying move next: " + e, e);
ResourceHelper.moveResource(previousViewsResource, config.getPreviousViewPath() + "_trash_" + UUID.randomUUID().toString());
logger.info("promote: recreating the previousviews node");
previousViewsResource = ResourceHelper.getOrCreateResource(resourceResolver, config.getPreviousViewPath());
}
// step 2: retire the existing established view.
// Note that there must always only be one. But if there's more, retire
// them all now.
final Iterator<Resource> it = establishedViewsResource.getChildren().iterator();
boolean first = true;
while (it.hasNext()) {
Resource retiredView = it.next();
if (first) {
first = !first;
if (logger.isDebugEnabled()) {
logger.debug("promote: moving the old established view to previous views: " + retiredView.getPath());
}
ResourceHelper.moveResource(retiredView, previousViewsResource.getPath() + "/" + retiredView.getName());
} else {
if (logger.isDebugEnabled()) {
logger.debug("promote: retiring an erroneously additionally established node " + retiredView.getPath());
}
resourceResolver.delete(retiredView);
}
}
// step 3: move the winning vote resource under the
// establishedViewsResource
// 3a: set the leaderid
final Iterator<Resource> it2 = winningVoteResource.getChild("members").getChildren().iterator();
String leaderElectionId = null;
String leaderid = null;
int membersCount = 0;
while (it2.hasNext()) {
Resource aMember = it2.next();
membersCount++;
String leid = aMember.adaptTo(ValueMap.class).get("leaderElectionId", String.class);
if (leaderElectionId == null || (leid != null && leid.compareTo(leaderElectionId) < 0)) {
leaderElectionId = leid;
leaderid = aMember.getName();
}
}
if (logger.isDebugEnabled()) {
logger.debug("promote: leader is " + leaderid + " - with leaderElectionId=" + leaderElectionId);
}
ModifiableValueMap winningVoteMap = winningVoteResource.adaptTo(ModifiableValueMap.class);
winningVoteMap.put("leaderId", leaderid);
winningVoteMap.put("leaderElectionId", leaderElectionId);
winningVoteMap.put("promotedAt", Calendar.getInstance());
winningVoteMap.put("promotedBy", slingId);
// 3b: move the result under /established
final String newEstablishedViewPath = establishedViewsResource.getPath() + "/" + winningVoteResource.getName();
logger.info("promote: promoting to new established node (#members: " + membersCount + ", path: " + newEstablishedViewPath + ")");
ResourceHelper.moveResource(winningVoteResource, newEstablishedViewPath);
// step 4: delete all ongoing votings...
final Iterable<Resource> ongoingVotingsChildren = ongoingVotingsResource.getChildren();
if (ongoingVotingsChildren != null) {
Iterator<Resource> it4 = ongoingVotingsChildren.iterator();
while (it4.hasNext()) {
Resource anOngoingVoting = it4.next();
logger.info("promote: deleting ongoing voting: " + anOngoingVoting.getName());
resourceResolver.delete(anOngoingVoting);
}
}
// step 5: make sure there are no duplicate ongoingVotings nodes
// created. if so, cleanup
final Iterator<Resource> it5 = ongoingVotingsResource.getParent().getChildren().iterator();
while (it5.hasNext()) {
Resource resource = it5.next();
if (!resource.getPath().startsWith(config.getOngoingVotingsPath())) {
continue;
}
if (resource.getPath().equals(config.getOngoingVotingsPath())) {
// then it's [0] so to speak .. which we're not cleaning up
continue;
}
logger.warn("promote: cleaning up a duplicate ongoingVotingPath: " + resource.getPath());
resourceResolver.delete(resource);
}
logger.debug("promote: done with promotiong. saving.");
resourceResolver.commit();
logger.info("promote: promotion done (#members: " + membersCount + ", path: " + newEstablishedViewPath + ")");
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class JcrResourceBundleTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
cleanRepository();
RepositoryUtil.registerNodeType(getSession(), getClass().getResourceAsStream("/SLING-INF/nodetypes/jcrlanguage.cnd"));
RepositoryUtil.registerNodeType(getSession(), getClass().getResourceAsStream("/SLING-INF/nodetypes/message.cnd"));
resolver = new ResourceResolver() {
@Override
public Iterator<Resource> findResources(String query, String language) {
try {
final Query q = getSession().getWorkspace().getQueryManager().createQuery(query, language);
final QueryResult result = q.execute();
final NodeIterator nodes = result.getNodes();
return new Iterator<Resource>() {
@Override
public boolean hasNext() {
return nodes.hasNext();
}
@Override
public Resource next() {
final Node node = nodes.nextNode();
return new TestResource(resolver, node);
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
} catch (NamingException ne) {
return null;
} catch (RepositoryException re) {
return null;
}
}
@Override
public Resource getResource(Resource base, String path) {
// TODO Auto-generated method stub
return null;
}
@Override
public Resource getResource(String path) {
try {
final Node n = getSession().getNode(path);
return new TestResource(resolver, n);
} catch (NamingException ne) {
//ignore
} catch (RepositoryException re) {
//ignore
}
return null;
}
@Override
public String[] getSearchPath() {
return new String[] { "/apps/", "/libs/" };
}
@Override
public Iterator<Resource> listChildren(final Resource parent) {
try {
final Node n = getSession().getNode(parent.getPath());
final NodeIterator nodes = n.getNodes();
return new Iterator<Resource>() {
@Override
public boolean hasNext() {
return nodes.hasNext();
}
@Override
public Resource next() {
final Node node = nodes.nextNode();
return new TestResource(resolver, node);
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
} catch (final RepositoryException re) {
// ignore
return null;
} catch (final NamingException e) {
// ignore
return null;
}
}
@Override
public String map(HttpServletRequest request, String resourcePath) {
// TODO Auto-generated method stub
return null;
}
@Override
public String map(String resourcePath) {
// TODO Auto-generated method stub
return null;
}
@Override
public Iterator<Map<String, Object>> queryResources(String query, String language) {
try {
final Query q = getSession().getWorkspace().getQueryManager().createQuery(query, language);
final QueryResult result = q.execute();
final String[] colNames = result.getColumnNames();
final RowIterator rows = result.getRows();
return new Iterator<Map<String, Object>>() {
@Override
public boolean hasNext() {
return rows.hasNext();
}
@Override
public Map<String, Object> next() {
Map<String, Object> row = new HashMap<String, Object>();
try {
Value[] values = rows.nextRow().getValues();
for (int i = 0; i < values.length; i++) {
Value v = values[i];
if (v != null) {
row.put(colNames[i], values[i].getString());
}
}
} catch (RepositoryException re) {
// ignore
}
return row;
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
} catch (NamingException ne) {
return null;
} catch (RepositoryException re) {
return null;
}
}
@Override
public Resource resolve(HttpServletRequest request, String absPath) {
// TODO Auto-generated method stub
return null;
}
@Override
public Resource resolve(HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
@Override
public Resource resolve(String absPath) {
// TODO Auto-generated method stub
return null;
}
@Override
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
// TODO Auto-generated method stub
return null;
}
@Override
public void close() {
// nothing to do
}
@Override
public String getUserID() {
return null;
}
@Override
public boolean isLive() {
return true;
}
@Override
public ResourceResolver clone(Map<String, Object> authenticationInfo) {
return null;
}
@Override
public Iterator<String> getAttributeNames() {
return null;
}
@Override
public Object getAttribute(String name) {
return null;
}
@Override
public Iterable<Resource> getChildren(Resource parent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void delete(Resource resource) throws PersistenceException {
// TODO Auto-generated method stub
}
@Override
public Resource create(Resource parent, String name, Map<String, Object> properties) throws PersistenceException {
// TODO Auto-generated method stub
return null;
}
@Override
public void revert() {
// TODO Auto-generated method stub
}
@Override
public void commit() throws PersistenceException {
// TODO Auto-generated method stub
}
@Override
public boolean hasChanges() {
// TODO Auto-generated method stub
return false;
}
@Override
public String getParentResourceType(Resource resource) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getParentResourceType(String resourceType) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isResourceType(final Resource resource, final String resourceType) {
return resourceType.equals(resource.getResourceType());
}
@Override
public void refresh() {
// TODO Auto-generated method stub
}
@Override
public Resource getParent(Resource child) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean hasChildren(Resource resource) {
// TODO Auto-generated method stub
return false;
}
@Override
public Resource copy(String srcAbsPath, String destAbsPath) throws PersistenceException {
// TODO Auto-generated method stub
return null;
}
@Override
public Resource move(String srcAbsPath, String destAbsPath) throws PersistenceException {
// TODO Auto-generated method stub
return null;
}
};
createTestContent();
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class MongoDBNoSqlResourceProviderIT method testRoot.
@Override
protected Resource testRoot() {
if (this.testRoot == null) {
try {
Resource root = context.resourceResolver().getResource("/");
Resource providerRoot = root.getChild("test");
if (providerRoot == null) {
providerRoot = context.resourceResolver().create(root, "test", ImmutableMap.<String, Object>of(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED));
}
this.testRoot = context.resourceResolver().create(providerRoot, UUID.randomUUID().toString(), ImmutableMap.<String, Object>of(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED));
} catch (PersistenceException ex) {
throw new RuntimeException(ex);
}
}
return this.testRoot;
}
Aggregations