Add “command prompt here” to right click menu of folder

Friday, 8 June 2007, 2:11 | Category : Technology
Tags :

Open notepad and paste following content and save the file as cmd.reg. After creation, double clik the file and say yes on option alert box.

REGEDIT4

[HKEY_CLASSES_ROOTDirectoryshelldos]
@=”Command Prompt here”

[HKEY_CLASSES_ROOTDirectoryshelldoscommand]
@=”cmd.exe /k cd “%1″”

How to create a new user on oracle

Friday, 8 June 2007, 1:43 | Category : Database, Technology
Tags :

create user newUser identified by userPassword;
grant create session to newUser_
grant create table to newUser;
GRANT create view TO newUser;
GRANT CREATE SEQUENCE TO newUser;
GRANT CREATE PROCEDURE TO newUser;
GRANT CREATE ANY TRIGGER TO newUser;
GRANT CREATE ANY TYPE TO newUser;
alter user newUser quota unlimited on users;

How to schedule jobs in Oracle

Friday, 8 June 2007, 1:41 | Category : Database, Technology
Tags :

BEGIN
DBMS_SCHEDULER.DROP_JOB (‘TEST_JOB’);
END;
/

BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => ‘TEST_JOB’,
job_type => ‘PLSQL_BLOCK’,
job_action => ‘UPDATE TABLE tblName set value=5;’,
start_date => sysdate,
repeat_interval => ‘FREQ=HOURLY; INTERVAL=1’, /* every one hour */
enabled => TRUE,
comments => ‘Test_JOb’);
END;
/

BEGIN
DBMS_SCHEDULER.ENABLE (‘TEST_JOB’);
END;
/

How to send email from oracle stored procedure

Friday, 8 June 2007, 1:40 | Category : Database, Internet, Technology
Tags : ,

BEGIN
UTL_MAIL.SEND (
sender => ‘[email protected]’,
recipients => ‘rst@yahoo.’,
subject => ‘test Oracle’,
message => ‘testing orcl’);
END;
/

How to drop everything on a oracle schema

Friday, 8 June 2007, 1:39 | Category : Database, Technology
Tags :

BEGIN
FOR cur_rec IN (SELECT table_name, constraint_name
FROM user_constraints
WHERE constraint_type = ‘R’) LOOP
EXECUTE IMMEDIATE ‘ALTER TABLE ‘ || cur_rec.table_name || ‘ DROP CONSTRAINT ‘ || cur_rec.constraint_name;
END LOOP;
FOR cur_rec IN (SELECT object_name, object_type
FROM user_objects) LOOP
BEGIN
EXECUTE IMMEDIATE ‘DROP ‘ || cur_rec.object_type || ‘ ‘ || cur_rec.object_name;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
END LOOP;
execute immediate ‘purge recyclebin’;
END;
/

Selecting random rows from an oracle table

Friday, 8 June 2007, 1:20 | Category : Database, Technology
Tags :

SELECT * FROM (SELECT * FROM TEST_TABLE ORDER BY dbms_random.value) WHERE rownum <= 5;
This will give 5 random rows from table.

Executing Javascript in Java code

Monday, 4 June 2007, 13:52 | Category : Java, Javascript, Technology
Tags :

you need follwing libraries to use javascript code in java.
1. Apache BSF : http://jakarta.apache.org/bsf/
2. Mozilla Rhino : http://www.mozilla.org/rhino/

Sample Code:

TestBean bean = new TestBean();
bean.setText(“hello”);
BSFManager bsfManager = new BSFManager();
bsfManager.declareBean(“bean”, bean, TestBean.class);
String jssrc=”bean.setText(bean.getText()+’World’);”; //javascript code
bsfManager.eval(“javascript”, “Test”, 0, 0, jsSrc);
System.out.println(bean.getText()); // will print hello World
//Bean Code
class TestBean {
private String name;
public String getName() {
return name;
}
public String setName(String name) {
this.name=name;
}
}

Using WorkManager in JBoss

Friday, 27 April 2007, 23:00 | Category : J2EE, Java, Technology
Tags :

You can schedule any work (thread that needs to be run) in Jboss using WorkManager API (JCA).
Below is a sample code that can be called from any ejb/servlet/mdb or any server side application running in jboss container.

Context jndiContext = new InitialContext();
MBeanServerConnection mconn = (MBeanServerConnection) jndiContext.lookup( “jmx/invoker/RMIAdaptor”);
ObjectName objectName = new ObjectName(“jboss.jca:service=WorkManager”);
JBossWorkManagerMBean jwm = (JBossWorkManagerMBean) MBeanServerInvocationHandler.newProxyInstance(mconn,objectName,
JBossWorkManagerMBean.class,false);
WorkManager workmanager = jwm.getInstance();
Work work = new WorkImpl();
WorkListener listener = new WorkListenerImpl();
System.out.println(” Scheduling some work”);
workmanager.scheduleWork(work,1000,new ExecutionContext(),listener);
System.out.println(“MDB:: Scheduled some Work”);
//Sample code for WorkListenerImpl
class WorkListenerImpl implements WorkListener
{
public void workAccepted(WorkEvent workEvent) {
System.out.println(“WorkListener:: work accepted by container”);
}
public void workRejected(WorkEvent workEvent) {
System.out.println(“WorkListener:: work rejected by container”);
}
public void workStarted(WorkEvent workEvent) {
System.out.println(“WorkListener:: work started by container”);
}
public void workCompleted(WorkEvent workEvent) {
System.out.println(“WorkListener:: work completed by container”);
}
}
//Sample code for WorkImpl 
public class WorkImpl implements Work, Serializable
{
public void release()
{
System.out.println(“Work:: Somebody want to kill me. I am releasing resources.”);
}
public void run()
{
System.out.println(“Work:: I am here for some work”);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(“Work:: Work finished”);
}
}

