Package org.apache.sling.api.resource

Examples of org.apache.sling.api.resource.ModifiableValueMap


            // SLING-2879 - revert/refresh resourceResolver here to work
            // around a potential issue with jackrabbit in a clustered environment
            resourceResolver.revert();
            resourceResolver.refresh();

            final ModifiableValueMap myInstanceMap = myInstance.adaptTo(ModifiableValueMap.class);
            final Set<String> keys = new HashSet<String>(myInstanceMap.keySet());
            for(final String key : keys) {
                if (newProps.containsKey(key)) {
                    // perfect
                    continue;
                } else if (key.indexOf(":")!=-1) {
                    // ignore
                    continue;
                } else {
                    // remove
                  myInstanceMap.remove(key);
                }
            }

            boolean anyChanges = false;
            for(final Entry<String, String> entry : newProps.entrySet()) {
              Object existingValue = myInstanceMap.get(entry.getKey());
              if (entry.getValue().equals(existingValue)) {
                  // SLING-3389: dont rewrite the properties if nothing changed!
                    if (logger.isDebugEnabled()) {
                        logger.debug("doUpdateProperties: unchanged: {}={}", entry.getKey(), entry.getValue());
                    }
                  continue;
              }
              if (logger.isDebugEnabled()) {
                  logger.debug("doUpdateProperties: changed: {}={}", entry.getKey(), entry.getValue());
              }
              anyChanges = true;
                myInstanceMap.put(entry.getKey(), entry.getValue());
            }

            if (anyChanges) {
                resourceResolver.commit();
            }
View Full Code Here


            final ResourceResolver resourceResolver = announcementsResource.getResourceResolver();
            Map<String, Object> properties = new HashMap<String, Object>();
            properties.put("topologyAnnouncement", announcementJson);
            resourceResolver.create(announcementsResource, getPrimaryKey(), properties);
        } else {
            final ModifiableValueMap announcementChildMap = announcementChildResource.adaptTo(ModifiableValueMap.class);
            announcementChildMap.put("topologyAnnouncement", announcementJson);
        }
    }
View Full Code Here

                return;
            }

            final Resource resource = ResourceHelper.getOrCreateResource(
                    resourceResolver, myClusterNodePath);
            final ModifiableValueMap resourceMap = resource.adaptTo(ModifiableValueMap.class);

            if (firstHeartbeatWritten!=-1 && lastHeartbeatWritten!=null) {
              // SLING-2892: additional paranoia check
              // after the first heartbeat, check if there's someone else using
              // the same sling.id in this cluster
              final long timeSinceFirstHeartbeat =
                  System.currentTimeMillis() - firstHeartbeatWritten;
              if (timeSinceFirstHeartbeat > 2*config.getHeartbeatInterval()) {
                // but wait at least 2 heartbeat intervals to handle the situation
                // where a bundle is refreshed, and startup cases.
                final Calendar lastHeartbeat = resourceMap.get("lastHeartbeat", Calendar.class);
                if (lastHeartbeat!=null) {
                  // if there is a heartbeat value, check if it is what I've written
                  // the last time
                  if (!lastHeartbeatWritten.getTime().equals(lastHeartbeat.getTime())) {
                    // then we've likely hit the situation where there is another
                    // sling instance accessing the same repository (ie in the same cluster)
                    // using the same sling.id - hence writing to the same
                    // resource
                    logger.error("issueClusterLocalHeartbeat: SLING-2892: Detected unexpected, concurrent update of: "+
                        myClusterNodePath+" 'lastHeartbeat'. If not done manually, " +
                        "this likely indicates that there is more than 1 instance running in this cluster" +
                        " with the same sling.id. My sling.id is "+slingId+"." +
                        " Check for sling.id.file in your installation of all instances in this cluster " +
                        "to verify this! Duplicate sling.ids are not allowed within a cluster!");
                  }
                }
              }

              // SLING-2901 : robust paranoia check: on first heartbeat write, the
              //              'runtimeId' is set as a property (ignoring any former value).
              //              If in subsequent calls the value of 'runtimeId' changes, then
              //              there is someone else around with the same slingId.
              final String readRuntimeId = resourceMap.get("runtimeId", String.class);
              if ( readRuntimeId == null ) { // SLING-3977
                  // someone deleted the resource property
                  firstHeartbeatWritten = -1;
              } else if (!runtimeId.equals(readRuntimeId)) {
                logger.error("issueClusterLocalHeartbeat: SLING-2091: Detected more than 1 instance running in this cluster " +
                    " with the same sling.id. My sling.id is "+slingId+", " +
                " Check for sling.id.file in your installation of all instances in this cluster " +
                "to verify this! Duplicate sling.ids are not allowed within a cluster!");
                logger.error("issueClusterLocalHeartbeat: sending TOPOLOGY_CHANGING before self-disabling.");
                discoveryService.forcedShutdown();
                logger.error("issueClusterLocalHeartbeat: disabling discovery.impl");
                activated = false;
                if (context!=null) {
                  // disable all components
                  try {
              context.getBundleContext().getBundle().stop();
            } catch (BundleException e) {
              logger.warn("issueClusterLocalHeartbeat: could not stop bundle: "+e, e);
              // then disable all compnoents instead
              context.disableComponent(null);
            }
                }
                return;
              }
            }
            resourceMap.put("lastHeartbeat", currentTime);
            if (firstHeartbeatWritten==-1) {
              resourceMap.put("runtimeId", runtimeId);
            }
            if (resetLeaderElectionId || !resourceMap.containsKey("leaderElectionId")) {
                int maxLongLength = String.valueOf(Long.MAX_VALUE).length();
                String currentTimeMillisStr = String.format("%0"
                        + maxLongLength + "d", System.currentTimeMillis());

                String prefix = "0";

                String leaderElectionRepositoryDescriptor = config.getLeaderElectionRepositoryDescriptor();
                if (leaderElectionRepositoryDescriptor!=null && leaderElectionRepositoryDescriptor.length()!=0) {
                    // when this property is configured, check the value of the repository descriptor
                    // and if that value is set, include it in the leader election id

                    final Session session = resourceResolver.adaptTo(Session.class);
                    if ( session != null ) {
                        String value = session.getRepository()
                                .getDescriptor(leaderElectionRepositoryDescriptor);
                        if (value != null && value.equalsIgnoreCase("true")) {
                            prefix = "1";
                        }
                    }
                }
                resourceMap.put("leaderElectionId", prefix + "_"
                        + currentTimeMillisStr + "_" + slingId);
                resetLeaderElectionId = false;
            }
            resourceResolver.commit();

