1 5 6 package org.exoplatform.services.jcr.impl.storage.filesystem.nodedata; 7 8 9 import javax.jcr.*; 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileOutputStream ; 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import org.apache.commons.codec.binary.Base64; 16 import org.exoplatform.services.jcr.core.NodeChange; 17 import org.exoplatform.services.jcr.core.NodeData; 18 import org.exoplatform.services.jcr.impl.core.NodeImpl; 19 import org.exoplatform.services.jcr.impl.core.PropertyImpl; 20 import org.exoplatform.services.jcr.storage.Container; 21 22 23 29 30 public final class FileNodeContainer extends BaseNodeContainer { 31 32 FileNodeContainer(String jcrPath, File storage) { 33 super(jcrPath, "nt:file", storage); 34 } 35 36 public NodeData getNodeByPath(String relPath) { 37 if (relPath.length() == 0) 38 return getRootNode(); 39 else 40 return getContentNode(relPath); 41 42 } 43 44 public List getChildren(String relPath) { 45 ArrayList list = new ArrayList (); 46 if (relPath.length() == 0) 47 list.add(getJcrPath("/jcr:content")); 48 return list; 50 } 51 52 public void add(Node node) throws ItemExistsException, RepositoryException { 53 try { 54 String relPath = parseRelPath(node.getPath()); 55 if (relPath.equals("/jcr:content")) { 58 updateContent((NodeData)node); 59 } 60 } catch (Exception e) { 61 throw new RepositoryException("Adding node failed. Reason:" + e.getMessage()); 62 } 63 } 64 65 public final void update(Node node) throws RepositoryException { 66 String relPath; 67 try { 68 relPath = parseRelPath(node.getPath()); 69 } catch (Exception e) { 70 return; 71 } 72 73 if (relPath.equals("/jcr:content")) updateContent(node); 75 } 76 77 public final void delete(String absPath) { 78 try { 79 String relPath = parseRelPath(absPath); 80 log.debug("FileNode Container delete node:" + absPath + "at "+relPath); 81 if (relPath.length() == 0) 82 storage.delete(); 83 else if (relPath.equals("/jcr:content")) 84 cleanContent(); 85 } catch (Exception e) { 86 return; 87 } 88 } 89 90 91 93 protected NodeImpl getContentNode(String relPath) { 94 if (!relPath.equals("/jcr:content")) 95 return null; 96 97 NodeImpl node; 98 try { 99 node = new NodeImpl(getJcrPath(relPath), getContentProps()); 100 } catch (Exception e) { 101 return null; 102 } 103 104 return node; 105 } 106 107 protected List getContentProps() { 108 109 ArrayList props = new ArrayList (); 110 props.add(getContentProperty("/jcr:content/jcr:primaryType")); 111 PropertyImpl contentProp = getContentProperty("/jcr:content/exo:content"); 112 if(contentProp != null) 113 props.add(contentProp); 114 props.add(getContentProperty("/jcr:content/jcr:uuid")); 115 116 return props; 117 118 } 119 120 121 protected PropertyImpl getContentProperty(String relPath) { 122 123 if (!relPath.startsWith("/jcr:content")) 124 return null; 125 126 try { 127 if (relPath.equals("/jcr:content/jcr:primaryType")) 128 return new PropertyImpl(getJcrPath("/jcr:content/jcr:primaryType"), 129 new StringValue("nt:content"), PropertyType.STRING); 130 131 if (relPath.equals("/jcr:content/jcr:uuid")) 132 return new PropertyImpl(getJcrPath("/jcr:content/jcr:uuid"), 133 new StringValue(new String (Base64.encodeBase64( (containerPath).getBytes()))), PropertyType.STRING) ; 134 135 139 if (relPath.equals("/jcr:content/exo:content")) { 140 FileInputStream fis = new FileInputStream (storage); 141 if(fis.available() == 0) 142 return null; 143 byte[] buffer = new byte[fis.available()]; 144 fis.read(buffer); 145 fis.close(); 146 return new PropertyImpl(getJcrPath("/jcr:content/exo:content"), 147 new StringValue(new String (buffer)), PropertyType.STRING); 148 } 149 150 } catch (Exception e) { 151 e.printStackTrace(); 152 return null; 153 } 154 155 return null; 156 } 157 158 private void cleanContent() { 159 FileOutputStream fos = null; 160 try { 161 fos = new FileOutputStream (storage); 162 fos.write(new byte[0]); 163 fos.close(); 164 } catch (Exception e) { 165 e.printStackTrace(); 166 } 167 168 } 169 170 private void updateContent(Node node) throws RepositoryException { 171 try { 172 FileOutputStream fos = new FileOutputStream (storage); 173 System.out.println("EXO:CONTENT ---- "+node); 174 Property prop = ((NodeImpl)node).getPermanentProperty("exo:content"); 175 if(prop == null) 176 return; 177 byte[] buffer = prop.getString().getBytes(); 180 fos.write(buffer); 181 fos.close(); 182 } catch (Exception e) { 184 e.printStackTrace(); 185 throw new RepositoryException("FileNodeContainer.updateProperty() failed. Reason:"+e); 186 } 187 } 188 189 } 190 | Popular Tags |