Tuesday, September 22, 2009

Formatting dates in Java

In this post I will write about formatting dates in Java in different formats (patterns).

The Java classes that I will be using in the examples are:

The class Date represents a specific instant in time, with millisecond precision.

Format is an abstract base class for formatting locale-sensitive information such as dates, messages, and numbers.

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

A pattern of special characters is used to specify the format of the date. This example demonstrates some of the characters. For a complete listing, see the javadoc documentation for the SimpleDateFormat class.

Note: default locale (which, in the author's case) is Locale.ENGLISH. If the example is run in a different locale, the text (e.g., month names) will not be there: This example formats dates using the  same.

   

    Format formatter; 
   
    // The year
    formatter = new SimpleDateFormat("yy");    // 02
    formatter = new SimpleDateFormat("yyyy");  // 2002
   
    // The month
    formatter = new SimpleDateFormat("M");     // 1
    formatter = new SimpleDateFormat("MM");    // 01
    formatter = new SimpleDateFormat("MMM");   // Jan
    formatter = new SimpleDateFormat("MMMM");  // January
   
    // The day
    formatter = new SimpleDateFormat("d");     // 9
    formatter = new SimpleDateFormat("dd");    // 09
   
    // The day in week
    formatter = new SimpleDateFormat("E");     // Wed
    formatter = new SimpleDateFormat("EEEE");  // Wednesday
   
    // Get today's date
    Date date = new Date();
   
    // Some examples
    formatter = new SimpleDateFormat("MM/dd/yy");
    String s = formatter.format(date);
    // 01/09/02
   
    formatter = new SimpleDateFormat("dd-MMM-yy");
    s = formatter.format(date);
    // 29-Jan-02

Thursday, September 17, 2009

How to change the HTTP port for Oracle 10g XE

Oracle XE uses the embedded http listener that comes with the XML DB (XDB) to serve http requests. The default port for HTTP access is 8080.

It also supports the WebDAV protocol just as well as FTP.

You can determine the current configuration using the following commands when you connect to XE as the oracle user SYSTEM (or any other DBA):

C:\WINDOWS\system32>sqlplus system@xe

SQL*Plus: Release 10.2.0.1.0 - Production on Wed Aug 26 00:19:35 2009

Copyright (c) 1982, 2004, Oracle. All rights reserved.

Enter password:

Connected to:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production

SQL> -- get current status
SQL> select dbms_xdb.gethttpport as "HTTP-Port"
, dbms_xdb.getftpport as "FTP-Port" from dual;

HTTP-Port  FTP-Port
---------- ----------
8080
       0


You can change the http port and the ftp port to whatever you like (keep in mind that you need special privileges for ports < 1024 on Unix/Linux systems.)

SQL> -- set http port and ftp port
SQL> begin
2 dbms_xdb.sethttpport('80');
3 dbms_xdb.setftpport('2100');
4 end;
5 /

PL/SQL procedure successfully completed.

SQL> select dbms_xdb.gethttpport as "HTTP-Port"
, dbms_xdb.getftpport as "FTP-Port" from dual;

HTTP-Port  FTP-Port
---------- ----------
80
          2100


If you only want to use the database without allowing access via http or ftp then you can disable both:

SQL> -- disable http and ftp access
SQL> begin
2 dbms_xdb.sethttpport('0');
3 dbms_xdb.setftpport('0');
4 end;
5 /

PL/SQL procedure successfully completed.

SQL> -- get current status
SQL> select dbms_xdb.gethttpport as "HTTP-Port"
, dbms_xdb.getftpport as "FTP-Port" from dual;

HTTP-Port  FTP-Port
---------- ----------
0
           0


Alternatively, after logging in into sqlplus from command prompt, execute the following:

SQL> EXEC DBMS_XDB.SETHTTPPORT(8081);

the HTTP port will be changed to 8081 from the default port number 8080

Thursday, September 3, 2009

Import *.dmp file into Oracle 10g XE on Linux (Ubuntu)

Recently I installed Oracle 10g XE on my laptop on Windows Vista and was happy to see the same was available for Linux (Ubuntu). I downloaded the Linux version of Oracle 10g XE and installed it on Ubuntu. Installation is a straight forward process, just double click on the file and make sure you see the details before closing the installation window. You will need to run a command with "root" privileges to complete the Oracle installation. The command is as bellow:

sudo /etc/init.d/oracle-xe configure

You will be asked for Oracle configuration: port numbers and the password for the "system" and "sys" accounts.

After the installation you will want to import the data into Oracle from the .dmp file. Doing so on Windows is easy using Toad. The Linux equivalent for Toad, Tora for some reasons does not wants to connect to Oracle on Ubuntu and does not comes with an option to select the database as Oracle. You may try the other free softwares: SQL Developer from Oracle and Navicat 8 Lite for Oracle.

For importing the .dmp file into Oracle make sure the user with which you have logged in is a member of the "dba" group. Make the appropriate changes using the User and Groups from the Settings. Once this is done you can use the "imp" executable to import the .dmp file. The general syntax is as follows:

imp userid=system file=user_a.dmp log=imp.log fromuser=a touser=b
Click here for a detailed description on importing data into Oracle from a .dmp file.
Example for using the "imp" utility:

./imp userid=a file=/home/abc/dag.DMP log=/home/abc/exp_dag.log fromuser=b touser=a

abc = Linux user
a = Oracle schema (user) into which the data is to be imported
b = Oracle schema(user) from which the backup was taken.

Make sure to change your directory to

/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin

before you run the "imp" utility. If everything goes right then all the data will be imported into Oracle 10g XE.

Tuesday, September 1, 2009

Using CVSNT with Eclipse

My previous post was about using Version Control System in Eclipse and it also described CSV (Concurrent Version System). In this post I'll describe how CSVNT can be used with Eclipse for version control.
CVSNT is a version control system. Using it, you can record the history of your source files. With CVSNT, you can easily retrieve old versions to see exactly which change caused the bug. This can sometimes be a big help. You could of course save every version of every file you have ever created. This would however waste an enormous amount of disk space. CVSNT stores all the versions of a file in a single file in a clever way that only stores the differences between versions.

CVSNT started out as a bunch of shell scripts written by Dick Grune, posted to the newsgroup comp.sources.unix in the volume 6 release of December, 1986. While no actual code from these shell scripts is present in the current version of CVSNT much of the CVSNT conflict resolution algorithms come from them.

CVSNT can be downloaded from here and it is available as both free edition and paid CVS Suite.
Once you have downloaded CVSNT you can follow the steps below.

Setting up CVSNT
1. Download the latest version (2.0.58d) of CVSNT from www.cvsnt.org
2. Install it using the default options offered by the installer. You may have to reboot.
3. CVSNT should be installed in a folder of the same name under Start...Programs. Start the Service control panel application.
4. On the Service Status tab, make sure both services are running.
5. On the Compatibilty tab, turn on all three checkboxes for the "Generic non-cvsnt" client. (This instruction comes from the CVSNT Release Notes.)
6. From the Repositories tab click "Add" to create a new repository.
       a) You will need to select a directory for your repository. I suggest you create a new directory dedicated to this repository.
       b) After you select a directory location, CVSNT will automatically fill in the Name field. Do not change the name! Eclipse is expecting the repository location as a *NIX format directory, so CVSNT fakes it out using the name. Copy the name to a text editor so you can put it in Eclipse exactly as defined in CVSNT.
       c) Click OK to create the repository.

