Template.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.pdd.core.dynamic;

  21. import java.io.Serializable;
  22. import java.util.Map;

  23. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  24. import org.openspcoop2.utils.SemaphoreLock;

  25. /**
  26.  * Template
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class Template implements Serializable {

  33.     /**
  34.      *
  35.      */
  36.     private static final long serialVersionUID = 1L;

  37.     private String name;
  38.     private byte[] template;
  39.     private Map<String, byte[]> templateIncludes;
  40.    
  41.     public Template(String name, byte[] template) {
  42.         this(name, template, null);
  43.     }
  44.     public Template(String name, byte[] template, Map<String, byte[]> templateIncludes) {
  45.         this.name = name;
  46.         this.template = template;
  47.         this.templateIncludes = templateIncludes;
  48.     }

  49.     public String getName() {
  50.         return this.name;
  51.     }
  52.     public byte[] getTemplate() {
  53.         return this.template;
  54.     }
  55.     public Map<String, byte[]> getTemplateIncludes() {
  56.         return this.templateIncludes;
  57.     }
  58.    
  59.     private boolean correctClassBackwardCompatibility = false;
  60.     private synchronized void _correctClassBackwardCompatibility() {
  61.         if(!this.correctClassBackwardCompatibility) {
  62.             Map<String, String> map = null;
  63.             OpenSPCoop2Properties op2 = OpenSPCoop2Properties.getInstance();
  64.             if(op2!=null) {
  65.                 map = op2.getTrasformazioni_backwardCompatibility();
  66.             }
  67.             if(map!=null && !map.isEmpty()) {
  68.                 if(this.template!=null) {
  69.                     boolean modify = false;
  70.                     String s = new String(this.template);
  71.                     for (String key : map.keySet()) {
  72.                         if(s.contains(key)) {
  73.                             String newValue = map.get(key);
  74.                             s = s.replaceAll(key, newValue);
  75.                             modify = true;
  76.                         }
  77.                     }
  78.                     if(modify) {
  79.                         this.template = s.getBytes();
  80.                     }
  81.                 }
  82.                 if(this.templateIncludes!=null && !this.templateIncludes.isEmpty()) {
  83.                     for (String idTemplate : this.templateIncludes.keySet()) {
  84.                         byte[]template = this.templateIncludes.get(idTemplate);
  85.                         if(template!=null) {
  86.                             boolean modify = false;
  87.                             String s = new String(template);
  88.                             for (String key : map.keySet()) {
  89.                                 if(s.contains(key)) {
  90.                                     String newValue = map.get(key);
  91.                                     s = s.replaceAll(key, newValue);
  92.                                     modify = true;
  93.                                 }
  94.                             }
  95.                             if(modify) {
  96.                                 template = s.getBytes();
  97.                                 this.templateIncludes.put(idTemplate, template); // update
  98.                             }
  99.                         }
  100.                     }
  101.                 }
  102.             }
  103.             this.correctClassBackwardCompatibility=true;
  104.         }
  105.     }
  106.     private void correctClassBackwardCompatibility() {
  107.         if(!this.correctClassBackwardCompatibility) {
  108.             this._correctClassBackwardCompatibility();
  109.         }
  110.     }
  111.    
  112.     private transient freemarker.template.Template templateFreeMarker;
  113.     private transient org.apache.velocity.Template templateVelocity;
  114.     private ZipTemplate templateZip;
  115.    
  116.     private transient org.openspcoop2.utils.Semaphore _lock = null;
  117.     private synchronized void initLock() {
  118.         if(this._lock==null) {
  119.             this._lock = new org.openspcoop2.utils.Semaphore("Template");
  120.         }
  121.     }
  122.     public org.openspcoop2.utils.Semaphore getLock(){
  123.         if(this._lock==null) {
  124.             initLock();
  125.         }
  126.         return this._lock;
  127.     }
  128.    
  129.     private void initTemplateFreeMarker() throws DynamicException{
  130.         if(this.templateFreeMarker==null) {
  131.             SemaphoreLock lock = null;
  132.             try {
  133.                 lock = this.getLock().acquire("initTemplateFreeMarker");
  134.             }catch(Throwable t) {
  135.                 throw new DynamicException(t.getMessage(),t);
  136.             }
  137.             try {
  138.                 this.correctClassBackwardCompatibility();
  139.                 this.templateFreeMarker = DynamicUtils.buildFreeMarkerTemplate(this);
  140.             }finally {
  141.                 this.getLock().release(lock, "initTemplateFreeMarker");
  142.             }
  143.         }
  144.     }
  145.     public freemarker.template.Template getTemplateFreeMarker() throws DynamicException{
  146.         if(this.templateFreeMarker==null) {
  147.             initTemplateFreeMarker();
  148.         }
  149.         return this.templateFreeMarker;
  150.     }
  151.    
  152.     private void initTemplateVelocity() throws DynamicException{
  153.         if(this.templateVelocity==null) {
  154.             SemaphoreLock lock = null;
  155.             try {
  156.                 lock = this.getLock().acquire("initTemplateVelocity");
  157.             }catch(Throwable t) {
  158.                 throw new DynamicException(t.getMessage(),t);
  159.             }
  160.             try {
  161.                 this.correctClassBackwardCompatibility();
  162.                 this.templateVelocity = DynamicUtils.buildVelocityTemplate(this);
  163.             }finally {
  164.                 this.getLock().release(lock, "initTemplateVelocity");
  165.             }
  166.         }
  167.     }
  168.     public org.apache.velocity.Template getTemplateVelocity() throws DynamicException{
  169.         if(this.templateVelocity==null) {
  170.             initTemplateVelocity();
  171.         }
  172.         return this.templateVelocity;
  173.     }
  174.    
  175.     private void initTemplateZip() throws DynamicException{
  176.         if(this.templateZip==null) {
  177.             SemaphoreLock lock = null;
  178.             try {
  179.                 lock = this.getLock().acquire("initTemplateZip");
  180.             }catch(Throwable t) {
  181.                 throw new DynamicException(t.getMessage(),t);
  182.             }
  183.             try {
  184.                 this.templateZip = new ZipTemplate(this.name, this.template);
  185.             }finally {
  186.                 this.getLock().release(lock, "initTemplateZip");
  187.             }
  188.         }
  189.     }
  190.     public ZipTemplate getZipTemplate() throws DynamicException{
  191.         if(this.templateZip==null) {
  192.             initTemplateZip();
  193.         }
  194.         return this.templateZip;
  195.     }
  196. }