在 Apache 目录服务器中存储 Java 对象,第 2 部分:在 ApacheDS 中存储、搜索和检索 Java 对象(下)
2010-04-19 00:00:00 来源:WEB开发网清单 9 包含 15 个步骤,前 14 个步骤完全与 清单 8 中的前 14 个步骤相同。两个应用程序的惟一区别是 清单 9 的步骤 15,在这一步骤中,SearchResult.getObject() 返回一个已编组的对象。您自己可以检查返回对象的名称,对这一点进行验证。在完成检查之后,要将对象类型转换为 MarshalledObject。
Java 对象的序列化形式位于这个编组对象的内部,所以现在要调用 MarshalledObject 类的 get() 方法获得 Java 对象,然后可以在确定对象的类之后进行类型转换。
应用程序 8. 编辑和更新存储的对象
在目前为止的示例中,我的重点都放在存储和搜索 Java 对象上。下一个应用程序将显示如何编辑和更新已经存储在 ApacheDS 中的 Java 对象。
清单 10 中的 UpdateAlicePreferences 应用程序更新的 Java 对象是在 清单 4 中与 person 对象类混合的对象:
清单 10. UpdateAlicePreferences
public class UpdateAlicePreferences {
public UpdateAlicePreferences() {
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=java.rmi.MarshalledObject)";
//------------------------------------------
//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();
entryRDN = sr1.getName();
if (obj != null && obj instanceof MarshalledObject)
{
MarshalledObject mObj= (MarshalledObject) obj;
MessagingPreferences pref =
(MessagingPreferences) mObj.get();
//Step16: Updating your java object
pref.setStyles("http://www.mystyles.com/preferences");
//Step17: Setting a new context for data updation
String bindContext = entryRDN + "," + searchContext;
//Step18: Reading attributes in a new collection
Attributes attrs1 = sr1.getAttributes();
//Step19:Updating data
ctx.rebind(bindContext, pref, attrs);
System.out.println("Updating
the MessagingPreferences succeed");
}
}//while
}//if
}//while
}//if (ne != null)
} catch (Exception e) {
System.out.println("Operation failed: " + e);
}
}
public static void main(String[] args) {
UpdateAlicePreferences updateAlicePreferences =
new UpdateAlicePreferences();
}
}
- ››apache设置域名绑定 以及绑定不起作用的排查
- ››apache rewrite将指定URL转向指定的几个服务器
- ››apache配置文件httpd.comf部分参数说明
- ››Apache+Mysql+PHP+phpMyAdmin+Mac OS X 10.7 Lion...
- ››apache+tomcat负载均衡_项目实例
- ››apache mysql php 源码编译使用
- ››Apache添加mod_aspdotnet.so支持ASP.NET配置指南
- ››Apache中改变php.ini的路径
- ››Apache2.2与Tomcat6整合及虚拟主机配置
- ››Apache+php+mysql在windows下的安装与配置图解
- ››服务器群集:Windows 2000 和 Windows Server 200...
- ››服务器维护经验谈 图解DHCP故障排除
更多精彩
赞助商链接