Return-Path: X-Original-To: apmail-sling-commits-archive@www.apache.org Delivered-To: apmail-sling-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B6423F80D for ; Thu, 4 Apr 2013 09:52:29 +0000 (UTC) Received: (qmail 47631 invoked by uid 500); 4 Apr 2013 09:35:57 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 47566 invoked by uid 500); 4 Apr 2013 09:35:55 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 47525 invoked by uid 99); 4 Apr 2013 09:35:53 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 04 Apr 2013 09:35:53 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 04 Apr 2013 09:35:52 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id AF520238897A; Thu, 4 Apr 2013 09:35:32 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1464397 - in /sling/trunk/samples/slingshot: pom.xml src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java Date: Thu, 04 Apr 2013 09:35:32 -0000 To: commits@sling.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130404093532.AF520238897A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: cziegeler Date: Thu Apr 4 09:35:32 2013 New Revision: 1464397 URL: http://svn.apache.org/r1464397 Log: Update sample to use CRUD api Modified: sling/trunk/samples/slingshot/pom.xml sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java Modified: sling/trunk/samples/slingshot/pom.xml URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/pom.xml?rev=1464397&r1=1464396&r2=1464397&view=diff ============================================================================== --- sling/trunk/samples/slingshot/pom.xml (original) +++ sling/trunk/samples/slingshot/pom.xml Thu Apr 4 09:35:32 2013 @@ -112,7 +112,7 @@ org.apache.sling org.apache.sling.api - 2.1.0 + 2.3.0 provided Modified: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java?rev=1464397&r1=1464396&r2=1464397&view=diff ============================================================================== --- sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java (original) +++ sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java Thu Apr 4 09:35:32 2013 @@ -26,35 +26,40 @@ import org.apache.felix.scr.annotations. import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import org.apache.sling.api.resource.LoginException; -import org.apache.sling.api.resource.PersistableValueMap; +import org.apache.sling.api.resource.ModifiableValueMap; import org.apache.sling.api.resource.PersistenceException; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceResolverFactory; import org.apache.sling.sample.slingshot.Constants; import org.osgi.service.event.Event; +import org.osgi.service.event.EventConstants; import org.osgi.service.event.EventHandler; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * This is a samle for observation which adds tags to new resources. + * This is a sample for observation which adds tags to new resources. */ @Component(immediate=true) @Service(value=EventHandler.class) @Properties({ @Property(name="service.description", value="Apache Sling - Slingshot Tagging Service"), - @Property(name="event.topics", value=org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED) + @Property(name=EventConstants.EVENT_TOPIC, value=org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED) }) public class AutomaticTaggingService implements EventHandler { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + @Reference private ResourceResolverFactory resourceResolverFactory; private final Random random = new Random(System.currentTimeMillis()); private final String MIXIN_TYPE_PROPERTY = "jcr:mixinTypes"; + private final String MIXIN_TYPE_PHOTO = "slingshot:Photo"; /** * @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event) @@ -67,16 +72,16 @@ public class AutomaticTaggingService resolver = this.resourceResolverFactory.getAdministrativeResourceResolver(null); final Resource r = resolver.getResource(path); if ( r != null && r.isResourceType(Constants.RESOURCETYPE_PHOTO) ) { - final PersistableValueMap pvm = r.adaptTo(PersistableValueMap.class); - if ( pvm != null ) { - String[] types = pvm.get(MIXIN_TYPE_PROPERTY, String[].class); + final ModifiableValueMap mvm = r.adaptTo(ModifiableValueMap.class); + if ( mvm != null ) { + String[] types = mvm.get(MIXIN_TYPE_PROPERTY, String[].class); if ( types == null ) { - pvm.put(MIXIN_TYPE_PROPERTY, "slingshot:Photo"); + mvm.put(MIXIN_TYPE_PROPERTY, MIXIN_TYPE_PHOTO); } else { String[] newTypes = new String[types.length + 1]; System.arraycopy(types, 0, newTypes, 0, types.length); - newTypes[types.length] = "slingshot:Photo"; - pvm.put(MIXIN_TYPE_PROPERTY, newTypes); + newTypes[types.length] = MIXIN_TYPE_PHOTO; + mvm.put(MIXIN_TYPE_PROPERTY, newTypes); } final int tagsValue = this.random.nextInt(8); @@ -90,12 +95,12 @@ public class AutomaticTaggingService if ( (tagsValue & 4) == 4 ) { tags.add("Cool"); } - pvm.put(Constants.PROPERTY_SLINGSHOT_TAGS, tags.toArray(new String[tags.size()])); + mvm.put(Constants.PROPERTY_SLINGSHOT_TAGS, tags.toArray(new String[tags.size()])); try { - pvm.save(); - } catch (PersistenceException e) { - LoggerFactory.getLogger(this.getClass()).info("Ups", e); + resolver.commit(); + } catch (final PersistenceException e) { // we just ignore this for now + logger.info("Unable to add tags to photo: " + r.getPath(), e); } } }