January 2007 Archives

windows vista 来了

| No Comments | No TrackBacks

  2000元最低售价超过预期

  研发五年、耗资百亿元的微软下一代操作系统Vista将在今天正式亮相。

  在中关村所有卖场,印有Vista字样的浅蓝色广告招贴随处可见,几乎每一个电脑品牌的店铺门前,都有促销人员热情地游说,恨不得在10秒之内便能拉近消费者和新技术之间仍然遥远的距离。

  电脑厂商迎来集体狂欢

  目前,几乎所有的PC厂商都只在强调两件事:技术实力够强,跟微软的关系够密切,他们要迅速抢下份额。

  联想在月初就推出了十余款Vista电脑,几乎涵盖台式机、笔记本所有品牌。就在上周,联想的渠道商们也开始备战,订单的一大半都押在Vista上。

  方正电脑也在昨天下午发力,赶在微软发布之前推出首批Vista新品,更拉来多位微软全球高管助阵,以显示微软在新战役中对自己的支持。TCL同样在昨天发布Vista电脑,并抛出“歌手张靓颖试用”的噱头,让竞争更加热闹。

  相比来说,两家国际厂商惠普和戴尔似乎要低调许多,但惠普方面极力否认这种说法,并表示早已在去年秋天推出Vista升级计划,准备得足够充分。

  PC厂商的期望将从今天开始逐一兑现,毕竟距离上次操作系统发布已有五年之久。目前,中国的电脑存量至少已逼近5000万台,每年还在以千万计的速度递增,但在价格竞争限制下,庞大的出货数量并不意味着盈利,无论如何,Vista目前是刺激销量和售价的唯一兴奋点。

  Vista中文版最高2600元

  PC厂商们纷纷强调,Vista电脑的售价不会有太大提升。但对于已经拥有电脑,想要过渡到Vista时代的消费者来说,Vista软件的零售价似乎偏高,至少在中国,Vista的销售要更多依靠和电脑厂商的合作。

  昨天,在微软的合作伙伴连邦软件店,Vista系统软件针对不同用户的四个版本已经上架,价格最低的一款定价2000元,最高2600元。

  这样的价格跟此前的预期不符。曾有来自微软内部的消息称,Vista中文版的售价将调低45%,甚至还有消息称Vista中文版的售价在300至500元。

  目前,中关村电子卖场已经有小贩兜售价值10元的Vista测试版。但令人惊讶的是,盗版者对Vista正式版软件保持着异乎寻常的冷静,和电脑厂商的热情完全不同。

  “Vista还不太稳定,我们想再等等看。”一个兜售者这样说。当然,这也很可能是微软新的防盗技术令盗版者更难下手。

This page describes how to install into an existing Eclipse configuration.

Contents
[hide]

Introduction

If you are already an Eclipse user, you can install Aptana as a plug-in directly into your current Eclipse configuration.

JSP生成HTML

| No Comments | No TrackBacks

1、JSP
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="java.text.*" %>
<%@ page import="javax.servlet.*,javax.servlet.http.*"%>
<jsp:useBean id="conn" scope="page" class="DBLink.DBSQL"/>
<jsp:useBean id="ReadTemplates" scope="page" class="ball.news.ReadTemplates"/>
<jsp:useBean id="WriteHtml" scope="page" class="ball.news.WriteHtml"/>
<jsp:useBean id="ReplaceAll" scope="page" class="ball.news.ReplaceAll"/>
<%
//request.setCharacterEncoding("gb2312");

try{
String dir=request.getRealPath(".");
DiskFileUpload fu = new DiskFileUpload();


fu.setSizeMax(4194304); //设置文件大小. 这里文件只能上传4M以内的

fu.setSizeThreshold(4096); //设置缓冲大小.

fu.setRepositoryPath(dir+"/ball/news/images"); //设置临时目录.

List fileItems = fu.parseRequest(request); //解析请求,返回一个集合.


Iterator i = fileItems.iterator();
String fieldvalue="";
String ff = "";
String picname="false";
Object tt = "";
Vector v = new Vector();

while(i.hasNext())
{

FileItem fi = (FileItem)i.next();

if(fi.isFormField()) //这是用来确定是否为文件属性,
{

String fieldName = fi.getFieldName(); //这里取得表单名
fieldvalue=fi.getString(); //这里取得表单值

v.addElement(fieldvalue);

}

else //这里开始外理文件
{

File fullFile = new File(fi.getName());
ff = fullFile.getName();
String rr = "";


java.util.Date date2 = new java.util.Date();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); //取得时间
String str2 = formatter.format(date2);
StringTokenizer st = new StringTokenizer(ff,".");
if (st.hasMoreTokens()){
String test12 = st.nextToken();
rr = st.nextToken();
//System.out.println(rr);
}
if (rr.equals("")&&!rr.equals("gif")&&!rr.equals("jpg")&&!rr.equals("jpeg"))
{
picname = "false";
}

else
{
picname = str2+"."+rr; //以时间为图片名称

File savedFile = new File(getServletContext().getRealPath("/ball/news/images/"),picname);

fi.write(savedFile); //上传到服务器
}
//System.out.println("picname------------------------"+picname);
}
//System.out.println("v------------------------"+v);
}
String[] flag = {"<temp_title>","<temp_date>","<temp_author>","<temp_content>","<str_Temp>","<temp_picture>"};

//将数据写入到数据库
Object newtype1 = v.elementAt(0);
String t = newtype1.toString();
int newtype = Integer.parseInt(t);
Object rowid1 = v.elementAt(1);
String rowid = rowid1.toString();
Object title1 = v.elementAt(2);
String title = title1.toString();
Object content1 = v.elementAt(3);
String content = content1.toString();
// System.out.println(down);
java.util.Date date = new java.util.Date();
String strdate = date.toLocaleString();

java.util.Date StrDate1 = new java.util.Date();
String StrDate = StrDate1.toLocaleString(); // 新闻发布时间
//String newtype="0";
conn.openDB();
String sql = "select top 5 * from b_news where newtype="+newtype+" order by id desc";
String strTemp="<tr><td>相关新闻</td></tr>";
ResultSet rs = conn.executeQuery(sql);
while (rs.next())
{
String t2 = rs.getString(2);
String t4 = rs.getString(4);
strTemp += "<tr><td>";
strTemp +="<a href=../../../"+t4+">";
strTemp += t2;
strTemp +="</a>";
}
strTemp +="</td></tr>";
rs.close();


//读取模板

String filePath = "";
filePath = request.getRealPath("\\ball\\news\\pnews.template");
String templateContent = null;
try{
templateContent = ReadTemplates.getTlpContent(filePath);
System.out.println(templateContent);
}
catch(Exception e)
{
System.out.println("error to template!");
}
//替换模板中的内容
//System.out.println("picname--------------------------------"+picname);

