서비스 만들기

이 장에서는 XPLATFORM 응용프로그램이 사용하는 Server용 Service에 대해서 설명합니다.
XPLATFORM 프로그램은 화면(Form)에서 입력 받은 데이터를 서버로 전송할 때 transaction()함수를 사용합니다. Transaction()함수는 입력 받은 Variable과 Dataset을 XML Format으로 변형하여 Server의 JSP Service에게 전달합니다.
JSP Service의 결과값은 transaction() 호출 시에 등록한 Callback 함수에서 ErrorCode, ErrorMsg 인자를 통해 확인이 가능합니다.
Server용 Service는 JSP뿐만 아니라 Java Servlet, ASP, ASP.NET, PHP 등의 언어로 작성할 수 있습니다. 본 예제에서는 화면(Form)에서 입력 받은 데이터를 JSP를 사용하여 데이터를 관리하는 방법을 설명합니다.
작성된 JSP Service는 웹브라우저를 통해서 테스트 할 수 없습니다. 왜냐하면,
매개변수로 Variable과 Dataset을 사용하기 때문에 웹브라우저를 통해서는 해당 매개변수를 서버에 전달 할 수 없기 때문입니다.

본 예제 JSP에 전달되는 XML Format Data는 XPLATFORM 라이브러리(XPLATFORM X-API)가 아닌 다른 파서(Parser)를 이용하여 분석할 수 있습니다. 하지만, XPLATFORM X-API의 전용객체를 이용하시면 불필요한 분석작업을 구현하실 필요가 없으므로 프로그램을 좀더 쉽게 작성할 수 있습니다.

XPLATFORM 에서 사용되는 XML Data는 본 예제와 다른 방법으로 만들어도 XML 규칙만 지켜진다면 어떤 언어, 어떤 서버를 사용하셔도 무방합니다.

일반적으로 사용되는 서버구성 예

서비스 작성언어 : Java 계열(Java Servlet, JSP), ASP 계열(ASP, ASP.NET)

웹서버 : Apache, WAS에 포함되어 있는 웹서버

데이터베이스(Database) : Oracle, MS SQLServer, Sybase, DB2

WAS(Web Application Server) : WebLogic, WebSphere, JEUS, Tomcat, Resin

Server요청 서비스를 작성할 때는 UX-Studio를 사용하지 않고 일반 웹에디터(eclips, Visual Studio .NET, NotePad, …)를 사용합니다.

본 예제는 JSP를 사용하여 Tomcat에서 실행이 가능하도록 구현되었습니다.

서비스들의 설명

demo용 XPLATFORM 응용프로그램을 실행하기 위해서는 3개의 Server용 서비스들이 필요합니다.

초기자료 생성 서비스 – initdata.jsp

// 1.라이브러리 import (XPLATFORM X-API포함)
// 2.Mime Type 정의
// 3.XPLATFORM 기본객체(PlatformData) 생성
try {
	// 5.Data 처리
	// 6.ErrorCode, ErrorMsg 처리하기 (성공 메시지)
} Catch (에러) {
	// 6.ErrorCode, ErrorMsg 처리하기 (실패 메시지)
}
// 7.결과 Data Client에게 보내기
<!-- 1.library import -->
<%@ page import="java.io.*" %>
<%@ page import="com.tobesoft.xplatform.data.*" %>
<%@ page import="com.tobesoft.xplatform.tx.*" %>

<!-- 2. mime type define -->
<%@ page contentType="text/xml; charset=UTF-8" %>

<%
/** 3.XPLATFORM Basic Object creation **/
PlatformData pdata = new PlatformData();

/** ErrorCode, ErrorMsg creation **/
int nErrorCode = 0;
String strErrorMsg = "START";

