instructions provided

braintreesmileSoftware and s/w Development

Aug 15, 2012 (5 years and 3 months ago)

331 views

Setting up
NetBeans

6.9 & Glassfish V3


There are some problems with the
NetBeans

6.9 /Glassfish V3 combination used in
the laboratories. Operations like “create DataSource” can be performed in different
contexts


in some they work, in others they don’t.


The following sequence of actions should be followed to get your
NetBeans

system
set up correctly. Really, the Appserver parts (Glassfish V3) is only needed for the
EJB assignment but it would be best to set up everything when you first start.


1.

If you h
ave used
NetBeans

in earlier subjects such as CSCI213, CSCI222, or
CSCI399 then you should remove all existing directories associated with
NetBeans
.

Look for hidden folders .
n
et
b
eans
, .
n
et
b
eans
-
derby, .
n
et
b
eans
-
registration,
.personaldomain etc. Remove the
m.

Remove old
NetBeans

projects from the
NetBeans
Projects directory


delete them
to save space.


2.

Start
NetBeans

6.9 from the Applications/Programming menu:




3.

First, set up a database connection so that you can access your tables on the Oracle
database s
ystem that runs on the “wraith” computer:




You will need to add the ojdbc14.jar driver


it’s in /usr/local/instantclient_10_2.


Then create a connection that uses this driver


connecting to database csci at port
1521 on the server “wraith”; supply you
r database user name and password.




Check that it is working and that you can view your tables in the Oracle database.




Close the connection.


4.

Checkout the basics in NetBeans.


Create a standard Java application
.


NetBeans only loads elements when fi
rst used


so wait while Java application
side of NetBeans gets set up.




5.

You simply need a “Hello World” app for this test:




(User “Mark” is one of the fake accounts with student level privileges that I use
for testing.)


Code and build, then run




6.

Close that test project, and create an “Enterprise Project”




7.

Enterprise projects have to run on application servers


you haven’t got one yet so
you are prompted to choose one
; pick Glassfish Server 3
:




It’s located in /usr/local. Once you have

selected that you will be prompted to
define a “domain”


essentially its your private configuration for running a copy
of Glassfish. Specify a folder, called domain1, in your home directory.




Your domain gets created


you can look at the creation re
port later once the
project set up is completed:




With a server chosen, you can continue to specify project details


typically you
will want both web and application clients (only the application client is actually
created in this test sequence).





8.

The next task is to define connection pools and JDBC data sources that will be
hosted in the server. (It’s here that there are inconsistencies in the way NetBeans
handles things. It should be possible to define these connections later when
configuring a
“persistence.xml” file


but when done that way it seems that
NetBeans 6.9 misses out on copying the database drivers to a directory where
Glassfish can find them. So define the connection pool and JDBC resource now
to avoid problems later.)


Pick the new

application, and add a “Glassfish” file


one for connection pools:




The pool is to be created using the information that you entered when you tested
your database connection back in step 3.

Need to invent a connection pool name
and later a JNDI name


e.g. oraclePool, jdbc/ora





9.

You have only defined some data for connections


no code yet. But “deploy” the
application to get these new database components registered and the driver files
copied to appropriate places:






10.

Look at your servers in
the services tab.


Unfortunately, NetBeans 6.9 is listing the server that belongs to “root” (this is the
one that would be used in a production environment) as well as your personal
configuration. Then, NetBeans realizes that you cannot access the control

files of
that server and insists on telling you this


again, and again, and again.




It is possible to right click the listing and get the menu with a remove option


just
keep entering enter to dismiss the warning dialogs.


You should then be able

to see details of your server with the new JDBC
resources (connection pool and JDBC resource that you added).





11.

View the “Admin Console” of your server
-






12.

Check that the database connections really are set up:




You need to be able to successf
ully “Ping” your database:






13.

That is all that has to be done now.




The rest of the work can be deferred until there is an assignment or exercise using
either the “web” container (servlets) or the EJB container (“session beans).


But it would be sensi
ble to complete setting things up now rather than later.


14.

Put a password on your administrator.


