org.grails.web.converters.configuration.ChainedConverterConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grace-plugin-converters Show documentation
Show all versions of grace-plugin-converters Show documentation
Grace Framework : Grace Plugin Converters
/*
* Copyright 2004-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.grails.web.converters.configuration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import grails.core.support.proxy.DefaultProxyHandler;
import grails.core.support.proxy.ProxyHandler;
import grails.util.Environment;
import org.grails.web.converters.Converter;
import org.grails.web.converters.exceptions.ConverterException;
import org.grails.web.converters.marshaller.ObjectMarshaller;
/**
* An immutable ConverterConfiguration which chains the lookup calls for ObjectMarshallers
* for performance reasons.
*
* @author Siegfried Puchbauer
* @author Graeme Rocher
*
* @since 1.1
*/
@SuppressWarnings("rawtypes")
public class ChainedConverterConfiguration implements ConverterConfiguration {
private final List> marshallerList;
private final ChainedObjectMarshaller root;
private final String encoding;
private final Converter.CircularReferenceBehaviour circularReferenceBehaviour;
private final boolean prettyPrint;
private final ProxyHandler proxyHandler;
private final boolean cacheObjectMarshallerByClass;
private Map> objectMarshallerForClassCache;
private final boolean developmentMode = Environment.isDevelopmentMode();
private final ObjectMarshaller NULL_HOLDER = new ObjectMarshaller() {
public boolean supports(Object object) {
return false;
}
public void marshalObject(Object object, C converter) throws ConverterException {
}
};
public ChainedConverterConfiguration(ConverterConfiguration cfg) {
this(cfg, new DefaultProxyHandler());
}
public ChainedConverterConfiguration(ConverterConfiguration cfg, ProxyHandler proxyHandler) {
this.marshallerList = cfg.getOrderedObjectMarshallers();
this.proxyHandler = proxyHandler;
this.encoding = cfg.getEncoding();
this.prettyPrint = cfg.isPrettyPrint();
this.cacheObjectMarshallerByClass = cfg.isCacheObjectMarshallerByClass();
if (this.cacheObjectMarshallerByClass) {
this.objectMarshallerForClassCache = new ConcurrentHashMap<>();
}
this.circularReferenceBehaviour = cfg.getCircularReferenceBehaviour();
List> oms = new ArrayList<>(this.marshallerList);
Collections.reverse(oms);
ChainedObjectMarshaller prev = null;
for (ObjectMarshaller om : oms) {
prev = new ChainedObjectMarshaller(om, prev);
}
this.root = prev;
}
public ObjectMarshaller getMarshaller(Object o) {
ObjectMarshaller marshaller = null;
Integer cacheKey = null;
if (!this.developmentMode && this.cacheObjectMarshallerByClass && o != null) {
cacheKey = System.identityHashCode(o.getClass());
marshaller = this.objectMarshallerForClassCache.get(cacheKey);
if (marshaller != this.NULL_HOLDER && marshaller != null && !marshaller.supports(o)) {
marshaller = null;
}
}
if (marshaller == null) {
marshaller = this.root.findMarhallerFor(o);
if (cacheKey != null) {
this.objectMarshallerForClassCache.put(cacheKey, marshaller != null ? marshaller : this.NULL_HOLDER);
}
}
return marshaller != this.NULL_HOLDER ? marshaller : null;
}
public String getEncoding() {
return this.encoding;
}
public Converter.CircularReferenceBehaviour getCircularReferenceBehaviour() {
return this.circularReferenceBehaviour;
}
public boolean isPrettyPrint() {
return this.prettyPrint;
}
public List> getOrderedObjectMarshallers() {
return this.marshallerList;
}
public ProxyHandler getProxyHandler() {
return this.proxyHandler;
}
public boolean isCacheObjectMarshallerByClass() {
return this.cacheObjectMarshallerByClass;
}
@SuppressWarnings("hiding")
public class ChainedObjectMarshaller implements ObjectMarshaller {
private final ObjectMarshaller om;
private final ChainedObjectMarshaller next;
public ChainedObjectMarshaller(ObjectMarshaller om, ChainedObjectMarshaller next) {
this.om = om;
this.next = next;
}
public ObjectMarshaller findMarhallerFor(Object o) {
if (supports(o)) {
return this.om;
}
return this.next != null ? this.next.findMarhallerFor(o) : null;
}
public boolean supports(Object object) {
return this.om.supports(object);
}
public void marshalObject(Object object, C converter) throws ConverterException {
this.om.marshalObject(object, converter);
}
}
}