建立ejb各个接口和bean的大致框架的文件

发表于:2007-07-01来源:作者:点击数: 标签:
createfile.jsp %@page contenttype=text/html;charset=gb2312% %@page import= java .io.*% % try { String ejb_type=request.getParameter(ejb_type); String ejb_name=request.getParameter(ejb_name); String server_type=request.getParameter(server_ty
createfile.jsp

<%@page contenttype="text/html;charset=gb2312"%>
<%@page import="java.io.*"%>
<%
try
{
    String ejb_type=request.getParameter("ejb_type");
    String ejb_name=request.getParameter("ejb_name");
    String server_type=request.getParameter("server_type");
    String package_name=request.getParameter("package_name");
    String Home_method=request.getParameter("Home_method").trim();
    String Remote_method=request.getParameter("Remote_method").trim();
    int location=0;
    String temp="";
    boolean isSession;

    PrintWriter pw_Home=new PrintWriter(new FileWriter(ejb_name+"//"+ejb_name+"Home.java"));
    PrintWriter pw_Remote=new PrintWriter(new FileWriter(ejb_name+"//"+ejb_name+"Remote.java"));
    PrintWriter pw_EJB=new PrintWriter(new FileWriter(ejb_name+"//"+ejb_name+"EJB.java"));
    if (!package_name.equals(""))
    {
        pw_Home.println("package "+package_name.trim()+";");
        pw_Remote.println("package "+package_name.trim()+";");
        pw_EJB.println("package "+package_name.trim()+";");
    }

    //bean接口贯穿于Home与Remote接口中
    pw_EJB.println();
    if (!ejb_type.equals("entity"))
    {
        //session
        isSession=true;
        pw_EJB.println("public class "+ejb_name+"EJB implements javax.ejb.SessionBean");
        pw_EJB.println("{");

        pw_EJB.println("    public void ejbRemove()");
        pw_EJB.println("    {");
        pw_EJB.println("    }");
        pw_EJB.println();
        pw_EJB.println("    public void ejbActivate()");
        pw_EJB.println("    {");
        pw_EJB.println("    }");
        pw_EJB.println();
        pw_EJB.println("    public void ejbPassivate()");
        pw_EJB.println("    {");
        pw_EJB.println("    }");
        pw_EJB.println();
        pw_EJB.println("    public void setSessionContext(javax.ejb.SessionContext sc)");
        pw_EJB.println("    {");
        pw_EJB.println("    }");
        pw_EJB.println();
    }
    else
    {
        //entity
        isSession=false;
        pw_EJB.println("public class "+ejb_name+"EJB implements javax.ejb.EntityBean");
        pw_EJB.println("{");

        pw_EJB.println("    public void ejbRemove()");
        pw_EJB.println("    {");
        pw_EJB.println("    }");
        pw_EJB.println();
        pw_EJB.println("    public void ejbActivate()");
        pw_EJB.println("    {");
        pw_EJB.println("    }");
        pw_EJB.println();
        pw_EJB.println("    public void ejbPassivate()");
        pw_EJB.println("    {");
        pw_EJB.println("    }");
        pw_EJB.println();
        pw_EJB.println("    public void ejbLoad()");
        pw_EJB.println("    {");
        pw_EJB.println("    }");
        pw_EJB.println();
        pw_EJB.println("    public void ejbStore()");
        pw_EJB.println("    {");
        pw_EJB.println("    }");
        pw_EJB.println();
        pw_EJB.println("    public void unsetEntityContext()");
        pw_EJB.println("    {");
        pw_EJB.println("    }");
        pw_EJB.println();
        pw_EJB.println("    public void setEntityContext(javax.ejb.EntityContext ec)");
        pw_EJB.println("    {");
        pw_EJB.println("    }");
        pw_EJB.println();
    }
    

    //Home interface
    pw_Home.println();
    pw_Home.println("public interface "+ejb_name+"Home extends javax.ejb.EJBHome");
    pw_Home.println("{");
    for(int i=0;i<Home_method.length();i++)
    {
        if(Home_method.charAt(i)==10 || i==(Home_method.length()-1))
        {
            temp=Home_method.substring(location,i+1).trim();
            //超界则退出
            if(temp.length()<=1)break;
            
            int temp_blank=temp.lastIndexOf(" ");
            if (temp_blank==-1)
            {
/*                if(temp.indexOf("(")==-1)
                {
                    pw_Home.print("    public "+ejb_name+"Remote "+temp+"() throws javax.rmi.RemoteException");
                }
                else
                {
                    pw_Home.print("    public "+ejb_name+"Remote "+temp+" throws javax.rmi.RemoteException");
                }
*/
                response.sendRedirect("error.jsp");
            }
            else
            {
                //bean
                if(temp.substring(temp_blank+1).equals(ejb_name+"Remote"))
                {
                    //session 返回void   remote 返回主键
                    if(isSession)
                    {
                        pw_EJB.print("    public void ejb"+temp.substring(0,1).toUpperCase()+temp.substring(0,temp_blank).substring(1));
                    }
                    else
                    {
                        String primaryKey=request.getParameter("primaryKey");
                        pw_EJB.print("    public "+primaryKey+"  ejb"+temp.substring(0,1).toUpperCase()+temp.substring(0,temp_blank).substring(1));
                    }
                }
                else
                {
                    pw_EJB.print("    public "+temp.substring(temp_blank+1)+" ejb"+temp.substring(0,1).toUpperCase()+temp.substring(0,temp_blank).substring(1));
                }

                if(temp.indexOf("(")==-1)
                {
                    pw_Home.print("    public "+temp.substring(temp_blank+1)+" "+temp.substring(0,temp_blank)+"() throws javax.rmi.RemoteException");
                    pw_EJB.print(" () ");
                    
                }
                else
                {
                    pw_Home.print("    public "+temp.substring(temp_blank+1)+" "+temp.substring(0,temp_blank)+" throws javax.rmi.RemoteException");
                }
            }

            //exception
            if(temp.substring(0,6).equals("create"))
            {
                pw_Home.print(",javax.ejb.CreateException");
                pw_EJB.print(" throws javax.ejb.CreateException");
            }
            else if(temp.substring(0,4).equals("find"))
            {
                pw_Home.print(",javax.ejb.FinderException");
                pw_EJB.print(" throws javax.ejb.FinderException");
            }
            pw_Home.println(";");

            pw_EJB.println();
            pw_EJB.println("    {");
            pw_EJB.println("    }");
            pw_EJB.println();

            location=i+1;
        }
    }
    pw_Home.println("}");
    location=0;

    //Remote interface
    pw_Remote.println();
    pw_Remote.println("public interface "+ejb_name+"Remote extends javax.ejb.EJBObject");
    pw_Remote.println("{");
    for(int i=0;i<Remote_method.length();i++)
    {
        if(Remote_method.charAt(i)==10 || i==(Remote_method.length()-1))
        {
            temp=Remote_method.substring(location,i+1).trim();
            //超界则退出
            if(temp.length()<=1)break;
            
            int temp_blank=temp.lastIndexOf(" ");
            if (temp_blank==-1)
            {
/*                if(temp.indexOf("(")==-1)
                {
                    pw_Remote.print("    public void "+temp+"() throws javax.rmi.RemoteException");
                }
                else
                {
                    pw_Remote.print("    public void "+temp+" throws javax.rmi.RemoteException");
                }
*/
                response.sendRedirect("error.jsp");
            }
            else
            {
                if(temp.indexOf("(")==-1)
                {
                    pw_Remote.print("    public "+temp.substring(temp_blank+1)+" "+temp.substring(0,temp_blank)+"() throws javax.rmi.RemoteException");
                    pw_EJB.print("    public "+temp.substring(temp_blank+1)+" "+temp.substring(0,temp_blank)+"()");
                }
                else
                {
                    pw_Remote.print("    public "+temp.substring(temp_blank+1)+" "+temp.substring(0,temp_blank)+" throws javax.rmi.RemoteException");
                    pw_EJB.print("    public "+temp.substring(temp_blank+1)+" "+temp.substring(0,temp_blank));
                }
            }

            pw_Remote.println(";");

            pw_EJB.println();
            pw_EJB.println("    {");
            pw_EJB.println("    }");
            pw_EJB.println();

            location=i+1;
        }
    }
    pw_Remote.println("}");
    location=0;

    pw_EJB.println("}");

    pw_Home.close();
    pw_Remote.close();
    pw_EJB.close();
    %>
    <html>
    <body>

    <script language="javascript">
    function err(form,flag,server)
    {
        if (flag=="false")
        {
            if (form.primkey_field.value=="")
            {
                window.alert("请输入主键名称");
                form.primkey_field.focus();
                return false;
            }
            if (stringFilter(form.primkey_field.value)==1)
            {
                form.primkey_field.focus();
                form.primkey_field.value="";
                return false;
            }        
        }
/*        if (server=="weblogic")
        {
            if (form.jndi_name.value=="")
            {
                window.alert("请输入JNDI名称");
                form.jndi_name.focus();
                return false;
            }
            if (stringFilter(form.jndi_name.value)==1)
            {
                form.jndi_name.focus();
                form.jndi_name.value="";
                return false;
            }        
        }
*/        return true;
    }
    function stringFilter(strFilter)
    {
        for (i=0;i<strFilter.length;i++)
        {
            if((strFilter.charAt(i)>="A" && strFilter.charAt(i)<="Z")||(strFilter.charAt(i)>="a"&&strFilter.charAt(i)<="z")||(strFilter.charAt(i)>="0"&&strFilter.charAt(i)<="9")||(strFilter.charAt(i)=="_"))
            {}
            else
            {
                window.alert ("您的输入中有非法字符");
                return 1;
            }
        }
        return 0;
    }
    </script>

    <center>
        <font size="5" color="red">现在进行Development Deploy配置(也即.xml的配置)</font><br>

        <form action="createxml.jsp?ejb_type=<%=ejb_type%>&ejb_name=<%=ejb_name%>&server_type=<%=server_type%>&package_name=<%=package_name%>&prim_key_class=<%=request.getParameter("primaryKey")%>" method="post" name="form1" onsubmit="return err(form1,@#<%=isSession%>@#,@#<%=server_type%>@#)">
        <table border="1" width="500">
            <tr>
                <td align="right" width="300">
                    请选择事务处理的类型:
                </td>
                <td align="left" width="200">
                    <SELECT NAME="transaction_type">
                        <option value="Container">Container</option>
                    </SELECT>
                </td>
            </tr>

            <tr>
                <td align="right" width="300">
                    请选择事务的属性:
                </td>
                <td align="left" width="200">
                    <SELECT NAME="trans_attribute">
                        <option value="Required">Required</option>
                    </SELECT>
                </td>
            </tr>
            <%if (ejb_type.equals("entity"))
            {%>
                <tr>
                    <td align="right" width="300">
                        请选择持久行的类型:
                    </td>
                    <td align="left" width="200">
                        <SELECT NAME="persistence_type">
                            <option value="Bean">Bean</option>
                            <option value="Container">Container</option>
                        </SELECT>
                    </td>
                </tr>
                <tr>
                    <td align="right" width="300">
                        reentrant
                    </td>
                    <td align="left" width="200">
                        <SELECT NAME="reentrant">
                            <option value="false" selected>false</option>
                            <option value="true">true</option>
                        </SELECT>
                    </td>
                </tr>
                <tr>
                    <td align="right" width="300">
                        请输入主键名称:
                    </td>
                    <td align="left" width="200">
                        <INPUT TYPE="text" NAME="primkey_field">
                    </td>
                </tr>

            <%}
            if(server_type.equals("weblogic"))
            {
            %>
                <tr>
                    <td align="right" width="300">
                        <%if(isSession)
                        {%>
                            请输入在缓存中最大的beans数:
                        <%}else{%>
                            请输入在pool池中最大的beans数:
                        <%}%>
                    </td>
                    <td align="left" width="200">
                        <INPUT TYPE="text" NAME="max" value="100">
                    </td>
                </tr>
                <tr>
                    <td align="right" width="300">
                        请输入JNDI名称:
                    </td>
                    <td align="left" width="200">
                        <INPUT TYPE="text" NAME="jndi_name" value="<%=ejb_name+"_JNDI"%>">
                    </td>
                </tr>
            <%}%>
            <tr>
                <td colspan="2" align="center"><INPUT TYPE="submit" value="提交">   <INPUT TYPE="reset" value="重填"></td>
            </tr>
        </table>
        </form>

    </center>

    </body>
    </html>
    <%
}
catch(Exception e)
{
    out.print(e.getMessage());
}%>

原文转自:http://www.ltesting.net