1. Go to sun application server and setting something like following:
2. create a java file: PropertiesFactory.java and save it like directory gov/hud/cpd/gmp/business/service
package 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:
package gov.hud.cpd.gmp.business.service;
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();
ftpConn.setUsername(prop.getProperty("username"));
ftpConn.setPassword(prop.getProperty("password"));
ftpConn.setSshhost(prop.getProperty("sshhost"));
} catch (Exception e) {
e.printStackTrace();
}
return ftpConn;
}
}
and dao file:
package gov.hud.cpd.gmp.business.service;
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
<resource-ref>
<description>Ftp JNDI Reference</description>
<res-ref-name>FtpConnection</res-ref-name>
<res-type>java.util.Properties</res-type>
<res-auth>Container</res-auth>
</resource-ref>
7. in sun-web.xml
<resource-ref>
<res-ref-name>FtpConnection</res-ref-name>
<jndi-name>FtpConnection</jndi-name>
</resource-ref>
No comments:
Post a Comment