templateContent = ReplaceAll.replace(templateContent,flag[0],title);
templateContent = ReplaceAll.replace(templateContent,flag[1],StrDate);
//templateContent = ReplaceAll.replace(templateContent,flag[2],editer);
templateContent = ReplaceAll.replace(templateContent,flag[3],content);
templateContent = ReplaceAll.replace(templateContent,flag[4],strTemp);
templateContent = ReplaceAll.replace(templateContent,flag[5],picname);

// 根据时间得文件名与路径名

Calendar calendar = Calendar.getInstance();
String fileName = String.valueOf(calendar.getTimeInMillis()) +".html";
String pathName = request.getRealPath("ball/news")+"\\"+ calendar.get(Calendar.YEAR) + "\\"+ (calendar.get(Calendar.MONTH)+1) +"\\"+ calendar.get(Calendar.DAY_OF_MONTH)+"\\";
String url = calendar.get(Calendar.YEAR) + "/"+ (calendar.get(Calendar.MONTH)+1) +"/"+ calendar.get(Calendar.DAY_OF_MONTH)+"/";
url +=fileName;
//System.out.println(url);

try{
WriteHtml.save(templateContent,pathName,fileName);
}catch(Exception e){
System.out.println("error to html!-----------"+e.getMessage());
}


//写入数据库
String sqlInsert = null;
if (!picname.equals("false")){
sqlInsert = "insert into b_news (title,content,url,picture,newtype,addtime,rowid) values ('"+title+"','"+content+"','"+url+"','"+picname+"','"+newtype+"',getdate(),'"+rowid+"')";
}
else
{
picname="images/"+picname;
sqlInsert = "insert into b_news (title,content,url,newtype,addtime,rowid) values ('"+title+"','"+content+"','"+url+"','"+newtype+"',getdate(),'"+rowid+"')";
}
//System.out.println("sql insert---------------"+sqlInsert);
conn.executeUpdate (sqlInsert);
}
catch(Exception e)
{
System.out.println("upload error------------------"+e.getMessage());
}



%>
<%out.println("新闻生成html成功了!");%>

2、WriteHtml
WriteHtml.java

package ball.news;


import java.io.*;


public class WriteHtml
{


    public WriteHtml()
    {
    }


    public static void save(String s, String s1, String s2)
       
    {
        try
        {
            a(s1);
            FileOutputStream fileoutputstream = new FileOutputStream(s1 + s2);
            byte abyte0[] = s.getBytes();
            fileoutputstream.write(abyte0);
            fileoutputstream.close();
        }
        catch(IOException e)
        {
         System.out.println("write html error"+e.getMessage());
        }
    }


    private static void a(String s)
    {
       try
       {
        File file = new File(s);
        if(!file.exists())
            file.mkdirs();
       }
       catch (Exception e)
       {
        System.out.println("mkdirs error!"+e.getMessage());
       }
    }
}

3、ReadTemplates
ReadTemplates.java

package ball.news;
import java.io.*;
import java.util.*;


public class ReadTemplates
{


    private String temp = null;
    private Object a = new Object();


    public ReadTemplates()
    {
    }


    public String getTlpContent(String s)
       
    {
        if(temp == null)
            synchronized(a)
            {
                if(temp == null)
                    try
                    {
                        System.out.println("----------------------------");
                        temp = a(s);
                        //System.out.println("test the temp"+temp);
                    }
                    catch(Exception e)
                    {      
                       System.out.println("get tlpconente error"+e.getMessage());               
                    }
            }
        return temp;
    }


    private synchronized String a(String s)
       
    {
        String s1 = null;
        try
        {
            FileInputStream fileinputstream = new FileInputStream(s);
            int i = fileinputstream.available();
            byte abyte0[] = new byte[i];
            fileinputstream.read(abyte0);
            fileinputstream.close();
            s1 = new String(abyte0);
        }
        catch(IOException e)
        {
         System.out.println("ioexception error"+e.getMessage()); 
        }
        return s1;
    }


}

Java Configuration for Blandware AtLeap

| No Comments | No TrackBacks

To have a stable, robust and fast work of AtLeap you should configure your JVM (Java Virtual Machine) properly. JVM configuration is realized by force of passing parameters. For example for Tomcat you should specify CATALINA_OPTS environment variable, for Resin you should pass it directly into startup script (httpd.exe or httpd.sh), for JBoss you should define JAVA_OPTS environment variable, as for other servlet containers see its documentation.

Let’s examine typical set of JVM arguments




  • -server



Enable HotSpot Server JIT java compiler. It generates more robust and optimized code, but it slower than HotSpot Client (-client option) during compilation. See for more information http://java.sun.com/docs/hotspot/index.html http://java.sun.com/products/hotspot/ I recommend use –client for development mode and –server for production mode.



  • -Djava.awt.headless=true



You need it to have a correct work of graphics parts if the X server is not installed on Linux.



  • -Dfile.encoding=UTF-8



Specify it to make sure.



  • -Xms128m



Specify the initial size, in bytes, of the memory allocation pool (128 megabytes in our case).



  • -Xmx256m



Specify the maximum size, in bytes, of the memory allocation pool (256 megabytes in our case). It should be between ½ and ¾ of physical memory. It is need to avoid problem OutOfMemoryError. You may also be able to avoid this problem by setting the command-line flags -Xmx and -Xms to the same value to prevent the VM from trying to expand the heap. Note that simply increasing the value of -Xmx will not help when no swap space is available.



  • -XX:PermSize=64m



Specify the initial size of the permanent generation (where the VM stores the classes and methods data).



  • -XX:MaxPermSize=128m



Specify the max size of the permanent generation (where the VM stores the classes and methods data). If case of the -server option is enabled the default value equal to 64m but in case of java.lang.OutOfMemoryError: PermGen space error you should increase this value. TIP This option does not exist in Bea JRockit and IBM JVMs.



  • -XX:NewSize=16m



Specify the initial size of the young generation (where the VM stores the short-lived objects).



  • -XX:MaxNewSize=24m



Specify the maximum size of the young generation (where the VM stores the short-lived objects).

Please carefully read Java documentation for your platform. For example TIP for Solaris it is recommended to specify -XX:-UseLWPSynchronization option.


If your machine has more than two CPU the following options can be useful: -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled They allow to enable Concurrent Low Pause Collector.


If your machine has more than two CPU and your Sun JDK 1.5.0_06 or more you can try to use -XX:+UseParallelGC -XX:+UseParallelOldGC -XX:+UseAdaptiveSizePolicy in order to enable throughput collector.


if you have a IPv6 OS core but need to work with IPv4 addresses specify -Djava.net.preferIPv4Stack=true


ALERT! If you startup Tomcat under Windows as service you should specify JVM options via Windows registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ApacheTomcat\Parameters (add JVM Option Number N string value and increase JVM Option Count).


