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
046        Space sp;
047
048    String contextName = null;
049    String in = null;
050    String out = null;
051    String source = null;
052
053    Long timeout;
054
055        private List<Element> contextValues = null;
056
057        public void initService() {
058                NameRegistrar.register(getName(), this);
059
060        }
061
062        public void startService() {
063                // we re-register just in case the component was soft-stopped
064                NameRegistrar.register(getName(), this);
065        new Thread(this).start();
066        }
067
068        public void stopService() {
069                NameRegistrar.unregister(getName());
070        }
071
072        public void run() {
073                Thread.currentThread().setName(getName());
074                while (running()) {
075
076                        Object o = sp.in(in, timeout);
077
078                if (o != null) {
079                            Context ctx = new Context();
080                                ctx.put(contextName, o);
081                                
082                                if (contextValues != null) {
083                                        for (Element e : contextValues) {
084                                                ctx.put(e.getName(),e.getValue());
085                                        }
086                                }
087                                
088                        sp.out(out, ctx);
089                        }
090                }
091        }
092
093
094        public void setConfiguration(Configuration cfg)
095                        throws ConfigurationException {
096                super.setConfiguration(cfg);
097
098                Element persist = getPersist();
099
100                String ssp = persist.getChildText("space");
101
102                sp = SpaceFactory.getSpace(ssp != null ? ssp : "");
103
104                String sTimeout = persist.getChildText("timeout");
105                timeout = sTimeout == null ? 10000 : Long
106                                .parseLong(sTimeout);
107
108                contextName = persist.getChildText("context-name");
109        if (contextName == null) {
110            throw new ConfigurationException(
111                    "Missing 'context-name' property - the context name of the object received on 'in'");
112                }
113        
114        in = persist.getChildText("in");
115        if (in == null) {
116            throw new ConfigurationException(
117                    "Missing 'in' property - the queue to process objects from.");
118                }
119        
120        out = persist.getChildText("out");
121        if (out == null) {
122            throw new ConfigurationException(
123                    "Missing 'out' property - the target queue of the created context");
124                }
125        
126        Element values = persist.getChild("context-values");
127        if (values != null) {
128                contextValues = values.getChildren();
129        }
130        
131        }
132        
133        public void dump(PrintStream p, String indent) {
134                String inner = indent + "  ";
135                p.println(indent + "<ContextMaker name='" + getName() + "'>");
136                    for (Element e : contextValues) {
137                        p.println(indent+"<"+indent+e.getName()+">"+e.getValue()+"</"+indent+e.getName()+">");
138                    }
139                p.println(indent + "</ContextMaker>");
140        }
141}