Lab 2. JNDI Browser
Java EE. Celem ćwiczenia jest implementacja i uruchomienie prostej przeglądarki JNDI.
Rozpoczynamy ćwiczenia od utworzenia aplikacji WebJndi.
Alternatywnie wybieramy inny serwer aplikacyjny, np. WildFly.
Aby ułatwić budowę interfejsu graficznego aplikacji dodajemy do projektu obsługę frameworka JavaServer Faces:
Następnie w gałęzi Source Packages tworzymy pakiet com.example.jndi, w nim zaś nasz pierwszy BackingBean:
Edytujemy kod źródłowy klasy BackingBean wprowadzając zawartość:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.jndi;
import java.util.logging.Logger;
import java.util.HashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
/**
*
* @author Mariusz
*/
@ManagedBean(name = „backing”, eager = true)
@RequestScoped
@Path(„/jndi”)
public class BackingBean {
private static Logger log = Logger.getLogger(BackingBean.class.getName());
public static Map<String, Object> toMap(Context ctx) throws NamingException {
String namespace = ctx instanceof InitialContext ? ctx.getNameInNamespace() : „”;
HashMap<String, Object> map = new HashMap<String, Object>();
log.info(„> Listing namespace: „ + namespace);
NamingEnumeration<NameClassPair> list = ctx.list(namespace);
while (list.hasMoreElements()) {
NameClassPair next = list.next();
String name = next.getName();
String jndiPath = namespace + name;
Object lookup;
try {
log.info(„> Looking up name: „ + jndiPath);
Object tmp = ctx.lookup(jndiPath);
if (tmp instanceof Context) {
lookup = toMap((Context) tmp);
} else {
lookup = tmp.toString();
}
} catch (Throwable t) {
lookup = t.getMessage();
}
map.put(name, lookup);
}
return map;
}
@GET
public Map<String, Object>getRestJndiTree() throws NamingException{
Map<String, Object> map = toMap(new InitialContext());
return map;
}
private String mapToString(Map<String, Object> map, int level) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Object> entry : map.entrySet()) {
sb.append(„<div style=\”padding-left: „);
sb.append(10 * level);
sb.append(„px;\”>”);
if (entry.getValue() instanceof Map) {
sb.append(„<br/><b>”);
sb.append((String) entry.getKey());
sb.append(„</b> => „);
sb.append(„<div style=\”padding-left: „);
sb.append(10 * (level+1));
sb.append(„px;\”>”);
sb.append(„{„);
sb.append(mapToString((Map<String, Object>) entry.getValue(), level + 2));
sb.append(„}”);
sb.append(„</div>”);
sb.append(„<br/>”);
} else if (entry.getValue() instanceof String) {
sb.append(„<br/>{<br/><b>”);
sb.append((String) entry.getKey());
sb.append(„</b> => „);
sb.append((String) entry.getValue());
sb.append(„<br/>}<br/>”);
} else {
Object o = entry.getValue();
o.toString();
}
sb.append(„</div>”);
}
return sb.toString();
}
public String getJndiTree() throws NamingException {
Map<String, Object> map = toMap(new InitialContext());
String s = mapToString(map, 1);
return s;
}
private TreeNode mapToNode(Map<String, Object> map, TreeNode parentNode){
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() instanceof Map) {
JndiTreePosition pos = new JndiTreePosition(entry.getKey(), „”);
TreeNode node0 = new DefaultTreeNode(pos, parentNode);
mapToNode((Map)entry.getValue(), node0);
} else if (entry.getValue() instanceof String) {
JndiTreePosition pos = new JndiTreePosition(entry.getKey(), (String)entry.getValue());
TreeNode node0 = new DefaultTreeNode(pos, parentNode);
}
}
return parentNode;
}
public TreeNode getJndiPrimefacesTree() throws NamingException{
TreeNode root;
root = new DefaultTreeNode(„Glassfish JNDI Tree”, null);
Map<String, Object> map = toMap(new InitialContext());
root = mapToNode(map, root);
return root;
}
private JndiTreePosition selectedDocument;
public JndiTreePosition getSelectedDocument() {
return selectedDocument;
}
public void setSelectedDocument(JndiTreePosition selectedDocument) {
this.selectedDocument = selectedDocument;
}
}
W kolejnym kroku, w pakiecie com.example.jndi, tworzymy kolejną klasę, tym razem o nazwie JndiTreePosition:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.jndi;
/**
*
* @author Mariusz
*/
public class JndiTreePosition {
String name;
String value;
public JndiTreePosition(String name, String value){
this.name=name;
this.value=value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Uruchomienie aplikacji JSF wymaga utworzenia definicji warstwy widoku w postaci podstrony XHTML o nazwie index.xhtml:
<?xml version=’1.0′ encoding=’UTF-8′ ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Glassfish JNDI Tree</title>
</h:head>
<h:body>
<h1>Glassfish JNDI Tree</h1>
<h:outputText value="${backing.jndiTree}" escape="false"/>
<br />
<h:link outcome="treetable" value="TreeTable View of JNDI Tree" />
</h:body>
</html>
…..