- WebLogic Workshop Programming Basics
- Creating Applications and Projects
- Deploying and Configuring Applications
- In Brief
Creating Applications and Projects
As we introduced in Chapter 2, WebLogic Workshop creates J2EE enterprise applications containing one or more projects, libraries, modules, and security roles. Before Workshop, just laying out properties and configuring an application required a considerable amount of effort. However, Workshop makes creating new Web applications quick and easy. To create a basic Workshop application, select File, New, Application from the WebLogic Workshop menu. Figure 3.8 shows the New Application dialog box. In this figure, (A) indicates the types of applications displayed in the right pane; by default, all application types are shown. The application type list, labeled as (B), indicates the type of application being created. Default applications are basic Web applications and include XML Schema support. A default application contains a core, but minimal, application as a development starting point. Empty applications include Web application development support, but without a starting application. Portal applications are default applications that include additional portal support services, including a data project supporting rules, campaigns, and other portal features. Portal applications require a Portal-enabled server. Process applications are default applications that include integration and business process services and require an Integration-enabled server.
Figure 3.8 The New Application dialog box.
The directory path, labeled (C), represents the root directory where the application will be created. The new application name, labeled (D), represents the name of the application as well as the topmost directory within the root directory. The Server text box, labeled (E), represents the target server for the application.
The Server text box is the most important field in the New Application dialog box. In the WebLogic Workshop programming world, applications and servers are tightly coupled. This tight coupling is a result of applications requiring specific services. Various servers provide different services and different feature sets. For example, an application might require WebLogic Portal or Integration support. Out of the box, WebLogic Workshop contains server definitions for the following:
WorkshopA Web application and Schema-enabled server supporting Web application development.
PortalA WebLogic Portalenabled server, supporting Web application and portal services and including all the services of the Workshop server.
IntegrationA WebLogic Integrationenabled server, supporting Web application development using business processes and including all the services of the Workshop server.
End2endA WebLogic Platformenabled server containing all the services previously mentioned.
When building an application for the first time, you must choose a server that provides the required services. If you intend to create a Web application, select the Workshop server; otherwise, choose a server consistent with your development needs. Chapter 15, "Taking Your Applications from Development to Production," discusses the WebLogic Configuration Wizard. This wizard enables users to create domain definitions that can include any combination of the services required to support different application types.
On disk, Workshop applications are managed as expanded enterprise applications, but can be packaged into Enterprise Archives (EARs) easily by selecting Build, Build EAR from the main menu. The build process is examined further in "Building Applications," later in this chapter.
After creating a default Web application, WebLogic Workshop displays the Application pane (see Figure 3.9).
Figure 3.9 A Workshop application with Schema and basic Web application support.
Starting WebLogic Server for Application Testing
Remember, you cannot test or debug applications without a running instance of WebLogic Server. Workshop has a number of menu shortcuts for starting, stopping, and running the WebLogic Server Console from the Workshop environment. To start an instance of WebLogic Server for an application, select Tools, WebLogic Server, Start WebLogic Server from the Workshop menu. To stop a running instance, select Tools, WebLogic Server, Stop WebLogic Server. To start the WebLogic Console, choose Tools, WebLogic Server, WebLogic Console. In addition, you can deploy, undeploy, or redeploy applications by using the Tools menu.
Workshop Applications Breakdown
Workshop Web applications are divided into three major sections. One major section is the Web application itself, named yournameWeb by default, which contains
-Controller.jpfA default Page Flow used as the starting point of application development. Error.jsp and Index.jsp are two default pages used by the Page Flow.
resourcesA folder containing a set of images, header and template JSP files, and associated stylesheets that can be used as the basis of a common look for a Web application. Note that template.jsp and header.jsp have a simplified look and feel and should not be confused with a portal look and feel.
WEB-INFA folder containing all the XML-based application deployment descriptors, tag libraries, and XML definitions required to support Page Flows. Workshop manages almost all files in the WEB-INF folder, and these files should not be edited by hand.
Another major section is the default Schema project, which contains the following:
SchemasA folder used to house any XML Schema files. Schemas can be added to this project by drag and drop. Newly added schemas are compiled, and then the generated XML Beans are added to the application's libraries folder. Application developers can use the generated XML Bean classes to create, delete, update, or otherwise manage associated XML.
The last major section is supporting libraries and security definitions common to the application folders:
LibrariesA folder containing all support libraries. You can add libraries to a project by dragging them into the Libraries folder, which maps to the APP-INF\lib directory of an enterprise application.
ModulesA folder containing all deployed J2EE enterprise resources. The Modules folder contains the application.xml and weblogic-application.xml files, which Workshop manages automatically. This folder maps to the META-INF folder of an enterprise application.
Security RolesA folder containing application-defined security roles. The roles defined in an application are normally mapped to WebLogic Server groups. Security entries map to Security-role definitions in the application.xml file.
Workshop Projects
As we saw in Chapter 2, workshop applications contain one or more projects. These are the most common project types:
Web ApplicationThese projects are standard Web applications packaged as Web Application Archive (WAR) files. Web Application projects contain a default Page Flow and support for creating new Page Flows, accessing existing controls, and creating new controls. Typically, Web projects are used to make Web applications available for use with browsers, but the application's functionality can also be made available via Web services.
Web ServiceThese projects are targeted at making application functionality available as a Web service using WSDL and are packaged into WAR files. Note that Web Service projects can contain Page Flows as well as Web services.
ControlControl projects consist solely of control classes, which are designed to be used by other developers to create portals, Web applications, and so forth. Control projects are packaged into Java archive (JAR) files and can be dropped into another application's lib directory.
EJBEJB projects consist solely of EJBs and any support classes. EJB projects are packaged into JAR files and can also be used by being dropped directly into another application's lib directory.
SchemaSchema projects contain XML Schema files. Schema files are then compiled into XML Beans, a generated set of classes for managing XML documents. XML Beans are an exciting new BEA technology for creating, accessing, and working with XML. XML Beans are examined in detail in Chapter 10, "Handling Data in WebLogic Workshop: Working with Schemas and XML Beans."
Java ProjectThese projects contain packages of Java classes. Java projects are used to create reuseable class libraries and are packaged in JAR files.
In addition, if you have purchased or downloaded WebLogic Workshop Platform Edition, you can create other project types, such as
ProcessProcess projects consist of a default business process definition and compile into a JAR file that other projects or applications can access. Process projects can also be made available as Web services and accessed by other Web services platforms.
Portal and DatasyncPortal projects contain all the required infrastructure for creating portal applications. Creating a portal project results in the creation of a parallel datasync project containing rules, campaigns, placeholders, and other portal-specific features. Portal applications are packaged into EAR files.
To create a new project, select File, New, Project from the Workshop menu. Alternatively, select your application in the Application tab, right-click, and choose New Project. In the New Project dialog box (see Figure 3.10), select the type of project you would like to add. The type of project list, labeled (D) in Figure 3.10, displays subsets of the project types in the pane labeled (E). The All option displays all project types, and the other options display subsets of project types. Select the type, labeled (D), and the subtype, labeled (E), and enter your new project name in the space labeled (F). A new project is then added to Workshop, and corresponding entries are added in the underlying XML deployment descriptors.
Application Default Services
Applications are created supporting a default set of services. For example, Web applications do not support portal functionality by default. You can add portal or other project support to applications or projects by selecting the project, right-clicking, and choosing Install, support service from the menu. Support service could be WebLogic Portal or some other new service you want to use in your application. In this way, you can later add Portal or Integration support to a core Web application. Note that you might also need to select a new server because the core server might not include the runtime services required to support the new additions.
Figure 3.10 The New Project dialog box.
With all the Workshop project types, the question is "When do you use one project type versus another?" When you need to build a portal project, the answer is clear. However, as a rule of thumb, you should use a different project whenever you are creating reuseable units of functionality or to separate unrelated functional areas. As we examine controls, portals, integration and other project and application types we will also examine when it is appropriate to create a new project or application, and when to extend the current application.
Installing Application Functionality
WebLogic Workshop is flexible in what kind of project to create. Web applications, portal projects, and process projects are the norm. However, what happens if you want a combination project, one that perhaps spans portals and workflows? One of the simplest ways to add functionality to a project is to select the root project, right-click, and choose Install. For example, you can insert portal support into a Web application by choosing Install, Portal.
Unfortunately, installing new support into an existing application is somewhat of a problem. Depending on the combination of resources, not everything works as planned, and it's never clear what the root cause is. So how can you create a project and be sure it will support the features you want? Practice has shown that the best way to develop an application is to start with the simplest project that supports the job at hand. In practice, that means creating controls (EJB, business process, or general) within their own projects, unit testing in an application that supports only those features you want, and then ultimately creating an overall application that supports all the features previously tested as standalone and integrates them together.
Use the following guidelines:
Create custom, rowset, and EJB controls in control projects. Unit test in Web applications.
Create business process controls in business process control projects. Delegate all background processing to custom controls. Place business logic only in business process controls.
Create portals and portlets in portal projects. Add only tested controls to portal projects.
Create overall applications that start with portal or business process support. Install other functionality to the application as required. Add tested logic via controls projects.
Accessing and Exposing Data via Controls
Applications, whether they are designed to support Web services or be accessed as Web or portal applications, require data as content and for decision logic. In Workshop applications, logic and data are provided by controls, which are the heart of any WebLogic Workshop application. Figure 3.2 introduced the Workshop controls model; Figure 3.11 shows a deeper view. Remember, workshop supports two different types of controls: resource or extensible controls (based on J2EE resources), resources, and custom controls written by developers. Custom controls are normally used to encapsulate data access or add business logic. To the developer, controls look much like standard Java objects; however, in addition to traditional methods, controls provide a number of other capabilities, such as
-
Asynchronous method invocationThe ability to call a long-running method and return immediately while the method continues in the background. (This is shown as a slashed arrow pointing into the control in Figure 3.11.)
Figure 3.11 A deeper look at controls.
-
Callback methodThe ability to invoke a method back to the calling client from a control. (This is shown as a slashed arrow pointing out of the control in Figure 3.11.)
ConversationsThe ability to call methods that start, continue, and end a conversation.
Transaction supportControl methods are an implicit part of a transaction without any developer work.
One area that's becoming more important in messaging is connectivity. Three specific types of connected behavior can occur in today's networks: fully connected, in which the client and server are connected all the time; disconnected, in which the client is never connected to a server; and partially connected, in which the client is sometimes connected to the server and sometimes not. The control's runtime engine has support specifically targeted toward connectivity, namely the capability to manage a conversation. Controls can clearly define what methods start, continue, and end a conversation. More important, controls provide mechanisms for obtaining conversation IDs so that applications can disconnect and then later reestablish a conversation. The ability to reestablish a conversation is key to supporting a partially connected network model. Conversations, asynchronous methods, and callbacks are covered in detail in Chapters 7, "Using the NetUI Tag Library Toolbox to Develop Sophisticated JSPs," 9, "Working with Web Services," and 11, "Working with Workflows."
Adding Controls to Page Flows
The two most common uses of controls are to gather data for display and to make a logic decision. To use a control, you must add it to a Page Flow, open an action, and call methods on the control. Figure 3.12 shows two methods of adding a control to a Page Flow. If the control is part of the same application, it can be added by dragging the associated .jcx (Java extensible control) or .jcs (Java custom control) file into the right side of a Page Flow's Action View. Otherwise, you can right-click in the right side of a Page Flow Action View, select Controls, and then add the control. In either case, Workshop then prompts for the variable name of the new control and adds the control to your application. Listing 3.3 shows the code annotations added to a Controller when a control is added. Controls can then be accessed in actions like regular Java classes.
Figure 3.12 Adding a control to a Page Flow.
Listing 3.3 Annotations Marking a Variable As a Control
public class Controller extends PageFlowController { /** * @common:control */ private controls.Weather weather; /** * @common:control */ private db.games gamesDBCtrl;
Accessing Databases with Rowsets
One of the most common types of controls is the rowset control. The GamesDBCtrl shown in the previous example is a simple RowSet control enhanced to return value objects. Rowset controls are based on the java.sql.RowSet interface and provide a generic mechanism for accessing database data over a network. Rowsets are derived from the java.sql.ResultSet class and inherit much of the parent behavior. Rowsets can be used to add data, query for data, traverse rows of data, and delete data.
The RowSet Wizard generates several methods; the following two methods are used to read data:
RowSet getAllTbleName(DatabaseFilter)This method return all rows for a given table and is filtered based on the database filter used as input to a where clause. Note that even if no filtering is required, you must provide a DatabaseFilter.
RowSet detailsTbleName(key)This method returns the requested row based on the supplied key.
Rowset data is accessed by using one of the type-specific rowset methods, a generic example of which is shown here. All rowset accessor methods use the following form:
Type getType(String ColName);
In this form, type can be String, Int, Double, Float, Short, and so on. See the java.sql.RowSet documentation for a complete list.
Rowsets can return multiple rows of data. After a select clause, rowsets are initially positioned before the first row. You can iterate over a rowset by using the next() method.
The Wonderland Casino is actually made up of a number of casinos. Descriptive data about the casinos, games, hotels, and so on is stored in a database that could be accessed by using RowSet objects. The WONDERLAND.CASINOS table contains the complete list. Its SQL definition is shown here:
CREATE TABLE "WONDERLAND"."CASINOS" ( "ID" INT IDENTITY, "NAME" VARCHAR(20), "DESCR" VARCHAR(512) );
Assuming you have a RowSet object that contains all the casinos, you could traverse the list using the following:
While (allData.next()) { System.out.println("ID=" + allData.getInt("ID"); System.out.println("Name=" + allData.getString("NAME"); System.out.println("Desc=" + allData.getString("DESCR"); }
Wonderland Casino Sample Data
The Wonderland Casino database definition is in the {CDROM}:\db directory on this book's CD. You can add the Wonderland database to PointBase, the database tool packaged with WebLogic Workshop, by running the mkdb.cmd command script. First, you need to edit the set-dbenv.cmd script to point to your BEA install directory. To run the script, open a command prompt, and copy the scripts to a directory where they can be edited. Edit set-dbenv.cmd and then run mkdb.cmd. Additionally, if PointBase is running somewhere other than localhost:9093/ workshop, you need to update the pbcmd.cmd script appropriately.
The use of NetUI tags to present data is covered in detail in Chapter 7. However, you could easily display the entire list of RowSet data using a netui-data:repeater tag in a JSP. Assuming you returned a rowset of data from the Casinos table defined in a Page Flow as public RowSet casinos, you could access the data as a table using the code shown in Listing 3.4, with important lines shaded.
Listing 3.4 The netui-data:repeater Tag
<netui-data:repeater dataSource="{pageFlow.casinos}"> <netui-data:repeaterHeader> <table border="1"> <tr> <td><b>ID</b></td> <td><b>Name</b></td> <td><b>Description</b></td> </tr> </netui-data:repeaterHeader> <netui-data:repeaterItem> <tr> <td> <netui:label value="{container.item.ID}" /> </td> <td> <netui:label value="{container.item.NAME}" /> </td> <td> <netui:label value="{container.item.DESCR}" /> </td> </tr> </netui-data:repeaterItem> <netui-data:repeaterFooter> </table> </netui-data:repeaterFooter> </netui-data:repeater>
Adding and Deleting with Rowsets
In additional to reading data, rowsets support deleting, inserting, and updating data. The Casino Maintenance department has requested adding new functionality to the Wonderland Web site to support listing, adding, and deleting casinos. A new Page Flow, CasinoMaintenance, has been added to the Wonderland application. It supports listing, adding, and deleting casino table entries. To add an entry to a rowset, do the following:
Obtain a template row by using detailsTBLNameTemplate().
Update the row values.
Insert the new row into the rowset.
Insert the rowset contents into the database.
Listing 3.5 shows an example of these steps.
Listing 3.5 The doAdd Action of CasinoMaintenanceController.jsp
protected Forward doAdd(AddForm form) { String name = form.name; String description = form.description; try { RowSet newCasino = casinosdb.detailsCasinosTemplate(); newCasino.updateString("NAME",name); newCasino.updateString("DESCR",description); newCasino.insertRow(); // add the row into the rowset casinosdb.insertCasinos(newCasino); // add the row into the db } catch(SQLException sqle) { System.out.println("Unexpected SQLException on add:" +sqle); } return new Forward("success"); }
Deleting a row from a database follows a similar pattern. To delete a row, follow these steps:
Obtain a rowset containing the row based on the row's key.
Set the rowset to the row you want to delete.
Delete the row from the rowset.
Delete the row from the database.
Listing 3.6 shows an example of these steps.
Listing 3.6 The doDelete action of CasinoMaintenanceController.jsp
protected Forward doDelete(DeleteForm form) { String id = form.ID; try { RowSet row = casinosdb.detailsCasinos(new Integer(id)); row.absolute( 1 ); row.deleteRow(); casinosdb.deleteCasinos(row); } catch (SQLException sqle) { System.out.println("Unexpected SQLException on delete:" +sqle); } return new Forward("success"); }
Creating Simple Rowset Controls
Rowset controls are the heart of any database application. There are, in fact, two types of database access controls. Database controls, which are similar to rowset controls, require all data access to be coded by hand. Rowset controls go a step further than database controls, in that they provide a core set of access methods, based on java.sql.RowSets, for accessing data. Developers are free to add functionality, but a set of core methods are supplied.
To create a rowset control, select a folder where the control will be created and follow these steps:
-
Select File, New, Other File Types from the Workshop menu (see Figure 3.13).
(Same as step A.)
Select Business Logic in the left pane.
-
Select RowSet Control in the right pane (see Figure 3.13), and click Create.
Figure 3.13 Creating a rowset control.
Enter the control variable name.
Select an appropriate datasource.
-
Select the methods to create, either Query and Update a Database Table (for traditional tables) or Query a View or Table (no update methods generated). The remainder of the steps for adding a rowset control are shown in Figure 3.14.
Figure 3.14 Selecting methods, columns, and a primary key source in the RowSet Control Wizard.
Select the schema and table name, and click Next.
Select the columns to include, and click Next.
Select the primary key mechanism, and click Create.
When you're finished, a new rowset control is created in the folder you selected. The new control can be used to access the table from a Page Flow or another control.