Skip to content

Commit 209be6c

Browse files
author
Jeff Bornemann
committed
Extract parent existence logic into JCRUtil
1 parent a55671d commit 209be6c

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

src/main/groovy/com/twcable/grabbit/client/batch/steps/validation/ValidJobDecider.groovy

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ package com.twcable.grabbit.client.batch.steps.validation
1717

1818
import com.twcable.grabbit.client.batch.ClientBatchJob
1919
import com.twcable.grabbit.client.batch.ClientBatchJobContext
20+
import com.twcable.grabbit.jcr.JCRUtil
2021
import groovy.transform.CompileStatic
2122
import groovy.util.logging.Slf4j
23+
import javax.jcr.Session
2224
import org.springframework.batch.core.JobExecution
2325
import org.springframework.batch.core.StepExecution
2426
import org.springframework.batch.core.job.flow.FlowExecutionStatus
2527
import org.springframework.batch.core.job.flow.JobExecutionDecider
2628

27-
import javax.jcr.PathNotFoundException
28-
import javax.jcr.RepositoryException
29-
import javax.jcr.Session
30-
3129
/**
3230
* This class serves as a validation gate for jobs in-flight. It should be the first step on the client when running a job to determine
3331
* if the job is job that is safe, and valid to execute.
@@ -57,29 +55,14 @@ class ValidJobDecider implements JobExecutionDecider {
5755
@Override
5856
FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
5957
String jobPath = jobExecution.jobParameters.getString(ClientBatchJob.PATH)
60-
//For processing, remove trailing /
61-
jobPath = jobPath.replaceFirst(/\/$/, '')
62-
//Get the parent's path (if applicable) and determine if it exists already
63-
final parts = jobPath.split('/')
64-
//No parent, so nothing to worry about
65-
if(parts.length <= 2) return JOB_VALID
66-
67-
final parentPath = parts[0..-2].join('/')
68-
final Session session = theSession()
69-
try {
70-
session.getNode(parentPath)
71-
} catch(PathNotFoundException pathException) {
72-
log.warn "${jobPath} is not a valid job path. Make sure a parent is synched or created before this job is run"
73-
log.debug pathException.toString()
74-
return JOB_INVALID
58+
if(JCRUtil.writingToExistingNode(jobPath, theSession())) {
59+
log.debug "${ValidJobDecider.class.canonicalName} Job determined to be valid for job path ${jobPath}"
60+
return JOB_VALID
7561
}
76-
catch(RepositoryException repoException) {
77-
log.error "${RepositoryException.class.canonicalName} Something went wrong when accessing the repository at ${this.class.canonicalName} for job path ${jobPath}!"
78-
log.error repoException.toString()
62+
else {
63+
log.warn "${jobPath} is not a valid job path. Make sure a parent is synched or created before this job is run"
7964
return JOB_INVALID
8065
}
81-
log.debug "${ValidJobDecider.class.canonicalName} Job determined to be valid for job path ${jobPath}"
82-
return JOB_VALID
8366
}
8467

8568
}

src/main/groovy/com/twcable/grabbit/jcr/JcrUtil.groovy

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package com.twcable.grabbit.jcr
1818

1919
import groovy.transform.CompileStatic
2020
import groovy.util.logging.Slf4j
21+
import javax.jcr.PathNotFoundException
22+
import javax.jcr.RepositoryException
2123
import org.apache.sling.api.resource.ResourceResolver
2224
import org.apache.sling.api.resource.ResourceResolverFactory
2325
import org.apache.sling.jcr.api.SlingRepository
@@ -164,4 +166,30 @@ public class JCRUtil {
164166
return withResourceResolver(resolverFactory, "admin", closure)
165167
}
166168

169+
/**
170+
* @return true if the node at the path being written is being written to an existing node
171+
*/
172+
static boolean writingToExistingNode(@Nonnull final String pathBeingWritten, @Nonnull final Session session) {
173+
//For processing, remove trailing /
174+
final String thePath = pathBeingWritten.replaceFirst(/\/$/, '')
175+
//Get the parent's path (if applicable) and determine if it exists already
176+
final parts = thePath.split('/')
177+
//No parent, so nothing to worry about
178+
if(parts.length <= 2) return true
179+
180+
final String parentPath = parts[0..-2].join('/')
181+
try {
182+
session.getNode(parentPath)
183+
} catch(PathNotFoundException pathException) {
184+
log.debug pathException.toString()
185+
return false
186+
}
187+
catch(RepositoryException repoException) {
188+
log.error "${RepositoryException.class.canonicalName} Something went wrong when accessing the repository at ${this.class.canonicalName} for path ${pathBeingWritten}!"
189+
log.error repoException.toString()
190+
return false
191+
}
192+
return true
193+
}
194+
167195
}

0 commit comments

Comments
 (0)