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.space; 020 021import org.jpos.iso.ISOException; 022import org.jpos.iso.ISOMsg; 023import org.jpos.iso.ISOSource; 024import org.jpos.q2.Q2; 025 026import java.io.IOException; 027import java.io.Serializable; 028import java.util.UUID; 029 030@SuppressWarnings("unused unchecked") 031public class SpaceSource implements ISOSource, SpaceListener<String,ISOMsg>, Serializable { 032 private static final long serialVersionUID = -2629671264411649185L; 033 034 private transient Space isp = SpaceFactory.getSpace(); 035 private transient LocalSpace sp; 036 private String key; 037 private long timeout; 038 private boolean connected; 039 040 public SpaceSource(LocalSpace sp, ISOSource source, long timeout) { 041 this.key = "SS." + UUID.randomUUID().toString(); 042 this.connected = source.isConnected(); 043 this.sp = sp; 044 sp.addListener(key, this, timeout + 10000L); 045 isp.out (key, source, timeout); 046 } 047 048 public void init (LocalSpace sp, long timeout) { 049 this.sp = sp; 050 this.timeout = timeout; 051 } 052 053 @Override 054 public void send(ISOMsg m) throws IOException, ISOException { 055 if (sp == null) 056 throw new IOException ("Space not configured"); 057 sp.out(key, m, timeout); 058 } 059 060 @Override 061 public boolean isConnected() { 062 return connected; // should be called _was_ connected 063 } 064 065 @Override 066 public void notify(String key, ISOMsg m) { 067 sp.removeListener(this.key, this); 068 ISOSource source = (ISOSource) isp.inp (key); 069 if (m != null && source != null && source.isConnected()) { 070 try { 071 source.send((ISOMsg) m.clone()); 072 sp.inp(key); // actually pick it 073 } catch (Exception e) { 074 Q2.getQ2().getLog().warn(e); 075 } 076 } 077 } 078}