1 13 package info.magnolia.cms.module; 14 15 import info.magnolia.cms.beans.config.ModuleLoader; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.core.ItemType; 19 import info.magnolia.cms.core.SystemProperty; 20 import info.magnolia.cms.security.AccessDeniedException; 21 22 import java.io.BufferedOutputStream ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.io.LineNumberReader ; 29 import java.util.Enumeration ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.jar.JarEntry ; 34 import java.util.jar.JarFile ; 35 36 import javax.jcr.PathNotFoundException; 37 import javax.jcr.RepositoryException; 38 39 import org.apache.commons.collections.map.ListOrderedMap; 40 import org.apache.commons.lang.StringUtils; 41 42 43 48 public final class ModuleUtil { 49 50 54 public interface IncludeMatcher { 55 56 60 boolean match(String name); 61 62 67 String transform(String name); 68 } 69 70 74 public static class DirectoryIncludeMatcher implements IncludeMatcher { 75 76 private String path; 77 78 public DirectoryIncludeMatcher(String path) { 79 this.path = path; 80 } 81 82 public boolean match(String name) { 83 return name.startsWith(path); 84 } 85 86 public String transform(String name) { 87 return StringUtils.removeStart(name, path); 88 } 89 } 90 91 94 private ModuleUtil() { 95 } 96 97 100 public static final int DATA_BLOCK_SIZE = 1024 * 1024; 101 102 111 public static void registerProperties(HierarchyManager hm, String name) throws IOException , AccessDeniedException, 112 PathNotFoundException, RepositoryException { 113 Map map = new ListOrderedMap(); 114 115 InputStream stream = ModuleUtil.class.getResourceAsStream("/" + name.replace('.', '/') + ".properties"); LineNumberReader lines = new LineNumberReader (new InputStreamReader (stream)); 120 121 String line = lines.readLine(); 122 while (line != null) { 123 line = line.trim(); 124 if (line.length() > 0 && !line.startsWith("#")) { String key = StringUtils.substringBefore(line, "=").trim(); String value = StringUtils.substringAfter(line, "=").trim(); map.put(key, value); 128 } 129 line = lines.readLine(); 130 } 131 lines.close(); 132 stream.close(); 133 registerProperties(hm, map); 134 } 135 136 public static void registerProperties(HierarchyManager hm, Map map) throws AccessDeniedException, 137 PathNotFoundException, RepositoryException { 138 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { 139 String key = (String ) iter.next(); 140 String value = (String ) map.get(key); 141 142 String name = StringUtils.substringAfterLast(key, "."); String path = StringUtils.substringBeforeLast(key, ".").replace('.', '/'); Content node = createPath(hm, path); 145 node.getNodeData(name, true).setValue(value); 146 } 147 } 148 149 public static Content createPath(HierarchyManager hm, String path) throws AccessDeniedException, 150 PathNotFoundException, RepositoryException { 151 return createPath(hm, path, ItemType.CONTENTNODE); 152 } 153 154 public static Content createPath(HierarchyManager hm, String path, ItemType type) throws AccessDeniedException, 155 PathNotFoundException, RepositoryException { 156 path = StringUtils.removeStart(path, "/"); 158 159 String [] names = path.split("/"); Content node = hm.getRoot(); 161 for (int i = 0; i < names.length; i++) { 162 String name = names[i]; 163 if (node.hasContent(name)) { 164 node = node.getContent(name); 165 } 166 else { 167 node = node.createContent(name, type); 168 } 169 } 170 return node; 171 } 172 173 178 public static void installFiles(JarFile jar) throws Exception { 179 IncludeMatcher matcher = new IncludeMatcher() { 180 181 public boolean match(String name) { 182 if (!name.equals("/") && !name.endsWith("/") && !name.startsWith("CH") && !name.startsWith("META-INF") && !name.endsWith(".JAR")) { return true; 188 } 189 return false; 190 } 191 192 public String transform(String name) { 193 return name; 194 } 195 }; 196 197 installFiles(jar, matcher); 198 } 199 200 public static void installFiles(JarFile jar, final String path) throws Exception { 201 installFiles(jar, new DirectoryIncludeMatcher(path)); 202 } 203 204 211 public static void installFiles(JarFile jar, IncludeMatcher matcher) throws Exception { 212 213 String root = null; 214 try { 216 File f = new File (SystemProperty.getProperty(SystemProperty.MAGNOLIA_APP_ROOTDIR)); 217 if (f.isDirectory()) { 218 root = f.getAbsolutePath(); 219 } 220 } 221 catch (Exception e) { 222 } 224 225 if (root == null) { 226 throw new Exception ("Invalid magnolia " + SystemProperty.MAGNOLIA_APP_ROOTDIR + " path"); } 228 229 Map files = new HashMap (); 230 Enumeration entries = jar.entries(); 231 while (entries.hasMoreElements()) { 232 JarEntry entry = (JarEntry ) entries.nextElement(); 233 String name = entry.getName().toUpperCase(); 234 235 if (matcher.match(name.toUpperCase())) { files.put(new File (root, matcher.transform(name)), entry); 238 } 239 } 240 241 String error = StringUtils.EMPTY; 243 Iterator iter = files.keySet().iterator(); 244 while (iter.hasNext()) { 245 File file = (File ) iter.next(); 246 String s = StringUtils.EMPTY; 247 if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { 248 s = "Can't create directories for " + file.getAbsolutePath(); } 250 else if (!file.getParentFile().canWrite()) { 251 s = "Can't write to " + file.getAbsolutePath(); } 253 if (s.length() > 0) { 254 if (error.length() > 0) { 255 error += "\r\n"; } 257 error += s; 258 } 259 } 260 261 if (error.length() > 0) { 262 throw new Exception ("Errors while installing files: " + error); } 264 265 iter = files.keySet().iterator(); 267 while (iter.hasNext()) { 268 File file = (File ) iter.next(); 269 JarEntry entry = (JarEntry ) files.get(file); 270 271 int byteCount = 0; 272 byte[] data = new byte[DATA_BLOCK_SIZE]; 273 274 InputStream in = null; 275 BufferedOutputStream out = null; 276 277 try { 278 in = jar.getInputStream(entry); 279 out = new BufferedOutputStream (new FileOutputStream (file), DATA_BLOCK_SIZE); 280 281 while ((byteCount = in.read(data, 0, DATA_BLOCK_SIZE)) != -1) { 282 out.write(data, 0, byteCount); 283 } 284 } 285 finally { 286 try { 287 out.close(); 288 } 289 catch (Exception e) { 290 } 292 } 293 } 294 } 295 296 307 public static Content createMinimalConfiguration(Content node, String name, String className, String version) 308 throws AccessDeniedException, PathNotFoundException, RepositoryException { 309 node.createNodeData("version").setValue(version); node.createNodeData("license"); node.createContent("Config"); node.createContent("VirtualURIMapping", ItemType.CONTENTNODE); 314 Content register = node.createContent(ModuleLoader.CONFIG_NODE_REGISTER, ItemType.CONTENTNODE); 315 register.createNodeData("moduleName"); register.createNodeData("moduleDescription"); register.createNodeData("class").setValue(className); register.createNodeData("repository"); register.createContent("sharedRepositories", ItemType.CONTENTNODE); register.createContent("initParams", ItemType.CONTENTNODE); return node; 322 } 323 324 } | Popular Tags |