001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.q2.qbean;
020
021import org.jdom2.Element;
022import org.jdom2.JDOMException;
023import org.jpos.core.ConfigurationException;
024import org.jpos.core.Environment;
025import org.jpos.core.XmlConfigurable;
026import org.jpos.iso.ISOException;
027import org.jpos.iso.ISOUtil;
028import org.jpos.q2.QBeanSupport;
029import org.jpos.q2.QFactory;
030
031import java.io.IOException;
032import java.security.GeneralSecurityException;
033
034/**
035 * QBean that materialises {@code <template>} children of its persist element
036 * into actual deploy descriptors when the bean initialises.
037 */
038public class TemplateDeployer extends QBeanSupport implements XmlConfigurable {
039    /** Default constructor; no instance state to initialise. */
040    public TemplateDeployer() {}
041    Element config;
042
043    @Override
044    protected void initService() throws ISOException, GeneralSecurityException, IOException, JDOMException {
045        for (Element e : config.getChildren("template")) {
046            try {
047                String resource = Environment.get(e.getAttributeValue("resource"));
048                String descriptorPrefix = Environment.get(e.getAttributeValue("descriptor-prefix"));
049                if (QFactory.isEnabled(e)) {
050                    String prefix = Environment.get(e.getTextTrim());
051                    String[] ss = ISOUtil.commaDecode(prefix);
052                    for (String s : ss) {
053                        s = s.trim();
054                        getServer().deployTemplate(resource, "%s_%s.xml".formatted(descriptorPrefix, s), s);
055                    }
056                } else
057                    getLog().warn("%s (%s) not enabled".formatted(resource, descriptorPrefix));
058            } catch (Exception ex) {
059                getLog().error(ex);
060            }
061        }
062    }
063
064    @Override
065    public void setConfiguration(Element config) throws ConfigurationException {
066        this.config = config;
067    }
068}