JNDI Custom Resource †
- 実行する Glassfish のインスタンスに固有の情報は、Glassfish に設定する
- 管理コンソールの [Reource]-[JNDI]-[Custom Resources] で設定する
- Resource Type は、java.util.Properties に設定する
- Factory には、PropertiesFactory? を設定する
- 設定値を JNDI で直接 lookup しない。Properties 全体を lookup する。
src †
package com.mycompany.sampleweb;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "CustomResource", urlPatterns = {"/CustomResource"})
public class CustomResource extends HttpServlet {
@Resource(lookup = "APP_PROPERTY")
private Properties resource;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet CustomResource</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Lookup from Initial Context</h1>");
InitialContext ictx = new InitialContext();
Properties prop = (Properties) ictx.lookup("APP_PROPERTY");
out.println("<table border='1'>");
for (Map.Entry<Object, Object> entry : prop.entrySet()) {
out.println("<tr><td>" + entry.getKey() + "</td><td>" + entry.getValue() + "</td></tr>");
}
out.println("</table>");
out.println("<h1>Lookup from Injection @Resource</h1>");
out.println("<table border='1'>");
for (Map.Entry<Object, Object> entry : resource.entrySet()) {
out.println("<tr><td>" + entry.getKey() + "</td><td>" + entry.getValue() + "</td></tr>");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
} catch (NamingException ex) {
Logger.getLogger(CustomResource.class.getName()).log(Level.SEVERE, null, ex);
throw new ServletException(ex);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
実行結果 †
java.util.Properties 以外の Resource †
- 出来合いの PropertiesFactory? で必要十分だけど
- 何かで使うかもしれないので調べておいた
- Resource
public class EnvBean {
private java.util.Map<String, Object> prop = null;
public EnvBean() {
super();
prop = new java.util.concurrent.ConcurrentHashMap<String, Object>();
}
public void setProperty(final String key, final Object val) {
prop.put(key, val);
}
public Object getProperty(final String key) {
return prop.get(key);
}
}
- Factory
public class EnvFactory implements javax.naming.spi.ObjectFactory {
public EnvFactory() {
super();
}
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
throws Exception {
EnvBean env = new EnvBean();
if (obj instanceof javax.naming.Reference) {
javax.naming.Reference ref = (javax.naming.Reference) obj;
Enumeration<javax.naming.RefAddr> attributes = ref.getAll();
while(attributes.hasMoreElements()) {
javax.naming.RefAddr attr = attributes.nextElement();
env.setProperty(attr.getType(), attr.getContent());
}
}
return env;
}
}
- できた資産は、${GLASSFISH_HOME}/domains/domain1/lib または、${GLASSFISH_HOME}/domains/domain1/lib/classes に格
納する
- ${GLASSFISH_HOME}/domains/domain1/lib : JAR ファイル
- ${GLASSFISH_HOME}/domains/domain1/lib/classes : classファイル。パッケージのディレクトリを作る必要があることに注意
- @Resource(lookup="MY_ENV") EnvBean? env;
- EnvBean? env = ictx.lookup("MY_ENV");
Custom Resource には、日本語を使わない †
Java#Glassfish