Class Loading in Java

Friday, 27 April 2007, 14:09 | Category : Java
Tags :

Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class that is already running in the JVM. So how is the very first class loaded? The very first class is specially loaded with the help of static main() method declared in your class. All the subsequently loaded classes are loaded by the classes, which are already loaded and running. A class loader creates a namespace. All JVMs include at least one class loader that is embedded within the JVM called the primordial (or bootstrap) class loader. Now let’s look at non-primordial class loaders. The JVM has hooks in it to allow user defined class loaders to be used in place of
primordial class loader. Let us look at the class loaders created by the JVM.

  1. Bootstrap (primordial) – Not reloadable – Loads JDK internal classes, java.* packages. (as defined in the sun.boot.class.path system property, typically loads rt.jar and i18n.jar)
  2. Extensions – Not reloadable – Loads jar files from JDK extensions directory (as defined in the java.ext.dirs system property – usually lib/ext directory of the JRE)
  3. System – Not reloadable – Loads classes from system classpath (as defined by the java.class.path property, which is set by the CLASSPATH environment variable or –classpath or –cp command line options)

Classes loaded by Bootstrap class loader have no visibility into classes loaded by its descendants (ie Extensions and Systems class loaders). The classes loaded by system class loader have visibility into classes loaded by its parents (ie Extensions and Bootstrap class loaders).
If there were any sibling class loaders they cannot see classes loaded by each other. They can only see the classes loaded by their parent class loader.

Class loaders are hierarchical and use a delegation model when loading a class. Class loaders request their parent to load the class first before attempting to load it themselves. When a class loader loads a class, the child class loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Classes loaded by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not true.

Two objects loaded by different class loaders are never equal even if they carry the same values, which mean a class is uniquely identified in the context of the associated class loader. This applies to singletons too, where each class loader will have its own singleton.

Static class loading:
Classes are statically loaded with Java’s “new” operator.
class MyClass {
public static void main(String args[])
{
Car c = new Car();
}
}
A NoClassDefFoundException is thrown if a class is referenced with Java’s “new” operator (i.e. static loading) but the runtime system cannot find the referenced class.

Dynamic class loading:
Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time.
Class.forName (String className); //static method which returns a Class
The above static method returns the class object associated with the class name. The string className can be supplied dynamically at run time. Unlike the static loading, the dynamic loading will decide whether to load the class Car or the class Jeep at runtime based on a properties file and/or other runtime conditions. Once the class is dynamically loaded the following method returns an instance of the loaded class. It’s just like creating a class object with no arguments.
class.newInstance (); //A non-static method, which creates an instance of a class (ie creates an object).
Jeep myJeep = null ;
//myClassName should be read from a properties file or Constants interface.
//stay away from hard coding values in your program.
String myClassName = “au.com.Jeep” ;
Class vehicleClass = Class.forName(myClassName) ;
myJeep = (Jeep) vehicleClass.newInstance();
myJeep.setFuelCapacity(50);
A ClassNotFoundException is thrown when an application tries to load in a class through its string name using the following methods but no definition for the class with the specified name could be found:

  • The forName(..) method in class – Class.
  • The findSystemClass(..) method in class – ClassLoader.
  • The loadClass(..) method in class – ClassLoader.

Reading Mails from secure exchange server in Java

Thursday, 26 April 2007, 16:46 | Category : Internet, J2EE, Java, Technology
Tags : , ,

1. Export the certificate for the exchange server from Internet explorer Certificate export wizard e.g. after opening https://webmail.hostname.com, (View certificate >Details > Copy to File). Say the exported file name is certificate.cer

2. Create keystore from the certificate using java keytool (which will create keystore file in current directory). It will ask for a password, give anything (it does not matter)

keytool –import -alias webmail.hostname.com -keystore keystore -file certificate.cer

3. Now While running the Java application pass the VM parameter : –Djavax.net.ssl.trustStore=Path_To_keystoreFile

Sample code to read mail is provided below (Its uses JEC API: http://www.javaexchangeconnector.com/)

import java.text.*;
import java.util.*;
import jec.*;
import jec.dto.*;
public class FetchSecureExchangeMail {
public static void main(String args[]) throws Exception {
ExchangeConnectorFactory factory = new ExchangeConnectorFactory();
ExchangeConnectorInterface connector = factory.createExchangeConnector(“webmail.hostname.com”, “username”,”password”, “Exchange”, true, “[email protected]”);
SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date sinceDate = dateFormat.parse(“2007-04-25 06:00:00″);
HashSet fromEmailAddressFilter = null;
HashSet toEmailAddressFilter = null;
int maxEmailsReturned = 100;
ArrayList emailsArrayList = connector.getEmails(
sinceDate,
fromEmailAddressFilter,
toEmailAddressFilter,
maxEmailsReturned);
System.out.println(“emailsArrayList.size() : ” + emailsArrayList.size());
if (emailsArrayList.size() > 0) {
ExchangeEmailDTO email1 = (ExchangeEmailDTO) emailsArrayList.get(0);
System.out.println(“email1 email1.getSubject(): ” +
email1.getSubject());
}
}
}