The Wayback Machine - https://web.archive.org/all/20050510101545/http://blogs.sun.com:80/roller/page/geertjan/20050505
Default style (Cherry Eve). Switch styles (Capricorn). XML Feed Calendar
All | Who am I? | NetBeans IDE
20050505 Thursday May 05, 2005

Quick Notes for Installing Mevenide in NetBeans IDE 4.1

  1. Download and install Maven.
  2. Download and extract Mevenide. Then replace grammar-0.6.nbm with grammar-0.6.1.nbm which you can get here.
  3. Some fiddly bits:

    • If you're behind a proxy, put its port and host in a build.properties file in your user home directory (the directory in which the .maven user directory is found).

      maven.proxy.host = 
      maven.proxy.port = 
    • If you want to use JDK 1.5, set JAVA_HOME to JDK 1.4 but start the IDE with JDK 1.5 in etc/netbeans.conf:

      netbeans_jdkhome="c:\Program Files\Java\jdk1.5.0_01"
  4. Build the Maven repository:

    maven plugin:download -DgroupId=mevenide -Dversion=1.1 -DartifactId=maven-nbm-plugin

    maven nbm:fill-repository

  5. Start the IDE, go to Tools > Update Center and install the manually downloaded models that are in the directory where you extracted Mevenide. Remember to use the correct grammar NBM file. (You'll get a warning if this goes wrong.) Accept all the defaults and restart the IDE.

  6. Click Ctrl-Shift-N and in the New Project wizard you'll see the Maven category with its project templates:

May 05 2005, 04:12:23 AM PDT Permalink

20050504 Wednesday May 04, 2005

Efficient Tomcat Management from NetBeans IDE 4.1 with Ant

Until yesterday, I'd never been aware of the fact that the Tomcat Manager has a web page from which applications can be started, stopped, reloaded, and undeployed. In the Runtime window, when you start the Tomcat Web Server and then expand the Web Applications node, one of the subnodes is called /manager. However, when you right-click this node and choose Open in Browser, you go to this URL:

http://localhost:8084/manager/

This brings up an odd little page containing a few weird links. However, if you go to this URL instead, assuming the bundled Tomcat Web Server is running at the default port 8084, you get a whole different picture altogether:

http://localhost:8084/manager/html/list

When you go there, this is what you'll get (click to enlarge):

Now this is really helpful (especially compared to what I get when I choose Open in Browser from the /manager node in the Runtime window). In this page, there are links for each application deployed to the Tomcat Web Server. These links allow me to start, stop, reload, and undeploy my application. And there are various other things available from this page, things I haven't even discovered yet.

Dealing with the Manager's Password

However, before I'm able to access it, I am asked for a username and password, but the only acceptable username/password combination is that of a user who has been assigned the "manager" role. This username and password is located in the tomcat-users.xml file which is found in Tomcat's Home directory (which is, by default, within your user directory). This tomcat-users.xml file was discussed in an earlier blog, where I wrote about an Ant script to open the file in a text editor so that you can see what the IDE-generated password is for the IDE-generated user "IDE", which is a user with the "manager" role. (This is generated by the IDE to help you, because Tomcat doesn't provide a user that is assigned the "manager" role, because this is a powerful role for which you have to specifically create a user yourself or, in the IDE's case, by the IDE.)

But a better solution than clicking a menu item in the IDE to open the tomcat-users.xml file (so that you can see the IDE-generated password) is to use Ant in a different way:

  1. Create a copy of the tomcat-users.xml file in a local directory, outside of the IDE's user directory.
  2. Create a user and assign it the "manager" role.
  3. Create an Ant script to copy this file over the tomcat-users.xml file in the IDE's installation directory:

      <target name="copy-my-personal-tomcat-users-file" description="Copy Tomcat Users">
         <copy  file="C:\tomcat-users.xml" 
               todir="C:\Program Files\netbeans-4.1rc2\enterprise1\jakarta-tomcat-5.5.7\conf" />
      </target>
  4. Create a menu item, toolbar button, or shortcut key to invoke the target.

Now, whenever I have a fresh IDE installation, I need only run the above target for the tomcat-users.xml file to be copied to Tomcat Base directory (which is the bundled Tomcat Web Server's directory inside the IDE's installation directory). Then, when the IDE creates the Tomcat Home directory inside my user directory, which happens the first time that the bundled Tomcat Web Server is started, it will copy the tomcat-users.xml file in the Tomcat Base directory into the Tomcat Home directory. And, since I know what username/password combination has been assigned to the "manager" role (because I defined those once myself, in my local directory), I will never need to dig into the user directory to find the Tomcat Home directory so that I can locate the tomcat-users.xml file in the Tomcat Home directory's conf subdirectory in order to find out what the manager's password is.


Question of the Day. Deepak Jacob asks: "I have added a menu named start tomcat (ide-wide-targets.xml). But Tomcat opens its own window and runs in it. I want to run it inside my Netbeans output window."

Answer: The reason for this is that at startup Tomcat opens two successive DOS-boxes: the first contains a short list of parameters and their values (for CATALINA_HOME, CATALINA_BASE, JAVA_HOME, etc.), the second contains various types of error messages. When using an Ant script to start Tomcat from the IDE, the output of the first DOS-box goes to the Output window, while the output of the second DOS-box -- because it has nowhere else to go -- opens in a separate DOS-box. To circumvent this situation, you can change Tomcat's startup.bat script so that all the output goes to one DOS-box, which means that everything ends up in the Output window and no second DOS-box is opened. In the startup.bat script, there's this line right at the end:

call "%EXECUTABLE%" start %CMD_LINE_ARGS%

Change the word "start" to "run". That's it. Now everything will work the way you want it to. I've successfully tested this on Tomcat 4 and Tomcat 5.5.7.


May 04 2005, 12:09:11 AM PDT Permalink

20050503 Tuesday May 03, 2005

Generate TLDs in NetBeans IDE 4.1 with XDoclet

Follow the Java trail in the NetBeans IDE 4.1 Tag Library Tutorial. At the end of the Java trail, you should have two taghandlers (i.e., written in Java, not in JSP syntax) called DisplayLogoTag.java and FormatTextTag.java, together with a tag library descriptor (TLD) file called SimpleTagLib.tld. That's how the tutorial ends, more or less, but in real life your tag handlers often change and evolve so that it can be time-consuming and boring to manually re-synchronize them with the TLD. Hence, to automate the process, we can use XDoclet from within the IDE to re-generate the TLD after changes have been made to the tag handlers. We can also add a menu item to our IDE project (but only if our IDE project is a free-form project) so that we can easily invoke the re-generation process.

Fast Track: Too lazy to create the application yourself? Or maybe you just want to see how TLD re-generation works, without creating the application. Either way, you can download the application here. And then open it in the IDE and take all the steps below to quickly set up the re-generation functionality.

  1. Download and extract XDoclet 1.2.2.

  2. Add XDoclet tags to the DisplayLogoTag.java tag handler.

    • Add this in the comment block directly above the class declaration (i.e., NOT above the package declaration):

      * @jsp.tag name="DisplayLogoTag" bodycontent="empty"
    • Add this in the comment block directly above the setSize method:

      * @jsp.attribute name="size" required="true" rtexprvalue="true" type="java.lang.String"
  3. Add XDoclet tags to the FormatTextTag.java tag handler.

    • Add this in the comment block directly above the class declaration (i.e., NOT above the package declaration):

      * @jsp.tag name="FormatTextTag" bodycontent="scriptless"
    • Add this in the comment block directly above the setStyle method:

      * @jsp.attribute name="style" required="true" rtexprvalue="true" type="java.lang.String"
  4. Create the webdoclet-based target in your project's build.xml file.

     <target name="webdoclet-tld" depends="init, init-xdoclet" 
         description="Regenerate TLD">
    
          <echo>+---------------------------------------------------+</echo>
          <echo>|                                                   |</echo>
          <echo>| C R E A T I N G    T L D                          |</echo>
          <echo>|                                                   |</echo>
          <echo>+---------------------------------------------------+</echo>
    
          <webdoclet destdir="${web.docbase.dir}/WEB-INF"            
                   excludedtags="@author"         
                        verbose="false">
                
              <fileset dir="${src.dir}">
                  <include name="**/*Tag.java"/>
              </fileset>
    
              <jsptaglib Jspversion="2.0" taglibversion="1.0" shortName="simpletaglib"
                            uri="http://netbeans.org/tlds/SimpleTagLib" 
                            filename="SimpleTagLib.tld" validateXML="true"/>
    
         </webdoclet>
    
     </target>

    If you add this target to a different Ant file, you can import it into your project's own build.xml file by including this line at the top of your build.xml file:

    <import file="path-to-external-Ant-file/name-of-Ant-file.xml"/>
  5. Optionally, you can set up a contextual menu for the project to invoke this target. But this is possible for free-form projects only. Alternatively, maybe you want to create a target that re-generates all the TLDs in all the projects in the IDE. In that case, you can add a menu item or toolbar button to the IDE itself (not to an individual project) to invoke the target.
  6. Now, when you invoke the webdoclet-tld target, Ant picks up all the information from the XDoclet tags that you've defined in the tag handlers. If, for example, you change a tag handler so that its body content is "scriptless" instead of "empty", you need only change the bodycontent value of the @jsp.tag XDoclet tag, run the webdoclet-tld target, and the TLD is re-generated with the updated information.

Note that XDoclet currently does not support JSP 2.0. Therefore, if your tag handlers extend SimpleTagSupport, XDoclet will not generate the TLD file correctly. Therefore, just before you invoke the target, change the SimpleTagSupport to TagSupport, and then change it back after you've finished re-generating the TLD.

May 03 2005, 12:14:41 AM PDT Permalink

20050502 Monday May 02, 2005

Quickly Unclutter the NetBeans IDE Source Editor

Sometimes the Source Editor gets really cluttered with open files. Instead of individually closing the files you don't want, by clicking the little cross on the right side of each file's tab at the top of the Source Editor, you can just right-click the tab for the file that you do want to keep... and then choose Close Other Documents:

When you do this, all other files are closed and only the one that is currently selected remains open. That saves you a lot of clicking.

May 02 2005, 06:28:53 AM PDT Permalink

20050501 Sunday May 01, 2005

Drag-and-Drop Deployment to JBoss in NetBeans IDE 4.1

In a previous blog entry (Efficient Deployment via Ant Scripts from NetBeans IDE 4.1), I suddenly understood what deployment really means, thanks to my having to write an Ant script for this purpose:

 <target name="jboss-deploy" description="Deploy to JBoss">
      <copy file="${dist.dir}/${jar.name}" todir="${jboss.deploy.dir}"/>
 </target>

Since this target clearly does nothing more than copy an archive file (EAR, WAR, or EJB-JAR) to the server's deployment directory, it is 100% identical to manually copying the archive file from the directory where it is created to the directory from which the server can deploy it. Were it not for the fact that manually digging through the filesystem to find those directories is more cumbersome than creating the Ant script that does the same thing automatically, there'd be no point in writing the Ant script in the first place. But what if there were an easy way of finding those directories, from inside the IDE? Ah, but there is... thanks to the Favorites window! Three simple steps to set everything up:

  1. Click Ctrl-3 in the IDE and the Favorites window appears.
  2. Drag the window to the right side of the Source Editor.
  3. Right-click the Favorites node, which is the highest node in the window, select Add to Favorites, and browse to the JBoss server's deploy directory.

The result of the above steps should be the following (click to enlarge):

And now, after you've built the application, you can just drag the archive file from the Files window, across the Source Editor, and into the server's deploy directory. That's it! Mission accomplished -- you've deployed your application! (Note that the Source Editor has a cool vertical split-screen in the illustration above.)

It's a good idea to only start the server after you've dragged the archive file into the server's deployment directory. This way, you get the benefit of any application-related error messages that the server outputs into the IDE's Output window during start up. For example, if you start the JBoss server after you've dragged the application into it's deployment directory, the JBoss server will produce errors during start up when it can't find a jboss-web.xml file, if needed, or if a database server hasn't been started up.

Another cool thing that results from this setup is that undeployment is as simple as right-clicking an archive file in the Favorites window and choosing Delete. On top of that, any text files in the Favorites window can be opened in the Source Editor. This is particularly useful for XML files, because you can then use the Source Editor's XML validation. This is even more particularly useful when you're setting up a connection pool on JBoss, because the data source is defined in an XML file which you not only have to dig through the filesystem to locate, but -- if you open the file in a plain text editor -- which you also cannot validate. So far, though, the most significant advantage I've found to this way of working is that all the files I need are really accessible and everything is snugly integrated in the IDE -- without even a single Ant script.


Question of the Day. Charles Ditzel asks: "Could I add a menu item to kick off dbVisualizer from NetBeans?"

Answer: Of course. Any application that can be started by an operating system can be started by Ant. That's because Ant can call an application's executable. For example, an Ant script for starting dbVisualizer goes as follows:

  <target name="Start-App-DbVisualizer">
    <exec executable="C:\Program Files\DbVisualizer-4.2.2\dbvis.exe" />
  </target> 

FYI, here are all my IDE-wide targets: ide-wide-targets.xml

After you create the Ant target for starting the dbVisualizer, right-click the target's node in the Files window, and choose Create Shortcut. Then you can add a menu item, toolbar button, or shortcut key which you can use to invoke the dbVisualizer.


May 01 2005, 06:21:56 AM PDT Permalink

20050429 Friday April 29, 2005

Save Demi Moore in NetBeans IDE 4.1!

Let's say you've extended the IDE by creating a new menu, toolbar, menu item, or toolbar button. For example, you've created menu items that are hooked up to Ant scripts that start and stop JBoss, Tomcat 4, WebSphere, and JRun from the IDE. Or you've customized your window layouts and editor settings. Or maybe you've added a module to the IDE. Or maybe you've added some other customization... such as the Demi Moore macro. What happens when you delete your user directory without saving it beforehand? You lose all your customizations. So you've got to remember that you have, for example, hooked an Ant script to the menu bar and that therefore you've got to save the user directory before you delete it. Normally, you delete the user directory long after you've customized the IDE. Hence, you might forget that you've customized it, that you're very comfortable with the customizations, and that you therefore need to save the user directory in order to maintain your customizations. Wouldn't it be cool if you could save the user directory very quickly right after you've added some new customization? Wouldn't it be cool if you could click a menu item or button or keyboard shortcut from inside the IDE, and save your user directory to some safe place in your filesystem, seconds after customizing the IDE in some way? Well, you can. And it's really easy. Add this to the build.xml file:

  <target name="save-demi-moore" description="Save Demi Moore">
     <copy  todir="c:\DemiMoore\config">
           <fileset dir="C:\Documents and Settings\${user.name}\.netbeans\${release.number}\config"/>
     </copy>
  </target>

You'd need to set the user.name property and release.number property somewhere in a properties file and then, if either the user name or release number change, you'd only have to change the two properties to update the script. Note that this script copies everything in the user directory's config directory to a directory called c:\DemiMoore\config. Therefore, if you don't want to copy everything, you can append an excludes attribute to the dir attribute and then list all the directories or files you'd like to exclude. (Examples of the use of the excludes attribute can be found elsewhere in this blog.)

Then expand the build.xml node in the IDE (it's in the Files window if you're using a standard project and in the Projects window if you're using a free-form project). You'll see that each target has its own subnode within the build.xml node. Right-click the save-demi-moore node, choose Create Shortcut, and add the save-demi-moore target as a menu item, toolbar button, or keyboard shortcut. You could, for example, add it in the File menu. (When you add an Ant script to a menu, it is placed at the top of the list of menu items within the menu. To re-order the menu items, go to Tools > Options, then expand IDE Configuration, Look and Feel, and right-click Menu Bar or any of its nodes and then choose Reorder.) This is where I've placed it:

Now, whenever you customize the IDE in any way, you can copy all the configurations in one click to a safe place. And then, when you delete your user directory and the IDE creates a new one for you, you can reverse the procedure. If you create an Ant script to copy everything back to the user directory, and hook it up to a menu item in the IDE, you won't be able to use that menu item the first time you have a fresh and clean user directory, of course, because the menu item won't be in your clean user directory.

Therefore, why not add both the "Save Demi Moore" and the "Revert Demi Moore" Ant scripts to the IDE-wide-targets.xml file. (This is a build.xml file that I've been building over the past blog entries. It contains IDE-wide targets, such as starting and stopping JBoss, opening StarOffice in the IDE, and other things that are not related to a particular project. A target that relates to a particular project is, for example, one that deploys a particular project to a particular server. See previous blog entries for details on this subject.) This means that when I have a fresh user directory, I just need to open this IDE-wide-targets.xml file in the IDE as a free-form project, run the "Revert Demi Moore" script, and all my saved customizations are back in the user directory.

However, I then need to restart the IDE, so that it can load the new menus, menu items, etc. (And then I need to re-order the menus, because new menus end up on the extreme left of the menu bar, irrespective of where I've ordered them before. This means that the order of menus is not saved in my user directory, which is probably a bug.) Still, that's probably less work than digging through all my directories to find the place where I need to paste the saved config directory. And, on top of that, it's just a lot more fun to do it from the IDE, using an Ant script I made myself.

Apr 29 2005, 12:09:30 AM PDT Permalink

20050428 Thursday April 28, 2005

Kickstart Wicket in NetBeans IDE 4.1

"Wicket is a new Java web application framework that takes simplicity, separation of concerns and ease of development to a whole new level. Wicket pages can be mocked up, previewed and later revised using standard WYSIWYG HTML design tools. Dynamic content processing and form handling is all handled in Java code using a first-class component model backed by POJO data beans that can easily be persisted using your favourite technology." (From What is Wicket?.)

  1. Download and unzip the wicket-kickstart-1.0 sources.
  2. In the IDE, click Ctrl-Shift-N. (Or choose File > New Project.) The New Project Wizard appears. Choose Web and then Web Application with Existing Sources:

  3. Click Next and browse to the location where you unzipped the Wicket Kickstart sources. Select the "wicket-kickstart-1.0" folder for the Location field. Name the project "Kickstart". Click Next and Finish.

From a traditional NetBeans IDE (or probably any other) perspective, the structure of the kickstart project is novel, because the HTML file is located within the source package:

However, the reason for this is that everything in a wicket project is nothing more than a component and, in Tim Boudreau's words, "both the HTML and Java code needed are as simple as the things they're supposed to do". He also says that Wicket is "possibly the cleanest solution to POJO web apps that I've seen at release candidate phase". Matt Raible also has some cool things to say about Wicket here. One thing I personally like about Wicket's philosophy is that it "does not introduce any special syntax to HTML. Instead, it extends HTML in a standards-compliant way via a Wicket namespace that is fully compliant with the XHTML standard." They cite JSP as being "by far the worst offender, allowing the embedding of Java code directly in web pages", while most other frameworks "introduce some kind of special syntax to your HTML code". No special syntax! Wow. This is cool, in Wicket's own words, because special syntax "changes the nature of HTML from the kind of pure-and-simple HTML markup that web designers are familiar with, to some kind of special HTML. This special HTML can be more difficult to preview, edit and understand".

Now that you've got the Kickstart up and running (all you need to do to run the Kickstart application is right-click the project and choose Run Project), you should check out the Wicket Stuff site, which "provides additional Java web components to the core components supplied by the Wicket framework". The additional components include Spring, Hibernate, and Groovy. Or check out their cool samples.

Apr 28 2005, 12:19:52 AM PDT Permalink

20050427 Wednesday April 27, 2005

Easy JBoss Connection Pooling with NetBeans IDE 4.1 and XDoclet

Create the application described in the "Use a Java Class to Access the Data Source" section in the Connecting to Databases chapter of "Using NetBeans IDE 4.1", which is currently a work in progress. It consists of a Java class that accesses one of the default PointBase databases and displays two fields ("name" and "city") on a JSP page.

Fast Track: Too lazy to create the application yourself? Or maybe you just want to see how JBoss connection pooling works with NetBeans IDE 4.1. Either way, you can download the application here. And then open it in the IDE and take all the steps below to quickly set up JBoss connection pooling.

Note that the org.me.customer.CustomerDAO class accesses the database via a data source called jdbc/poolDB. It uses this code to make the connection to the data source:

private javax.sql.DataSource getPoolDB() throws javax.naming.NamingException {
        javax.naming.Context c = new javax.naming.InitialContext();
        return (javax.sql.DataSource) c.lookup("java:comp/env/jdbc/poolDB");
}

(The code above was generated by the IDE, as described in this blog entry.)

The jdbc/poolDB data source referred to in the code above doesn't exist yet. In the Connecting to Databases chapter you can see how to set up a data source on the Sun Java System Application Server and on the Tomcat Web Server, since both servers are officially supported in NetBeans IDE 4.1.

In the following steps, you will create a data source on the JBoss server and access it from NetBeans IDE 4.1.

  1. Create the data source. In the JBoss installation directory, go to docs/examples/jca and copy pointbase-ds.xml to your JBoss deployment directory. (The pointbase-ds.xml file, along with many other database-server-specific templates, is kindly provided by the JBoss folks.) For this example, the JBoss deployment directory is its autodeploy directory: server/default/deploy. Now customize the pointbase-ds.xml template so that it looks as follows:

    <datasources>
       <local-tx-datasource>
          <jndi-name>jdbc/poolDB</jndi-name>
          <use-java-context>false</use-java-context>
          <connection-url>jdbc:pointbase://localhost:9092/sample</connection-url>
          <driver-class>com.pointbase.jdbc.jdbcUniversalDriver</driver-class>
          <user-name>pbpublic</user-name>
          <password>pbpublic</password>
          <metadata><type-mapping>PointBase</type-mapping></metadata>
      </local-tx-datasource>
    </datasources>

    That's it. You now have a JBoss data source. It is called jdbc/poolDB and provides access to the Sample database that is part of the default PointBase installation.

  2. Set the JBoss Port Number. Make sure that you like JBoss's port number. The default port number is 8080. This may conflict with other port numbers (especially if you have been playing with multiple servers). Therefore, go here in the JBoss installation directory and take a look:

    \server\default\deploy\jbossweb-tomcat50.sar\server.xml

    Just search for "8080" in the file and change it to something else.

  3. Make the PointBase database driver available to JBoss. Do this by copying pbclient from the PointBase installation directory to the lib directory within the JBoss server's installation directory. For the JBoss autodeploy directory, the applicable lib directory is server/default/lib.
  4. Get Ant targets for starting and stopping JBoss from the IDE. Download servers-build.xml and servers-build.properties from Brian Leonard's article Integrating NetBeans with other J2EE Server Vendors. Put them in the same directory as where your application's build.xml file is found. Now open the build.xml file and add the following import statement after the existing import statement:

    <import file="servers-build.xml"/>
  5. Start JBoss from the IDE. Modify the jboss.home property in servers-build.properties and run the jboss-start target. Run it from inside the IDE (you can create a menu item, toolbar button, or shortcut key for it, as described in earlier blog entries). JBoss starts up and the Output window displays output received from JBoss. You'll see a lot of output and it might take a while. Somewhere near the end you should see something similar to the following (truncated here for easier reading):

    12:34:14,906 INFO  [WrapperDataSourceService] 
    Bound connection factory for resource adapter 
    for ConnectionManager 'jboss.jca:service=DataSourceBinding,name=jdbc/poolDB 
    to JNDI name 'jdbc/poolDB'

    This means that the jdbc/poolDB is available and that you can access it.

  6. Build the project to the JBoss autodeploy directory. Right-click the project in the Projects window to build it. (You can also build it in the Files window -- choose File > Set Main Project, set the current project as the main project, and click F11 whenever you want to build.) Modify the jboss-deploy target in servers-build.xml so that war.name is used instead of jar.name. Now run the jboss-deploy target. (If you haven't built the project, you'll get errors because the WAR file that the target tries to copy to the JBoss deployment directory hasn't been built yet.) This copies the application's WAR file to the JBoss autodeploy directory. In the Output window you should see something similar to the following:

    Copying 1 file to C:\jboss\jboss-4.0.1sp1\server\default\deploy
    BUILD SUCCESSFUL (total time: 0 seconds)

    Now that the application is in the JBoss autodeploy directory, JBoss deploys it automatically...

  7. Start PointBase. In the IDE, choose Tools > Pointbase Database > Start Local Pointbase Database.
  8. Access the application's URL. Modify the jboss.client.url property in servers-build.properties so that it points to the JBoss port number and so that the name of the project replaces the name used by Brian's project. Now run the jboss-runapp target. This opens the IDE's default browser and displays the application as found in the JBoss autodeploy directory. You will get a 404 error message in your browser, with this helpful description: "The requested resource (/CustomerNameAndCity) is not available." Back in the IDE, run the jboss-stop target. Once JBoss has stopped, run the jboss-start target again. JBoss will produce this error in the Output window (truncated here for readability):

    javax.naming.NamingException: resource-ref: 
    jdbc/poolDB has no valid JNDI binding. 
    Check the jboss-web/resource-ref.

    This means... we need a jboss-web.xml file, containing a <resource-ref> section. We will use XDoclet to generate the file as well as its <resource-ref> section.

This is how to add XDoclet to the mix:

  1. Get XDoclet. Download and extract XDoclet 1.2.2.
  2. Get the Ant target for creating the jboss-web.xml file from the IDE. Download xdoclet-web-build and xdoclet-web-build.properties from Brian Leonard's article Integrating NetBeans with other J2EE Server Vendors. Put them in the same directory as where your application's build.xml file is found. Open the xdoclet-web-build.properties file and make sure the xdoclet.root.dir property is set appropriately. Now open build.xml and add the following import statement after the existing import statement:

    <import file="xdoclet-web-build.xml"/>
  3. Add @jboss comments to a Java class. In org.me.customer.CustomerDAO, add the following as comments right above the class declaration:

    *  @jboss.resource-ref  res-ref-name="jdbc/poolDB"
    *                       jndi-name="jdbc/poolDB"

    This must be in the right place -- i.e., not in the comments right at the top of the file, but in the comments that come in between the import statements and the class declaration.

  4. Customize and run the target from the IDE. Modify the webdoclet target in xdoclet-web-build.xml so that the fileset looks as follows:

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

    Now run the webdoclet target. You'll see a nice big comment block in the Output window (thanks to Demi Moore) and when you go to your application's web/WEB-INF directory you will see your brand new jboss-web.xml file. This file contains the following important tags:

    <resource-ref>
      <res-ref-name>jdbc/poolDB</res-ref-name>
      <jndi-name>jdbc/poolDB</jndi-name>
    </resource-ref>
  5. Redeploy, including the jboss-web.xml file. Now build the project again, run the jboss-deploy target to deploy the WAR file to the JBoss deployment directory, and then run the jboss_runapp target again. You will now see the PointBase Sample database's "name" and "city" fields displayed in the IDE's default browser:

Apr 27 2005, 06:46:07 AM PDT Permalink

20050426 Tuesday April 26, 2005

Lucky You: No Deleting of Projects in NetBeans IDE 4.1

One cool thing about blogs.sun.com is that I can see exactly what someone typed in a search engine to get to my blog. So, what happens is, someone types in a query in a search engine (mostly Google, but many others too), a long list of links is returned, one of which is the one to my blog. When someone then clicks on the link to my blog, the query that they typed in is recorded somewhere in the settings pages of my blog. Cool, huh? (Check out Roumen's blog entry today, about Javadoc Support in NetBeans, which he wrote because several people found his blog after querying "javadoc NetBeans".)

Over the last few days I've been collecting the queries randomly, every once in a while, and here they are, just to show what kind of things people seem to be looking for:

One interesting thing is that every other day or so, there are people out there typing variations on "How do I delete a NetBeans IDE project?" (The capitalization of the "NETBEANS DELETE A PROJECT" in the list above is probably a reflection of the querier's frustration at not being able to work out how to delete a project in the IDE!) Well, you can't delete a project in the IDE (you can close it, however). As far as I'm aware, the reason for that is that a user might think that by deleting a project, they're only deleting it from the IDE (i.e., they might think they're just closing the project). Imagine the scene: After months and months of work your project is pretty much complete and you think to yourself: "Hmmm. I don't need this in my IDE anymore." So, you right-click the project and choose "Delete Project". You get a warning that says "Are you sure you want to do this?" and you think: "Yeah, sure. I don't need this in the IDE anymore." So you click OK and you've deleted your project from the file system! Lucky you -- this scenario will never happen in NetBeans IDE 4.1. If you want to delete your project, you'll have to do it in the file system. And you won't be able to blame NetBeans IDE when you do so accidentally...

Apr 26 2005, 02:54:07 AM PDT Permalink

20050425 Monday April 25, 2005

NetBeans IDE 4.1: No More Worrying About Database Connections

In one of my first blog entries (Servlet Connection Pooling for Dummies), I explained the cool NetBeans IDE feature that allows you to see a JSP's translated servlet after it has been deployed. It has to be deployed first before you can see it, because the servlet that you see is translated by the server, hence it needs to be there before it can be translated. In that blog entry I mentioned that one way to connect to a database from a servlet is to simply copy the translated servlet and use that. However, as also pointed out in that blog entry, the disadvantage of that method is that each server translates a JSP page differently, so that you'd end up with an application that isn't really portable. On top of that, it seems like the server generates a lot of garbage and, as a result, I've found the translated servlet quite hard to read.

I've been generally quite daunted by servlets -- and Java source files in general -- and have tended to stick to JSP pages. So, I was pleasantly surprised this morning, while updating the Connecting to Databases chapter for the Using NetBeans IDE 4.1 Guide, to discover that the IDE can generate a whole bunch of code for you -- specifically, the code that makes the connection with the database -- which is not only helpful in general but also means that there's at least one part of my code that I can rely on as being correct. When debugging my Java source code in the past, it was often hard to know exactly where a problem originated. Now, when I'm creating Java source files that access a database, I can be sure that at least the database connection is not the place where things have gone haywire.

And it's all made possible with this dialog box:

It appears when I right-click anywhere in the body of a class in the Source Editor and choose Enterprise Resources > Use Database. After specifying the JNDI name of the data source, together with the connection URL, I click OK and get this:

   private javax.sql.DataSource getPoolDB() throws javax.naming.NamingException {
        javax.naming.Context c = new javax.naming.InitialContext();
        return (javax.sql.DataSource) c.lookup("java:comp/env/jdbc/poolDB");
    }

Before, when I had to come up with the code for the database connection myself, I'd always be thinking, "Now is this the correct format for my connection string?" But now the IDE does all that thinking for me. And the IDE also gives me this in my in my web.xml file:

  <resource-ref>
    <description>jdbc:pointbase://localhost:9092/sample [pbpublic on PBPUBLIC]</description>
    <res-ref-name>jdbc/poolDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>

...and this in my sun-web.xml file (but nothing in context.xml in 4.1 -- it's a known issue):

  <resource-ref>
    <res-ref-name>jdbc/poolDB</res-ref-name>
    <jndi-name>jdbc/poolDB</jndi-name>
  </resource-ref>

So now, instead of thinking about the connection, I'm left thinking about how to use it. Pretty cool.

Apr 25 2005, 05:55:56 AM PDT Permalink