Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Advanced Internet Programming - EJB 31242/32549/ AJPCPEAdvanced Internet Programming INFO: What's New FAQ Software Machines Oracle Assignments MODULES: 00 Admin 01 Intro 02 Architecture 03 Servlets 04 JSP 05 JDBC 06 RMI 06 JNDI 07 EJB 08 XML 08 Web Services 09 Security 10 Transactions 11 Legacy 12 Review 99 Design LINKS: START page Faculty of IT UTS Module: EJB Lab exercise - Using Eclipse + Xdoclet to generate EJB In this lab exercise, you will use Eclipse + Xdoclet to create an Enterprise JavaBean. Level of Difficulty: 2 (moderately easy) Estimated time: 60 minutes Pre-requisites: Run wlenv to set your environment correctly Start your WebLogic server running in the background Rapid code development One of the biggest complaints about Enterprise JavaBeans as implemented by the J2EE specification is the complexity of generating what seems like fairly straight forward business component development. This issue resulted in the development of competing frameworks such as Spring MVC, Object-Relational Mapping systems such as Hibernate and Java Data Objects, where developers bypass the whole EJB infrastructure entirely. Sun recognised this by creating 2 new standards - the Java Persistence API (which front ends ORM systems such as Hibernate) and the EJB 3.0 specification (which uses Java 5 annotations to 'tag' classes and methods as Enterprise Java Beans) Using EJB 3.0 would be a simpler technique than using EJB 2.1, however, this is not implemented in the J2EE 1.4 specification and is a fundamental part of Java Enterprise Edition 5. Most Java application servers &/or tooling are not yet fully certified at the Java EE 5 spec, so we will concentrate on EJB 2.1 coding. One technique similar to EJB 3.0 is to use Java 5 annotations or Javadoc-style annotations in our code and let code generators create most of the tedious steps required to generate EJB's. In our environment, there are 2 main ways to do this 1. Use the proprietary WebLogic workshop "ejbgen" tool. 2. Use the open source (and popular) Xdoclet tool Using Weblogic Workshop EJBgen View the oracle weblogic documentation "Programming Weblogic enterprise java beans" at http://edocs.bea.com/wls/docs103/ejb/overview.html for how to do this Using Eclipse & XDoclet Xdoclet ( http://xdoclet.sourceforge.net ) is an open source product designed to remove most of the tedium when developing EJB's. The original product was called ejbdoclet but this tool is now generic and supports many other technologies such as Servlets, Hibernate, Spring & so on and has large support for major Application server vendors such as IBM and BEA WebLogic. XDoclet is designed to be run using Apache ANT, a Java 'make' tool. You normally create a build.xml file with various XDoclet specific 'tasks'. You then create Plain Old Java Object (POJO) classes which implement the business logic. You don't need to create tonnes of extra class files and deployment descriptors, everything is in one source file!! Using Eclipse Web Tools Project reduces the workload significantly further by providing templates and wizards to generate more code. Use the following steps to re-create the previous stateless session bean lab. Step 1: Create the project and settings for Xdoclet development Create a new EJB project by:  new -> select Show All Wizards, , type EJB in the filter text & select EJB Project   In the New EJB Project wizard: enter project name (HelloEJB), & change Configurations (EJB Project with XDoclet) [Next] On the Project Facets wizard, ensure EJBDoclet (Xdoclet) selected & version 1.2.3 (use dropdown on 1.2.3 to get this) Press [FINISH] to continue. You now need to set up the project's XDoclet settings From the project menu, Right Mouse Button -> Properties -> Xdoclet -> ejbdoclet -> select weblogic This will ensure that the Xdoclet generator will execute the weblogic specific tasks. Note that if you are using, say, JBoss, you would also select the JBoss task to generate the JBoss specific deployment descriptors. Step 2 - Create a new Session Bean First step is to create a package. Use New -> Package & call the package mybean To create a new EJB, select: New -> Xdoclet Enterprise JavaBean (or you may have to use New -> Other -> EJB -> Xdoclet Enterprise JavaBean) If you get a message "Annotation provider definition is not valid", please select the preferences link You will need to set up Xdoclet provider preferences: Make sure Enable xdoclet builder is checked. Xdoclet Home: /pub/ajpcpe/xdoclet, Select version: 1.2.3 You will also have to check the XDoclet settings AGAIN! (this is a known bug in the XDoclet wizard) Expand the Xdoclet menu on the left & select the ejbdoclet preferences. Ensure the Weblogic task is ticked (you can also select other tasks if you are using non-weblogic application servers such as JBoss) Once you do this step, you should not have to do this again for any further Enterprise Java Bean creation. Creating the bean After the previous setup set has been completed, you should be back to Create Enterprise Java Bean wizard. Ensure Session Bean is selected & press [NEXT]   Select or enter a package name eg: mybean Add a class name eg: HelloBean. Leave superclass as Object. & press [NEXT]   The next window will display some more parameters for the EJB. Adjust EJB name, description, & display name as necessary. ** IMPORTANT ** Make the JNDI name ejb/Hello (you could leave this as the default, but the standard is to have the ejb beans under the ejb subcontext) Leave State Type = Stateless & Transaction Type: Container Press [FINISH] to build your EJB You should then see the HelloBean.java file in the code editor and  some messages in the console about Xdoclet. It runs through various stages such as Init, Ejbdoclet. This is actually generating all the class files and deployment descriptors. Note: if you don't see the "Running " Generating weblogic-ejb-jar.xml message, you have failed to follow the Weblogic task setup above.   Step 3: Enhancing the Enterprise JavaBean f you look in the ejbModule folder of your EJB project, you should see in the mybean package the following files Hello.java              REMOTE interface Hello EJBObject = declarations HelloBean.java      IMPLEMENTATION  SessionBean + business methods HelloHome.java      HOME interface EJBHome HelloLocal.java      LOCAL interface  EJBLocalObject = declarations HelloLocalHome.java  LOCALHOME interface  EJBLocalHome HelloUtil.java             Utility class   helper class eg: getHome() We will only change HelloBean.java. Everything else is Generated!! Open the HelloBean.java file. We will add business methods into this file. Look for the automatically generated method called foo() Notice that there is a special javadoc tag in front called @ejb.interface-method view-type="remote" /** * * * @ejb.interface-method view-type="remote" * * @generated * * //TODO: Must provide implementation for bean method stub */ public String foo(String param) { return null; } These tags are what XDoclet uses to generate code with. By default, this means XDoclet will generate a declaration of this method (foo()) in the Remote interface. If you change the view-type from remote to both then XDoclet will generate the business method into both the remote and local interfaces. For our exercise, let's change this method to the same as the stateless session lab: /** * * * @ejb.interface-method view-type="both" * * @generated * */ public String hello(String name) { return ("Hello, " + name); } Notice that when you change any code in this file, XDoclet will automatically re-build the files. To stop this behaviour, you need to click on the main toolbar, select Project & de-select Build Automatically. However, be careful, since you will need to build manually if you don't build automatically. Do this via the RMB -> Run XDoclet command. Step 4: Deploy the bean There are 4 main ways to do this (assuming the server is running) (1) From Workshop, right click on the Server (BEA Weblogic Server v10.0 @ localhost) in the Servers view and click "add and remove projects". You then choose the EJB and select Add & [finish] This deploys the bean on the server.   (2) You can Export the bean to a Jar file. Select the EJB project, RMB -> Export -> Export as EJB Jar file. Copy the resultant JAR file into the weblogic/autodeploy directory   (3) From the weblogic console, use the [Lock & Edit] & choose Deployment -> Install option to upload the JAR file from step 2.   (4) You can create an EAR project file which has project dependencies on it (containing the lab Web project & the EJB project) You then use step 1 to add the EAR project to weblogic -OR- use Export -> Export as EAR file. This EAR file can then be deployed by copying to the weblogic/autodeploy directory or via the Weblogic console Deployments - install option For this lab exercise, just use the first option. Step 5: Creating the client: This is pretty much the same steps as the Stateless Session Bean lab. Let's use a JSP to test this EJB. You need to create a JSP into an existing Dynamic Web project (for example, your labs project) with the following code in it: <%@page import="mybean.*, javax.naming.*, javax.rmi.*" %> <% Context ctx = new InitialContext(); String jndi_name = "ejb/Hello"; out.println("Got context"); HelloHome hwsh; out.println("Looking for JNDI name " + jndi_name); Object ref = ctx.lookup(jndi_name); hwsh = (HelloHome) PortableRemoteObject.narrow( ref, HelloHome.class); out.println("Got home interface"); Hello hws = hwsh.create(); out.println("Created EJB"); out.println(hws.hello("mate")); %> A Better way - using the XDoclet xxxutil class XDoclet generates a utility class called bean_nameUtil.java to make your life easier. This provides a method called getHome() which replaces all the context lookups etc. This code implements a "Service Locator" pattern and can even cache the bean lookups to make your code very efficient. <%@page import="mybean.*, javax.naming.*, javax.rmi.*" %> <% HelloHome h = HelloUtil.getHome(); Hello bean = h.create(); out.println(bean.hello("mate")); %> A lot simpler eh? Your client code will need to have access to the mybean.Hello & mybean.HelloHome  classes. You have 2 options:  Copy the EJB jar file exported earlier (helloEJB.jar) to WebContent->WEB-INF->lib (The problem with this solution is that you need to update the jar whenever you change the EJB)  Set your Dynamic Web Project to have a "J2EE Dependency". You select your Web project (eg: lab) & RMB-> Properties, J2EE Modules Dependencies -> Select helloejb Jar module] This will result in the .jar file being placed into the WEB-INF/lib directory at deployment time automatically!!   Note: Normally you only need the Hello, HelloHome (& helloUtil) (& maybe the HelloLocal* equivalents) classes, but we will be lazy and import the whole lot. From a development point of view, it is better to let Eclipse manage all these relationships for you - so choose option 1 (the J2EE Dependency solution). Step 6: Running the client Run as usual. (select the web project , RMB -> Run As -> Run on Server). Normally most changes will get automatically deployed, as long as there is a dependency between the web project and the  EJB project. Note: when you look at the web project on the server view, you now see a + symbol next to the web project. Expanding this shows the EJB bean jar   Problems you may encounter You may need to re-deploy the EJB if you make any changes to the signatures of the bean methods or add new beans into the EJB project. Sometimes you Workshop does not correctly re-deploy changes into the dependent J2EE projects (such as your Web project). To fix this, re-do step 4 above.   Next step ... Now that you have followed a step-by-step walkthrough, the next step is to be a bit more independent. Modify the example above to include a new remote method that will add together two integers (i.e. it takes two integer arguments, and returns an integer result). This will involve changing the remote interface, and then re-running all the subsequent steps. © 2008 University of Technology, Sydney. All Rights Reserved. Redistribution without permission prohibited.