mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Template extension spring configuration support
- similar pattern to existing script bean extension support - new root model helper objects and custom methods can be added via spring configuration Cleanup of script extension spring support Fix to thread safety of configured script extension beans that use the Scopable interface git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5369 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* 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.alfresco.repo.template;
|
||||
|
||||
import org.alfresco.service.cmr.repository.TemplateExtensionImplementation;
|
||||
import org.alfresco.service.cmr.repository.TemplateImageResolver;
|
||||
import org.alfresco.service.cmr.repository.TemplateService;
|
||||
|
||||
/**
|
||||
* Abstract base class for a template extension implementation
|
||||
*
|
||||
* @author Kevin Roast
|
||||
*/
|
||||
public abstract class BaseTemplateExtensionImplementation implements TemplateExtensionImplementation
|
||||
{
|
||||
/** The template service instance */
|
||||
private TemplateService templateService;
|
||||
|
||||
/** The name of the template extension */
|
||||
private String extensionName;
|
||||
|
||||
/** The TemplateImageResolver for the current template execution thread */
|
||||
private ThreadLocal<TemplateImageResolver> resolver = new ThreadLocal<TemplateImageResolver>();
|
||||
|
||||
|
||||
/**
|
||||
* @param templateService The TemplateService to set.
|
||||
*/
|
||||
public void setTemplateService(TemplateService templateService)
|
||||
{
|
||||
this.templateService = templateService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.repository.TemplateExtensionImplementation#setTemplateImageResolver(org.alfresco.service.cmr.repository.TemplateImageResolver)
|
||||
*/
|
||||
public void setTemplateImageResolver(TemplateImageResolver resolver)
|
||||
{
|
||||
this.resolver.set(resolver);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.repository.TemplateExtensionImplementation#getTemplateImageResolver()
|
||||
*/
|
||||
public TemplateImageResolver getTemplateImageResolver()
|
||||
{
|
||||
return this.resolver.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers this template extension with the Template Service
|
||||
*/
|
||||
public void register()
|
||||
{
|
||||
this.templateService.registerExtension(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the template extension
|
||||
*
|
||||
* @return the name of the template extension
|
||||
*/
|
||||
public String getExtensionName()
|
||||
{
|
||||
return extensionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param extensionName The template extension name.
|
||||
*/
|
||||
public void setExtensionName(String extensionName)
|
||||
{
|
||||
this.extensionName = extensionName;
|
||||
}
|
||||
}
|
@@ -32,8 +32,6 @@ import org.alfresco.repo.jscript.CategoryTemplateNode;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.cmr.repository.TemplateImageResolver;
|
||||
import org.alfresco.service.cmr.repository.TemplateNode;
|
||||
import org.alfresco.service.cmr.search.CategoryService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
@@ -42,19 +40,29 @@ import org.alfresco.service.namespace.QName;
|
||||
*
|
||||
* @author Andy Hind
|
||||
*/
|
||||
public final class Classification
|
||||
public class Classification extends BaseTemplateExtensionImplementation
|
||||
{
|
||||
private ServiceRegistry services;
|
||||
private TemplateImageResolver imageResolver;
|
||||
private StoreRef storeRef;
|
||||
|
||||
public Classification(StoreRef storeRef, ServiceRegistry services, TemplateImageResolver imageResolver)
|
||||
/**
|
||||
* Sets the service registry
|
||||
*
|
||||
* @param services the service registry
|
||||
*/
|
||||
public void setServiceRegistry(ServiceRegistry services)
|
||||
{
|
||||
this.storeRef = storeRef;
|
||||
this.services = services;
|
||||
this.imageResolver = imageResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param storeUrl The store ref url to set.
|
||||
*/
|
||||
public void setStoreUrl(String storeUrl)
|
||||
{
|
||||
this.storeRef = new StoreRef(storeUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all the category nodes in a given classification.
|
||||
*
|
||||
@@ -110,7 +118,7 @@ public final class Classification
|
||||
ArrayList<CategoryTemplateNode> categoryNodes = new ArrayList<CategoryTemplateNode>(cars.size());
|
||||
for (ChildAssociationRef car : cars)
|
||||
{
|
||||
categoryNodes.add(new CategoryTemplateNode(car.getChildRef(), this.services, this.imageResolver));
|
||||
categoryNodes.add(new CategoryTemplateNode(car.getChildRef(), this.services, getTemplateImageResolver()));
|
||||
}
|
||||
return categoryNodes;
|
||||
}
|
||||
|
@@ -43,7 +43,7 @@ import freemarker.template.TemplateNumberModel;
|
||||
* dateCompare(dateA, dateB) - 1 if dateA if greater than dateB
|
||||
* dateCompare(dateA, dateB, millis) - 1 if dateA is greater than dateB by at least millis, else 0
|
||||
*/
|
||||
public final class DateCompareMethod implements TemplateMethodModelEx
|
||||
public class DateCompareMethod extends BaseTemplateExtensionImplementation implements TemplateMethodModelEx
|
||||
{
|
||||
/**
|
||||
* @see freemarker.template.TemplateMethodModel#exec(java.util.List)
|
||||
|
@@ -34,10 +34,8 @@ import freemarker.template.TemplateNumberModel;
|
||||
|
||||
/**
|
||||
* @author Roy Wetherall
|
||||
*
|
||||
|
||||
*/
|
||||
public final class DateIncrementMethod implements TemplateMethodModelEx
|
||||
public class DateIncrementMethod extends BaseTemplateExtensionImplementation implements TemplateMethodModelEx
|
||||
{
|
||||
/**
|
||||
* @see freemarker.template.TemplateMethodModel#exec(java.util.List)
|
||||
|
@@ -35,6 +35,7 @@ import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.TemplateException;
|
||||
import org.alfresco.service.cmr.repository.TemplateExtensionImplementation;
|
||||
import org.alfresco.service.cmr.repository.TemplateImageResolver;
|
||||
import org.alfresco.service.cmr.repository.TemplateNode;
|
||||
import org.alfresco.service.cmr.repository.TemplateProcessor;
|
||||
@@ -100,7 +101,7 @@ public class FreeMarkerProcessor implements TemplateProcessor
|
||||
{
|
||||
this.contentService = contentService;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the default template encoding
|
||||
*
|
||||
@@ -338,18 +339,13 @@ public class FreeMarkerProcessor implements TemplateProcessor
|
||||
// current date/time is useful to have and isn't supplied by FreeMarker by default
|
||||
model.put("date", new Date());
|
||||
|
||||
// Session support
|
||||
model.put("session", new Session(services, imageResolver));
|
||||
|
||||
// Classification support
|
||||
|
||||
model.put("classification", new Classification(companyHome.getStoreRef(), services, imageResolver));
|
||||
|
||||
// add custom method objects
|
||||
model.put("hasAspect", new HasAspectMethod());
|
||||
model.put("message", new I18NMessageMethod());
|
||||
model.put("dateCompare", new DateCompareMethod());
|
||||
model.put("incrementDate", new DateIncrementMethod());
|
||||
// add the template extensions to the model
|
||||
// the extensions include custom root helper objects and custom template method objects
|
||||
for (TemplateExtensionImplementation ext : services.getTemplateService().getExtensions())
|
||||
{
|
||||
ext.setTemplateImageResolver(imageResolver);
|
||||
model.put(ext.getExtensionName(), ext);
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
@@ -24,14 +24,11 @@
|
||||
*/
|
||||
package org.alfresco.repo.template;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.service.cmr.repository.TemplateNode;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
import freemarker.ext.beans.BeanModel;
|
||||
import freemarker.ext.beans.StringModel;
|
||||
import freemarker.template.TemplateMethodModelEx;
|
||||
import freemarker.template.TemplateModelException;
|
||||
import freemarker.template.TemplateScalarModel;
|
||||
@@ -44,9 +41,9 @@ import freemarker.template.TemplateScalarModel;
|
||||
* Method returns whether a TemplateNode has a particular aspect applied to it. The aspect
|
||||
* name can be either the fully qualified QName or the short prefixed name string.
|
||||
* <p>
|
||||
* Usage: hasAspect(TemplateNode node, String aspect)
|
||||
* Usage: hasAspect(TemplateNode node, String aspect) - 1 on true, 0 on false
|
||||
*/
|
||||
public final class HasAspectMethod implements TemplateMethodModelEx
|
||||
public class HasAspectMethod extends BaseTemplateExtensionImplementation implements TemplateMethodModelEx
|
||||
{
|
||||
/**
|
||||
* @see freemarker.template.TemplateMethodModel#exec(java.util.List)
|
||||
|
@@ -37,11 +37,11 @@ import freemarker.template.TemplateScalarModel;
|
||||
*
|
||||
* Custom FreeMarker Template language method.
|
||||
* <p>
|
||||
* Method an I18N message resolved for the current locale and specified message ID.
|
||||
* Returns an I18N message resolved for the current locale and specified message ID.
|
||||
* <p>
|
||||
* Usage: message(String id)
|
||||
*/
|
||||
public final class I18NMessageMethod implements TemplateMethodModelEx
|
||||
public class I18NMessageMethod extends BaseTemplateExtensionImplementation implements TemplateMethodModelEx
|
||||
{
|
||||
/**
|
||||
* @see freemarker.template.TemplateMethodModel#exec(java.util.List)
|
||||
|
@@ -32,20 +32,20 @@ import org.alfresco.service.cmr.repository.TemplateImageResolver;
|
||||
*
|
||||
* @author Andy Hind
|
||||
*/
|
||||
public class Session
|
||||
public class Session extends BaseTemplateExtensionImplementation
|
||||
{
|
||||
|
||||
private ServiceRegistry services;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private TemplateImageResolver imageResolver;
|
||||
|
||||
public Session(ServiceRegistry services, TemplateImageResolver imageResolver)
|
||||
/**
|
||||
* Sets the service registry
|
||||
*
|
||||
* @param services the service registry
|
||||
*/
|
||||
public void setServiceRegistry(ServiceRegistry services)
|
||||
{
|
||||
this.services = services;
|
||||
this.imageResolver = imageResolver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the current authentication ticket.
|
||||
*
|
||||
|
@@ -26,11 +26,15 @@ package org.alfresco.repo.template;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.alfresco.service.cmr.repository.ScriptImplementation;
|
||||
import org.alfresco.service.cmr.repository.TemplateException;
|
||||
import org.alfresco.service.cmr.repository.TemplateExtensionImplementation;
|
||||
import org.alfresco.service.cmr.repository.TemplateProcessor;
|
||||
import org.alfresco.service.cmr.repository.TemplateService;
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -40,6 +44,8 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
/**
|
||||
* Implementation of the TemplateService using Spring configured template engines.
|
||||
*
|
||||
* @author Kevin Roast
|
||||
*/
|
||||
public class TemplateServiceImpl implements TemplateService, ApplicationContextAware
|
||||
@@ -58,6 +64,10 @@ public class TemplateServiceImpl implements TemplateService, ApplicationContextA
|
||||
/** Threadlocal instance for template processor cache */
|
||||
private static ThreadLocal<Map<String, TemplateProcessor>> processors = new ThreadLocal<Map<String, TemplateProcessor>>();
|
||||
|
||||
/** List of global template extensions */
|
||||
private List<TemplateExtensionImplementation> globalExtensions = new ArrayList<TemplateExtensionImplementation>();
|
||||
|
||||
|
||||
/**
|
||||
* Set the application context
|
||||
*
|
||||
@@ -68,6 +78,22 @@ public class TemplateServiceImpl implements TemplateService, ApplicationContextA
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.repository.TemplateService#registerExtension(org.alfresco.service.cmr.repository.TemplateExtensionImplementation)
|
||||
*/
|
||||
public void registerExtension(TemplateExtensionImplementation extension)
|
||||
{
|
||||
this.globalExtensions.add(extension);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.repository.TemplateService#getExtensions()
|
||||
*/
|
||||
public List<TemplateExtensionImplementation> getExtensions()
|
||||
{
|
||||
return this.globalExtensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param defaultTemplateEngine The default Template Engine name to set.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user