package com.ostrichmyself.ia.msgswitch;
import java.io.IOException;
import java.util.List;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import com.ostrichmyself.ia.iservice.OperationService;
import com.ostrichmyself.util.xml2obj.ServiceStructure;
import com.ostrichmyself.util.xml2obj.ServiceXmlParser;
/**
* 接收消息并转发到不同的Service
*/
public class MessageSwitch {
private BundleContext bundleContext;
public MessageSwitch(BundleContext bundleContext)
{
this.bundleContext = bundleContext;
}
/**
* 循环接收
*/
public void continueReceive() {
Thread continueT = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
byte[] data = new byte[56];
try {
Thread.sleep(1000);
System.out.println("输入格式:Service名称 参数一 参数二");
System.out.println("目前Service名称有:AddService 和 MultiService");
System.in.read(data);
} catch (InterruptedException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
switchService(new String(data).trim());
}
}
});
continueT.start();
}
private void switchService(String dataFromClient)
{
dataFromClient = dataFromClient.trim();
String[] args = dataFromClient.split(" ");
int len = args.length;
if (len != 3)
{
return;
}
int first = Integer.valueOf(args[1].trim());
int second = Integer.valueOf(args[2].trim());
String servcieName = args[0].trim();
ServiceXmlParser parser = new ServiceXmlParser();
List<ServiceStructure> listService = parser.getListService();
for ( int i = 0; i < listService.size(); i++)
{
ServiceStructure item = listService.get(i);
String sName = item.getServiceName();
if (servcieName.equalsIgnoreCase(sName))
{
ServiceReference serviceReference = bundleContext.getServiceReference(item.getServiceClassPath());
OperationService operation = (OperationService) bundleContext.getService(serviceReference);
int result = operation.operate(first, second);
System.out.println("结果为::" + result);
}
}
}
}
评论15
最新资源