Java Code Examples for org.apache.sling.api.resource.ResourceResolver#close()

The following examples show how to use org.apache.sling.api.resource.ResourceResolver#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageStoreImpl.java    From sling-samples with Apache License 2.0 6 votes vote down vote up
public void saveAll(Iterator<Message> iterator) throws IOException {
    ResourceResolver resolver = null;
    try {
        resolver = getResourceResolver();
        int mcount = 0;
        while (iterator.hasNext()) {
            Message msg = iterator.next();
            save(resolver, msg);

            mcount++;
            if (mcount % 100 == 0) {
                logger.debug(mcount+" messages processed.");
            }
        }
        logger.info(mcount+" messages processed.");
    } catch(LoginException e) {
        throw new RuntimeException("LoginException", e);
    } finally {
        if(resolver != null) {
            resolver.close();
        }
    }
}
 
Example 2
Source File: SlingHelper.java    From APM with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve values from repository with wrapped impersonated session (automatically opened and closed).
 */
@SuppressWarnings("unchecked")
public static <T> T resolve(ResourceResolverFactory factory, String userId, ResolveCallback callback)
    throws ResolveException {
  ResourceResolver resolver = null;
  try {
    resolver = getResourceResolverForUser(factory, userId);
    return (T) callback.resolve(resolver);
  } catch (Exception e) {
    throw new ResolveException(RESOLVE_ERROR_MESSAGE, e);
  } finally {
    if (resolver != null && resolver.isLive()) {
      resolver.close();
    }
  }
}
 
Example 3
Source File: SlingHelper.java    From APM with Apache License 2.0 6 votes vote down vote up
/**
 * Do some operation on repository (delete or update resource etc) with wrapped impersonated session
 * (automatically opened and closed).
 */
public static void operate(ResourceResolverFactory factory, String userId, OperateCallback callback)
    throws OperateException {
  ResourceResolver resolver = null;
  try {
    resolver = getResourceResolverForUser(factory, userId);
    callback.operate(resolver);
    resolver.commit();
  } catch (Exception e) {
    throw new OperateException(OPERATE_ERROR_MESSAGE, e);
  } finally {
    if (resolver != null && resolver.isLive()) {
      resolver.close();
    }
  }
}
 
Example 4
Source File: ResourceResolverConsumer.java    From AEM-Rules-for-SonarQube with Apache License 2.0 6 votes vote down vote up
public Tag findTag(String tagId, Asset asset, Session session) {
    Tag tag = null;
    ResourceResolver resourceResolver = null;

    try {
        resourceResolver = getResourceResolver(session);
        TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
        tag = tagManager.resolve(tagId);
    } finally {
        if (null != resourceResolver && resourceResolver.isLive()) {
            resourceResolver.close();
        }
    }

    return tag;
}
 
Example 5
Source File: SolrPageListener.java    From aem-solr-search with Apache License 2.0 6 votes vote down vote up
public void handleEvent(final Event event) {
		if (disabled) return;

		SolrClient solr = getSolrIndexClient();

		PageEvent pageEvent = PageEvent.fromEvent(event);
		if (pageEvent == null) return;

		ResourceResolver resourceResolver = null;
		try {
				resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
				for (Iterator<PageModification> iter = pageEvent.getModifications(); iter.hasNext(); )
						handlePageModification(iter.next(), solr, resourceResolver);
		} catch (Exception e) {
				LOG.error("Could not get ResourceResolver instance or handle page modification", e);
				return;
		} finally {
				if (resourceResolver != null && resourceResolver.isLive())
						resourceResolver.close();
		}
}
 
Example 6
Source File: MessageStoreImpl.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
public void save(Message msg) throws IOException {
    ResourceResolver resolver = null;
    try {
        resolver = getResourceResolver();
        save(resolver, msg);
    } catch (LoginException e) {
        throw new RuntimeException("LoginException", e);
    } finally {
        if(resolver != null) {
            resolver.close();
        }
    }
}
 
