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    protected Logger logger = null;
040    protected String realm = null;
041    protected ISOPackager standardPackager = null;
042    protected Configuration cfg;
043
044    public PackagerWrapper() {
045        super();
046    }
047
048    public abstract byte[] pack (ISOComponent c) throws ISOException;
049
050    public abstract int unpack (ISOComponent c, byte[] b) throws ISOException;
051    
052    public String getFieldDescription(ISOComponent m, int fldNumber) {
053        return standardPackager != null ? 
054            standardPackager.getFieldDescription (m, fldNumber) : "";
055    }
056    public void setPackager(ISOPackager packger)
057    {
058        this.standardPackager=packger;
059    }
060    public ISOPackager getPackager()
061    {
062        return standardPackager;
063    }
064
065    public void setLogger (Logger logger, String realm) {
066        this.logger = logger;
067        this.realm  = realm;
068        if (standardPackager instanceof LogSource)
069            ((LogSource) standardPackager).setLogger (logger, realm);
070    }
071    public String getRealm () {
072        return realm;
073    }
074    public Logger getLogger() {
075        return logger;
076    }
077    /**
078     * requires <code>inner-packager</code> property
079     * @param cfg Configuration object
080     * @throws ConfigurationException
081     */
082    public void setConfiguration (Configuration cfg) 
083        throws ConfigurationException
084    {
085        this.cfg = cfg;
086        String packagerName = cfg.get ("inner-packager");
087        try {
088            Class p = Class.forName(packagerName);
089            setPackager ((ISOPackager) p.newInstance());
090            if (standardPackager instanceof Configurable)
091                ((Configurable)standardPackager).setConfiguration (cfg);
092        } catch (ClassNotFoundException e) {
093            throw new ConfigurationException ("Invalid inner-packager", e);
094        } catch (InstantiationException e) {
095            throw new ConfigurationException ("Invalid inner-packager", e);
096        } catch (IllegalAccessException e) {
097            throw new ConfigurationException ("Invalid inner-packager", e);
098        }
099    }
100}
101