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.iso.packager;
020
021import org.jpos.core.Configurable;
022import org.jpos.core.Configuration;
023import org.jpos.core.ConfigurationException;
024import org.jpos.iso.ISOComponent;
025import org.jpos.iso.ISOException;
026import org.jpos.iso.ISOPackager;
027import org.jpos.util.LogSource;
028import org.jpos.util.Logger;
029
030/**
031 * Wrapper on standard packager
032 * @author bharavi gade
033 * @version $Revision$ $Date$
034 * @see ISOPackager
035 */
036public abstract class PackagerWrapper 
037    implements ISOPackager, LogSource, Configurable
038{
039    /** Logger receiving wrapped pack/unpack diagnostic events. */
040    protected Logger logger = null;
041    /** Logger realm associated with this packager. */
042    protected String realm = null;
043    /** Underlying packager that this wrapper delegates to. */
044    protected ISOPackager standardPackager = null;
045    /** Configuration applied to this wrapper (and propagated to the inner packager when configurable). */
046    protected Configuration cfg;
047
048    /** Default constructor. */
049    public PackagerWrapper() {
050        super();
051    }
052
053    public abstract byte[] pack (ISOComponent c) throws ISOException;
054
055    public abstract int unpack (ISOComponent c, byte[] b) throws ISOException;
056    
057    public String getFieldDescription(ISOComponent m, int fldNumber) {
058        return standardPackager != null ? 
059            standardPackager.getFieldDescription (m, fldNumber) : "";
060    }
061    /**
062     * Replaces the underlying packager.
063     *
064     * @param packger inner packager to delegate to
065     */
066    public void setPackager(ISOPackager packger)
067    {
068        this.standardPackager=packger;
069    }
070    /**
071     * Returns the underlying packager.
072     *
073     * @return the inner packager, or {@code null} if none is configured
074     */
075    public ISOPackager getPackager()
076    {
077        return standardPackager;
078    }
079
080    public void setLogger (Logger logger, String realm) {
081        this.logger = logger;
082        this.realm  = realm;
083        if (standardPackager instanceof LogSource)
084            ((LogSource) standardPackager).setLogger (logger, realm);
085    }
086    public String getRealm () {
087        return realm;
088    }
089    public Logger getLogger() {
090        return logger;
091    }
092    /**
093     * requires <code>inner-packager</code> property
094     * @param cfg Configuration object
095     * @throws ConfigurationException if configuration is invalid
096     */
097    public void setConfiguration (Configuration cfg) 
098        throws ConfigurationException
099    {
100        this.cfg = cfg;
101        String packagerName = cfg.get ("inner-packager");
102        try {
103            Class p = Class.forName(packagerName);
104            setPackager ((ISOPackager) p.newInstance());
105            if (standardPackager instanceof Configurable)
106                ((Configurable)standardPackager).setConfiguration (cfg);
107        } catch (ClassNotFoundException e) {
108            throw new ConfigurationException ("Invalid inner-packager", e);
109        } catch (InstantiationException e) {
110            throw new ConfigurationException ("Invalid inner-packager", e);
111        } catch (IllegalAccessException e) {
112            throw new ConfigurationException ("Invalid inner-packager", e);
113        }
114    }
115}
116