Permalink
Browse files

pulled in metaclass updates from master

  • Loading branch information...
2 parents 463a450 + a699940 commit e764a37a47ce75449c24b124739f78dfeb927d81 @mszu mszu committed May 21, 2012
View
8 pom.xml
@@ -24,7 +24,7 @@
<!-- dependency versions -->
<slf4j.version>1.5.2</slf4j.version>
- <groovy.version>1.8.5</groovy.version>
+ <groovy.version>1.8.6</groovy.version>
<jcr.version>2.0</jcr.version>
<servlet.version>2.4</servlet.version>
<cq.version>5.4.0</cq.version>
@@ -40,7 +40,7 @@
<compiler.plugin.version>2.3.2</compiler.plugin.version>
<xml.plugin.version>1.0</xml.plugin.version>
<exec.plugin.version>1.2</exec.plugin.version>
- <felix.bundle.plugin.version>2.3.5</felix.bundle.plugin.version>
+ <felix.bundle.plugin.version>2.3.7</felix.bundle.plugin.version>
<scr.plugin.version>1.7.4</scr.plugin.version>
<assembly.plugin.version>2.2.1</assembly.plugin.version>
<jar.plugin.version>2.3.2</jar.plugin.version>
@@ -337,7 +337,7 @@
<version>${cq.version}</version>
<scope>provided</scope>
</dependency>
-
+
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.api</artifactId>
@@ -350,7 +350,7 @@
<version>${sling.jcr.api.version}</version>
<scope>provided</scope>
</dependency>
-
+
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
View
151 src/main/content/jcr_root/apps/groovyconsole/components/console/POST.groovy
@@ -1,30 +1,27 @@
import com.citytechinc.cqlibrary.groovyconsole.builder.JcrBuilder
+import com.citytechinc.cqlibrary.groovyconsole.metaclass.MetaClassRegistry
-import com.day.cq.wcm.api.Page
-import com.day.cq.wcm.api.PageManager
+import groovy.json.JsonBuilder
+import groovy.transform.Field
-import javax.jcr.Node
-import javax.jcr.PropertyType
-import javax.jcr.Value
import javax.jcr.Session
-import groovy.json.JsonBuilder
-
import org.apache.commons.lang.time.StopWatch
import org.codehaus.groovy.control.MultipleCompilationErrorsException
import org.slf4j.LoggerFactory
-final def resolver = resource.resourceResolver
+import com.day.cq.wcm.api.PageManager
+
+@Field log = LoggerFactory.getLogger('groovyconsole')
-registerMetaClasses()
+MetaClassRegistry.registerMetaClasses()
+resolver = resource.resourceResolver
session = resolver.adaptTo(Session)
pageManager = resolver.adaptTo(PageManager)
-log = LoggerFactory.getLogger('groovyconsole')
-
def encoding = 'UTF-8'
def stream = new ByteArrayOutputStream()
def printStream = new PrintStream(stream, true, encoding)
@@ -143,136 +140,4 @@ def sanitizeStacktrace(t) {
def clean = newTrace.toArray(newTrace as StackTraceElement[])
t.stackTrace = clean
-}
-
-def registerMetaClasses() {
- Node.metaClass {
- iterator {
- delegate.nodes
- }
-
- recurse { c ->
- c(delegate)
-
- delegate.nodes.each { node ->
- node.recurse(c)
- }
- }
-
- getNodeSafe { relativePath ->
- def node = delegate
-
- relativePath.split("/").each { path ->
- if (node.hasNode(path)) {
- node = node.getNode(path)
- } else {
- node = node.addNode(path)
- }
- }
-
- node
- }
-
- getNodeSafe { name, nodeTypeName ->
- delegate.hasNode(name) ? delegate.getNode(name) : delegate.addNode(name, nodeTypeName)
- }
-
- getProperty { String name ->
- def result = null
-
- if (delegate.hasProperty(name)) {
- def method = Node.class.getMethod('getProperty', String)
- def property = method.invoke(delegate, name)
-
- if (property.multiple) {
- result = property.values.collect { getResult(it) }
- } else {
- result = getResult(property.value)
- }
- } else {
- result = ""
- }
-
- result
- }
-
- setProperty { String name, value ->
- if (value) {
- if (value instanceof Object[]) {
- def values = value.collect { valueFactory.createValue(it) }.toArray(new Value[0])
-
- def method = Node.class.getMethod('setProperty', String, Value[])
-
- method.invoke(delegate, name, values)
- } else {
- def jcrValue = valueFactory.createValue(value)
-
- def method = Node.class.getMethod('setProperty', String, Value)
-
- method.invoke(delegate, name, jcrValue)
- }
- } else {
- if (delegate.hasProperty(name)) {
- def method = Node.class.getMethod('getProperty', String)
-
- def property = method.invoke(delegate, name)
-
- property.remove()
- }
- }
- }
- }
-
- Page.metaClass {
- iterator {
- delegate.listChildren()
- }
-
- recurse { Closure c ->
- c(delegate)
-
- delegate.listChildren().each { child ->
- child.recurse(c)
- }
- }
-
- getNode {
- delegate.contentResource?.adaptTo(Node)
- }
-
- getProperty { String name ->
- def node = delegate.contentResource?.adaptTo(Node)
-
- node ? node[name] : null
- }
- }
-}
-
-def getResult(value) {
- def result = null
-
- switch(value.type) {
- case PropertyType.BINARY:
- result = value.binary
- break
- case PropertyType.BOOLEAN:
- result = value.boolean
- break
- case PropertyType.DATE:
- result = value.date
- break
- case PropertyType.DECIMAL:
- result = value.decimal
- break
- case PropertyType.DOUBLE:
- result = value.double
- break
- case PropertyType.LONG:
- result = value.long
- break
- case PropertyType.STRING:
- result = value.string
- }
-
- result
}
View
2 src/main/content/jcr_root/etc/groovyconsole/scripts/samples/FindComponent.groovy
@@ -1,6 +1,6 @@
getPage('/content/geometrixx').recurse { page ->
page.node.recurse { node ->
- if ('geometrixx/components/title' == node['sling:resourceType']) {
+ if ('geometrixx/components/title' == node.get('sling:resourceType')) {
println node.path
}
}
View
156 src/main/groovy/com/citytechinc/cqlibrary/groovyconsole/metaclass/MetaClassRegistry.groovy
@@ -0,0 +1,156 @@
+package com.citytechinc.cqlibrary.groovyconsole.metaclass
+
+import javax.jcr.Node
+import javax.jcr.PropertyType
+import javax.jcr.Value
+
+import org.codehaus.groovy.runtime.InvokerHelper
+
+import com.day.cq.wcm.api.Page
+
+class MetaClassRegistry {
+
+ static void registerMetaClasses() {
+ def registry = InvokerHelper.metaRegistry
+
+ registry.removeMetaClass(Node)
+ registry.removeMetaClass(Page)
+
+ Node.metaClass {
+ iterator {
+ delegate.nodes
+ }
+
+ recurse { c ->
+ c(delegate)
+
+ delegate.nodes.each { node ->
+ node.recurse(c)
+ }
+ }
+
+ get { String name ->
+ def result = null
+
+ if (delegate.hasProperty(name)) {
+ def property = delegate.getProperty(name)
+
+ if (property.multiple) {
+ result = property.values.collect { getResult(it) }
+ } else {
+ result = getResult(property.value)
+ }
+ } else {
+ result = ''
+ }
+
+ result
+ }
+
+ set { String name, value ->
+ if (value) {
+ def valueFactory = delegate.session.valueFactory
+
+ if (value instanceof Object[]) {
+ def values = value.collect { valueFactory.createValue(it) }.toArray(new Value[0])
+
+ delegate.setProperty(name, values)
+ } else {
+ def jcrValue = valueFactory.createValue(value)
+
+ delegate.setProperty(name, jcrValue)
+ }
+ } else {
+ if (delegate.hasProperty(name)) {
+ delegate.getProperty(name).remove()
+ }
+ }
+ }
+
+ getNodeSafe { relativePath ->
+ def node = delegate
+
+ relativePath.split('/').each { path ->
+ if (node.hasNode(path)) {
+ node = node.getNode(path)
+ } else {
+ node = node.addNode(path)
+ }
+ }
+
+ node
+ }
+
+ getNodeSafe { name, nodeTypeName ->
+ delegate.hasNode(name) ? delegate.getNode(name) : delegate.addNode(name, nodeTypeName)
+ }
+
+ removeNode { name ->
+ if (delegate.hasNode(name)) {
+ delegate.getNode(name).remove()
+ }
+ }
+ }
+
+ Page.metaClass {
+ iterator {
+ delegate.listChildren()
+ }
+
+ recurse { Closure c ->
+ c(delegate)
+
+ delegate.listChildren().each { child ->
+ child.recurse(c)
+ }
+ }
+
+ getNode {
+ delegate.contentResource?.adaptTo(Node)
+ }
+
+ get { String name ->
+ def node = delegate.contentResource?.adaptTo(Node)
+
+ node ? node.get(name) : null
+ }
+
+ set { String name, value ->
+ def node = delegate.contentResource?.adaptTo(Node)
+
+ if (node) {
+ node.set(name, value)
+ }
+ }
+ }
+ }
+
+ private static def getResult(value) {
+ def result = null
+
+ switch(value.type) {
+ case PropertyType.BINARY:
+ result = value.binary
+ break
+ case PropertyType.BOOLEAN:
+ result = value.boolean
+ break
+ case PropertyType.DATE:
+ result = value.date
+ break
+ case PropertyType.DECIMAL:
+ result = value.decimal
+ break
+ case PropertyType.DOUBLE:
+ result = value.double
+ break
+ case PropertyType.LONG:
+ result = value.long
+ break
+ case PropertyType.STRING:
+ result = value.string
+ }
+
+ result
+ }
+}

0 comments on commit e764a37

Please sign in to comment.