56213123 JPOS Switch Tutorial

March 18, 2017 | Author: janata | Category: N/A
Share Embed Donate


Short Description

Download 56213123 JPOS Switch Tutorial...

Description

(ISO 8583 + JPOS + Log4J) in Java Socket Programming Oct 7, '08 2:10 PM for ever yone SECTION 1. INTRODUCTION Chapter 1.1. ISO 8583 You can read it's explanation first in Wikipedia. Chapter 1.2. JPOS JPOS is opensource framework for ISO 8583 . Source code and library are free but documentation is comercial. We can downlo ad it here. Chapter 1.3. Log4J Log4J is opensource logging library. We can downl oad it's library here and read it's mini e-book first written by Mr. Endy in her e. Chapter 1.4. Java Socket Programming There's no explanation. Just need the ba sic knowledge of Java network programmin g and Java Thread Programming and let's coding. SECTION 2. CODING First we prepare that all we need. In this time, we w ill use NetBeans as IDE. Be sides JPOS framework and Log4J library that I have m entioned above, we still nee d: 1. Xerces-Java XML parser library (xercesImpl.ja r) 2. Spring framework 3. Jakarta Apache Commons Logging library Chapter 2.1. IS O 8583 + JPOS + Log4J In NetBeans, create a new Java application named MyApp. Cr eate a package named c om.ndung.iso8583. JPOS framework need a packager to set w hich ISO 8583 version t hat will be used. In this time we will use ISO 8583 vers ion 1987. Download first iso87ascii.xml and place it in that package. Create a c lass as packager factory named PackagerFactory.java. package com.ndung.iso8583; import java.io.InputStream; import org.jpos.iso.ISOException; import org.jpos.is o.ISOPackager; import org.jpos.iso.packager.GenericPackager; public class Packag erFactory { public static ISOPackager getPackager() { ISOPackager packager = nul l; try { String filename = "iso87ascii.xml"; InputStream is = PackagerFactory.cl ass.getResourceAsStream(filename) ; packager = new GenericPackager(is); } catch (ISOException e) { e.printStackTrace(); } return packager; } } And then create a class named MessageHandler.java that will be used to handle re ceived message. Download first iso87asciiProperties.xml that will be used to tra nslate bits of ISO message to become a String message that can be read easily.

package com.ndung.iso8583; import import import import import import import java .io.FileInputStream; java.io.IOException; java.util.Properties; org.apache.log4j .Logger; org.jpos.iso.ISOException; org.jpos.iso.ISOMsg; org.jpos.iso.ISOPackage r; public class MessageHandler { private static ISOPackager packager = PackagerFact ory.getPackager(); private Logger logger = Logger.getLogger( getClass() ); publi c String process(ISOMsg isomsg) throws Exception { logger.info("ISO Message MTI is "+isomsg.getMTI()); logger.info("Is ISO message a incoming message? "+isomsg. isIncoming()); logger.info("Is ISO message a outgoing message? "+isomsg.isOutgoi ng()); logger.info("Is ISO message a request message? "+isomsg.isRequest()); log ger.info("Is ISO message a response message? "+isomsg.isResponse()); String mess age = ""; for (int i=0;i<128;i++){ if (isomsg.hasField(i)){ message += loadXM LProperties().getProperty(Integer.toString(i))+ "="+ isomsg.getValue(i)+"\n"; } } logger.info(message); return message; } public ISOMsg unpackRequest(String mes sage) throws ISOException, Exception { ISOMsg isoMsg = new ISOMsg(); isoMsg.setP ackager(packager); isoMsg.unpack(message.getBytes()); isoMsg.dump(System.out, " "); return isoMsg ; } public String packResponse(ISOMsg message) throws ISOExcep tion, Exception { message.dump(System.out, " "); return new String( message.pack () ) ; } public Properties loadXMLProperties(){ Properties prop = new Properties (); try{ FileInputStream input=new FileInputStream("iso87asciiProperties.xml" ); prop.loadFromXML(input); input.close(); } catch(IOException e){ e.printStackTra ce(); } return prop; } }

Chapter 2.2. Java Socket Programming + Log4J Create a package named com.ndung.so cket and then create four classes named Serve rConfig.java, SocketServerHandlerF actory.java, SocketServerHandler.java, SocketC onnectionServer. Before that crea te log4j.properties as logging configuration in default package. # Category Conf iguration log4j.rootLogger=INFO,Konsole,Roll # Console Appender Configuration lo g4j.appender.Konsole=org.apache.log4j.ConsoleAppender log4j.appender.Konsole.lay out=org.apache.log4j.PatternLayout # Date Format based on ISO 8601 : %d log4j.appen der.Konsole.layout.ConversionPattern=%d [%t] %5p %c  %m%n # Roll Appender Configur ation log4j.appender.Roll=org.apache.log4j.RollingFileAppender log4j.appender.Ro ll.File=/home/ndung/NetBeansProjects/MyApp/log/myApp.log log4j.appender.Roll.Max FileSize=10KB log4j.appender.Roll.MaxBackupIndex=2 log4j.appender.Roll.layout=or g.apache.log4j.PatternLayout # Date Format based on ISO 8601 : %d log4j.appender.Ro ll.layout.ConversionPattern=%d [%t] %p (%F:%L)  %m%n package com.ndung.socket; public class ServerConfig { private int port; public i nt getPort() { return port; } public void setPort(int port) { this.port = port; } } package com.ndung.socket; import com.ndung.iso8583.MessageHandler; import java.i o.IOException; import java.net.Socket; public class SocketServerHandlerFactory { private MessageHandler messageHandler; public SocketServerHandlerFactory(Messag eHandler messageHandler) { this.messageHandler = messageHandler; } public Socket ServerHandler createHandler(Socket socket) throws IOException { return new Socke tServerHandler(socket, messageHandler); } } package com.ndung.socket; import com.ndung.iso8583.MessageHandler;