try {
	/** 4. receive client request **/
	// not need to receive

	/** 5. handle data : save data to file **/
	/** 5.1 create Dataset and input basic data to the Dataset **/
	DataSet ds = new DataSet("dsPerson");
	ds.addColumn("pid",DataTypes.INT);
	ds.addColumn("person_name",DataTypes.STRING, 8);
	ds.addColumn("home_telno", DataTypes.STRING, 8);
	ds.addColumn("home_address", DataTypes.STRING, 16);
	ds.addColumn("company", DataTypes.STRING, 8);
	ds.addColumn("company_telno", DataTypes.STRING, 8);
	ds.addColumn("company_fax", DataTypes.STRING, 8);
	ds.addColumn("company_address", DataTypes.STRING, 8);
	ds.addColumn("email", DataTypes.STRING, 8);
	ds.addColumn("mobile", DataTypes.STRING, 8);
	ds.addColumn("jikgub", DataTypes.STRING, 8);
	ds.addColumn("dept", DataTypes.STRING, 8);
	ds.addColumn("remark", DataTypes.STRING, 8);

	int row = 0;
	int i = 0;
	String[] names = new String[2];
	String[] tel1 = new String[2];
	String[] tel2 = new String[2];
	String[] tel3 = new String[2];
	String[] tel4 = new String[2];
	String[] address = new String[2];
	String[] company = new String[2];
	String[] email = new String[2];

	names[0] = "Steven";
	names[1] = "Charls";
	email[0] = "Steven@hp.com";
	email[1] = "Charls@dell.com";
	address[0] = "NewYork City";
	address[1] = "Manhattan City";
	company[0] = "HP";
	company[1] = "Dell";
	homepage[1] = "www.dell.com";
	tel1[0] = "02-123-4567";
	tel1[1] = "02-321-7654";
	tel2[0] = "02-987-6543";
	tel2[1] = "02-789-3456";
	tel3[0] = "02-456-7890";
	tel3[1] = "02-654-0987";
	tel4[0] = "010-1234-7890";
	tel4[1] = "010-4321-0987";

	for (i = 0; i < 2; i++)
	{
		row = ds.newRow(); 
		ds.set(row,"pid",i);
		ds.set(row,"person_name",names[i]);
		ds.set(row,"home_telno",tel1[i]);
		ds.set(row,"home_address",address[i]);
		ds.set(row,"company",company[i]);
		ds.set(row,"company_telno",tel2[i]);
		ds.set(row,"company_fax",tel3[i]);
		ds.set(row,"company_address",address[i]);
		ds.set(row,"email",email[i]);
		ds.set(row,"mobile",tel4[i]);
		ds.set(row,"jikgub","0" + (i + 1));
		ds.set(row,"dept","0" + (i + 1));
		ds.set(row,"remark",i);
	}

	pdata.addDataSet(ds);

	/** 5.2 save Dataset to a file **/
	String targetFilename = "./saveFile.bin";
	OutputStream target = new FileOutputStream(targetFilename);
	PlatformResponse res = new PlatformResponse(target, 
	PlatformType.CONTENT_TYPE_BINARY);
	res.setData(pdata);
	res.sendData();
	target.close(); 
	System.out.println("after file write.."); 

	/** 6.1 set the ErrorCode and ErrorMsg about success**/
	nErrorCode = 0;
	strErrorMsg = "SUCC";

} catch (Throwable th) {
	/** 6.2 set the ErrorCode and ErrorMsg about fail **/
	nErrorCode = -1;
	strErrorMsg = th.getMessage();
}

/** 6.3 save the ErrorCode and ErrorMsg for sending Client **/
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);