Setting up Eclipse

1. Open the CVS Repository Perspective (Window...Open Perspective...Other...CVS Repository Exploring).
2. With a right-click in the CVS Repositories pane, select New...Repository Location from the context menu. There's also a button at the top of the pane, but those button icons take some getting used to. They do have descriptive popups if you hover your mouse over them, though.
3. In the Add CVS Repository window that pops up, insert the following info, without the quotes. 
     a) Host: "127.0.0.1" (a standard TCP/IP self-reference)
     b) Repository path: Paste the repository name you copied from CVSNT.
     c) User/Password: Your Windows username and password (password optional.)
     d) Connection Type: "pserver"
     e) Use Default Port: Yes
     f) Validate connection on Finish: Yes
     g) Save Password: Up to you.
     h) Click Finish.

With the Validate connection on Finish option turned on, Eclipse will tell you whether or not it was able to connect to the repository. If not, it gives you the option of going back to the setup screen to change settings. If it can connect, the repository will will appear in the list of CVS Repositories.

If your Windows account requires a password, Eclipse will prompt you for it if you didn't provide it. If you don't save the password, Eclipse will prompt for it whenever it opens the repository.

Using Version Control System in Eclipse

Version Control System is an important component of Source Configuration Management (SCM). Using it, you can record the history of sources files, and documents. One such utility which Eclipse supports out of the box is CSV (Concurrent Versions System).
Most of today's applications are developed by a team of people. Even small projects that involve only a few developers require tight control over changes in source code. This is a task for source-code management software. Two core features must be supported by source-code version-control software:
  • A way to coordinate and integrate changes into the source code
  • A history of the work submitted by the team
