1、获取JCR操作会话对象
public Session createJCRSession(){
Session sion = null;
try {
sion = template.getSessionFactory().getSession();
} catch (RepositoryException e) {
_log.error("获取JCR操作会话对象失败:", e);
}
return sion;
}
2、判断节点是否存在
public boolean existsNode(Session sion, String nodePath){
boolean ret = false;
try {
ret = sion.nodeExists(nodePath);
} catch (RepositoryException e) {
_log.error("判断节点【" + nodePath + "】是否存在失败:", e);
}
_log.debug("节点【" + nodePath + "】是否存在:【" + ret + "】。");
return ret;
}
3、保存JCR操作结果
public void saveJCRSession(Session sion){
try {
sion.save();
} catch (Exception e) {
_log.error("保存JCR操作会话对象失败:", e);
}
}
4、关闭JCR操作会话对象
public void closeJCRSession(Session sion){
if(null != sion)
sion.logout();
}
5、按照路径存储文件
public void writerBinProperty(Node inode, String filePath){
String inodeName = "";
try {
ValueFactory vfactory = template.getValueFactory();
inodeName = inode.getName();
File file = new File(filePath);
MimeTable mt = MimeTable.getDefaultTable();
String mimeType = mt.getContentTypeFor(file.getName());
if(mimeType==null)
mimeType = "application/octet-stream";
inode.setProperty("jcr:mimeType", mimeType);
inode.setProperty("jcr:data", vfactory.createBinary(new FileInputStream(file)));
} catch (Exception e) {
_log.error("在节点【" + inodeName + "】上增加文件【" + filePath + "】失败:", e);
}
}
6、使用节点读取文件
public byte[] readBinProperty(Node inode){
String inodeName = "";
byte[] retby = null;
try{
inodeName = inode.getName();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Binary bin = inode.getProperty("jcr:data").getBinary();
byte[] buf = new byte[0x1000];
for (int cnt, pos = 0; (cnt = bin.read(buf, pos)) > 0; pos += cnt) {
out.write(buf, 0, cnt);
}
retby = out.toByteArray();
} catch (Exception ex) {
_log.error("在节点【" + inodeName + "】上获取文件信息时失败:", ex);
}
return retby;
}
7、获取节点上的自定义信息
public String readProperty(Node inode) {
StringBuffer propStr = new StringBuffer();
try {
PropertyIterator itor = inode.getProperties();
while(itor.hasNext()){
Property porp = itor.nextProperty();
if(porp.getName().startsWith("test"))
propStr.append(porp.getName);
}
} catch (RepositoryException e) {
try {
_log.error("获取节点【" + inode.getName() + "】上的属性失败:", e);
} catch (RepositoryException e1) {
_log.error("获取节点信息异常:", e1);
}
}
return propStr.toString();
}