As result for Linux we can get following line CATALINA_OPTS="-server -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Xms128m -Xmx256m -XX:PermSize=64m -XX:MaxPermSize=128m -XX:NewSize=16m -XX:MaxNewSize=24m"


As for options for Ant you can use e.g. the following line: ANT_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -Xms64m -Xmx128m"


ALERT! Be careful, non-standard JVM options (which begins from -XX:) can be applied only for Sun's JVM. In order to make tuning of IBM or Bea's JVMs please read documentation.


Below links can be useful during JVM tuning:



-- AndreyGrebnev - 29 July 2006

Eclipse插件 包含swing插件

| No Comments | No TrackBacks
看到很多朋友问起Eclipse插件问题,所以索性就将自己所用过的总结一下。呵呵,说是全攻略只当胀胀眼球,俺能用过的插件会有多少呀,虽然才疏学浅,为了不让自己的痛苦重复在似俺当年般的菜虫身上……

tomcatPluginV31beta.zip (下载地址:http://www.sysdeo.com/eclipse/tomcatplugin),比较常用的插件
文件路径(以下全部指解压后的文件存放的相对路径):tomcat_plug\eclipse\plugins\com.sysdeo.eclipse.tomcat_3.1.0.beta
用途:Tomcat插件,用来在Eclipse中启动Tomcat服务器
版本:支持Tomcat5.x及Eclipse3.x

org.vssplugin_1.6.1.zip(下载地址:http://sourceforge.net/projects/vssplugin
文件路径:vss_plug\eclipse\plugins\org.vssplugin_1.6.1
用途:团队协作开发时版本控制,当然Eclipse集成了CVS
版本:支持VSS6与Eclipse3.x(不过最好用Eclipse3.1)

org.objectweb.lomboz_3.1.0.N20050106.zipemf-sdo-runtime-I200503181320.rar(下载地址:http://forge.objectweb.org/project/showfiles.php?group_id=97
文件路径:lomboz_plug/eclipse/plugins(拷贝解压后的plugins目录下的内容到此目录)
                    lomboz_plug/eclipse/features(拷贝解压后的features目录下的内容到此目录)
用途:IBM的J2EE插件,用于J2EE应用项目开发
版本:支持Eclipse3.1、其它JSP及EJB容器(如WebSpere、Weblogic、Resin、Jboss、Tomcat、 Oracle 9i/AS、Sun Java System Application Server Editor、Orian、JRun)的各种版本
所需插件:emf-sdo-runtime-I200503181320.rar

 

HibernateSynchronizer-3.1.1.zip(下载地址:http://prdownloads.sourceforge.net/hibernatesynch/
文件路径:hibernate_plug/eclipse/plugins/HibernateSynchronizer-3.1.1
用途:Hibernate同步插件,用来自动生成Hibernate的同步代码
版本:支持Hibernate3.05(没测试过是否支持Hibernate3.x)、Eclipse3.1

EnterpriseWorkbenchInstaller_4.0.3GA_E3.1.exe(下载地址:http://www.myeclipseide.com/Downloads+index-req-viewsdownload-sid-10.html,注意:要注册一个用户,登陆后才能下载)
用法:先安装Windows版本的Myeclipse,然后进入它的安装目录,将plugins和features两个目录的内容复制到新建的文件夹(假 设为:myeclipse4.0.3)内,做成Eclipse的Myeclipse插件,然后卸掉刚才安装的Myeclipse软件
文件路径:myeclipse_plug/eclipse/plugins(拷贝myeclipse4.0.3的plugins目录下的内容到此目录)
                    myeclipse_plug/eclipse/features(拷贝myeclipse4.0.3的features目录下的内容到此目录)
版本:该版本支持Eclipse3.1.1,集成了上述的所有插件或者插件的演变版本,只要装上了该插件就可以进行B/S开发了,特别适合新手(因为Eclipse的插件有个版本匹配问题,新手很难搞掂,浪费时间)

NLpack1-eclipse-SDK-3.1.1a-win32.zipNLpack1_FeatureOverlay-eclipse-SDK-3.1.1.zip(下载地址:http://download.eclipse.org/eclipse/downloads/drops/L-3.1.1_Language_Packs-200510051300/index.php
用法:解压上述文件,复制解压后的plugins和features两个目录放在新建的文件夹(假设为:LanguagePackFeature)内,做成Eclipse语言包插件
文件路径:language_plug/eclipse/plugins(拷贝LanguagePackFeature的plugins目录内容到此目录)
                    language_plug/eclipse/features(拷贝LanguagePackFeature的features目录内容到此目录)
用途:Eclipse多国语言包,如果你的系统是简体中文的,该语言包也是简体中文的
版本:支持Eclipse3.1.1(注意,这里不是Eclipse3.1)

GEF-runtime-3.1.zipemf-sdo-runtime-2.1.0.zipVE-runtime-1.1.0.1.zip(下载地址:http://www.eclipse.org/vep/WebContent/main.php
用法:解压上述文件,复制解压后的plugins和features两个目录放在新建的文件夹(假设为:VE-runtime-1.1.0.1)内,做成Eclipse的Visul Editor插件
文件路径:VE_plug/eclipse/plugins(拷贝VE-runtime-1.1.0.1的plugins目录下的内容到此目录)
                    VE_plug/eclipse/features(拷贝VE-runtime-1.1.0.1的features目录下的内容到此目录)
用途:Eclipse可视化界面开发工具
版本:支持Eclipse3.1.x

 


想要更多插件请参考(http://www.eclipseplugincentral.com/PNphpBB2+file-index-c-1.html)、(http://www.open-open.com),里面都有详细的归类,所有的开源软件在http://sourceforge.net里面都可以搜索得到。

 

有关插件安装问题,四种常用的方法在此特别注明:

1. “帮助”->“软件更新”->“查找并安装”->“搜索要安装的新功能部件”->“新建远程站点”(此种方式用于在线更新)

2. “帮助”->“软件更新”->“查找并安装”->“搜索要安装的新功能部件”->“新建本地站点”(如果插件已经下载到了本地,请不要用第一种方法)

3. 直接拷贝plugins和features两个目录下的内容置于$Eclipse_Home$/对应的plugins和features下面

4. 用link外链接与外部插件关联

最菜的,一般用第一种方法,而大部分生手一般选择第二或者第三种方法,用得习惯的一般选择最后一种方式。此四类方法优劣势对比如下:
        前三种方法都会将插件文件拷贝至相$Eclipse_Home$/对应的plugins和features目录下,从本质上看,没多大区 别,并且插件只能安装和禁用,不能卸载(当然,如果你对插件对应的目录和文件都很熟悉的话,可以通过直接删除拷进去的文件来达到卸载插件的目的),但方法 一和方法二在安装插件的时候很容易出错或者是产生冲突,特别是当你用了Myeclipse插件、中文包的同时,又想安装 HibernateSynchronizer、Jode Compiler(Class反编译工具)、Visual Editor等插件时,及有可能导致 Myeclipse插件和中文包失效。

        所以,如果插件已经下载到了本地,请直接拷贝至$Eclipse_Home$/对应的plugins和features目录下,也就是用方法三,这样能避免冲突。

方法四是将所有的插件用一个外部目录存放起来,假如是D:\plug-in,将上面所示的插件目录文件全部拷贝到该目录下,比如Tomcat插件,此时的文件路径就是D:\plug-in\tomcat_plug\eclipse\plugins\com.sysdeo.eclipse.tomcat_3.1.0.beta(请注意,方法四一定要严格这样的目录路径放置文件)。然后在$Eclipse_Home$下新建一个links目录,并在links目录下建立关联文件,假如是tomcat.link,在建立的关联文件中加入如下语句:
path=
D:\\plug-in\\tomcat_plug
也可以写成下面的形式
path=D:/plug-in/tomcat_plug
还可以写成相对路径的形式

        剩下的事情,不用我说你肯定都知道了,就是重启Eclipse,在Dos窗口下进入Eclipse安装目录,键入命令eclipse -clean,回车,或者进入$Eclipse_Home$/configuration目录,删除org.eclipse.update后再重新启动Eclipse。



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=563485


[收藏到我的网摘]   yeno发表于 2005年12月28日 05:45:00 

链接
 
    eclipse : http://www.eclipse.org

    
    eclipse project downloads : http://download.eclipse.org/eclipse/downloads/  
    

eclipse 中文语言包[3.1.x]:   http://download.eclipse.org/eclipse/downloads/drops/L-3.1.1_Language_Packs-200510051300/

备注:
    其他版本的语言包在 http://download.eclipse.org/eclipse/downloads/                   eclipse project downloads [/URL]页面的[COLOR=orangered]Language Pack[/COLOR]选项后面的链接.
    和
    Eclipse 平台入门[IBM官方介绍]:[URL=http://www-128.ibm.com/developerworks/cn/linux/opensource/os-ecov/index.html]>>[/URL]

    Visual Editor Project:  http://www.eclipse.org/vep/

    Visual Editor Project FAQ [常见问答]: http://www.eclipse.org/vep/WebContent/faq.html

    利用Eclipse Visual Editor 项目构建GUI [IBM官方] : http://www-128.ibm.com/developerworks/cn/linux/opensource/os-ecvisual/index.html

 

安装:[以Eclipse3.1.1为列]

   1.下载 Eclipse SDK 3.1.1: http://download.eclipse.org/eclipse/downloads/

   2.安装当前版本的eclipse和相关支持类库:http://download.eclipse.org/tools/ve/downloads/在VE的下载页面有当前版本的支持类库 如:

  在VE 下载页面:http://download.eclipse.org/tools/ve/downloads/ 

选择 Visual Editor 1.1.0.1版本 http://download.eclipse.org/tools/ve/downloads/drops/R-1.1.0.1-200509071822/index.html的支持类库:


   Eclipse build eclipse-SDK-3.1: 
   EMF build 2.1.0:  
   GEF Build 3.1: 

   3.然后下载 Visual Editor 1.1.0.1版本 : VE-SDK-1.1.0.1.zip http://www.eclipse.org/downloads/download.php?file=/tools/ve/downloads/drops/R-1.1.0.1-200509071822/VE-SDK-1.1.0.1.zip

   4.下载的都是ZIP压缩文件, 在eclipse关闭时, 分别把 EMF build 2.1.0,GEF Build 3.1,VE- SDK-1.1.0,分别解压,请将其压缩包中plugins和features目录下的内容解压到eclipse安装目录的相应文件夹.
  
   5.[安装后启动后始终没有"visual class"此项目,终于找到解决办法] 关闭eclipse.把Eclipse安装目录Configuration里除了Config.ini其他文件夹都删掉.

   6.启动 eclipse .新键项目. 然后在工具栏上的"新建Java类" 图标上,点击右边的小箭头,下面的扩展菜单上会出现"Visual Calss".这个就是一个Visual Editor的入口.(转载)

1. 创建项目
 
·  新建一个java项目:hibernatebegin_2,注意选中“创建单独的源文件夹和输出文件夹”,同时添加“用户库”:hibernate。
 
2.  映射文件user.hbm.xml
 
·  新建一个包,包名:javamxj.hibernate,然后在此包下新建一个文件,文件名:user.hbm.xml。
user.hbm.xml

<?xml version="1.0" encoding="gbk"?>
<!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd//en"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
   
<hibernate-mapping>
       
    <class name="javamxj.hibernate.user" table="usertable2">
       
        <meta attribute="class-description">
            运行 hbm2java 任务, 利用 hbm.xml文件生成java类文件
            @author javamxj(分享java快乐)
            @link blog: htpp://javamxj.mblogger.cn
                        htpp://blog.csdn.net/javamxj/
        </meta>
       
        <id name="id" type="string" unsaved-value="null" length="32" column="id">
            <generator class="uuid.hex"/>
        </id>
       
        <property name="username" type="string" not-null="true" length="24">
            <meta attribute="field-description">@param 用户名</meta>
        </property>
       
        <property name="password" type="string" not-null="true" >
            <column name="密码" length="24" not-null="true"></column>
        </property>
       
    </class>
</hibernate-mapping>  

●  与上篇文章中的user.hbm.xml文件比较,可以发现标签上增加了许多额外的设定。
·  这里<meta>标签中的内容将插入到类的javadoc说明去。
·  <id>标签使用 uuid.hex 来定义主键的产生算法,uuid算法使用ip地址、jvm的启动时间、系统时间和一个计数值来产生主键。
·  <property>标签中的<column>用于生成数据表中的列。
 
 
3. 构建文件build.xml
 
·  将上篇文章中的“hibernate.cfg.xml”配置文件复制到src目录下。
//重新排版,可能格式不太正确
<?xml version='1.0' encoding='GBK'?>
<!DOCTYPE hibernate-configuration    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>   
    <session-factory>        
    <!-- 是否将运行期生成的SQL输出到日志以供调试 -->        
    <property name="show_sql">true</property>                
    <!-- SQL方言,这里设定的是MySQL -->        
    <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>                
    <!-- JDBC驱动程序 -->        
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>                
    <!-- JDBC URL, "?useUnicode=true&amp;characterEncoding=GBK" 表示使用GBK进行编码 -->        
    <property name="connection.url">jdbc:mysql://localhost:3306/HibernateTest?useUnicode=true&amp;characterEncoding=GBK</property>                
    <!-- 数据库用户名 -->        
    <property name="connection.username">root</property>                
    <!-- 数据库密码 -->        
    <property name="connection.password">javamxj</property>        
    <!-- 指定User的映射文件 -->        
    <mapping resource="javamxj/hibernate/User.hbm.xml"/>           
    </session-factory>
</hibernate-configuration> 

·  在项目根目录下建立一个build.xml,这个文件含有四个任务,这里会用到“generate-code”、“schemaexport”两个任务,至于用法可以看注释。要注意环境变量的设置要符合自己的实际配置,比如库文件目录的设置是"d:/java/hibernate/lib",是沿用上篇文章中的设置。

build.xml

<?xml version="1.0" encoding="gbk"?>
<project name="利用工具开发hibernate" default="help" basedir=".">

    <!-- ******  环境设置,可以根据自己的实际配置自行更改 ***** -->
    <!-- 源文件目录, 可以通过 项目->属性->java构建路径 更改 -->
    <property name="src.dir" value="./src" />
    <!-- 输出的class文件目录,可以通过 项目->属性->java构建路径 更改 -->
    <property name="class.dir" value="./bin" />
    <!-- 库文件目录  -->
    <property name="lib.dir" value="d:/java/hibernate/lib" />

    <!-- 定义类路径 -->

<path id="project.class.path">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<pathelement location="${class.dir}" />
</path>

<!-- ************************************************************** -->
<!-- 使用说明 -->
<!-- ************************************************************** -->
<target name="help">
<echo message="利用工具开发hibernate" />
<echo message="-----------------------------------" />
<echo message="" />
<echo message="提供以下任务:" />
<echo message="" />
<echo message="generate-code --> 运行hbm2java,利用 hbm.xml 文件生成java类文件" />
<echo message="generate-hbm --> 运行hibernatedoclet,生成 hibernate 类的映射文件" />

<echo message="schemaexport --> 运行schemaexport,利用 hbm.xml 文件生成数据表" />
<echo message="" />
</target>


<!-- ************************************************************** -->
<!-- hbm2java 任务 -->
<!-- ************************************************************** -->
<target name="generate-code" >
<echo message="运行 hbm2java 任务, 利用 hbm.xml 文件生成java类文件"/>

<taskdef name="hbm2java"
classname="net.sf.hibernate.tool.hbm2java.hbm2javatask"
classpathref="project.class.path">
</taskdef>

<hbm2java output="${src.dir}">
<fileset dir="${src.dir}">
<include name="**/*.hbm.xml"/>
</fileset>
</hbm2java>
</target>


<!-- ************************************************************** -->
<!-- hibernatedoclet 任务 -->
<!-- ************************************************************** -->

<target name="generate-hbm" >
<echo message="运行hibernatedoclet,生成 hibernate 类的映射文件"/>

<taskdef name="hibernatedoclet"
classname="xdoclet.modules.hibernate.hibernatedoclettask"
classpathref="project.class.path">
</taskdef>

<hibernatedoclet destdir="${src.dir}"
excludedtags="@version,@author,@todo" force="true" encoding="gbk"
verbose="true">

<fileset dir="${src.dir}">
<include name="**/*.java"/>
</fileset>

<hibernate version="2.0" xmlencoding="gbk" />
</hibernatedoclet>
</target>


<!-- ************************************************************** -->
<!-- schemaexport 任务 -->
<!-- ************************************************************** -->
<target name="schemaexport">

<echo message="运行schemaexport,利用 hbm.xml 文件生成数据表"/>

<taskdef name="schemaexport"
classname="net.sf.hibernate.tool.hbm2ddl.schemaexporttask"
classpathref="project.class.path">
</taskdef>

<schemaexport config="${src.dir}/hibernate.cfg.xml" quiet="no"
text="no" drop="no" output="schema-export.sql">
</schemaexport>
</target>

</project>

从hbm文件生成ddl

| No Comments | No TrackBacks

网上收集整理的一些资料:

1、
<property   name="hibernateProperties"> 
  <props> 
  <prop   key="hibernate.dialect"> 
  org.hibernate.dialect.MySQLDialect 
  </prop> 
  <prop   key="hibernate.hbm2ddl.auto">create</prop> 
  <prop   key="hibernate.show_sql">true</prop> 
  </props> 
</property>

信息周刊:虚拟城市里的真金白银

| No Comments | No TrackBacks

欧美的一些公司和机构正在把他们的核心业务建立在一家中国公司提供的全景技术上。

在重案现场,美国联邦调查局(FBI)会对案发现场进行拍照,再将这些照片用软件拼接成 360度全景现场,这些全景现场在分析案情的时候会派上大用场。办案人员拖动鼠标就可以身临其境地在虚拟现场走动,并能全方位放大某个细节图像来寻找线 索,这样不仅可以避免破坏现场,还能方便事后对案件进行分析。不仅如此,在商业领域,全景技术也得到了广泛应用,西门子公司(Siemens)也采用全景 技术对分布在全世界的电厂进行归档记录和管理,华纳兄弟电影公司(Warner Bros)的《特洛伊》放弃传统拍摄方式,采用全景技术来实现影片中声势浩大的场面背景。

使用 AppFuse 快速构建 J2EE 应用

| No Comments | No TrackBacks

示例介绍

    本文的示例实现对员工信息的增删查改等基本功能。用 Tapestry 实现表示层,用 Hibernate 开发持久层,用 Spring 提供事务控制等跨模块服务,并用 Acegi 进行安全管理。本示例只用到一个域模型:Employee,下面是它的 UML 图。


图 1. Employee UML 图
Employee UML 图 

搭建开发环境

本文的代码开发平台采用的是 Windows 操作系统,因此,以下环境设置也是针对 Windows 操作系统的。

  • 从 AppFuse 下载页面 下载 appfuse-tapestry-1.9.3-src.zip,并解压缩在任意目录下。这个 zip 已经定制了使用 Tapestry 作为表现层的实现框架,因而使用起来较为直接。
  • 从 http://java.sun.com 下载最新的 JDK,并安装或解压缩到任意目录下。本文采用 JDK 1.5.0。设置环境变量 JAVA_HOME 指向 JDK 所在的目录,并在 PATH 中添加 %JAVA_HOME%/bin。
  • 从 http://jakarta.apache.org/tomcat 下载最新版的 Tomcat,并安装或解压缩到任意目录下。本文采用 Tomcat 5.5.17。设置环境变量 CATALINA_HOME 指向 Tomcat 的安装目录。
  • 从 http://ant.apache.org下载最新版的 Ant,并解压缩到任意目录下。AppFuse 要求的最低版本是 1.6.2,本文采用的是 1.6.5。设置 ANT_HOME 指向 Ant 所在的目录,并在 PATH 中添加 %ANT_HOME%/bin。另外,要拷贝一个 junit.jar 到 %ANT_HOME%/lib 下,如果 lib 下没有 junit.jar,AppFuse 的脚本在运行时会给出警告信息。junit.jar 可以从 http://www.junit.org 获得,也可以从 %AppFuse%/lib/junit3.8.1 目录下获得。
  • 从 http://www.mysql.com 下载最新版的 MySQL,并安装或解压缩到任意目录下。本文采用的是 5.0。
  • 从 http://www.eclipse.org 下载 Eclipse 3.1 或 3.2,安装到任意目录下。

    AppFuse 的 Ant 脚本可以在命令行中运行,也可以在 Eclipse 里运行。有关如何在 Eclipse 里执行 Ant 脚本,请参考《用Eclipse开发AppFuse应用》。到此,我们已经为 AppFuse 开发应用准备好了环境,下面让我们开始使用 AppFuse 创建项目。

新建项目

   AppFuse 的便捷与强大之处在于它已经为我们提供了多种开源框架的集成,并且通过使用 Ant 将所有的构建过程自动化。另外,AppFuse 利用 XDoclet 能够为我们生成绝大多数重要的代码,例如 dao 类、service 类以及测试用例,等等,并且能够将大量的配置文件也一并生成好,从而极大地节省了开发人员的时间。

    用 AppFuse 进行开发通常有三种模式:“自上而下”,“自下而上”以及“混合模式”。采用“自上而下”(由 Java 对象向数据库对象创建的过程)的方式固然比较符合“面向对象”的设计思维,但是为此要编写大量的 XDoclet 的 tag 也确是一件痛苦的事情。相比较而言,采用“自下而上”(由数据库对象生成 Java 对象的过程)就显得简单许多 -- 只需要提供数据库表结构。然而,对于较为复杂的系统,尤其是类之间具有大量的关联的情形,仍然需要采用“自上而下”的创建模式。因此,在实际的项目开发 中,将两种模式进行混合使用比较常见,这也就是“混合”模式。本文采用“自下而上”的模式。

struts+Hibernate设计用户登陆代码

| No Comments | No TrackBacks

Jsp代码:
 
Login:
 
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html>
    <head>
    <html:base/>
    <html:errors/>
       <title>JSP for loginForm form</title>
    </head>
    <body>
    <html:base/>
    <html:errors/>
       <html:form action="/login">
           uname &nbsp: <html:text property="uname" size="10"/><html:errors property="uname"/><br/>
           passwd : <html:password property="passwd" size="10"/><html:errors property="passwd"/><br/>
          
           <html:submit/><html:reset/>
           ${err}
       </html:form>
       <html:link page="/register.do">注册新用户</html:link>
    </body>
</html>
 
 
Register:
 
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html>
 
    <head>
   
    <html:base/>
    <html:errors/>
   
       <title>JSP for registerForm form</title>
    </head>
    <body>
       <html:form action="/register">
           passwd : <html:password property="passwd"/><html:errors property="passwd"/><br/>
           uname : <html:text property="uname"/><html:errors property="uname"/><br/>
          
           <html:submit value="确定"/><html:cancel value="取消"/>
       <html:link page="/login.jsp">回到首页</html:link>
       </html:form>
    </body>
</html>
Success:
 
<%@ page language="java" import="java.util.*"contentType="text/html;charset=gb2312"%>
 
 
<html>
  <head>
   
   
    <title>My JSP 'success.jsp' starting page</title>
   
  
  </head>
  <body>
    success login!welcome to this jsp!!${user.uname}
</html>
 

Hibernate代码生成工具 设计全攻略

| No Comments | No TrackBacks

1.简述
Hibernate 是一个开放源代码的对象关系映射框架,它对JDBC进行了轻量级的对象封装,提供HQL查询语言,使得Java程序员可以随心所欲的使用对象编程思维来操 纵数据库。使用Hibernate,必须为配置映射文件—ClassMapping File和Configuration File,现在市场上提供了诸多Hibernate代码生成工具,比如:XDoclet,以及Hibernate官方自带的sechmaExport工 具。然而它们都有如下的缺点:仅提供一些基本的输入模版,用户仍需要时间进行配置和修改;没有提供自动的持久类以及InvokeBean的代码生成;不支 持图形界面;不支持对HibernateTestCase的代码生成。

Hibernate工具具备以下特点:
1. 根据UML生成的数据库模型,自动生成映射文件。
2. 根据UML产生Hibernate持久类。
3. Hibernate模型检验。
4. 生成自动测试代码。
5. 与Eclipse集成

Hibernate 代码生成工具采用基于PowerDeigner的模型扩展功能来实现Hibernate的代码生成。 PowerDesigner(以下简称PD)是一款一流的数据库建模工具(E-R模型设计、物理模型设计),同时它对UML、报表、XML、团队开发(知 识库Repository)都支持的相当好,所有模型都可以正向、逆向的相互转换。PD的模型是由它的元模型组成。简单的说,元模型就是组成模型的模型。 基于这些元模型,PD提供了一套GTL开发语言,可以轻易地扩展出自己的代码模版和流程,也可以对现有的语言模型进行修改以适应需求;PD支持利用 VBScript来扩展语言、改变PD模型、模型检测。对于Eclipse,PD也能做到快速的集成并可以利用JAVA语言来修改PD模型。


注:GTL可以说是一种面向对象的脚本语言,它可以在不同的元模型上加以扩展,增加诸如:添加生成文件、原型、菜单、代码模版、扩展属性 等等功能,如图1所示,将在后续部分详细的阐述。由于元模型是面向对象的(比如所有的类、接口等的元模型都继承于Classifier元模型),即如果在 Classifier中扩展了一个功能,那么继承它的模型均拥有这样的功能和脚本,也可以覆盖重写这样的功能,以实现多态概念。


打开扩展模型编辑窗口:选择ModelExtended Model Definition,在弹出的窗口的Toolbar上选择Import Extended Model Definition(倒数第二个按钮)来加载已有的模型,也可以新建扩展模型(选择Add Row按钮,然后在表格中双击要编辑的模型的第一列即可弹出如图1所示的界面)


为了让读者更好的了解PD的元模型概念,截取了Hibernate代码生成工具所用到的元模型架构图,如图2所示。 读者可以在PD的安装目录下找到:<PD安装路径>\Examples\MetaModel.oom,图表存放在PdOOM下,名字为 Class Objects。
由于篇幅原因,会摘取典型的代码和模型来讲解如何设计Hibernate代码生成工具。

Hibernate代码生成的总体架构流程(活动图)


Hibernate自动生成文件的原则是:
1) 子类的映射信息挂在根类的映射文件下(Root Class),即子类将不独立生成代码
2) Value-Type类以及没有持久化的类,将不生成代码。
3) 不为非类对象生成代码(比如接口等)

其中:
Check Models in Diagram: Hibernate模型检测,判断DomainModel是否符合Hibernate语法。如果出错,PD将抛出错误提示信息(提供自动纠错的功能)
Generate Configuration File:选择模型属性(Model->Model Properties),用户可以在Extended Attribute下设置配置信息,PD会根据配置信息生成Configuration File。
Get each class in Diagram:这是PD的机制,它会自动获取UML内的所有模型元素,并根据每个模型的扩展属性(Profile\Generated File)的流程来生成代码文件。
Generate Basic Mapping:类的基本映射,它包含id,composite-id,propery等。
Get SubClass Style:子类的映射,递归获取。
Generate Join:当持久类对应于多张表的映射时,需要用Join来指明。
Generate Association Mapping:根据类之间Association的类型来判断是哪种关连映射,本工具支持one-to-one,one-to-many,many- to-one,many-to-many(根据Association Properties的Detail标签下的Multiplicity来设置),支持集合Array,Set,List,bag,idbag(根据 ContainType来设置)。
Get Java Code Information:扩展模型是基于现有的语言模型,即扩展模型能够获取语言模型的模版和各种设置,对于Hibernate的Java持久类,我们只 须在Java代码下增加对应Attribute的Getter和Setter即可。%source%
Generate Invoke Bean: 生成Hibernate的CRUD函数。
Generate Test Case: Hibernate测试用例代码,将生成的随机数据来验证Hibernate的正确性。
Generate Log4J: 生成Log4J的配置文件
Generate ANT build.xml: 当用户在Generation Files下的Options中设置Ant为True,并配置了Ant的Lib的路径,则Hibernate Tools会生成build.xml, 然后将会自动运行Ant来测试Hibernate, 生成结果将会被Log4J存入日志。

2.技术要点
O/R Mapping
PD从8.0开始,就不断加强O/R Mapping,除了代码模型的生成以外,PD也生成O/R Mapping的定义,诸如生成EJB CMP组件。用户可以定义O/R Mapping来建立OOM与PDM之间的关系。
PD支持3种方式的O/R Mapping:
第一种是从类图转换成数据模型之后由PD自动建立连接(适用于自顶向下的设计过程);
第二种是从数据模型转换成类图之后由PD自动建立连接(适用于自下向上的设计过程);
第三种是建立类图和数据模型之后由用户手动建立(适用于同步设计或者是后期的修改)
建立过程:
第 一种:选择类图,然后设计持久类(POJO),完毕之后,选择Tools->Generate Physical Data Model。在弹出的窗口中,选择Detail标签,选中O/R Mapping选项,即可。读者可以选择是更新现有的数据库还是新建数据库。
第二种:与第一种建立过程相似,只是换成在数据库模型中选择Generate Object-Oriented Model
第 三种:用户必须首先建立DataSource,在左边WorkSpace树形目录下找到类文件,然后右击选择New\Data Source,在弹出的窗口的Models标签中选择 ,并选择想与之关联的数据库模型。在类模型中打开类属性(双击要建立O/R Mapping的类),选择Mapping选项卡,点击 按钮添加刚才建立的DataSource。然后点击 添加类映射的数据表,即建立完毕。
用户可以添加多张表,它表示一个类可能对应多张表的映射,Hibernate的映射则应该用Join属性来指明。
注:当用户建立了ClassSource,PD会根据类的属性和表的字段自动建立AttributeMapping,当然用户也可以在AttributeMapping属性页下对其映射进行修改。
大家知道,Hibernate的映射文件必须包含对表的表述(比如:column,property属性等等),在PD中,通过元模型中的Mapping可以轻易地获取对应的表信息。图4是GTL中用到的O/R Mapping元模型。
O/R Mapping架构图:

架构说明:
1) Association:
在Asoociation 元模型中,可以通过Mappings来获取AssociationMapping(即用户在Mapping for下选择的DataSource),每个AssociationMapping都会有SourceClassifiers,它是 AsoociationSource标签内的映射集合,如图5所示。SourceClassifiers集合中的元素,也就是数据模型中的 Reference元模型,如图6所示。
由于PD可以在Association Mapping下添加数据模型中的其他Reference和Table,所以在做Hibernate代码生成时,采用了类型判断,以避免因类型不匹配而造成的错误。



☺实现代码(表映射):(注:附录部分将介绍部分GTL语法)
.


☺实现代码(referenceColumnHelper模版):


2) Class和Attribute
获取数据库模型的方法与Association类似,在O/R Mapping中,ClassMapping的SourceFeature等价于数据表; AttributeMapping的SourceFeature等价于数据表中的列信息。图7给出了在编写过程中使用到的数据库元模型的架构图。
☺实现代码(获取类对应的表信息)

说明:Mappings.First.ClassSources表明当前Data Source下的表集合
☺实现代码(获取Attribute对应的列信息)
例如:%AttributeMappings.First.SourceFeature.Code%
%AttributeMappings.First.SourceFeature%对应于图7的Column元模型。


Extended Attribute
Hibernate 生成工具中对于映射文件,难免要遇到大量的选项让用户选择或者输入,这时,我们就可以利用PD提供的Extended Attribute来扩展模型的属性。对于UML模型,除了Dependency和Generalization,其余模型均有Extended Attribute来扩展。
设置Extended Attribute步骤:进入扩展模型编辑窗口,在Profile下选择一个元模型,然后右键选择Extended Attribute, PD已提供了多种默认的选项(加括号的),当然用户增加自己的Extended Attribute Type(在Profile下的shared目录上按右键选择Extended Attribute Type,然后在右边可以设置列项内容以及默认值等等。设置完之后读者就可以在模型的扩展属性中引用到自定义的类型),如图8所示。在GTL中,可以用% 属性名%引用到当前用户的选择值,也可以用%模版名%引用到GTL模版(读者可以选择模版并按F12跳转)。


Check Model
考 虑,由于基于UML建立的DomainModel,对于Hibernate语法来说,难免有一些语法上的错误,比如:在两个类之间建立 Association,但对应的表之间却没有Reference,或者是有Reference却没有Joins下的Column,这时我们应该给出一个 错误警告,提示用户检查。
以刚才提到的AssociationCheck为例,将介绍如何实现用户自定义的Check。
打开Hibernate扩展模型,选中Profile\Association,右击选中New\Custom Check。在右边,读者可以看到有四个选项卡,如图9所示。


Check Script: 用于检测模型,%Check%=true, 将不出现错误信息。
Autofix Script: 用于自动修复模型错误,%Fix%=false, 表示不修复该错误。当PD监测到错误时,用户可以在错误上右键选择Automatic Correction即可。对于自动修复的错误,会在图标右下角加上一个”+”号,如 ,表示PD以根据Autofix Script修复完成。
Global Script: 用于存放全局函数,在任何元模型的Check Model均可调用。
实现代码
Check Script: 用于检测Refence的建立正确与否。


注:PD会对每个模型进行检测(包括Package),所以如果读者希望通过程序来控制该模型是否被检测,只须在满足条件的语法段内加上%Check% = True (表示验证正确并跳过)即可。

Persistent Class持久类
对 于POJO的持久类,必须为其每个类的属性增加Getter和Setter函数。扩展模型是基于语言模型之上的,也就是说,语言模型中的模板等内容均可在 扩展模型中覆盖和重写。所以,在Hibernate扩展模型中采用重载的方法,扩展了Java语言模型的代码生成,保留了原先Java那部分代码,并在扩 展模型中加上自己的Getter和Setter模版。
在Java语言模型中,代码生成是依靠%source%模版来完成Java代码的,同时有一 个%initializers%模版实现当完成Attribute之后的一些初始化工作,所以在相同位置(Class元模型)的地方重写了% initializers%模版,代码如下:

在Attribute元模型中增加getterFunction 、setterFunction模版,代码如下:

然后在Class元模型中增加持久类的Generated Files,并在模版编辑框内输入%source%即可。

Generated Files
PD 在每个元模型中提供了文件生成的功能。在Class元模型上右键选择Generated Files。在右边窗口中,在 File Name 下输入生成后的文件名。由于Hibernate映射文件的文件名与类名相同,故输入%mappingFilename%,同时建立 mappingFilename 模版,并输入%Code%.hbm.xml。
注:所有的元模型都继承于NamedObject (NamedObject继承于BaseObject),在NamedObject中存放了各种元模型的标准属性,比如:模型名称(Name)、代码名称 (Code)、注释、描述等等。所以,为了取到类的名称,就在类元模型下输入%Code%.hbm.xml,那么PD会根据代码名称为每个类建立相应的文 件(前提是该类可以被生成,即已持久化)。

Model
Model元模型中主要实现对 Configuration Mapping的连接信息的配置,诸如,JDBC等。所用到的方法也就是Extended Attribute和Mapping代码模版。由于篇幅原因,不一一列举,读者可以参考刚才所讲,或者可以在PD安装目录下运行pdvbs11.chm来 获取帮助。

Task
Task在代码生成完毕之后被激活,然后PD会依次序执行选中的任务。 扩展了Hibernate模型,增加了ANT的Lib路径,生成的目录名称等等选项。在Generation\ Options下建立新的选项即可,类似于Extended Attribute。调用Options选项时,输入如下代码:%GenOptions.选项名% 即可获取。
对于Task,希望能够借助ANT 的build.xml配置进行自动测试。在PD中,JAVA语言模型已经提供了ANT的build.xml的自动生成并预留接口 (customExecuteTarget、customerProperties、customTaskDefs三个模版) ,目的为了能让扩展模型来重载,有兴趣的读者可以查看Java语言模型(LanguageEdit Current Object Language,浏览Java::Profile\Model\Templates\Ant\antTemplate)。

建立Task:
在Generation\Tasks上右键,选择New。输入Task Name(就是真正执行的显示名),取名为Run Unit Test。在下面的表格中选择已建立的Command,如果没有建立,则可以在Commands上新建。
Run Unit Test Command代码:


说明:
.execute_command为宏命令,用于执行外部的程序。
.execute_command '(' <cmd> [',' <args> [',' <mode>]] ')'
第一个参数为主命令,这里是CMD
第二个参数为主命令的参数,这里是/K ANT.BAT JUNIT
第二个参数为管道类型,PD提供cmd_ShellExecute和cmd_PipeOutput两种方式。前者采用独立的进程方式,后者将会阻塞PD直到任务完成,并且结果将显示在PD的Output窗口内,如图10所示。


3.结束语
PowerDesigner 是一款灵活性非常强的软件建模工具,基于它的元模型,读者可以随心所欲的用不同语言 (GTL, VBScript, Java, C#等) 来设计出自己的代码生成工具,甚至是语言模型、报表等等。本篇仅仅起到了一个抛砖引玉的作用,我相信,凭借着领域中的经验,读者一定也可以设计出更适合自 己的代码生成工具,简化流程、降低成本、加快开发。

附录:
列举了GTL的部分语法,读者也可以参阅PD安装目录下的pdvbs11.chm文件或者Sybase官方网站。
1. 资源文件
读者可以参考PD11的现有模型:
您 可以在安装目录下\ Resource Files\Extended Model Definitions找到现有的扩展模型;在安装目录下\ VB Scripts找到VBScript代码;Ole Automation目录下找到如何用JAVA,C#等其他语言来获取元模型来做自己的代码生成工具;Library目录下找到语言模型。在 Printable Docs目录下找到PDF文档(建议参考Advanced User Documentation.PDF文档)。
2. 语法
a. 设置变量

变量名必须用%来封装,即%变量名%
全局变量

变量区域
简单的说,当区域建立后(比如,循环,另一个模版等),在区域中调用区域外的变量,则需要用Outer.变量名来指明。
b. 循环

c. 判断条件

d. 集合

e. 特殊符号

1.使用Multi-Byte String Functions ,需要服务器安装相应模块
 
新建文件: modifier.mb_truncate.php
 
function smarty_modifier_mb_truncate($string, $length = 80, $etc = '...', $middle = false)
{
    if ($length == 0)
        return '';
    if (mb_strlen($string, 'UTF-8') > $length) {
        $length -= mb_strlen($etc);
        if(!$middle) {
            return mb_substr($string, 0, $length).$etc;
        } else {
            return mb_substr($string, 0, $length/2) . $etc . mb_substr($string, -$length/2, $length/2, 'UTF-8');
        }
    } else {
        return $string;
    }
}
 
2.
新建文件: modifier.mb_substring.php
 
 function smarty_modifier_substring($string, $from, $length = null)
{
    preg_match_all('/[\x80-\xff]?./', $string, $match);
    if(is_null($length))
    {
        $result = implode('', array_slice($match[0], $from));
    }
    else
    {
        $result = implode('', array_slice($match[0], $from, $length));
    }
    return $result;
}

为什么 Spring 这么了不起?

Spring 的创立者 Rod Johnson 以一种批判的眼光看待 Java™ 企业软件开发,并且提议很多企业难题都能够通过战略地使用 IOC 模式(也称作依赖注入)来解决。当 Rod 和一个具有奉献精神的开放源码开发者团队将这个理论应用于实践时,结果就产生了 Spring 框架。简言之,Spring 是一个轻型的容器,利用它可以使用一个外部 XML 配置文件方便地将对象连接在一起。每个对象都可以通过显示一个 JavaBean 属性收到一个到依赖对象的引用,留给您的简单任务就只是在一个 XML 配置文件中把它们连接好。


原文链接:http://www-128.ibm.com/developerworks/cn/java/j-sr2.html