View Full Code Here

            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } else if (type.equals(ModifiableValueMap.class)) {
            return (AdapterType) new ModifiableValueMap() {
               
                public Collection<Object> values() {
                    throw new UnsupportedOperationException();
                }
               
View Full Code Here

        initMocks("/content", new String[] { "read", "update"} );

        Resource resource = mock(Resource.class);
        when(resource.getPath()).thenReturn("/content");

        ModifiableValueMap valueMap = mock(ModifiableValueMap.class);
        when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(valueMap);

        when(resourceAccessGate.canRead(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
        when(resourceAccessGate.canUpdate(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
        Resource readableResource = resourceAccessSecurity.getReadableResource(resource);

        ModifiableValueMap resultValueMap = readableResource.adaptTo(ModifiableValueMap.class);


        resultValueMap.put("modified", "value");

        verify(valueMap, times(1)).put("modified", "value");
    }
View Full Code Here

        initMocks("/content", new String[] { "read", "update"} );

        Resource resource = mock(Resource.class);
        when(resource.getPath()).thenReturn("/content");

        ModifiableValueMap valueMap = mock(ModifiableValueMap.class);
        when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(valueMap);

        when(resourceAccessGate.canRead(resource)).thenReturn(ResourceAccessGate.GateResult.DENIED);
        when(resourceAccessGate.canUpdate(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
        Resource readableResource = resourceAccessSecurity.getReadableResource(resource);
View Full Code Here

        initMocks("/content", new String[] { "read", "update"} );

        Resource resource = mock(Resource.class);
        when(resource.getPath()).thenReturn("/content");

        ModifiableValueMap valueMap = mock(ModifiableValueMap.class);
        when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(valueMap);

        when(resourceAccessGate.canRead(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
        when(resourceAccessGate.canUpdate(resource)).thenReturn(ResourceAccessGate.GateResult.DENIED);
        Resource readableResource = resourceAccessSecurity.getReadableResource(resource);

        ModifiableValueMap resultValueMap = readableResource.adaptTo(ModifiableValueMap.class);

        assertNull(resultValueMap);
    }
View Full Code Here

        initMocks("/content", new String[] { "read", "update"} );

        Resource resource = mock(Resource.class);
        when(resource.getPath()).thenReturn("/content");

        ModifiableValueMap valueMap = mock(ModifiableValueMap.class);
        when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(valueMap);

        when(resourceAccessGate.canRead(resource)).thenReturn(ResourceAccessGate.GateResult.GRANTED);
        when(resourceAccessGate.canUpdate(resource)).thenReturn(ResourceAccessGate.GateResult.DENIED);
        Resource readableResource = resourceAccessSecurity.getReadableResource(resource);
View Full Code Here

        }
      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();
      if (logger.isDebugEnabled()) {
View Full Code Here

            final Config config,
            final String newViewId, String initiatorId, final Set<String> liveInstances) throws PersistenceException {
        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();
View Full Code Here

TOP

Related Classes of org.apache.sling.api.resource.ModifiableValueMap

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.