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.iso;
020
021import org.jdom2.Element;
022import org.jpos.core.Configuration;
023import org.jpos.core.ConfigurationException;
024import org.jpos.q2.QBeanSupport;
025import org.jpos.space.Space;
026import org.jpos.space.SpaceFactory;
027import org.jpos.transaction.Context;
028import org.jpos.util.Loggeable;
029import org.jpos.util.NameRegistrar;
030
031import java.io.PrintStream;
032import java.util.List;
033
034/**
035 * A utility QBean to provide the ability to monitor an 'in' queue for items that will be placed in
036 *  Context, along with any specified context-values and then place on the 'out' queue - for a
037 *  TransactionManager to process.
038
039 * @author Mark Salter
040 * @version $Revision: 2854 $ $Date: 2010-01-02 10:34:31 +0000 (Sat, 02 Jan 2010) $
041 */
042@SuppressWarnings("unchecked")
043public class ContextMaker extends QBeanSupport implements Runnable,
044                Loggeable {
045    /** Default constructor; no instance state to initialise. */
046    public ContextMaker() {}
047
048        Space sp;
049
050    String contextName = null;
051    String in = null;
052    String out = null;
053    String source = null;
054
055    Long timeout;
056
057        private List<Element> contextValues = null;
058
059        public void initService() {
060                NameRegistrar.register(getName(), this);
061
062        }
063
064        public void startService() {
065                // we re-register just in case the component was soft-stopped
066                NameRegistrar.register(getName(), this);
067        new Thread(this).start();
068        }
069
070        public void stopService() {
071                NameRegistrar.unregister(getName());
072        }
073
074        public void run() {
075                Thread.currentThread().setName(getName());
076                while (running()) {
077
078                        Object o = sp.in(in, timeout);
079
080                if (o != null) {
081                            Context ctx = new Context();
082                                ctx.put(contextName, o);
083                                
084                                if (contextValues != null) {
085                                        for (Element e : contextValues) {
086                                                ctx.put(e.getName(),e.getValue());
087                                        }
088                                }
089                                
090                        sp.out(out, ctx);
091                        }
092                }
093        }
094
095
096        public void setConfiguration(Configuration cfg)
097                        throws ConfigurationException {
098                super.setConfiguration(cfg);
099
100                Element persist = getPersist();
101
102                String ssp = persist.getChildText("space");
103
104                sp = SpaceFactory.getSpace(ssp != null ? ssp : "");
105
106                String sTimeout = persist.getChildText("timeout");
107                timeout = sTimeout == null ? 10000 : Long
108                                .parseLong(sTimeout);
109
110                contextName = persist.getChildText("context-name");
111        if (contextName == null) {
112            throw new ConfigurationException(
113                    "Missing 'context-name' property - the context name of the object received on 'in'");
114                }
115        
116        in = persist.getChildText("in");
117        if (in == null) {
118            throw new ConfigurationException(
119                    "Missing 'in' property - the queue to process objects from.");
120                }
121        
122        out = persist.getChildText("out");
123        if (out == null) {
124            throw new ConfigurationException(
125                    "Missing 'out' property - the target queue of the created context");
126                }
127        
128        Element values = persist.getChild("context-values");
129        if (values != null) {
130                contextValues = values.getChildren();
131        }
132        
133        }
134        
135        public void dump(PrintStream p, String indent) {
136                String inner = indent + "  ";
137                p.println(indent + "<ContextMaker name='" + getName() + "'>");
138                    for (Element e : contextValues) {
139                        p.println(indent+"<"+indent+e.getName()+">"+e.getValue()+"</"+indent+e.getName()+">");
140                    }
141                p.println(indent + "</ContextMaker>");
142        }
143}