As team members produce new work, they share this work by committing those changes to the repository. Similarly, when they wish to get the latest available work, they update their local workspaces to the changes on the repository. This means the project repository is constantly changing as team members submit new work. In the other words, the repository should represent the current state of the project. At any point, team members can update their workspaces from the repository and know they are up to date.

Maintaining history is also important so you can compare the current work against previous versions, and if needed, revert to the older work. Coordinating the team's work so that there is only one definition of the current project state, and containing the integrated work of the team are also essential to managing version control. This coordination is probably the hardest goal to achieve.

An optimal model is one where any member of the team can make changes to any resource he has access to. Because two team members can commit changes to the same resource, conflicts can occur and must be dealt with. This model assumes that conflicts are rather unique. Unfortunately, no source code exists in isolation; it typically contains implicit or explicit dependencies on other resources. Source code has references to artifacts described in other source code resources. And this is the point where source-code management software ends its work because it isn't a substitute for project management. Project managers must do their work: coordinating others' work, and looking after schedules, project phases, and release dates. Further, source-code management is not a substitute for developer communication.


How the Eclipse Platform supports code management

The Eclipse Platform offers the capability to share code and work as a team on a software project. Eclipse supports a wide range of code-management solutions, thanks to its plug-in architecture (however, CVS support comes out of box). The focal point of the Eclipse Platform architecture is the workspace. The workspace maintains everything necessary for building and testing a software project. It contains the objects (source code and resources). It also holds the configuration settings for the project, the IDE, and the plug-ins. The workspace is locally maintained on a developer's machine, and teams collaborate through external repositories that are meeting places for code parts from different developers. Repositories are accessible through the client-server architecture via the Internet.

The Eclipse Platform offers support for team development operations directly from the workspace. This support allows a developer to concurrently interact with several separate repositories and versions of code or projects. Resources within the workspace allow the team support component to deal with version and configuration management issues. Of course, single workspaces can access different types of repositories simultaneously. The Eclipse Platform does not provide its own code-management solution; it always depends on external systems. The Eclipse Platform has built-in support for only one (albeit the most popular) source-code management system: Concurrent Versions System (CVS).

What is CVS?

CVS started out as a collection of shell scripts in 1986, but it has evolved into the most popular source code version management solution for software developers. CVS is the open source client/server solution for code versioning and is available for variety of platforms, including Linux® and Windows® NT/2000/XP. See Resources for download links for CVS clients, servers, and source code.

In general, the primary function of CVS is to record the history of source files. When a group of developers work on the same project, CVS insulates them from each other. Every developer works separately, in his own directory and merges (from time to time) the results of the work with the CVS repository.

Eclipse has a built-in CVS client that is deeply integrated with the Eclipse Platform IDE, which is implemented as a separate perspective (CVS Repository Exploring Perspective) for interacting with CVS. General Eclipse settings for CVS are located under Window > Preferences window > Team. After switching to the CVS Repository Exploring Perspective, all CVS operations become available (go to Window > Open Perspective > Other > CVS Repository Exploring menu).

The first step is to set up a repository location that defines connection parameters for the selected CVS server/repository.

Source-code workflow with Eclipse/CVS

In the CVS team cooperation model, team members do all of their work on their own workbenches, isolated from others. Eventually, they will want to share their work. They do this via CVS repositories. CVS uses a branch model to support multiple courses of work that are isolated from each other, but still highly interdependent. Branches are where a development team shares and integrates ongoing work. A branch can be thought of as a shared workbench that is updated by team members as they make changes to the source code. This model allows individuals to work on a CVS team project, share their work with others as changes are made, and access the work of others as the project evolves.

A special branch, referred to as head, represents the main course of work in the repository (a head is often referred to as the trunk). As resources are committed to the branch, these dependencies can be affected. Ensuring the integrity of the dependencies is important because the branch represents the current project's state. Of course, at any point, a team member could make use of the branch contents as the basis for new work.

Those rules apply not only to the CVS: there are common steps for source-code management within team projects no matter what version-control software is in use. Below is an example workflow that uses Eclipse's built-in support for CVS:

1. Starting a new team project
Each new, empty Eclipse project can be shared through CVS (or any other supported source code management system). Developers can also share their existing code by migrating it to the repository. To do this, use the Team > Share Project option located in the context menu that displays when you click the project main folder.


Another option is to create a new workbench project by importing code from the selected branch of the CVS repository. Simply select the appropriate branch (or head), then select Checkout As Project from the context menu in the CVS Repository Exploring Perspective.

2. Working with code and making changes
Developers work with code locally via the Eclipse workbench, creating new resources, modifying existing ones, writing commentaries, and saving locally as they go.

3. Synchronizing local changes with the CVS repository
If a developer on a project is ready to commit his work, the first step is to perform the update operation. This will check the repository for incoming changes and add them to that developer's local workbench. This ensures that the developer knows the changes that might affect the integrity of what he is about to commit. Compare a local version against code stored in the repository using the Compare With option in the project context menu.
The next step is to resolve any conflict that eventually appears and try to compile the code again. If everything works, then perform the commit operation using the Team > Commit option from the project context menu, as shown below. This will integrate all changes into the repository.

4. Managing repositories
CVS allows developers to isolate changes into separate lines of development, known as branches. When one developer changes files on a branch, those changes do not appear on the main trunk or on other branches. Those branches are named subversions or code forks. Later, moving changes from one branch to another branch (or the main trunk) is performed by the merging operation. Then revisions are committed. This effectively copies the changes onto another branch. Eclipse makes it easy to move between development branches using the Team > Branch option from the project context menu.

Of course, when a development team maintains a large repository, it is necessary to have a control over commit and merge operations within projects. Eclipse/CVS integration offers a special view: the CVS Repository History. It gives a quick preview on changes in the repository performed by team members.

The Eclipse Platform comes with a few utilities that support code management. The most useful is the patch function. It compares code from two sources, such as a local workbench and the repository, then creates a UNIX®-like patch file containing code differences. This file can be sent to developers to upgrade source code to the newest version.
5. Disconnecting a project from CVS
When the project development has ended and the team wants to freeze source code, the final version of the project can be removed from the head repository. Disconnecting a project from CVS disables the repository operations that can be performed on the project and its resources, and optionally removes the CVS information associated with the project.

Disconnecting operations can be performed from the Team > Disconnect option in the project context menu. By choosing this option, the Confirm Disconnect from CVS dialog opens. When the project is disconnected from the repository, the team must then decide what to do with the CVS information. The first option is "Delete the CVS meta information," which disables the CVS team menu operations and removes the CVS folders and their contents from the file system. The second option is to use the "Do not delete the CVS meta information," which disables the CVS team menu operations, but leaves the CVS meta information.


Source of the above post: http://www.ibm.com/developerworks/library/os-ecshare/