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
034public class TemplateDeployer extends QBeanSupport implements XmlConfigurable {
035    Element config;
036
037    @Override
038    protected void initService() throws ISOException, GeneralSecurityException, IOException, JDOMException {
039        for (Element e : config.getChildren("template")) {
040            try {
041                String resource = Environment.get(e.getAttributeValue("resource"));
042                String descriptorPrefix = Environment.get(e.getAttributeValue("descriptor-prefix"));
043                if (QFactory.isEnabled(e)) {
044                    String prefix = Environment.get(e.getTextTrim());
045                    String[] ss = ISOUtil.commaDecode(prefix);
046                    for (String s : ss) {
047                        s = s.trim();
048                        getServer().deployTemplate(resource, "%s_%s.xml".formatted(descriptorPrefix, s), s);
049                    }
050                } else
051                    getLog().warn("%s (%s) not enabled".formatted(resource, descriptorPrefix));
052            } catch (Exception ex) {
053                getLog().error(ex);
054            }
055        }
056    }
057
058    @Override
059    public void setConfiguration(Element config) throws ConfigurationException {
060        this.config = config;
061    }
062}