在 Apache 目录服务器中存储 Java 对象,第 2 部分:在 ApacheDS 中存储、搜索和检索 Java 对象(下)
2010-04-19 00:00:00 来源:WEB开发网public class FetchAliceMessagingPreferences {
public FetchAliceMessagingPreferences() {
try
{
//------------------------------------------
//Step1: Setting up JNDI properties for ApacheDS
//------------------------------------------
InputStream inputStream = new FileInputStream( "ApacheDS.properties");
Properties properties = new Properties();
properties.load(inputStream);
properties.setProperty("java.naming.security.credentials", "secret");
//------------------------------------------
// Step2: Fetching a DirContext object
//------------------------------------------
DirContext ctx = new InitialDirContext(properties);
//---------------------------------------------
//Step3: Setting search context
//---------------------------------------------
String searchContext = "ou=users";
//--------------------------------------------
//Step4: Creating search attributes for Alice
//--------------------------------------------
Attribute cn = new BasicAttribute("cn");
Attribute objclass = new BasicAttribute("objectClass");
//putting attribute values
cn.add("Alice");
objclass.add("person");
//Instantiate an Attributes object and put search attributes in it
Attributes attrs = new BasicAttributes(true);
attrs.put(cn);
attrs.put(objclass);
//------------------------------------------
//Step5: Executing search
//------------------------------------------
NamingEnumeration ne = ctx.search(searchContext, attrs);
if (ne != null)
{
//Step 6: Iterating through SearchResults
while (ne.hasMore()) {
//Step 7: Getting individual SearchResult object
SearchResult sr = (SearchResult) ne.next();
//Step 8:
String entryRDN = sr.getName();
//---------------------------------------------
//Step9: Setting a new search context
//---------------------------------------------
searchContext = entryRDN + "," + searchContext;
//---------------------------------------------
//Step10: Creating search controls
//---------------------------------------------
SearchControls ctls = new SearchControls();
ctls.setReturningObjFlag(true);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//---------------------------------------------
//Step11: Creating filter
//---------------------------------------------
String filter = "(|(javaClassName=MessagingPreferences)
(javaClassName=ShippingPreferences))";
//------------------------------------------
//Step12: Executing search
//------------------------------------------
NamingEnumeration ne1 = ctx.search(searchContext, filter, ctls);
if (ne != null)
{
//Step13: Iterating through SearchResults
while (ne1.hasMore()) {
//Step14: Getting individual SearchResult object
SearchResult sr1 = (SearchResult) ne1.next();
//Step15: Getting preferences object
Object obj = sr1.getObject();
if (obj != null) {
if (obj instanceof MessagingPreferences){
MessagingPreferences pref =
(MessagingPreferences) obj;
}
else if (obj instanceof ShippingPreferences) {
ShippingPreferences pref = (ShippingPreferences) obj;
}
}
}//while(
}//if
}//while
}//if (ne != null)
} catch (Exception e) {
System.out.println("Operation failed: " + e);
}
}
public static void main(String[] args) {
FetchAliceMessagingPreferences searchAlicePreferences =
new FetchAliceMessagingPreferences();
}
}
- ››Apache添加mod_aspdotnet.so支持ASP.NET配置指南
- ››Apache中改变php.ini的路径
- ››Apache2.2与Tomcat6整合及虚拟主机配置
- ››Apache+php+mysql在windows下的安装与配置图解
- ››服务器群集:Windows 2000 和 Windows Server 200...
- ››服务器维护经验谈 图解DHCP故障排除
- ››Apache+Subversion完美结合,CentOS下实现版本控制...
- ››Apache HTTPServer2.2.16 发布
- ››Apache Tomcat 6.0.29 (稳定版)
- ››Apache HTTP Server 2.3.6 alpha 发布
- ››Apache+Subversion如何实现版本控制
- ››Apache+Subversion完美结合
更多精彩
赞助商链接