Skip to content

Commit 21cebe9

Browse files
committed
Merged from 1.3.x branch
1 parent f30a2b0 commit 21cebe9

File tree

11 files changed

+236
-53
lines changed

11 files changed

+236
-53
lines changed

src/java/grails/converters/JSON.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.commons.io.IOUtils;
2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23+
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil;
2324
import org.codehaus.groovy.grails.web.converters.AbstractConverter;
2425
import org.codehaus.groovy.grails.web.converters.Converter;
2526
import org.codehaus.groovy.grails.web.converters.ConverterUtil;
@@ -169,6 +170,7 @@ public void build(Closure c) throws ConverterException {
169170
* @throws ConverterException
170171
*/
171172
public void value(Object o) throws ConverterException {
173+
o = this.config.getProxyHandler().unwrapIfProxy(o);
172174
try {
173175
if (o == null || o.equals(JSONObject.NULL)) {
174176
writer.value(null);

src/java/org/codehaus/groovy/grails/web/converters/configuration/ChainedConverterConfiguration.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.codehaus.groovy.grails.web.converters.configuration;
1717

18+
import org.codehaus.groovy.grails.support.proxy.DefaultProxyHandler;
19+
import org.codehaus.groovy.grails.support.proxy.ProxyHandler;
1820
import org.codehaus.groovy.grails.web.converters.Converter;
1921
import org.codehaus.groovy.grails.web.converters.exceptions.ConverterException;
2022
import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller;
@@ -27,6 +29,8 @@
2729
* An immutable ConverterConfiguration which chains the lookup calls for ObjectMarshallers for performance reasons
2830
*
2931
* @author Siegfried Puchbauer
32+
* @author Graeme Rocher
33+
*
3034
* @since 1.1
3135
*/
3236
public class ChainedConverterConfiguration<C extends Converter> implements ConverterConfiguration<C> {
@@ -41,8 +45,14 @@ public class ChainedConverterConfiguration<C extends Converter> implements Conve
4145

4246
private final boolean prettyPrint;
4347

48+
private ProxyHandler proxyHandler;
49+
4450
public ChainedConverterConfiguration(ConverterConfiguration<C> cfg) {
51+
this(cfg, new DefaultProxyHandler());
52+
}
53+
public ChainedConverterConfiguration(ConverterConfiguration<C> cfg, ProxyHandler proxyHandler) {
4554
this.marshallerList = cfg.getOrderedObjectMarshallers();
55+
this.proxyHandler = proxyHandler;
4656

4757
encoding = cfg.getEncoding();
4858
prettyPrint = cfg.isPrettyPrint();
@@ -105,4 +115,8 @@ public void marshalObject(Object object, C converter) throws ConverterException
105115
}
106116

107117
}
118+
119+
public ProxyHandler getProxyHandler() {
120+
return this.proxyHandler;
121+
}
108122
}

src/java/org/codehaus/groovy/grails/web/converters/configuration/ConverterConfiguration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@
1515
*/
1616
package org.codehaus.groovy.grails.web.converters.configuration;
1717

18+
import org.codehaus.groovy.grails.support.proxy.ProxyHandler;
1819
import org.codehaus.groovy.grails.web.converters.Converter;
1920
import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller;
2021

2122
import java.util.List;
2223

2324
/**
2425
* @author Siegfried Puchbauer
26+
* @author Graeme Rocher
27+
*
2528
* @since 1.1
2629
*/
2730
public interface ConverterConfiguration<C extends Converter> {
2831

32+
/**
33+
* Lookup the ProxyHandler used to deal with proxies instances
34+
* @return The proxy handler
35+
*/
36+
public ProxyHandler getProxyHandler();
37+
2938
/**
3039
* Lookup the ObjectMarshaller with the highest priority that support to marshall the given object
3140
* @param o the object which is about to be converted

src/java/org/codehaus/groovy/grails/web/converters/configuration/ConvertersConfigurationInitializer.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import grails.util.GrailsConfig;
2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23+
import org.codehaus.groovy.grails.support.proxy.DefaultProxyHandler;
24+
import org.codehaus.groovy.grails.support.proxy.ProxyHandler;
2325
import org.codehaus.groovy.grails.web.converters.Converter;
2426
import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller;
2527
import org.codehaus.groovy.grails.web.converters.marshaller.ProxyUnwrappingMarshaller;
@@ -78,16 +80,17 @@ private void initJSONConfiguration() {
7880
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.json.ToStringBeanMarshaller());
7981

8082
boolean includeDomainVersion = includeDomainVersionProperty("json");
83+
ProxyHandler proxyHandler = getProxyHandler();
8184
if (GrailsConfig.get("grails.converters.json.default.deep", false)) {
8285
LOG.debug("Using DeepDomainClassMarshaller as default.");
83-
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.json.DeepDomainClassMarshaller(includeDomainVersion));
86+
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.json.DeepDomainClassMarshaller(includeDomainVersion, proxyHandler));
8487
} else {
85-
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.json.DomainClassMarshaller(includeDomainVersion));
88+
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.json.DomainClassMarshaller(includeDomainVersion, proxyHandler));
8689
}
8790
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.json.GroovyBeanMarshaller());
8891
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.json.GenericJavaBeanMarshaller());
8992

90-
DefaultConverterConfiguration<JSON> cfg = new DefaultConverterConfiguration<JSON>(marshallers);
93+
DefaultConverterConfiguration<JSON> cfg = new DefaultConverterConfiguration<JSON>(marshallers, proxyHandler);
9194
cfg.setEncoding(GrailsConfig.get("grails.converters.encoding", "UTF-8"));
9295
String defaultCirRefBehaviour = GrailsConfig.get("grails.converters.default.circular.reference.behaviour", "DEFAULT");
9396
cfg.setCircularReferenceBehaviour(Converter.CircularReferenceBehaviour.valueOf(
@@ -100,12 +103,12 @@ private void initJSONConfiguration() {
100103

101104
registerObjectMarshallersFromApplicationContext(cfg, JSON.class);
102105

103-
ConvertersConfigurationHolder.setDefaultConfiguration(JSON.class, new ChainedConverterConfiguration<JSON>(cfg));
106+
ConvertersConfigurationHolder.setDefaultConfiguration(JSON.class, new ChainedConverterConfiguration<JSON>(cfg, proxyHandler));
104107
}
105108

106109
private void initDeepJSONConfiguration() {
107-
DefaultConverterConfiguration<JSON> deepConfig = new DefaultConverterConfiguration<JSON>(ConvertersConfigurationHolder.getConverterConfiguration(JSON.class));
108-
deepConfig.registerObjectMarshaller(new org.codehaus.groovy.grails.web.converters.marshaller.json.DeepDomainClassMarshaller(includeDomainVersionProperty("json")));
110+
DefaultConverterConfiguration<JSON> deepConfig = new DefaultConverterConfiguration<JSON>(ConvertersConfigurationHolder.getConverterConfiguration(JSON.class), getProxyHandler());
111+
deepConfig.registerObjectMarshaller(new org.codehaus.groovy.grails.web.converters.marshaller.json.DeepDomainClassMarshaller(includeDomainVersionProperty("json"), getProxyHandler()));
109112
ConvertersConfigurationHolder.setNamedConverterConfiguration(JSON.class, "deep", deepConfig);
110113
}
111114

@@ -120,17 +123,18 @@ private void initXMLConfiguration() {
120123
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.xml.DateMarshaller());
121124
marshallers.add(new ProxyUnwrappingMarshaller<XML>());
122125
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.xml.ToStringBeanMarshaller());
126+
ProxyHandler proxyHandler = getProxyHandler();
123127

124128
boolean includeDomainVersion = includeDomainVersionProperty("xml");
125129
if (GrailsConfig.get("grails.converters.xml.default.deep", false)) {
126-
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.xml.DeepDomainClassMarshaller(includeDomainVersion));
130+
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.xml.DeepDomainClassMarshaller(includeDomainVersion, proxyHandler));
127131
} else {
128-
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.xml.DomainClassMarshaller(includeDomainVersion));
132+
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.xml.DomainClassMarshaller(includeDomainVersion, proxyHandler));
129133
}
130134
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.xml.GroovyBeanMarshaller());
131135
marshallers.add(new org.codehaus.groovy.grails.web.converters.marshaller.xml.GenericJavaBeanMarshaller());
132136

133-
DefaultConverterConfiguration<XML> cfg = new DefaultConverterConfiguration<XML>(marshallers);
137+
DefaultConverterConfiguration<XML> cfg = new DefaultConverterConfiguration<XML>(marshallers,proxyHandler);
134138
cfg.setEncoding(GrailsConfig.get("grails.converters.encoding", "UTF-8"));
135139
String defaultCirRefBehaviour = GrailsConfig.get("grails.converters.default.circular.reference.behaviour", "DEFAULT");
136140
cfg.setCircularReferenceBehaviour(Converter.CircularReferenceBehaviour.valueOf(
@@ -141,12 +145,23 @@ private void initXMLConfiguration() {
141145
Boolean prettyPrint = GrailsConfig.get("grails.converters.xml.pretty.print", defaultPrettyPrint);
142146
cfg.setPrettyPrint(prettyPrint);
143147
registerObjectMarshallersFromApplicationContext(cfg, XML.class);
144-
ConvertersConfigurationHolder.setDefaultConfiguration(XML.class, new ChainedConverterConfiguration<XML>(cfg));
148+
ConvertersConfigurationHolder.setDefaultConfiguration(XML.class, new ChainedConverterConfiguration<XML>(cfg,proxyHandler));
145149
}
146150

151+
private ProxyHandler getProxyHandler() {
152+
ProxyHandler proxyHandler;
153+
if(applicationContext != null) {
154+
proxyHandler = applicationContext.getBean(ProxyHandler.class);
155+
}
156+
else {
157+
proxyHandler = new DefaultProxyHandler();
158+
}
159+
return proxyHandler;
160+
}
161+
147162
private void initDeepXMLConfiguration() {
148-
DefaultConverterConfiguration<XML> deepConfig = new DefaultConverterConfiguration<XML>(ConvertersConfigurationHolder.getConverterConfiguration(XML.class));
149-
deepConfig.registerObjectMarshaller(new org.codehaus.groovy.grails.web.converters.marshaller.xml.DeepDomainClassMarshaller(includeDomainVersionProperty("xml")));
163+
DefaultConverterConfiguration<XML> deepConfig = new DefaultConverterConfiguration<XML>(ConvertersConfigurationHolder.getConverterConfiguration(XML.class), getProxyHandler());
164+
deepConfig.registerObjectMarshaller(new org.codehaus.groovy.grails.web.converters.marshaller.xml.DeepDomainClassMarshaller(includeDomainVersionProperty("xml"), getProxyHandler()));
150165
ConvertersConfigurationHolder.setNamedConverterConfiguration(XML.class, "deep", deepConfig);
151166
}
152167

src/java/org/codehaus/groovy/grails/web/converters/configuration/DefaultConverterConfiguration.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
package org.codehaus.groovy.grails.web.converters.configuration;
1717

1818
import groovy.lang.Closure;
19+
20+
import org.codehaus.groovy.grails.support.proxy.DefaultProxyHandler;
21+
import org.codehaus.groovy.grails.support.proxy.ProxyHandler;
1922
import org.codehaus.groovy.grails.web.converters.Converter;
2023
import org.codehaus.groovy.grails.web.converters.marshaller.ClosureOjectMarshaller;
2124
import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller;
@@ -45,6 +48,8 @@ public class DefaultConverterConfiguration<C extends Converter> implements Conve
4548

4649
private Converter.CircularReferenceBehaviour circularReferenceBehaviour;
4750

51+
private ProxyHandler proxyHandler;
52+
4853
public String getEncoding() {
4954
return encoding != null ? encoding : (delegate != null ? delegate.getEncoding() : null);
5055
}
@@ -83,21 +88,42 @@ public void setCircularReferenceBehaviour(Converter.CircularReferenceBehaviour c
8388
}
8489

8590
public DefaultConverterConfiguration() {
91+
proxyHandler = new DefaultProxyHandler();
8692
}
8793

8894
public DefaultConverterConfiguration(ConverterConfiguration<C> delegate) {
95+
this();
8996
this.delegate = delegate;
9097
this.prettyPrint = delegate.isPrettyPrint();
9198
this.circularReferenceBehaviour = delegate.getCircularReferenceBehaviour();
9299
this.encoding = delegate.getEncoding();
93100
}
101+
102+
public DefaultConverterConfiguration(ProxyHandler proxyHandler) {
103+
this.proxyHandler = proxyHandler;
104+
}
105+
106+
public DefaultConverterConfiguration(ConverterConfiguration<C> delegate, ProxyHandler proxyHandler) {
107+
this(proxyHandler);
108+
this.delegate = delegate;
109+
this.prettyPrint = delegate.isPrettyPrint();
110+
this.circularReferenceBehaviour = delegate.getCircularReferenceBehaviour();
111+
this.encoding = delegate.getEncoding();
112+
}
94113

95114
public DefaultConverterConfiguration(List<ObjectMarshaller<C>> oms) {
115+
this();
96116
int initPriority = -1;
97117
for(ObjectMarshaller<C> om : oms) {
98118
registerObjectMarshaller(om, initPriority--);
99119
}
100120
}
121+
122+
public DefaultConverterConfiguration(List<ObjectMarshaller<C>> oms, ProxyHandler proxyHandler) {
123+
this(oms);
124+
this.proxyHandler = proxyHandler;
125+
}
126+
101127

102128
public void registerObjectMarshaller(ObjectMarshaller<C> marshaller) {
103129
registerObjectMarshaller(marshaller, DEFAULT_PRIORITY);
@@ -142,5 +168,9 @@ public int compareTo(Entry entry) {
142168
return priority == entry.priority ? entry.seq - seq : entry.priority - priority;
143169
}
144170
}
171+
172+
public ProxyHandler getProxyHandler() {
173+
return this.proxyHandler;
174+
}
145175

146176
}

src/java/org/codehaus/groovy/grails/web/converters/configuration/ImmutableConverterConfiguration.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.codehaus.groovy.grails.web.converters.configuration;
1717

18+
import org.codehaus.groovy.grails.support.proxy.DefaultProxyHandler;
19+
import org.codehaus.groovy.grails.support.proxy.ProxyHandler;
1820
import org.codehaus.groovy.grails.web.converters.Converter;
1921
import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller;
2022

@@ -37,12 +39,20 @@ public class ImmutableConverterConfiguration<C extends Converter> implements Con
3739

3840
private final boolean prettyPrint;
3941

42+
private ProxyHandler proxyHandler;
43+
4044
public ImmutableConverterConfiguration(ConverterConfiguration<C> cfg) {
45+
this(cfg, new DefaultProxyHandler());
46+
}
47+
48+
public ImmutableConverterConfiguration(ConverterConfiguration<C> cfg, ProxyHandler proxyHandler) {
4149
marshallers = Collections.unmodifiableList(cfg.getOrderedObjectMarshallers());
4250
encoding = cfg.getEncoding();
4351
prettyPrint = cfg.isPrettyPrint();
4452
circularReferenceBehaviour = cfg.getCircularReferenceBehaviour();
53+
this.proxyHandler = proxyHandler;
4554
}
55+
4656

4757
/**
4858
* @see ConverterConfiguration#getMarshaller(Object)
@@ -80,4 +90,8 @@ public boolean isPrettyPrint() {
8090
public List<ObjectMarshaller<C>> getOrderedObjectMarshallers() {
8191
return marshallers;
8292
}
93+
94+
public ProxyHandler getProxyHandler() {
95+
return this.proxyHandler;
96+
}
8397
}

src/java/org/codehaus/groovy/grails/web/converters/marshaller/json/DeepDomainClassMarshaller.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.codehaus.groovy.grails.web.converters.marshaller.json;
1717

18+
import org.codehaus.groovy.grails.support.proxy.ProxyHandler;
19+
1820
/**
1921
* @author Siegfried Puchbauer
2022
* @since 1.1
@@ -24,6 +26,11 @@ public class DeepDomainClassMarshaller extends DomainClassMarshaller {
2426
public DeepDomainClassMarshaller(boolean includeVersion) {
2527
super(includeVersion);
2628
}
29+
30+
public DeepDomainClassMarshaller(boolean includeVersion, ProxyHandler proxyHandler) {
31+
super(includeVersion, proxyHandler);
32+
}
33+
2734

2835
protected boolean isRenderDomainClassRelations() {
2936
return true;

src/java/org/codehaus/groovy/grails/web/converters/marshaller/json/DomainClassMarshaller.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
import grails.converters.JSON;
1919
import org.apache.commons.logging.Log;
2020
import org.apache.commons.logging.LogFactory;
21+
import org.codehaus.groovy.grails.commons.GrailsClassUtils;
2122
import org.codehaus.groovy.grails.commons.GrailsDomainClass;
2223
import org.codehaus.groovy.grails.commons.GrailsDomainClassProperty;
23-
import org.codehaus.groovy.grails.commons.GrailsClassUtils;
24+
import org.codehaus.groovy.grails.support.proxy.DefaultProxyHandler;
25+
import org.codehaus.groovy.grails.support.proxy.ProxyHandler;
2426
import org.codehaus.groovy.grails.web.converters.ConverterUtil;
2527
import org.codehaus.groovy.grails.web.converters.exceptions.ConverterException;
2628
import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller;
2729
import org.codehaus.groovy.grails.web.json.JSONWriter;
28-
import org.hibernate.collection.AbstractPersistentCollection;
29-
import org.hibernate.Hibernate;
3030
import org.springframework.beans.BeanWrapper;
3131
import org.springframework.beans.BeanWrapperImpl;
3232

@@ -41,11 +41,18 @@ public class DomainClassMarshaller implements ObjectMarshaller<JSON> {
4141
private final Log log = LogFactory.getLog(getClass());
4242

4343
private boolean includeVersion = false;
44+
private ProxyHandler proxyHandler;
4445

4546
public DomainClassMarshaller(boolean includeVersion) {
47+
this(includeVersion, new DefaultProxyHandler());
48+
}
49+
50+
public DomainClassMarshaller(boolean includeVersion, ProxyHandler proxyHandler) {
4651
this.includeVersion = includeVersion;
52+
this.proxyHandler = proxyHandler;
4753
}
4854

55+
4956
public boolean isIncludeVersion() {
5057
return includeVersion;
5158
}
@@ -60,7 +67,7 @@ public boolean supports(Object object) {
6067

6168
public void marshalObject(Object value, JSON json) throws ConverterException {
6269
JSONWriter writer = json.getWriter();
63-
70+
value = proxyHandler.unwrapIfProxy(value);
6471
Class clazz = value.getClass();
6572
GrailsDomainClass domainClass = ConverterUtil.getDomainClass(clazz.getName());
6673
BeanWrapper beanWrapper = new BeanWrapperImpl(value);
@@ -93,23 +100,17 @@ public void marshalObject(Object value, JSON json) throws ConverterException {
93100
if (referenceObject == null) {
94101
writer.value(null);
95102
} else {
96-
if (referenceObject instanceof AbstractPersistentCollection) {
97-
// Force initialisation and get a non-persistent Collection Type
98-
AbstractPersistentCollection acol = (AbstractPersistentCollection) referenceObject;
99-
acol.forceInitialization();
100-
if (referenceObject instanceof SortedMap) {
101-
referenceObject = new TreeMap((SortedMap) referenceObject);
102-
} else if (referenceObject instanceof SortedSet) {
103-
referenceObject = new TreeSet((SortedSet) referenceObject);
104-
} else if (referenceObject instanceof Set) {
105-
referenceObject = new HashSet((Set) referenceObject);
106-
} else if (referenceObject instanceof Map) {
107-
referenceObject = new HashMap((Map) referenceObject);
108-
} else {
109-
referenceObject = new ArrayList((Collection) referenceObject);
110-
}
111-
} else if(!Hibernate.isInitialized(referenceObject)) {
112-
Hibernate.initialize(referenceObject);
103+
referenceObject = proxyHandler.unwrapIfProxy(referenceObject);
104+
if (referenceObject instanceof SortedMap) {
105+
referenceObject = new TreeMap((SortedMap) referenceObject);
106+
} else if (referenceObject instanceof SortedSet) {
107+
referenceObject = new TreeSet((SortedSet) referenceObject);
108+
} else if (referenceObject instanceof Set) {
109+
referenceObject = new HashSet((Set) referenceObject);
110+
} else if (referenceObject instanceof Map) {
111+
referenceObject = new HashMap((Map) referenceObject);
112+
} else if (referenceObject instanceof Collection){
113+
referenceObject = new ArrayList((Collection) referenceObject);
113114
}
114115
json.convertAnother(referenceObject);
115116
}

0 commit comments

Comments
 (0)