Sun Application Server Startup Command: /opt/SUNWappserver/bin/asadmin start-domain --domaindir /opt/SUNWappserver/domains --user admin

Friday, March 30, 2007

JNDI on Sun Application Server 8.0

1. Go to sun application server and setting something like following:

2. create a java file: and save it like directory gov/hud/cpd/gmp/business/service


import java.util.Hashtable;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;

public class PropertiesFactory implements ObjectFactory {

public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception {

Reference ref=(Reference)obj;
Properties props = new Properties();
props.setProperty("username", (String)(ref.get("username").getContent()));
props.setProperty("password", (String)(ref.get("password").getContent()));
props.setProperty("sshhost", (String)(ref.get("sshhost").getContent()));

return props;


3. make a jar file(only this file) using comment under root folder in cmd:
jar cf ftpJndi.jar *.*
(Remember to restart sun application server.)
4. copy it to [sun folder]\AppServer\domains\domain1\lib\ext as a library
or add in classpath in program as library.
  • If sun application server cannot find this factory class, It will directory return java.naming.Reference type in prop = (Reference) envCtx.lookup(dsName); below.

5. create another file in program like following:

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public final class JndiConnector {

public static ftpConnectionDao getContextName() throws NamingException {
ftpConnectionDao ftpConn = null;
Properties prop = null;
try {
InitialContext ctx = new InitialContext();
String dsName = "FtpConnection";
Context envCtx = (Context) ctx.lookup("java:comp/env");
prop = (Properties) envCtx.lookup(dsName);
ftpConn = new ftpConnectionDao();

} catch (Exception e) {
return ftpConn;

and dao file:

public class ftpConnectionDao {

private String username;
private String password;
private String sshhost;
public String getSshhost() {
return sshhost;
public void setSshhost(String sshhost) {
this.sshhost = sshhost;
public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;
public String getUsername() {
return username;
public void setUsername(String username) {
this.username = username;

6. in web.xml
<description>Ftp JNDI Reference</description>

7. in sun-web.xml

No comments: