Thursday, September 29, 2011

execute a java class that has main method in maven

mvn exec:java -Dexec.mainClass="com.rogers.App"

about logging

speaking about logging,most people just choose one to use,JUL,Log4J,or SLF4J,but there are some complicated situation that could cause problems
you could just use log4J in your project,but if the component you dependent use others.so if you develop a component or framework,you better use apache common-logging(JCL),it provide a interface for logging,so it will enough for developing,but at run time,you need provide a implementation,such as SLF4J or log4j.

when we use third party component,such as spring framework,we want use log4J or slf4j,but,we need exclude JCL,and binding SLF4J with the log4J,so we can use log4J in our code,or if we do not binding slf4j with log4j,we can just use slf4J in our code.
here is the example that we can use slf4j or log4J in our code,but component we use is spring,commented part is using slf4J,we can put log configuration in one place,both spring and our code use same configuration and both logging correctly

package com.rogers;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Hello world!
*
*/
public class App {
//private static Logger log = LoggerFactory.getLogger(App.class);
private static Logger log = Logger.getLogger(App.class);
public static void main( String[] args )
{
log.info("hello,world---slf4j");
System.out.println( "Hello World!" );
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"application.xml"});
BO bo = ctx.getBean("bo",BO.class);
System.out.println("you name:"+bo.getName());
}
}

and here is the pom that config logging


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rogers</groupId>
<artifactId>springTest</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>springTest</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

Wednesday, September 28, 2011

tree traversal

there are three type of tree traversals
for example,if I have tree like this
A
/ \
B C
the preorder way access order will be ABC
the inorder way access order will be BAC
the post Order way access order will be BCA
this algorithm will cursively apply to all node we meet

About few tags in HTML

few rules that in HTML
1) The head must have, at
minimum, two children, a meta element deļ¬ning the character set, and a title:
2) meta tag do not have a close tag,ex:
<meta http-equiv="content-type" content="text/html;charset=utf-8">
3) link tag in head also do not have a end tag ex:
<link type="text/css" rel="stylesheet" href="hello-world.css">
4) html5 doctype tag is like this
<!DOCTYPE HTML>
XHTML 1.0 Transitional: is like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
and The DOCTYPE tells the browser how to render the document. Which DOCTYPE you use greatly affects
CSS styling and markup validation

Tuesday, September 27, 2011

pointer in c++

in C or C++,when we declare a variable like int x;we define normal variable with type int,but when we declare like int* y,or int *y,that means we declare a int pointer
we assign int value to x as normal
int x;
x = 100;
but for pointer,we assign like this
y = &x; //that means y will contains x's address
if we print *y ,that means we print value of x,because * get object of that pointer point to.
here is a table that summarize all of those operators



expressioncan be read as
*xpointed by x
&xaddress of x
x.ymember y of object x
x->ymember y of object pointed by x
(*x).ymember y of object pointed by x (equivalent to the previous one)
x[0]first object pointed by x
x[1]second object pointed by x
x[n](n+1)th object pointed by x

C++ in linux

did not try c++ in linux for a long time,

here is a example and how to compile it
code:
#include <iostream>
using namespace std;

class CRectangle {
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
};

void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}

int main () {
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}


compile: g++ test1.cpp -o test1

run: /test1

Thursday, September 22, 2011

template method pattern

template pattern provide abstract definition of methods in a class and,redefine it's behavior later on the fly and without change its structures,at same time it will keep overall execute step sequence.

abstract class Game{
void init();
void play();
void printWinner();
void exit();
void playTheGame(){
init();
play();
printWinner();
exit();
}
}

class MonoGame extends Game(){
void init(){
System.out.println("init monoGame");
}
void play(){
System.out.println("playing monoGame");
}
void printWinner(){
System.out.println("print winner in monoGame");
}
void exit(){
System.out.println("exit monoGame");
}
}
//define other classes with new behaviors with override the abstract method
public static void main(String args[]){
Game g = new MonoGame();
g.playTheGame();
//other type of games
}

visitor pattern

visitor pattern is used to add extra operations for group of class with out change their structures

interface Operation{
void op(){};
}
class AddOperation implements Operation{
void op(){
System.out.println("add operation");
}
}
class minusOperation implements Operation{
void op(){
System.out.println("minus");
}
}
//this can be used to get rid of the if or select case statements
public class Test{
public static void main(String args[]){
Operation o = new AddOperation();
o.op();
}
}

//here comes visitor pattern

interface Visitor(){
void visit(Operation o);
}

class ToAdd implements Visitor{
void visit(Operation o){
o.op();
}
}
class ToSubstract implements Visitor{
void visit(Operation o){
o.op();
}
}
public class MyTest{
public static void main(String[] args){
Operation op = new MinusOperation();
Visitor v = new ToSubstract();
v.visitor(op);
}
}

best example is Pizzas and delivery methods,for Pizza,we need to order it,all Pizza has this method,Delivery interface has a visit method that will pass Pizza and call Pizza's order method,so we add operations(different delivery method) to group of class( different Pizzas),we do not need to change Pizzas structure,becaus it is extracted to interface.

javascript closure definition

A closure is the local variables for a function - kept alive after the function has returned, or

A closure is a stack-frame which is not deallocated when the function returns. (as if a 'stack-frame' were malloc'ed instead of being on the stack!)

key points about difference betweeb interface and abstract class

this is simple question and be asked almost all interviewing,the most importance thing is to mention the key points
1)Interfaces are just signatures and have no implementations (code in method bodies) where as abstract classes can have some implementations.
2)You can extend only one abstract class but you can extend (or implement) multiple interfaces
3)Interfaces define a contract or signature and have no behavior but abstract classes have behavior

Wednesday, September 21, 2011

how to count number os sessions

following code will help to count number of session in a web application

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;


public class SessionCount implements HttpSessionListener{
private static int numberOfSessions;

/**
* @return the numberOfSessions
*/
public static int getNumberOfSessions() {
return numberOfSessions;
}

/**
* @param aNumberOfSessions the numberOfSessions to set
*/
public static void setNumberOfSessions(int aNumberOfSessions) {
numberOfSessions = aNumberOfSessions;
}

public void sessionCreated(HttpSessionEvent se) {
setNumberOfSessions(getNumberOfSessions() + 1);
}

public void sessionDestroyed(HttpSessionEvent se) {
setNumberOfSessions(getNumberOfSessions() - 1);
}

}

SQLDeveloper upcase allCode

SQLDeveloper aways upcase my code while I developing pl/SQL store procedure
the setting is in MEnu Tools|Preferences|Code Editor|Completion Insight|Change case as you type
this definitely should not be turn on as a default feature!!

Tuesday, September 20, 2011

easy way to find record in some table changed in specified date

SELECT A.BONDING_GROUP_SEQ,TO_TIMESTAMP(TO_CHAR(A.MODIFY_DATE,'DD-MM-RRRR HH24:MI:SS'),'DD-MM-RRRR HH24:MI:SS') FROM DTT_BONDING_GROUP a where trunc(A.MODIFY_DATE) = '19-SEP-11'

Monday, September 19, 2011

add popup in icefaces visual pages

<ice:panelPopup autoCentre="true" draggable="true" id="deletePanelPopup" modal="true" rendered="#{AMUChassisSessionBean.yesNoRendered}"
style="display: block; height: 48px; left: 1080px; top: 456px; position: absolute; width: 416px" title="DTT Dialog">
<f:facet name="header">
<ice:panelGrid cellpadding="0" cellspacing="0" columns="2" style="text-align: center;" width="100%">
<ice:outputText styleClass="PopupHeaderText" value="Device Activation and Topology Management"/>
<ice:commandButton actionListener="#{AMUChassis.closeButton_processAction}" alt="Close" id="yesNoPnlCloseBtn"
image="/resources/popupclose.gif" styleClass="PopupHeaderImage" title="Close Popup" type="button"/>
</ice:panelGrid>
</f:facet>
<f:facet name="body">
<ice:panelGroup styleClass="PopupBody">
<ice:outputText style="font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 12px; left: 416px; text-align: center;" value="Chassis #{AMUChassisSessionBean.originalChassisName} and all associated components will be deleted, continue ?"/>
<br/>
<br/>
<ice:commandButton actionListener="#{AMUChassis.yesButton_processAction}" id="yes"
style="position: relative; left: 120px; width: 80px" type="submit" value="Yes"/>
<ice:commandButton actionListener="#{AMUChassis.noButton_processAction}" id="no"
style="position: relative; left: 160px; width: 80px" type="submit" value="No"/>
</ice:panelGroup>
</f:facet>
</ice:panelPopup>

Friday, September 9, 2011

TreeMap vs HashMap vs HashTable

while using TreeMap and HashMap,find out that they are different
first TreeMap is sorted by key in nature,notice that if you put as key as negative number and it is save as String,then,it will mass up
you can put key as Long(negative),then it will work,HashMap is not sorted,but it will not keep the order that you put then in.
solution is to switch key and value,if they are both unique.

