动态定制了一次webservice

终于有一个功能是自己的思路,自己解决每一个问题,然后最终调试成功了。

一、确定需求

回学校了,手有点生疏了,但是,还是非常想找师兄讨论一下接下来的项目需求。将近用了快一个小时的时间,确定下来,这次核心功能是这样的:根据不同的条件来选择性的生成并发布webservice。也就是说可以通过界面操作的方式来选择webservice的返回值有哪些。再说的具体一点就是:比如,有三个表,分别存放了学生的个人资料表,学院表,系别表,用户界面中要求有这三个表的列名,用户通过操作选择服务端发布哪几个列的webservice。

二、确定思路

思路的整理是一个比较难搞定的问题。最初的最初,想的是生成两个java文件,然后改一个配置文件,所以,最初的问题就放在了获取数据库表的列名,这个问题,可以通过java中的反射机制来做。因此,就写了一个程序来获取其中某个表的列名,但是,小叶说,写代码要负责,经过他的开导,我把这个类抽象成了一个抽象类,幸亏听了小叶的,要不后面还要重写。接下来的主要问题就是判断,用户提交后,到底是要哪个表的哪个列,难住了……毕竟是三个表,其实,三个表还好,要是n多个表可是咋整?于是,思路被打断,开始思考……突然想到,为什么不在数据库中建立一个视图呢?于是,联系了一下师兄,得到师兄的允许,改为先建立视图,然后用hibernate操作视图。

现在有点清楚了,摆在面前的有这么几个问题:

1、在数据库中建立视图后,怎样用hibernate映射,和普通的表一样吗?而且,视图中没有主键的。

2、新建文件,主要是对文件的内容写入问题。

3、配置文件的修改,也就是要发布webservice--------用流的方式还是用xml解析

思路定下来了,然后就是解决这些问题。其实,最难解决的我觉得是第一个问题。

三、解决问题

1、视图映射问题

我们都知道,hibernate映射表的时候,如果没有主键,就会生成两个实体类。网上有些人说要在生成后改两个实体类,有的说不用改,直接用。我觉得,没有必要。具体的完成过程:

首先,说一下我的表的结构:

users表:usersid(主键)、username、password、depart(外键)

college表:colid(主键)、colname

department表:deid(主键)、dename、colid(外键)

(1)新建一个视图:

Code:
  1. create view usersinfor as select users.usersid as usersid,users.username as username,
  2. users.authority as authority,department.dename as department,   
  3. college.colname as colname from users, department,college   
  4.  where users.depart=department.deid and department.colid=college.colid  

(2)hibernate映射视图

就像普通的表那样,直接映射就可以了,但是,生成的内容是不一样的,因为视图中没有主键,不过不要紧,直接用就行了,唯一要注意的是写法有些不同,比如,要查询,应该这样写:

Code:
  1. "from Usersinfor U where U.id.usersid=?"  

要获取这个类中属性内容的时候,写法也不一样:

Code:
  1. String username = new String();   
  2. Usersinfor users = new Usersinfor();   
  3.  username = users.getId().getUsername();  

2、新建文件并写入

首先,文件名,我想的是用产生随机数的方法来对文件进行命名;读写的时候,要根据提交的checkboxlist的值生成相应内容,当然,要用流的方式。