Example 7
Source File: RatingPostServlet.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
@Override
protected void doPost(final SlingHttpServletRequest request,
        final SlingHttpServletResponse response)
throws ServletException, IOException {
    final String rating = request.getParameter(RatingsUtil.PROPERTY_RATING);
    final String userId = request.getRemoteUser();

    logger.debug("New rating from {} : {}", userId, rating);

    // save rating
    ResourceResolver resolver = null;
    try {
        resolver = factory.getServiceResourceResolver(null);

        final Resource reqResource = resolver.getResource(request.getResource().getPath());

        ratingsService.setRating(reqResource, userId, Double.valueOf(rating));

    } catch ( final LoginException le ) {
        throw new ServletException("Unable to login", le);
    } finally {
        if ( resolver != null ) {
            resolver.close();
        }
    }

    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");
    response.setStatus(200);

    final PrintWriter pw = response.getWriter();
    pw.print("{ ");
    pw.print(" \"rating\" : ");
    pw.print(String.valueOf(ratingsService.getRating(request.getResource())));
    pw.print("}");
}
 
Example 8
Source File: CommentPostServlet.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
@Override
protected void doPost(final SlingHttpServletRequest request,
        final SlingHttpServletResponse response)
throws ServletException, IOException {
    final String title = request.getParameter(CommentsUtil.PROPERTY_TITLE);
    final String text = request.getParameter(CommentsUtil.PROPERTY_TEXT);

    final String userId = request.getRemoteUser();

    logger.debug("New comment from {} : {} - {}", new Object[] {userId, title, text});
    // TODO - check values

    // save comment
    ResourceResolver resolver = null;
    try {
        resolver = factory.getServiceResourceResolver(null);

        final Resource reqResource = resolver.getResource(request.getResource().getPath());

        final Comment c = new Comment();
        c.setTitle(title);
        c.setText(text);
        c.setCreatedBy(userId);

        this.commentsService.addComment(reqResource, c);


        // send redirect at the end
        final String path = request.getResource().getPath();

        response.sendRedirect(resolver.map(request.getContextPath() + path + ".html"));
    } catch ( final LoginException le ) {
        throw new ServletException("Unable to login", le);
    } finally {
        if ( resolver != null ) {
            resolver.close();
        }
    }
}
 
Example 9
Source File: SetupService.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
@Activate
protected void activate(final BundleContext bc) throws IOException, LoginException, PersistenceException, RepositoryException {
    logger.info("Setting up SlingShot...");
    ResourceResolver resolver = null;
    try {
        resolver = this.factory.getServiceResourceResolver(null);
        setupContent(resolver);
    } finally {
        if ( resolver != null ) {
            resolver.close();
        }
    }
    logger.info("Finished setting up SlingShot");
}
 
Example 10
Source File: SampleServlet.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
public void checkCorrectJumpMethod() {
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = getResourceResolverForUserNested(resourceResolverFactory, null);
    } catch (LoginException e) {
        e.printStackTrace();
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
 
Example 11
Source File: ResourceResolverConsumer.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
public String findName(final String path) {
    String name = "";
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = resourceResolverProducer.produce();
        name = resourceResolver.getResource(path).getName();
    } finally {
        if (null != resourceResolver && resourceResolver.isLive()) {
            resourceResolver.close();
        }
    }
    return name;
}
 
Example 12
Source File: AutoclosableResourceResolver.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
public String resourceResolverInitialisedByResourceResolverProducerInTryBlockAndClosed(
    final String path) {
  ResourceResolver resourceResolver = null;
  try {
    resourceResolver = resourceResolverProducer.produce(); // Noncompliant
    name = resourceResolver.getResource(path).getName();
  } finally {
    if (null != resourceResolver && resourceResolver.isLive()) {
      resourceResolver.close();
    }
  }
  return name;
}