HashTable is older HashMap is new
HashTable is not allow null values.HashMap allows.

HashTable is synchnozed,HashMap is not.

Thursday, September 8, 2011

search a column references in whole database

if we looking for all reference to a specific column in whole database,we can
execute this:
select table_name, column_name from all_tab_columns where column_name like 'FACILITY_SEQ'
it willlist all tables that reference to column "FACILITY_SEQ"

config jetty port from mvn command and pom

maven-jetty-plugin run container at default port 8080.
if we want change the port,we can start jetty like this mvn jetty:run -Djetty.port=9090

or add following code inside pom's maven-jetty-plugin configuration tag


<systemProperties>
<systemProperty>
<name>jetty.port</name>
<value>9090</value>
</systemProperty>
</systemProperties>

Wednesday, September 7, 2011

struts2 pass-through actions

in struts2 if we define a action like this

<action name="Name">
<result>/chapterOne/NameCollector.jsp</result>
</action>

that means we just need to display the jsp page from this action,because the tag did not specify any java class for this action,so there is not java class related to this action.

filter all unread emails from gmail account

use "is:unread in:inbox" in search field to filter all unread email to display.
for hotmail, there is a button called unread in top

Friday, September 2, 2011

mvn site:site

this will create a report about the project include a left menu for About,dependencies,issue track etc,it also create surefire report.

Struts2 MVN practice

1) use MVN archetype:generate to create struts blank project number,choose number 198,give a artifactId and GroupId,choose the struts2 version and package and other parameters

2) after project created,we can not clean,compile and package project because of folowing error,we need fix some reference in pom.xml
Reason: Resolving expression: '${project.version}': Detected the following recursive expression cycle: [] for project rogers:helloWorld at /home/dttdev1/struts2/helloWorld/pom.xml

3) add following line in properties tag,change struts2.Version to a real struts2 version like 2.2.3,
delet all spring dependencies,and test class and packages
<project.version>1.0.0.0</project.version>
4) after that,we can clean,compile,package and test it inside jetty by mvn jetty:run

5)create a newexamples.xml file in same folder as struts.xml reside.
6) newexamples.xml content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<!-- - This file is included by the struts.xml file as an example - of how
to break up the configuration file into multiple files. -->
<struts>
<package name="newexamples" namespace="/mytest" extends="struts-default">
<action name="Name">
<result>/newexamples/namecollect.jsp</result>
</action>
<action name="HelloName" class="rogers.newexamples.TestAction">
<result name="SUCCESS">/newexamples/hello.jsp</result>
</action>
</package>
</struts>

7) include this xml file in struts.xml

<include file="newexamples.xml" />
8) create action class:

package rogers.newexamples;

public class TestAction {
private String name;
private String greeting;

public String execute() {
setGreeting("hello, " + getName());
return "SUCCESS";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getGreeting() {
return greeting;
}

public void setGreeting(String greeting) {
this.greeting = greeting;
}

}

9) create two jsp file in newexamples folders(namecollect.jsp and hello.jsp

<s:form action="HelloName">
<s:textfield name="name" label="Your name" />
<s:submit />
</s:form>


<s:property value="greeting"/>

start jetty,then point to http://localhost:8080/tutorial/mytest/Name,it will display namecollect.jsp page,after give a name,click submit,it will redirect to a hello.jsp page with a greeting massage

thinking: 1)if we put newexamples.xml in different folder,even we include the xml file,but when we visit the namecollect page by action Name,it will complain that action name Name can not be find,not action mapped to name "Name".

we define action like this
<action name="Name">
<result>/newexamples/namecollect.jsp</result>
</action>
this means when we visit http://localhost:8080/tutorial/mytest/Name
it will show namecollect.jsp page
inside namecollect.jsp page we define

<s:form action="HelloName">
<s:textfield name="name" label="Your name" />
<s:submit />
</s:form>

that means when we give a name and click submit,textfield content will be send to mapped action HelloName(TestAction class)'s property name,then call it's execute method to setup property greeting.

from mapped action HelloName definition

<action name="HelloName" class="rogers.newexamples.TestAction">
<result name="SUCCESS">/newexamples/hello.jsp</result>
</action>
we know that when execute return "SUCCESS",it will carry the java bean's property greeting to page hello.jsp and render the hello.jsp to explorer.

last ,dont forget to add a extra line to in pom.xml for jetty configuration

<scanTarget>src/main/resources/newexamples.xml</scanTarget>

Maven command to create a new Project

invoke mvn archetype:generate will promote a list of project type and Groupid and ArchetypeID to create new project