Code:
  1. package com.usersope.IO;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileInputStream;   
  5. import java.io.FileNotFoundException;   
  6. import java.io.FileOutputStream;   
  7. import java.util.List;   
  8. import java.util.Random;   
  9.   
  10. import com.opensymphony.xwork2.ActionSupport;   
  11. import com.usersope.IOUtil.FileOpe;   
  12. import com.usersope.IOUtil.UpString;   
  13. import com.usersope.IOUtil.XmlOpe;   
  14.   
  15. public class GetUrl extends ActionSupport {   
  16. private String sql = new String();   
  17. private List userslist;   
  18. File fileImpl;   
  19. File Ifile;   
  20. Random rand;   
  21. int i;   
  22. String filenameImpl = new String();   
  23. String filecontentImpl = new String();   
  24.   
  25. String Ifilename = new String();   
  26. String Ifilecontent = new String();   
  27. String servicename = new String();//配置文件中要用的   
  28.   
  29. String url = new String();   
  30.    public String execute(){   
  31.         //产生随机数   
  32.         rand =new Random();   
  33.         i=rand.nextInt(100);   
  34.           //要写入的字符串   
  35.           //找到文件,如果文件不存在,则新建.这里是新建文件   
  36.           filenameImpl = "Getmes"+i+"Impl";   
  37.           servicename = "Getmes"+i;   
  38.           Ifilename = "IGetmes"+i;   
  39.           fileImpl = new File("F://myProject//userservice//src//com//service//"+filenameImpl+".java");   
  40.           Ifile = new File("F://myProject//userservice//src//com//service//"+Ifilename+".java");   
  41.           while (fileImpl.exists()|| Ifile.exists()){   
  42.               //产生随机数   
  43.                 i=rand.nextInt(100);   
  44.                 filenameImpl = "Getmes"+i+"Impl";   
  45.                 servicename = "Getmes"+i;   
  46.                 fileImpl = new File("F://myProject//userservice//src//com//service//"+filenameImpl+".java");   
  47.                 Ifile = new File("F://myProject//userservice//src//com//service//"+Ifilename+".java");   
  48.           }   
  49.           FileOpe.createFiles(fileImpl);//用到了上面的静态方法   
  50.           FileOpe.createFiles(Ifile);   
  51.           //以上是建立两个文件   
  52.           //下面是写文件   
  53.           //接口里面的内容   
  54.           Ifilecontent = "package com.service;";   
  55.           Ifilecontent = Ifilecontent + "public interface " + Ifilename;   
  56.           Ifilecontent = Ifilecontent +  "{public String getmes(int userid);}";   
  57.           //接口实现里面的内容   
  58.           File filemedo1 = new File("F://myProject//userservice//src//com//service//ok1.txt");   
  59.           File filemedo2 = new File("F://myProject//userservice//src//com//service//ok2.txt");   
  60.   
  61.         try {   
  62.             FileInputStream fis1 = new FileInputStream(filemedo1);   
  63.             String s1 = FileOpe.readFileInputStr(fis1);//读取数据,并将读取到的数据存储到数组中   
  64.             filecontentImpl = filecontentImpl + s1;   
  65.             filecontentImpl = filecontentImpl + " " + filenameImpl +" implements "+Ifilename;   
  66.             FileInputStream fis2 = new FileInputStream(filemedo2);   
  67.             String s2 = FileOpe.readFileInputStr(fis2);//读取数据,并将读取到的数据存储到数组中   
  68.             filecontentImpl = filecontentImpl + s2;   
  69.         } catch (FileNotFoundException e1) {   
  70.             // TODO Auto-generated catch block   
  71.             e1.printStackTrace();   
  72.         } //创建流对象   
  73.             //将字符串的第一个字母变成大写   
  74.           for (int i = 0;i<userslist.size();i++){   
  75.             String str = (String)userslist.get(i);   
  76.             char[] aa = UpString.upcase(str);   
  77.             String bb = new String(aa);//字符数组转换为string类型   
  78.                
  79.             filecontentImpl = filecontentImpl + "idmes = idmes +"+"/""+"<" + str + ">"+"/"";   
  80.             filecontentImpl = filecontentImpl + "+users.getId().get";   
  81.             filecontentImpl = filecontentImpl + bb+"()+";   
  82.             filecontentImpl = filecontentImpl +"/"""</" + str +">"+"/";"+"      ";   
  83.           }   
  84.           filecontentImpl = filecontentImpl + "idmes = idmes + "+"/""+"</user>"+"/""+";"+"    "+"return idmes;}}";   
  85.                
  86.           FileOutputStream fos = null;   
  87.           try{   
  88.               fos = new FileOutputStream(fileImpl,true);//为true的时候是追加,默认是覆盖   
  89.               FileOpe.writeString(filecontentImpl,fos);   
  90.               fos = new FileOutputStream(Ifile,true);//为true的时候是追加,默认是覆盖   
  91.               FileOpe.writeString(Ifilecontent,fos);   
  92.           } catch (FileNotFoundException e){   
  93.   
  94.           }finally{   
  95.                try{   
  96.             fos.close();   
  97.                }catch(Exception e){   
  98.              e.printStackTrace();   
  99.            }   
  100.           }   
  101.      XmlOpe.add(servicename);   
  102.     //转到显示url的页面   
  103.     url = "http://localhost:8080/userservice/services/"+servicename+"?wsdl";    
  104.     return "url";   
  105. }   
  106. public String getUrl() {   
  107.     return url;   
  108. }   
  109. public void setUrl(String url) {   
  110.     this.url = url;   
  111. }   
  112. public List getUserslist() {   
  113.     return userslist;   
  114. }   
  115. public void setUserslist(List userslist) {   
  116.     this.userslist = userslist;   
  117. }   
  118.   
  119.   
  120.   
  121.   
  122. }   

其中,涉及到的问题有:将一个字符串的首写字母变为大写:

