001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.core.io.registry.context; 021 022import java.util.List; 023import java.util.Locale; 024import java.util.Map; 025import java.util.Set; 026 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.io.registry.reflect.Instantiations; 030 031/** 032 * A {@link ThreadLocal} delegate {@link RenderingContext} used for {@link Instantiations#SINGLETON} marshallers. 033 * 034 * @since 7.2 035 */ 036public final class ThreadSafeRenderingContext implements RenderingContext { 037 038 private final ThreadLocal<RenderingContext> ctx = new ThreadLocal<RenderingContext>(); 039 040 public ThreadSafeRenderingContext() { 041 } 042 043 /** 044 * Configure this {@link RenderingContext} to use the given {@link RenderingContext} in the current thread. 045 * 046 * @param delegates The underlying {@link RenderingContext}. 047 * @since 7.2 048 */ 049 public void configureThread(RenderingContext delegates) { 050 ctx.set(delegates); 051 } 052 053 /** 054 * Get the underlying {@link RenderingContext}. 055 * 056 * @return The underlying {@link RenderingContext}. 057 * @since 7.2 058 */ 059 public RenderingContext getDelegate() { 060 return ctx.get(); 061 } 062 063 @Override 064 public Locale getLocale() { 065 return ctx.get().getLocale(); 066 } 067 068 @Override 069 public String getBaseUrl() { 070 return ctx.get().getBaseUrl(); 071 } 072 073 @Override 074 public SessionWrapper getSession(DocumentModel document) { 075 return ctx.get().getSession(document); 076 } 077 078 @Override 079 public void setExistingSession(CoreSession session) { 080 ctx.get().setExistingSession(session); 081 } 082 083 @Override 084 public Set<String> getProperties() { 085 return ctx.get().getProperties(); 086 } 087 088 @Override 089 public Set<String> getFetched(String entity) { 090 return ctx.get().getFetched(entity); 091 } 092 093 @Override 094 public Set<String> getTranslated(String entity) { 095 return ctx.get().getTranslated(entity); 096 } 097 098 @Override 099 public Set<String> getEnrichers(String entity) { 100 return ctx.get().getEnrichers(entity); 101 } 102 103 @Override 104 public WrappedContext wrap() { 105 return ctx.get().wrap(); 106 } 107 108 @Override 109 public <T> T getParameter(String name) { 110 return ctx.get().getParameter(name); 111 } 112 113 @Override 114 public boolean getBooleanParameter(String name) { 115 return ctx.get().getBooleanParameter(name); 116 } 117 118 @Override 119 public <T> List<T> getParameters(String name) { 120 return ctx.get().getParameters(name); 121 } 122 123 @Override 124 public Map<String, List<Object>> getAllParameters() { 125 return ctx.get().getAllParameters(); 126 } 127 128 @Override 129 public void setParameterValues(String name, Object... values) { 130 ctx.get().setParameterValues(name, values); 131 } 132 133 @Override 134 public void setParameterListValues(String name, List<Object> values) { 135 ctx.get().setParameterListValues(name, values); 136 } 137 138 @Override 139 public void addParameterValues(String name, Object... values) { 140 ctx.get().addParameterValues(name, values); 141 } 142 143 @Override 144 public void addParameterListValues(String name, List<Object> values) { 145 ctx.get().addParameterListValues(name, values); 146 } 147 148}