Add a new user (e.g. “admin”) to the admin
-
realm with an assigned password.




Then,
on the next time you use the admin console
, log in as admin and remov
e the
“anonymous” user.



Also, it is probably useful to create a few users


tom, dick, harry, and sue


in a
couple of groups for any later exercises involving controlled role
-
dependent
access to resources.




15.

Resuming the creation of a little “Enterp
rise” application!


First, need to create code for handling object
-
relational mapping.


Here using the JPA


probably the TopLink implementation, though NetBeans 6.9
favours another.


Will need to grab the table definitions from Oracle, and generate the en
tity
classes.


This example assumes that you have created the Soccerleague table that is used in
some
other CSCI399/CSCI398 exercises and is used more extensively in the
CSCI398 EJB exercise:


drop table soccerleague;

drop sequence leagueseq;


create seque
nce leagueseq increment by 1 start with 1;


create table soccerleague (


gameid number primary key,


played date,


location varchar(64),


team1 varchar(32),


team2 varchar(32),


score1 number,


score2 number

);


insert into soccerleague values (


leaguese
q.nextval,


TO_DATE('2007
-
08
-
26','YYYY
-
MM
-
DD'),


'Members Equity Stadium',


'Perth', 'Newcastle', 0, 0);




...


16.

The application built here will simply retrieve all rows from the table.


17.

The entity classes are best created in their own separate project


a “Java Class
Library” project. I named the project “entities”.




This needs to reference the generic “Persistence” library (but not an actual
implementation like Toplink)




18.

Let the system write the code for dealing with the entities:




19.

Minor adj
ustments are often necessary.


If you are using Oracle sequences to provide “surrogate keys” then you need to
add these details (they aren’t captured directly as not clearly represented in Oracle
meta
-
data).


Sometimes the mappings are odd


it chose to us
e BigInteger values for my soccer
scores (it would be an unusual game that had a score that required a BigInteger to
encode it). I changed these back to “int”.




20.

Compile the “entities” project


this results in a .jar file with the class files.



21.

Add th
e entities.jar file to the “ejb” subproject of the Enterprise Application:




(Note the “relative path” / “absolute path” controls. If you intend to partly build
an application on your own computer and then try completing it in the lab, you
had better ch
oose “relative path”.)


22.

Define a package in the ejb sub
-
project for the beans.


Define a session bean


this will contain all the business code and will be called
from both web (servlet) and rich client application clients.




It should have both “local”
(web) and “remote” (application client) interfaces:




23.

Use “Insert Code” to add a “business method”. It will be added to the local and
remote interfaces (if you use a class like BigDecimal as a result or an argument,
you must either specify it completely



java.math.BigDecimal


or use
BigDecimal and then edit the “bean” and “interface” files to “Fix imports”).





24.

The ejb subproject needs to have the Toplink library as well as the entities.jar file
listed in its libraries.


It also needs a persistence
unit.




The data source is the jdbc/ora resource created earlier. The persistence provider
should be Toplink. You don’t want it to drop your existing tables when it
connects!




25.

You need to tell it that it works with the entity classes from the entiti
es.jar file:



26.

The EJB container will “inject” an entity manager into your session bean


you
just have to tell it which one (reference to the name in the persistence unit):




27.

You can now “build” the EJB subproject.


28.

Next, work on the client application

sub
-
project.


Add code (“Call Enterprise Bean” that will allow connection to the session bean:




29.

Obviously, the client needs the entities.jar library (this contains the definition of
the soccerleague entities that will be returned.


It’s not obvious wh
y it needs the library with the persistence implementation. But
it does. It’s because if a collection data member is “lazy loaded” and hasn’t been
loaded before an entity is returned, you get back an entity where the Collection
data member is not null bu
t is instead an instance of a place holder class from the
persistence implementation. So you need the implementation library to get that
class defined.




30.

The Enterprise application will, by default, be set up to run the web client. We
haven’t built one
. So change the Properties/Run to specify use of the client
application.





31.

It should run:




32.

Remove the test application from your server and invoke server shutdown.




You won’t need the AppServer until you get to a “web” or “ejb” assignment or
exer
cise.