插件下载地址:http://d.download.csdn.net/down/283929/lzy0709
1.在src下建立一个webservice-config.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Config>
<Webservice>
<UserMenuUrl>http://localhost:8080/portal/services/UserMenuWebservice</UserMenuUrl>
<UserUrl>http://localhost:8080/casServer/webservices/UserWebservice</UserUrl>
<OrganizationUrl>http://localhost:8080/casServer/webservices/OrganizationService</OrganizationUrl>
</Webservice>
</Config>
2.建立Config.java,注意导入的包为org.jdom.*下的DOM
package com.huadi.webclient;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class Config {
private static Config _instance = null;
private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
.getLog(Config.class);
//读取webservice-config.xml
public static final String CONFIG_PATH = "/webservice-config.xml";
private HashMap domains = new HashMap();
private Document doc;
public static String home;
private boolean newLines = false;
private Config() {
init();
}
public static Config getInstance() {
if (_instance == null) {
_instance = new Config();
}
return _instance;
}
public String getAbsolutePath() {
String path = new File(".").getAbsolutePath();
return path.substring(0, path.length() - 2);
}
private void init() {
try {
SAXBuilder builder = new SAXBuilder();
InputStream is = Config.class.getResourceAsStream(CONFIG_PATH);
Document doc = builder.build(is);
setDoc(doc);
Element root = getDoc().getRootElement();
if (!this.getConfigs(root)) {
log.error("init:" + CONFIG_PATH + "");
;
}
} catch (IOException ex) {
log.error("init:IOException", ex);
} catch (JDOMException ex) {
log.error("init:JDOMException", ex);
}
}
public static void serialize(String configPath) throws IOException {
if (_instance == null) {
return;
}
if (_instance.getDoc() == null) {
return;
}
Format format = Format.getCompactFormat();
format.setEncoding("gb2312");
format.setIndent(" ");
XMLOutputter outp = new XMLOutputter(format);
outp.output(_instance.getDoc(), new FileOutputStream(configPath));
}
private boolean getConfigs(Element root) {
if (!"Config".equals(root.getName())) {
return false;
}
for (int i = 0; i < root.getChildren().size(); i++) {
Element node = (Element) root.getChildren().get(i);
HashMap props = (HashMap) domains.get(node.getName());
if (props == null) {
props = new HashMap();
domains.put(node.getName(), props);
}
getProps(node, props);
}
return true;
}
private void getProps(Element root, HashMap hashMap) {
for (int i = 0; i < root.getChildren().size(); i++) {
Element node = (Element) root.getChildren().get(i);
hashMap.put(node.getName(), node.getText());
}
}
public ArrayList getDomainNames() {
ArrayList result = new ArrayList();
for (Iterator i = domains.entrySet().iterator(); i.hasNext();) {
Map.Entry e = (Map.Entry) i.next();
result.add(e.getKey());
}
return result;
}
public ArrayList getPropertyNames(String domainName) {
HashMap hashMap = (HashMap) domains.get(domainName);
ArrayList result = new ArrayList();
for (Iterator i = hashMap.entrySet().iterator(); i.hasNext();) {
Map.Entry e = (Map.Entry) i.next();
result.add(e.getKey());
}
return result;
}
public String getProperty(String domainName, String propertyName) {
HashMap domain = (HashMap) domains.get(domainName);
if (domain == null) {
return null;
}
return (String) domain.get(propertyName);
}
public void setProperty(String domainName, String propertyName,
String propertyValue) {
HashMap domain = (HashMap) domains.get(domainName);
if (domain == null) {
domain = new HashMap();
domains.put(domainName, domain);
}
domain.put(propertyName, propertyValue);
Element root = this.getDoc().getRootElement();
Element domainNode = root.getChild(domainName);
if (domainNode == null) {
domainNode = new Element(domainName);
root.addContent(domainNode);
}
Element propNode = domainNode.getChild(propertyName);
if (propNode == null) {
propNode = new Element(propertyName);
domainNode.addContent(propNode);
}
propNode.setText(propertyValue);
}
public void setDoc(Document doc) {
this.doc = doc;
}
public void setNewLines(boolean newLines) {
this.newLines = newLines;
}
public void setHome(String home) {
this.home = home;
}
public Document getDoc() {
return doc;
}
public boolean isNewLines() {
return newLines;
}
public String getHome() {
return home;
}
}
3.建立一个与服务提供的接口一样的接口,并包含相应的接口方法
4.建立一个ClientManager.java类,并在其中添加相应的CreateService()方法,此方法返回一个自己定义的接口对象
package com.huadi.webclient;
import java.net.MalformedURLException;
import org.apache.log4j.Logger;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.huadi.webclient.user.LoginService;
public class ClientManager {
private static Logger log = Logger.getLogger(ClientManager.class);
public static Object createManger(Class c, String urlType) {
String url = Config.getInstance().getProperty("Webservice", urlType);
Service serviceModel = new ObjectServiceFactory().create(c);
log.debug("callSoapServiceLocal(): got service model.");
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
Object obj = null;
try {
obj = factory.create(serviceModel, url);
} catch (MalformedURLException e) {
log.error("UserMenuManager.getUserMenus(): EXCEPTION: "
+ e.toString());
}
return obj;
}
//在此添加相应的方法
public static LoginService createUserService() {
return (LoginService) createManger(LoginService.class, "WebUrl");
}
}
5.写测试类Test.java,验证调用服务效果
package com.huadi.webclient.test;
import com.huadi.webclient.ClientManager;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//用创建的ClientManager类的抽象方法产生远程对象并调用相应的远程方法
String message=ClientManager.createUserService().login("admin", "admin");
System.out.println("message:"+message);
}
}
///导入相应的包
评论0