import import import import import import import java.io.BufferedReader; java.io.DataOutputStream; java.io.IOException; java.io.I nputStreamReader; java.net.Socket; org.apache.log4j.Logger; org.jpos.iso.ISOMsg; public class SocketServerHandler extends Thread{ private Logger logger = Logger. getLogger( getClass() ); private Socket serverSocket ; private BufferedReader in FromClient; private DataOutputStream outToClient; private MessageHandler message Handler; private String datafromClient; public SocketServerHandler(Socket socket , MessageHandler messageHandler) thr ows IOException { super("SocketHandler (" + socket.getInetAddress().getHostAddress() + ")" ); this.serverSocket = socket ; this.messageHandler = messageHandler; this.inFromClient = new BufferedReader(new InputStreamReader(serverSocke t.getInputStream())); this.outToClient = new Data OutputStream(serverSocket.getOutputStream()); } @Override public void run() { tr y { logger.info("Server is ready..."); while (true) { logger.info("There is a cl ient connected..."); outToClient.writeBytes("InfoServer version 0.1\n"); datafro mClient = inFromClient.readLine(); logger.info("Data From Client : "+datafromCli ent); ISOMsg isomsg = messageHandler.unpackRequest(datafromClient); outToClient. writeBytes(messageHandler.process(isomsg)); } } catch (IOException ioe) { logger .error("error: " + ioe); } catch (Exception e) { logger.error("error: " + e); } finally { try { if (inFromClient != null) inFromClient.close(); if (outToClient != null) outToClient.close(); if (serverSocket != null) serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } package com.ndung.socket;

import java.io.IOException; import java.net.ServerSocket; public class SocketCon nectionServer { private ServerConfig config; private SocketServerHandlerFactory handlerFactory; private boolean stop; public SocketConnectionServer(ServerConfig config, SocketServerHandlerFactor y handlerFactory) { this.config = config; thi s.handlerFactory = handlerFactory; } public void start() throws IOException { st op = false; final ServerSocket serverSocket = new ServerSocket(config.getPort()) ; new Thread(new Runnable() { public void run() { while (!stop) { try { handlerF actory.createHandler(serverSocket.accept()).sta rt(); } catch (IOException e) { e.printStackTrace(); } } } }).start(); } public void stop() { stop = true; } } C reate a socket server class named MyServer.java. This class also as a main clas s for our application. And then we will create a socket client class named MyCli ent.java. Before that create applicationContext.xml in default package as Sprin g configuration injection for our main class. <?xml version="1.0" encoding="U TF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.sprin gframework.org/dtd/spring-beans.dtd"> <beans> <bean id="socketConnec tionServer" class="com.ndung.socket.SocketConnectio nServer"> <constructor -arg> <ref local="config" /> </constructor-arg> <constructor-a rg> <ref local="socketServerHandlerFactory" /> </constructor-arg> </bean> <bean id="config" class="com.ndung.socket.ServerConfig"> &l t;property name="port"> <value>50000</value>

</property> </bean> <bean id="socketServerHandlerFactory" class=" com.ndung.socket.SocketServe rHandlerFactory"> <constructor-arg> <re f local="messageHandler" /> </constructor-arg> </bean> <bean i d="messageHandler" class="com.ndung.iso8583.MessageHandler"> </bean> &l t;/beans> package com.ndung.socket; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; publ ic class MyServer { public static void main(String[] args) throws IOException { ApplicationContext ctx = new ClassPathXmlApplicationContext("application Context .xml"); SocketConnectionServer server = (SocketConnectionServer) ctx.getBean("so cketConnectionServer"); server.start(); } } package com.ndung.socket; import import import import import import java.io.Buff eredReader; java.io.DataOutputStream; java.io.IOException; java.io.InputStreamRe ader; java.net.Socket; org.apache.log4j.Logger; public class MyClient { private final int MY_PORT=50000; private final String Ta rgetHost = "localhost"; private final String QUIT = "QUIT"; private Logger logge r = Logger.getLogger( getClass() ); public MyClient() { try { BufferedReader inF romUser = new BufferedReader(new InputStreamReader (System.in)); Socket clientSo cket = new Socket(TargetHost, MY_PORT); DataOutputStream outToServer = new DataO utputStream(clientSocket.get OutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamRead er(clientSocket.getInputStream())); logger.in fo(inFromServer.readLine()); boolean isQuit = false;

while (!isQuit) { System.out.print("Your data : "); String cmd = inFromUser.read Line(); cmd = cmd.toUpperCase(); if (cmd.equals(QUIT)) { isQuit = true; } outToS erver.writeBytes(cmd + "\n"); String message = inFromServer.readLine(); while (m essage!=null){ logger.info("From Server: " + message); message = inFromServer.re adLine(); } } outToServer.close(); inFromServer.close(); clientSocket.close(); } catch (IOException ioe) { logger.error("Error:" + ioe); } catch (Exception e) { logger.error("Error:" + e); } } public static void main(String[] args) { new My Client(); } } Run our application first. It means our main class (MyServer.java) will be run f irst. And then run client as much that we want. It means MyClient .java will be r un twice or more. And then in one of our client application cons ole enter an inp ut data. It means a String of ISO message. As example: 0210723A 00010A808400185936001410010999990110000000100000001007021533000001191533 1006100 7065656561006090102240000000901360020100236C0102240000000 Look in both of our ap plication console either MyServer or MyClient. What do you see? Btw, Happy Eid M ubarak...

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF