Justin Loh\’s Tech Blog

August 12, 2008

Request for Help

Filed under: 1 — jusloh @ 11:24 am

Hi Nilushi,

Unfortunately, I cannot assist you without getting involved immensely in the project and the latter requires a lot of time.  I am also heavily involved in other aspects of my work.  As such, I cannot help you.

However, the fact that you are looking at Hibernate meant that you are in the right direction.  You need to be able to model the domain objects that describe the CV.  For each object, you will have to come up with methods that make use of the object items or fields for storage or presentation (POJO).

If you work on this way, you should be able to model the solution and in time the codes required for your project.

All the best,

Justin Loh

July 3, 2007

Explanation of Method Chaining

Filed under: Object Relational Mapping — jusloh @ 4:38 pm

Method chaining is a programming style supported by
many Hibernate interfaces. This style is more popular in Smalltalk than
in Java and is considered by some people to be less readable and more
difficult to debug than the more accepted Java style. However, it’s convenient
in many cases, such as for the configuration snippets you’ve seen in the following code:

SessionFactory sessionFactory = new Configuration()
.configure("/persistence/auction.cfg.xml")
.setProperty(Environment.DEFAULT_SCHEMA, "CAVEATEMPTOR")
.addResource("auction/CreditCard.hbm.xml")
.buildSessionFactory();

If you do use this coding
style, it’s better to write each method invocation on a different line. Otherwise,
it may be difficult to step through the code in your debugger.

Another more complex example with a method, getEntitleReferenceService(),
that returns an object of type interface, EntitlementsReferenceService, which has
method getUser(String userID). A concrete class of RemoteEntitleReferenceService,
implements the EntitlementsReferenceService interface, thereby enable the use of
getUser(String userID).


boolean b = ( Services
.getInstance()
.getEntitleReferenceService()
.getUser(username)
!= null );

Step by Step Explanation:

Services.getInstance() returns an object of type: Services

On the Services object, I call the method: getEntitleReferenceService()

this method returns an object of type interface: EntitlementsReferenceService
The only class that implements that interface is: RemoteEntitleReferenceService.java

In RemoteEntitleReferenceService.java resides the method: getUser(String userID)

with method signature:

public User getUser(final String userId){}

June 19, 2007

vi or vim Editor Reference

Filed under: Linux — jusloh @ 5:05 am

Cursor control and position

h Left

Editing

j Down
k Up
l (or spacebar) Right
w Forward one word
b Back one word
e End of word
( Beginning of current sentence
) Beginning of next sentence
{ Beginning of current paragraph
} Beginning of next paragraph
[[ Beginning of current section
]] Beginning of next section A Append to end of current line
0 Start of current line i Insert before cursor
$ End of current line I Insert at beginning of line
^ First non-white character of current line o Open line above cursor
+ or RETURN First character of next line O Open line below cursor
First character of previous line ESC End of insert mode
n | character n of current line Ctrl-I Insert a tab
H Top line of current screen Ctrl-T Move to next tab position
M Middle line of current screen Backspace Move back one character
L Last line of current screen Ctrl-U Delete current line
nH n lines after top line of current screen Ctrl-V Quote next character
nL n lines before last line of current screen Ctrl-W Move back one word
Ctrl-F Forward one screen cw Change word
Ctrl-B Back one screen cc Change line
Ctrl-D Down half a screen C Change from current position to end of line
Ctrl-U Up half a screen dd Delete current line
Ctrl-E Display another line at bottom of screen ndd Delete n lines
Ctrl-Y Display another line at top of screen D Delete remainer of line
z RETURN Redraw screen with cursor at top dw Delete word
z . Redraw screen with cursor in middle d} Delete rest of paragraph
z – Redraw screen with cursor at bottom d^ Delete back to start of line
Ctrl-L Redraw screen without re-positioning c/pat Delete up to first occurance of pattern
Ctrl-R Redraw screen without re-positioning dn Delete up to next occurance of pattern
/text Search for text (forwards) dfa Delete up to and including a on current line
/ Repeat forward search dta Delete up to, but not including, a on current line
?text Search for text (backwards) dL Delete up to last line on screen
? Repeat previous search backwards dG Delete to end of file
n Repeat previous search J Join two lines
N Repeat previous search, but it opposite direction p Insert buffer after cursor
/text/+n Go to line n after text P Insert buffer before cursor
?text?-n Go to line n before text rx Replace character with x
% Find match of current parenthesis, brace, or bracket. Rtext Replace text beginning at cursor
Ctrl-G Display line number of cursor s Substitute character
nG Move cursor to line number n ns Substitute n characters
:n Move cursor to line number n S Substitute entire line
G Move to last line in file u Undo last change
    U Restore current line

x Delete current cursor position
X Delete back one character
nX Delete previous n characters
. Repeat last change
~ Reverse case
y Copy current line to new buffer
yy Copy current line
xyy Copy current line into buffer x
Xd Delete and append into buffer x
xp Put contents of buffer x
y]] Copy up to next section heading
ye Copy to end of word
     

File Handling

   
:w Write file



 


 


 

:w! Write file (ignoring warnings)
:w! file Overwrite file (ignoring warnings)
:wq Write file and quit
:q Quit
:q! Quit (even if changes not saved)
:w file Write file as file, leaving original untouched
ZZ Quit, only writing file if changed
😡 Quit, only writing file if changed
:n1,n2w file Write lines n1 to n2 to file
:n1,n2w >> file Append lines n1 to n2 to file
:e file2 Edit file2 (current file becomes alternate file)
:e! Reload file from disk (revert to previous saved version)
:e# Edit alternate file
% Display current filename
# Display alternate filename
:n Edit next file
:n! Edit next file (ignoring warnings)
:n files Specify new list of files
:r file Insert file after cursor
:r !command Run command, and insert output after current line

June 18, 2007

Why my crontab didn’t work?

Filed under: Linux — jusloh @ 5:41 pm

I have finally got my crontab to work and would like to share my few cents worth.

Reasons that could make crontab fail:

Reason 1:
Command line that did not work.

Solution:
Always test them out first on the command prompt before putting the command in the crontab -e.

Reason 2: Your command was too long and it caused a truncation of the command and inadvertently a EOF error in the /bin/sh shell that executes the command.

(e.g.I received these root email message: /bin/sh: -c: line 0: unexpected EOF while looking for matching `”‘
/bin/sh: -c: line 1: syntax error: unexpected end of file)

because I crontab -e the following line:

00 09 * * 1-5 /usr/bin/lynx -dump “http://myserver.web.com/messaging-web/SendSms?UserId=XXXXX &password=XXXXX&text=Daily%20sms%20test%20from%20MyServer&recipientNo=XXXXXXXX”

Note: I have use character ‘X’ to represent confidential numeric or alphanumeric characters. The url given was fictitious but you can make out your long command line to test out what I mean.

Solution: create a simple shell file (say /root/SendSms.sh) that has the long command line like:

#!/bin/sh
/usr/bin/lynx -dump “http://myserver.web.com/messaging-web/SendSms?UserId=XXXXX &password=XXXXX&text=Daily%20sms%20test%20from%20MyServer&recipientNo=XXXXXXXX”

“chmod 777 /root/SendSms.sh” to enable execution of SendSms.sh script file and test out the /root/SendSms.sh script on the command prompt. If it worked on the command prompt, then crontab -e to enter the following line

00 09 * * * /root/SendSms.sh

in crontab.

Reason 3: Testing your crontab scheduled task by scheduling the execution of task after 1 minute or few seconds of crontab -e being saved or installed. Cron usually checked after every minute for scheduled task, so scheduling the task 1 minute or few seconds after exiting crontab -e will not make it execute after 1 minute or few seconds later.

Solution: Schedule the task a few minutes later.

Why Model with UML?

Filed under: UML Modeling — jusloh @ 4:51 pm

Why Model with UML?

A write-up in Netbeans.org focusing on the question “Why should I model?”. The answers are two considerations:

  1. The benefits you can derived from going through the various stages of UML modeling.
  2. A reduction in frustrations for the engineer (could be you who is doing the maintenance after many months down the road) who has to go through thousands of lines of codes. Visual forms gives the maintenance engineer a better idea of the how the various components worked, how they were integrated together by the originator of the codes and how they were deployed.

Tutorials using Netbeans to create the various UML diagrams were provided by links.

June 15, 2007

Scheduling Tasks

Filed under: Linux — jusloh @ 5:16 am

crontab-Linux Command

crontab (cron table)

Schedule a command to run at a later time

SYNTAX     crontab [ -u user ] file     crontab [ -u user ] { -l | -r | -e }

Key  -l  List - display the current crontab entries.

  -r  Remove the current crontab.

  -e  Edit the current crontab using the editor specified by the
      VISUAL or EDITOR environment variables.
      After you exit from the editor, the modified crontab
      will be installed automatically.

Crontab is the program used to install, deinstall or list the tables used to drive the cron daemon in Vixie Cron.
Each user can have their own crontab, and though these are files in /var, they are not intended to be edited directly.

If the -u option is given, it specifies the name of the user whose crontab is to be tweaked. If this option is not given, crontab examines “your” crontab, i.e., the crontab of the person executing the command. Note that su can confuse crontab and that if you are running inside of su you should always use the -u option for safety’s sake.

cron file is used to install a new crontab from some named file or standard input if the pseudo-filename `-‘ is given.

Each line in the cron table follows the following format: 7 fields left to right

Field Meaning
1 Minute (0-59)
2 Hour (2-24)
3 Day of month (1-31)
4 Month (1-12, Jan, Feb, …)
5 Day of week (0-6) 0=Sunday, 1=Monday …
or Sun, Mon, Tue, Wed, Thur, Fri
6 User that the command will run as
7 Command to execute

There are several ways of specifying multiple values in a field:

• The comma (‘,’) operator specifies a list of values, for example: “1,3,4,7,8”
• The dash (‘-‘) operator specifies a range of values, for example: “1-6”, which is equivalent to “1,2,3,4,5,6”
• The asterisk (‘*’) operator specifies all possible values for a field. e.g. every hour or every day.

There is also an operator which some extended versions of cron support, the slash (‘/’) operator, which can be used to skip a given number of values. For example, “*/3” in the hour time field is equivalent to “0,3,6,9,12,15,18,21”; “*” specifies ‘every hour’ but the “/3” means that only the first, fourth, seventh…and such values given by “*” are used.

Cron will email to the user all output of the commands it runs, to silence this, redirect the output to a log file or to /dev/null

Example

Run /usr/bin/somecommand at 12.59 every day and supress the output (redirect to null)

59 12 * * * simon /usr/bin/somecommand >> /dev/null 2>&1

Permissions
If the allow file exists, then you must be listed therein in order to be allowed to use this command. If the allow file does not exist but the deny file does exist, then you must not be listed in the deny file in order to use this command. If neither of these files exists, then depending on site-dependent configuration parameters, only the super user will be allowed to use this command, or all users will be able to use this command.

Additional Guide and Examples:

Consider the following example that invokes the dummy program and runs the mail command at 16:30 (4:30 p.m.) daily, Monday through Friday.

30 16 * * 1-5 dummy -u admin -p password mail -s “Cron job test” jason@example.com >> /users/admin/crontab.log 2>&1

When you schedule tasks in a crontab file, use these guidelines, entering data in the order presented:

1. Schedule the time by defining the minutes after the hour first, then the hour of the day (in 24-hour time).

2. Schedule the day or days within a month to perform the operation. Use an asterisk (*) to indicate everyday.

3. Schedule the months of the year in which to perform the operation. Use an asterisk (*) to indicate every month.

4. Schedule the weekdays on which to perform the operation; for example, to schedule the operation to run daily, Monday through Friday, enter 1-5.

5. Enter the path for the application (dummy program).

7. Enter valid options for dummy program. (e.g. -u admin -p password).

8. Enter the command that you want to run.

9. Redirect the output (both stdout and stderror) to a logfile; for example, /users/admin/crontab.log 2>&1.

Note:  If you do not redirect the standard output and standard
error of your commands, any generated output or errors will
be mailed to you.


Note Crontab entries are space sensitive and require the data to be written on a single line for each entry you specify.

Creating or Changing a crontab File


Step 1 Enter the crontab -e command at the system prompt. This command opens the current contents of the crontab file in the text editor you have defined in the environment variable $EDITOR. A temporary file will be created by the text editor. Step 2 Change an existing crontab entry.Step 3 Add other crontab entries as desired.

Step 4 Save the file and quit the editor. This returns the modified file to the cron spool area of the operating system.

Step 5 Verify the crontab file by entering this command:

$ crontab -l

The system should display your crontab entries. Your crontab file is now ready to run at the times and days you defined.


Example crontab File

The following example crontab schedules several key tasks:

#

# cron entries

#

# run dummy and mail programs with logging every hour

0 * * * * dummy -u admin -p password mail -s “Cron job test” jason@example.com >> /tmp/cron.log 2>&1

#

# run dummy and mail programs with logging at 12:15 am

15 0 * * * dummy -u admin -p password mail -s “Cron job test” jason@example.com >> /tmp/cron.log 2>&1

#

# run dummy and mail programs with logging once a week on Sunday at 1:15am.

15 1 * * 0 dummy -u admin -p password mail -s “Cron job test” jason@example.com >> /tmp/cron.log 2>&1

#

# run dummy and mail programs with logging every night at 2:15 am.

15 2 * * * dummy -u admin -p password mail -s “Cron job test” jason@example.com >> /tmp/cron.log 2>&1

#

# run dummy and mail programs with logging once a week on Sunday at 3:15am

15 3 * * 0 dummy -u admin -p password mail -s “Cron job test” jason@example.com >> /tmp/cron.log 2>&1

Running PHP Scripts with Cron

Filed under: PHP Articles — jusloh @ 3:58 am

Tutorial Written By: Till

Lots of programmers like PHP for its ability to code and develop web applications fast. Code-debugging is a lot easier than with PERL or C. However, there is one thing a lot of developers are puzzled about, “How to run PHP Scripts with crontab?”

Cron is normally available on all Unix and Linux distributions; if you cannot access it, contact your root or server administrator. It is a daemon which allows you to schedule a program or script for a specific time of execution. If you want to learn more about cron, click here or type “man crontab” at your command prompt.

I have found myself in the need to run PHP scripts at specific times. For example, to update the content of a website, to remove expired articles, to send out e-mails on a given date and a lot more. While some may think that this is were PHP is doomed, I will show you how it’s done.

A Manual crontab?

The first solution that came to my mind was to run the script directly from my browser (e.g. http://www.mydomain.com/script.php). Since I need to run my script on a regular basis, I squashed that idea. My goodness, all the extra hassle is ridiculous.

An include?

Another possible solution is to include the script in one of the pages of the site, for example the very first: “index.php”. ()

The drawbacks to this solution are, that it works but when someone accesses the “index.php”. This could cause a lot of extra overhead produced by the script. If you get a lot of traffic, the script is executed 1000 times a day and adds a lot of usage on the database and the server.
On the other hand, if you do not get a lot of traffic, or people tend to access your site over another file, this will not work out as well. If you need to run the script on a regular intervals, this is not a solution.

Crontab!

Let’s suppose you either know what cron is or have read about it using the link above. We want to run our script once a minute. So where do we go from here? Here is how you can accomplish this task.

Your PHP setup

You will need to find out the answer to the following question, “Is my PHP installed as CGI or as an Apache module?”. To find out do the following: Create a new file, name it info.php (just an example), and put in the following code, “”. Upload to your webserver and go to it with your browser.

Now check for Server API (4th item from the top), if it says “CGI”, you have PHP compiled as CGI, if it reads “Apache”, you have it running as an Apache module.

Compiled CGI

If the answer to the question above is “CGI” then you need to add a line to your PHP script. It has to be the first line of your script and must contain your server’s PHP executable location:

#!/usr/local/bin/php -q

That looks a lot like PERL now, doesn’t it? After that let’s add the necessary command to our crontab. Edit /etc/crontab and add the following line:

* * * * * php /path/to/your/cron.php

Execute the following from the command line:

Shell> crontab crontab

Be sure your “script.php” has the necessary permissions to be executable (“chmod 755 script.php”). Now you are all set!

Apache module

If your PHP is installed using the Apache module, the approach is a little different. First, you need access to Lynx (Lynx Browser for more information). Lynx is a small web browser, generally available on Unix and Linux. Running your PHP script will not require you to add any additional lines. You simply have to edit your /etc/crontab file and add the following line:

* * * * * lynx -dump http://www.somedomain.com/cron.php

Please note that in general, you have to specify the entire URL (with “http://” and so on). But depending on your Lynx’s configuration, the URL might be relative; I suggest always using the absolute reference as in my example above – it always works. Again execute the following from the command line:

Shell> crontab crontab

That all it takes to get a cron job setup using PHP. Hope you have learned something new and will use it to save overhead time on the server and on the developer.

June 14, 2007

Apache Tutorial: .htaccess files – Apache HTTP Server

Filed under: Apache — jusloh @ 5:51 am

Apache Tutorial: .htaccess files – Apache HTTP Server

This tutorial gives an insight into using the various commands used in in .htaccess file and httpd.conf file which are used to configure access control to files and directories.

June 13, 2007

enabling mod rewrite in xampp

Filed under: PHP Articles — jusloh @ 1:56 pm

To enable mod_rewrite in xampp first go to the directory of installation \apache\conf and edit httpd.conf. Find the line which contains
#LoadModule rewrite_module modules/mod_rewrite.so
uncomment this(should be):

LoadModule rewrite_module modules/mod_rewrite.so

Also find AllowOverride None

Should be:

AllowOverride All

I really think it appears 2 or 3 times on the configuration file.

XOOPS Site – Installing PHP Debugger For PHPeclipse

Filed under: PHP Articles — jusloh @ 9:38 am

XOOPS Site – Installing PHP Debugger For PHPeclipse

This site gives the instructions for installing DBG debugger engine for use in PHPeclipse. I managed to get PHPeclipse(a.k.a Eclipse PHP Development Tools) debugging working with the instructions but the phpinfo did not display the information on DBG.

Next Page »

Blog at WordPress.com.