1 12 package info.magnolia.cms.beans.config; 13 14 import info.magnolia.cms.core.Path; 15 import info.magnolia.cms.core.SystemProperty; 16 import info.magnolia.cms.core.ie.DataTransporter; 17 import info.magnolia.context.MgnlContext; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Collection ; 22 import java.util.Comparator ; 23 import java.util.Iterator ; 24 import java.util.Set ; 25 import java.util.SortedSet ; 26 import java.util.TreeSet ; 27 28 import org.apache.commons.io.FileUtils; 29 import org.apache.commons.io.filefilter.FileFilterUtils; 30 import org.apache.commons.io.filefilter.IOFileFilter; 31 import org.apache.commons.lang.StringUtils; 32 import org.slf4j.Logger; 33 import org.slf4j.LoggerFactory; 34 35 36 41 public final class Bootstrapper { 42 43 46 private static Logger log = LoggerFactory.getLogger(Bootstrapper.class); 47 48 52 public interface BootstrapFilter { 53 54 boolean accept(String filename); 55 } 56 57 60 private Bootstrapper() { 61 } 63 64 71 public static void bootstrapRepositories(String [] bootdirs, BootstrapFilter filter) { 72 73 if (log.isInfoEnabled()) { 74 log.info("-----------------------------------------------------------------"); log.info("Trying to initialize repositories from:"); 76 for (int i = 0; i < bootdirs.length; i++) { 77 log.info(bootdirs[i]); 78 } 79 log.info("-----------------------------------------------------------------"); } 81 82 Iterator repositoryNames = ContentRepository.getAllRepositoryNames(); 83 while (repositoryNames.hasNext()) { 84 String repositoryName = (String ) repositoryNames.next(); 85 86 if (!bootstrapRepository(repositoryName, filter, bootdirs)) { 87 break; 89 } 90 91 log.info("Repository [{}] has been initialized.", repositoryName); } 93 } 94 95 101 public static boolean bootstrapRepository(String repositoryName, BootstrapFilter filter) { 102 return bootstrapRepository(repositoryName, filter, getBootstrapDirs()); 103 } 104 105 112 public static boolean bootstrapRepository(String repositoryName, BootstrapFilter filter, String [] bootdirs) { 113 Set xmlfileset = getBootstrapFiles(bootdirs, repositoryName, filter); 114 115 if (xmlfileset.isEmpty()) { 116 log.info("No bootstrap files found for repository [{}], skipping...", repositoryName); return true; 118 } 119 120 log.info("Trying to import content from {} files into repository [{}]", Integer.toString(xmlfileset.size()), repositoryName); 122 123 return bootstrapFiles(repositoryName, xmlfileset); 124 } 125 126 132 public static boolean bootstrapFiles(String repositoryName, Set filesSet) { 133 File [] files = (File []) filesSet.toArray(new File [filesSet.size()]); 134 return bootstrapFiles(repositoryName, files); 135 } 136 137 143 public static boolean bootstrapFiles(String repositoryName, File [] files) { 144 try { 145 for (int k = 0; k < files.length; k++) { 146 File xmlFile = files[k]; 147 if(log.isDebugEnabled()){ 148 log.debug("execute importfile {}", xmlFile); 149 } 150 DataTransporter.executeBootstrapImport(xmlFile, repositoryName); 151 } 152 } 153 catch (IOException ioe) { 154 log.error(ioe.getMessage(), ioe); 155 } 156 catch (OutOfMemoryError e) { 157 int maxMem = (int) (Runtime.getRuntime().maxMemory() / 1024 / 1024); 158 int needed = Math.max(256, maxMem + 128); 159 log.error("Unable to complete bootstrapping: out of memory.\n" + "{} MB were not enough, try to increase the amount of memory available by adding the -Xmx{}m parameter to the server startup script.\n" + "You will need to completely remove the magnolia webapp before trying again", Integer.toString(maxMem), Integer.toString(needed)); 163 return false; 164 } 165 return true; 166 } 167 168 177 public static SortedSet getBootstrapFiles(String [] bootdirs, final String repositoryName, final BootstrapFilter filter) { 178 SortedSet xmlfileset = new TreeSet (new Comparator () { 179 180 public int compare(Object file1obj, Object file2obj) { 182 File file1 = (File ) file1obj; 183 File file2 = (File ) file2obj; 184 185 String name1 = getName(file1); String name2 = getName(file2); 188 String ext1 = getExtension(file1); 189 String ext2 = getExtension(file2); 190 191 if(StringUtils.equals(ext1, ext2)){ 192 if (name1.length() != name2.length()) { 194 return name1.length() - name2.length(); 195 } 196 } 197 else{ 198 if(ext1.equalsIgnoreCase("xml")){ 200 return -1; 201 } 202 else if(ext2.equalsIgnoreCase("properties")){ 203 return 1; 204 } 205 } 206 207 return name1.compareTo(name2); 208 } 209 210 }); 211 212 for (int j = 0; j < bootdirs.length; j++) { 213 String bootdir = bootdirs[j]; 214 File xmldir = new File (bootdir); 215 if (!xmldir.exists() || !xmldir.isDirectory()) { 216 continue; 217 } 218 219 Collection files = FileUtils.listFiles(xmldir, new IOFileFilter(){ 220 public boolean accept(File file) { 221 return accept(file.getParentFile(), file.getName()); 222 } 223 public boolean accept(File dir, String name) { 224 return name.startsWith(repositoryName + ".") 225 && filter.accept(name) 226 && (name.endsWith(DataTransporter.XML) || name.endsWith(DataTransporter.ZIP) || name 227 .endsWith(DataTransporter.GZ) || name.endsWith(DataTransporter.PROPERTIES)); 228 } 229 }, FileFilterUtils.trueFileFilter()); 230 231 xmlfileset.addAll(files); 232 } 233 234 return xmlfileset; 235 } 236 237 private static String getExtension(File file){ 238 String ext = StringUtils.substringAfterLast(file.getName(),"."); 239 if(("." + ext).equals(DataTransporter.GZ) || ("." + ext).equals(DataTransporter.ZIP)){ 240 ext = StringUtils.substringAfterLast(StringUtils.substringBeforeLast(file.getName(), "."), "."); 241 } 242 return ext; 243 } 244 245 private static String getName(File file){ 246 String name = StringUtils.substringBeforeLast(file.getName(),"."); 247 if(name.endsWith(DataTransporter.XML) || name.endsWith(DataTransporter.PROPERTIES)){ 248 name = StringUtils.substringBeforeLast(file.getName(),"."); 249 } 250 return name; 251 } 252 253 257 public static String [] getBootstrapDirs() { 258 String bootdirProperty = SystemProperty.getProperty(SystemProperty.MAGNOLIA_BOOTSTRAP_ROOTDIR); 259 260 if (StringUtils.isEmpty(bootdirProperty)) { 261 return new String [0]; 262 } 263 264 String [] bootDirs = StringUtils.split(bootdirProperty); 265 266 for (int j = 0; j < bootDirs.length; j++) { 268 bootDirs[j] = Path.getAbsoluteFileSystemPath(bootDirs[j]); 269 } 270 return bootDirs; 271 } 272 273 } 274 | Popular Tags |