/** 7. send the result data to Client **/
HttpPlatformResponse res = new HttpPlatformResponse(response, 
PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(senddata);
res.sendData();
%>

Java 라이브러리 import

JSP 서비스를 작성하기 위해 java 기본 라이브러리를 import합니다.
XML Data를 생성할 때는 XPLATFORM 이 사용하는 Format을 준수하셔야 합니다.
XML Format Data는 “xerces”처럼 상용화되어 있는 파서(Parser)를 사용하셔도 되고, 직접 구현하여 사용하셔도 무방합니다.
하지만, XPLATFORM Java용 라이브러리(XPLATFORM X-API)를 사용하시면 XPLATFORM 전용객체를 만들어 사용하므로 XML Data Format을 별도로 파싱하지 않아도 객체의 기능을 이용하여 손쉽게 데이터를 추출해낼 수 있습니다.
XPLATFORM X-API 및 java 기본객체들을 import하는 코드는 아래와 같습니다.
<!-- 1.library import -->
<%@ page import="java.io.*" %>
<%@ page import="com.tobesoft.xplatform.data.*" %>
<%@ page import="com.tobesoft.xplatform.tx.*" %>

XPLATFORM XAPI 설치

XPLATFORM 라이브러리(XPLATFORM XAPI)는 xplatform-xapi-1.0.jar, commons-logging-1.1.1.jar 파일로 구성되어 있으며 XPLATFORM 구매 시에 제공받거나 ㈜투비소프트

기술지원 사이트(http://www.miplatform.co.kr) 를 통하여 제공받으실 수 있습니다.


연결된 라이브러리에서 사용되는 전용객체는 다음과 같습니다.

mime type정의

XML생성을 위한 mime type을 정의합니다.

<!-- 2. mime type define -->
<%@ page contentType="text/xml; charset=UTF-8" %>

XPLATFORM 기본객체(PlatformData) 생성하기

데이터를 처리하기 위한 XPLATFORM 기본객체를 선언합니다.
기본 객체인 PlatformData는 XPLATFORM 응용프로그램에서 사용하는 모든 Data(Dataset들, Variable들)를 한꺼번에 담을 수 있는 객체입니다.
PlatformData를 선언하는 코드는 다음과 같습니다.
/** 3.XPLATFORM Basic Object creation **/
PlatformData pdata = new PlatformData();

Dataset을 생성하여 File로 저장하기

Dataset을 생성하여 column정보를 입력하고 2개의 row을 생성한후 각 row마다 column들을 입력합니다.
생성한 DataSet은 i/o를 편하게 하기 위하여 PlatformData에 등록합니다.
PlatformData 객체를 사용하여 “./saveFile.bin”파일에 저장합니다.

Dataset을 생성하고 File에 저장하는 코드는 다음과 같습니다.

/** 5. handle data : save data to file **/
/** 5.1 create Dataset and input basic data to the Dataset **/
DataSet ds = new DataSet("dsPerson");
ds.addColumn("pid",DataTypes.INT);
ds.addColumn("person_name",DataTypes.STRING, 8);
ds.addColumn("home_telno", DataTypes.STRING, 8);
ds.addColumn("home_address", DataTypes.STRING, 16);
ds.addColumn("company", DataTypes.STRING, 8);
ds.addColumn("company_telno", DataTypes.STRING, 8);
ds.addColumn("company_fax", DataTypes.STRING, 8);
ds.addColumn("company_address", DataTypes.STRING, 8);
ds.addColumn("email", DataTypes.STRING, 8);
ds.addColumn("mobile", DataTypes.STRING, 8);
ds.addColumn("jikgub", DataTypes.STRING, 8);
ds.addColumn("dept", DataTypes.STRING, 8);
ds.addColumn("remark", DataTypes.STRING, 8);

int row = 0;
int i = 0;
String[] names = new String[2];
String[] tel1 = new String[2];
String[] tel2 = new String[2];
String[] tel3 = new String[2];
String[] tel4 = new String[2];
String[] address = new String[2];
String[] company = new String[2];
String[] email = new String[2];

names[0] = "Steven";
names[1] = "Charls";
email[0] = "Steven@hp.com";
email[1] = "Charls@dell.com";
address[0] = "NewYork City";
address[1] = "Manhattan City";
company[0] = "HP";
company[1] = "Dell";
tel1[0] = "02-123-4567";
tel1[1] = "02-321-7654";
tel2[0] = "02-987-6543";
tel2[1] = "02-789-3456";
tel3[0] = "02-456-7890";
tel3[1] = "02-654-0987";
tel4[0] = "010-1234-7890";
tel4[1] = "010-4321-0987";

for (i = 0; i < 2; i++)
{
	row = ds.newRow(); 
	ds.set(row,"pid",i);
	ds.set(row,"person_name",names[i]);
	ds.set(row,"home_telno",tel1[i]);
	ds.set(row,"home_address",address[i]);
	ds.set(row,"company",company[i]);
	ds.set(row,"company_telno",tel2[i]);
	ds.set(row,"company_fax",tel3[i]);
	ds.set(row,"company_address",address[i]);
	ds.set(row,"email",email[i]);
	ds.set(row,"mobile",tel4[i]);
	ds.set(row,"jikgub","0" + (i + 1));
	ds.set(row,"dept","0" + (i + 1));
	ds.set(row,"remark",i);
}

pdata.addDataSet(ds);

/** 5.2 save Dataset to a file **/
String targetFilename = "./saveFile.bin";
OutputStream target = new FileOutputStream(targetFilename);
PlatformResponse res = new PlatformResponse(target, 
PlatformType.CONTENT_TYPE_BINARY);
res.setData(pdata);
res.sendData();
target.close();

ErrorCode, ErrorMsg 처리하기

위 과정으로 기본적인 데이터의 처리가 구현되었습니다.
하지만 모든 경우에 예외상황이 발생할 수 있으므로 오류가 발생했을 때 처리하는 부분을 다음과 같이 처리하시는 것이 바람직합니다.
/** 6.1 ErrorCode, ErrorMsg creation **/
int nErrorCode = 0;
String strErrorMsg = "START";
try {
	/** 5. handle data : save data to file **/
	/** 6.2 set the ErrorCode and ErrorMsg about success**/
	nErrorCode = 0;
	strErrorMsg = "SUCC";
} catch (Throwable th) {
	/** 6.3 set the ErrorCode and ErrorMsg about fail **/
	nErrorCode = -1;
	strErrorMsg = th.getMessage();
}
/** 6.4 save the ErrorCode and ErrorMsg for sending Client **/
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);

결과값 Client에게 보내기

초기값이 정상적으로 File에 저장되었는지 여부를 Client에게 전달하기 위하여 PlatformData객체를 사용합니다. 이때, 앞에서 저장한 ErrorCode와 ErrorMsg가 Client에게 전달됩니다.

VariableList는 PlatformData의 멤버이므로 JSP 수행 결과값은 PlatformData 객체에 들어있습니다. 이제 PlatformData의 데이터를 XPLATFORM이 인정하는 XML Format으로 추출하여 전송하는 부분을 구현해 보겠습니다. 데이터를 전송하는 기능을 쉽게 구현하기 위해 XPLATFORM 라이브러리의 PlatformResponse 객체를 만들고 PlatformData 로부터 데이터를 출력시키는 코드는 다음과 같습니다.

/** 7. Send the result data to Client **/
HttpPlatformResponse res = new HttpPlatformResponse(response, PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(senddata);
res.sendData();

조회 서비스 – search.jsp

// 1.라이브러리 import (XPLATFORM X-API포함)
// 2.Mime Type 정의
// 3.XPLATFORM 기본객체(PlatformData) 생성
try {
	// 5.Data 처리
	// 6.ErrorCode, ErrorMsg 처리하기 (성공 메시지)
} Catch (에러) {
	// 6.ErrorCode, ErrorMsg 처리하기 (실패 메시지)
}
// 7.결과 Data Client에게 보내기
<!-- 1.library import -->
<%@ page import="java.io.*" %>
<%@ page import="com.tobesoft.xplatform.data.*" %>
<%@ page import="com.tobesoft.xplatform.tx.*" %>
 
<!-- 2. mime type define -->
<%@ page contentType="text/xml; charset=UTF-8" %>
<%
/** 3.XPLATFORM Basic Object creation **/
PlatformData readdata = new PlatformData();
 
/** 6.1 ErrorCode, ErrorMsg creation **/
int nErrorCode = 0;
String strErrorMsg = "START";
 
try {
	/** 4. receive client request **/
	// not need to receive
 
	/** 5. handle data : load data from file **/
	/** 5.1 load data from file **/
	String sourceFilename = "./saveFile.bin";;
	InputStream source = new FileInputStream(sourceFilename);
 
	PlatformRequest req = new PlatformRequest(source, PlatformType.CONTENT_TYPE_BINARY);
	req.receiveData();
	source.close();
 
	/** 5.1 copy data from loaded data to Dataset **/
	readdata = req.getData();
 
	/** 6.2 set the ErrorCode and ErrorMsg about success**/
	nErrorCode = 0;
	strErrorMsg = "SUCC";
 
} catch (Throwable th) {
	/** 6.3 set the ErrorCode and ErrorMsg about fail **/
	nErrorCode = -1;
	strErrorMsg = th.getMessage();
}
 
/** 6.4 save the ErrorCode and ErrorMsg for sending Client **/
VariableList varList = readdata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);

/** 7. send the result data to Client **/
HttpPlatformResponse res = new HttpPlatformResponse(response, PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(readdata);
res.sendData();
%>

Java 라이브러리 import

Java 라이브러리 import”과 내용이 동일합니다.

mime type정의

mime type정의”과 내용이 동일합니다.

XPLATFORM 기본객체(PlatformData) 생성하기

XPLATFORM 기본객체(PlatformData) 생성하기”과 내용이 동일합니다.

File의 내용을 읽어서 Dataset 생성하기

“./saveFile.bin”파일의 내용을 읽어서 PlatformData객체에 저장합니다. 이 PlatformData 객체에 Dataset이 담겨 있습니다.

File에서 내용을 읽어서 PlatformData에 담는 코드는 다음과 같습니다.

/** 5. handle data : load data from file **/
/** 5.1 load data from file **/
String sourceFilename = "./saveFile.bin";
InputStream source = new FileInputStream(sourceFilename);

PlatformRequest req = new PlatformRequest(source, 
PlatformType.CONTENT_TYPE_BINARY);
req.receiveData();
source.close();

/** 5.2 copy data from loaded data to Dataset **/
pdata = req.getData();

Errorcode, ErrorMsg 처리하기

ErrorCode, ErrorMsg 처리하기”과 내용이 동일합니다.

결과값 Client에게 보내기

JSP코드의 내용은 “결과값 Client에게 보내기”의 내용과 동일합니다. 
그러나 그 의미는 다릅니다. “결과값 Client에게 보내기”에서 사용된 PlatformData 객체는 ErrorCode와 ErrorMsg만을 담고 있지만, 이 장의 PlatformData는 Client가 사용하는 명함리스트 Dataset을 담고 있습니다.
이 Dataset은 “Errorcode, ErrorMsg 처리하기”의 코드를 수행하는 중에 자동으로 담겨집니다.

저장 서비스 – save_list.jsp

// 1.라이브러리 import (XPLATFORM X-API포함)
// 2.Mime Type 정의
// 3.XPLATFORM 기본객체(PlatformData) 생성
try {
	// 4.Client의 요구사항 받기
	// 5.Data 처리
	// 6.ErrorCode, ErrorMsg 처리하기 (성공 메시지)
} Catch (에러) {
	// 6.ErrorCode, ErrorMsg 처리하기 (실패 메시지)
}
// 7.결과 Data Client에게 보내기
<!-- 1.library import -->
<%@ page import="java.io.*" %>
<%@ page import="com.tobesoft.xplatform.data.*" %>
<%@ page import="com.tobesoft.xplatform.tx.*" %>

<!-- 2. mime type define -->
<%@ page contentType="text/xml; charset=UTF-8" %>

<%
/** 3.XPLATFORM Basic Object creation **/
PlatformData pdata = new PlatformData();

/** 6.1 ErrorCode, ErrorMsg creation **/
int nErrorCode = 0;
String strErrorMsg = "START";

try {
	/** 4. receive client request **/
	// create HttpPlatformRequest for receive data from client
	HttpPlatformRequest req = new HttpPlatformRequest(request);
	req.receiveData();
	/** 5. handle data : load data from file **/
	/** 5.1 load data from http object **/ 
	pdata = req.getData();

	/** get Dataset from received data **/
	DataSet ds = pdata.getDataSet("dsPerson");

	/** save data to file with init data **/
	String targetFilename = "./saveFile.bin";
	OutputStream target = new FileOutputStream(targetFilename);
	PlatformResponse res = new PlatformResponse(target, 
		PlatformType.CONTENT_TYPE_BINARY);
	res.setData(pdata);
	res.sendData();
	target.close(); 

	/** 6.2 set the ErrorCode and ErrorMsg about success **/
	nErrorCode = 0;
	strErrorMsg = "person list saved complete : row count("+ds.getRowCount()+")";

} catch (Throwable th) {
	/** 6.3 set the ErrorCode and ErrorMsg about fail **/
	nErrorCode = -1;
	strErrorMsg = th.getMessage();
	System.out.println("ERROR:"+strErrorMsg); 
}
/** 6.4 save the ErrorCode and ErrorMsg for sending Client **/
PlatformData senddata = new PlatformData();
VariableList varList = senddata.getVariableList();
varList.add("ErrorCode", nErrorCode);
varList.add("ErrorMsg", strErrorMsg);
/** 7. send the result data to Client **/
HttpPlatformResponse res = new HttpPlatformResponse(response, PlatformType.CONTENT_TYPE_XML,"UTF-8");
res.setData(senddata);
res.sendData();
%>

Java 라이브러리 import

Java 라이브러리 import”과 내용이 동일합니다.

mime type정의

mime type정의”과 내용이 동일합니다.

XPLATFORM 기본객체(PlatformData) 생성하기

XPLATFORM 기본객체(PlatformData) 생성하기”과 내용이 동일합니다.

Client로부터의 Request 받기

초기자료 생성 서비스 – initdata.jsp”과 “조회 서비스 – search.jsp”에서는 Client로부터의 Request를 별도로 받을 필요가 없었습니다. 왜냐하면 해당 JSP Service가 Client로부터 별도로 매개변수를 받지 않았기 때문입니다. 즉, 해당 JSP Service가 호출만 되면 Client가 원하는 작업을 할 수 있었습니다.

그러나, 이번 장에서는 Client로부터 매개변수로 Dataset을 받아야만 합니다.
Client로부터의 요구사항을 받는 코드는 다음과 같습니다.
/** 4. receive client request **/
// create HttpPlatformRequest for receive data from client
HttpPlatformRequest req = new HttpPlatformRequest(request);
req.receiveData();

Client로부터의 Request에 Data추출하여 File로 저장하기

Client로부터 받은 정보를 처리하기 위하여, 우선 해당 정보를 PlatformData로 변환합니다. PlatformData에 Dataset을 추출합니다. 여기서 추출된 Dataset에서 필요한 정보를 얻습니다. 여기서는 저장한 Row건수를 얻기 위하여 사용하는데, 그 사용예는 다음장에서 보입니다.
생성된 PlatformData를 “./saveFile.bin”파일에 저장합니다.

Client로부터의 Request에서 PlatformData를 추출하고 파일로 저장하는 코드는 다음과 같습니다.

/** 5. handle data : load data from file **/
/** 5.1 load data from http object **/ 
pdata = req.getData();

/** get Dataset from received data **/
DataSet ds = pdata.getDataSet("dsPerson");

/** save data to file with init data **/
String targetFilename = "./saveFile.bin";
OutputStream target = new FileOutputStream(targetFilename);
PlatformResponse res = new PlatformResponse(target, 
PlatformType.CONTENT_TYPE_BINARY);
res.setData(pdata);
res.sendData();
target.close();

Errorcode, ErrorMsg 처리하기

ErrorCode, ErrorMsg 처리하기”과 내용이 동일합니다. 단지, 성공할 경우의 strErrorMsg에 저장한 명합리스트의 건수를 담는 부분만 다릅니다.

결과값 Client에게 보내기

결과값 Client에게 보내기”과 내용이 동일합니다.