Code:
  1. public class UpString {   
  2.        
  3.     public static char[] upcase(String str) {   
  4.         char[] aa = str.toCharArray();   
  5.         if(aa[0] >96 && aa[0] < 123)    
  6.             aa[0]=(char) (aa[0]-32);//小写换大写   
  7.         //System.out.print(aa);   
  8.            
  9.         return aa;   
  10.            
  11.     }   
  12.   
  13. }  

文件的操作等。

3、配置文件的修改,即services.xml文件的修改

征求了小叶的意见,用解析的方式进行,这样也明了。只不过其中只要传递一个参数就ok了。

Code:
  1. import javax.xml.parsers.DocumentBuilderFactory;   
  2. import java.io.File;   
  3. import java.io.FileNotFoundException;   
  4. import java.io.FileOutputStream;   
  5. import java.io.IOException;   
  6.   
  7. import org.w3c.dom.*;   
  8. import org.xml.sax.SAXException;   
  9.   
  10. import javax.xml.parsers.*;   
  11. import javax.xml.transform.*;   
  12. import javax.xml.transform.dom.DOMSource;   
  13. import javax.xml.transform.stream.*;   
  14. import javax.xml.xpath.*;   
  15.   
  16. public class XmlOpe {   
  17. //参数就是生成的文件名   
  18.     public static void add(String servicename){   
  19.         DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();   
  20.         Element service=null;   
  21.         Element theElem=null;   
  22.         Element root=null ;   
  23.     try {   
  24.         factory.setIgnoringElementContentWhitespace(true);   
  25.            
  26.         DocumentBuilder db=factory.newDocumentBuilder();   
  27.         Document xmldoc=db.parse(new File("F://myProject//userservice//WebServices//services.xml"));   
  28.         root=xmldoc.getDocumentElement();   
  29.            
  30.         //---  新发布一个web service ----   
  31.         service=xmldoc.createElement("service");   
  32.         theElem=xmldoc.createElement("name");   
  33.         theElem.setTextContent(servicename);   
  34.         service.appendChild(theElem);   
  35.            
  36.         theElem=xmldoc.createElement("serviceClass");   
  37.         theElem.setTextContent("com.service."+"I"+servicename);   
  38.         service.appendChild(theElem);   
  39.   
  40.         theElem=xmldoc.createElement("implementationClass");   
  41.         theElem.setTextContent("com.service."+servicename+"Impl");   
  42.         service.appendChild(theElem);   
  43.            
  44.         theElem=xmldoc.createElement("style");   
  45.         theElem.setTextContent("wrapped");   
  46.         service.appendChild(theElem);   
  47.            
  48.         theElem=xmldoc.createElement("use");   
  49.         theElem.setTextContent("literal");   
  50.         service.appendChild(theElem);   
  51.            
  52.         theElem=xmldoc.createElement("scope");   
  53.         theElem.setTextContent("application");   
  54.         service.appendChild(theElem);   
  55.            
  56.            
  57.         System.out.print(1);   
  58.         root.appendChild(service);   
  59.         saveXml("F://myProject//userservice//WebServices//services.xml", xmldoc);   
  60.   
  61.     }catch (ParserConfigurationException e){   
  62.         e.printStackTrace();   
  63.     } catch (SAXException e) {   
  64.         // TODO Auto-generated catch block   
  65.         e.printStackTrace();   
  66.     } catch (IOException e) {   
  67.         // TODO Auto-generated catch block   
  68.         e.printStackTrace();   
  69.     }   
  70. }   
  71.     //将Document输出到文件   
  72.     public static void saveXml(String fileName, Document doc) {   
  73.         TransformerFactory transFactory=TransformerFactory.newInstance();   
  74.         try {   
  75.             Transformer transformer = transFactory.newTransformer();   
  76.             transformer.setOutputProperty("indent""yes");   
  77.   
  78.             DOMSource source=new DOMSource();   
  79.             source.setNode(doc);   
  80.             StreamResult result=new StreamResult();   
  81.             result.setOutputStream(new FileOutputStream(fileName));   
  82.                
  83.             transformer.transform(source, result);   
  84.         } catch (TransformerConfigurationException e){   
  85.             e.printStackTrace();   
  86.         } catch (TransformerException e) {   
  87.             e.printStackTrace();   
  88.         } catch (FileNotFoundException e) {   
  89.             e.printStackTrace();   
  90.         }      
  91.     }   
  92. }  

四、调试并发现问题

调试也花了一些时间,最终还是调试成功了,但是,其中有一个问题又出现了:每次生成webservice,都必须要求服务器重新启动才能生效。小叶曾经说过可以用监听器,不过,我没有试过,不知道行不行。暂时这个先做到这吧。

大家见笑了……

评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值