1 25 package org.snipsnap.config; 26 27 import org.snipsnap.app.Application; 28 import org.snipsnap.app.ApplicationManager; 29 import org.snipsnap.app.ApplicationStorage; 30 import org.snipsnap.container.Components; 31 import org.snipsnap.snip.HomePage; 32 import org.snipsnap.snip.Snip; 33 import org.snipsnap.snip.SnipSpace; 34 import org.snipsnap.snip.XMLSnipImport; 35 import org.snipsnap.snip.label.RenderEngineLabel; 36 import org.snipsnap.user.Permissions; 37 import org.snipsnap.user.Roles; 38 import org.snipsnap.user.User; 39 import org.snipsnap.user.UserManager; 40 import org.snipsnap.user.UserManagerFactory; 41 import org.snipsnap.net.admin.ThemeHelper; 42 43 import java.io.*; 44 import java.util.Collection ; 45 import java.util.Locale ; 46 import java.util.Properties ; 47 import java.util.Map ; 48 49 53 public class InitializeDatabase { 54 55 private static ThreadLocal output = new ThreadLocal (); 56 57 private static void message(String message) { 58 if (null != output.get()) { 59 PrintWriter writer = (PrintWriter) output.get(); 60 writer.println("[" + Application.get().getConfiguration().getName() + "] " + message); 61 writer.flush(); 62 } 63 } 64 65 public static String init(Configuration config, Writer w) throws Exception { 66 output.set(new PrintWriter(w)); 67 68 Application app = Application.get(); 69 app.setConfiguration(config); 70 71 ApplicationManager appManager = (ApplicationManager) Components.getComponent(ApplicationManager.class); 72 Collection prefixes = appManager.getPrefixes(); 73 74 if (prefixes != null && prefixes.contains(config.getPrefix())) { 75 throw new Exception ("the prefix " + config.getPrefix() + " already exists"); 76 } 77 78 Properties prefixProps = appManager.createApplication(config.getName(), config.getPrefix()); 79 String appOid = prefixProps.getProperty(ApplicationStorage.OID); 80 try { 81 message("created application oid: " + appOid); 82 app.storeObject(Application.OID, appOid); 83 84 new File(config.getFileStore()).mkdirs(); 85 88 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 90 91 createAdministrator(config); 92 93 String ping = config.get(Configuration.APP_PERM_WEBLOGSPING); 95 String notify = config.get(Configuration.APP_PERM_NOTIFICATION); 96 config.set(Configuration.APP_PERM_WEBLOGSPING, "deny"); 97 config.set(Configuration.APP_PERM_NOTIFICATION, "deny"); 98 99 InputStream data = getLocalizedResource("i18n.snipsnap", "snip", config.getLocale()); 101 XMLSnipImport.load(data, XMLSnipImport.OVERWRITE | XMLSnipImport.IMPORT_USERS | XMLSnipImport.IMPORT_SNIPS); 102 103 104 message("loading defaults into configuration space"); 105 createConfigSnipFromFile(Configuration.SNIPSNAP_CONFIG_API, "/defaults/apidocs.txt", space); 107 createConfigSnipFromFile(Configuration.SNIPSNAP_CONFIG_ASIN, "/defaults/asinservices.txt", space); 108 createConfigSnipFromFile(Configuration.SNIPSNAP_CONFIG_BOOK, "/defaults/bookservices.txt", space); 109 createConfigSnipFromFile(Configuration.SNIPSNAP_CONFIG_PING, "/defaults/weblogsping.txt", space); 110 createConfigSnipFromFile(Configuration.SNIPSNAP_CONFIG_ROBOTS, "/defaults/robotdetect.txt", space); 111 createConfigSnipFromFile(Configuration.SNIPSNAP_CONFIG_ROBOTS_TXT, "/defaults/robots.txt", space); 112 createConfigSnipFromFile(Configuration.SNIPSNAP_CONFIG_WIKI, "/defaults/intermap.txt", space); 113 114 Map themeFiles = ThemeHelper.getThemeDocuments(config, ThemeHelper.FILES); 115 XMLSnipImport.load(new FileInputStream((File) themeFiles.get(config.getTheme())), 116 XMLSnipImport.OVERWRITE | XMLSnipImport.IMPORT_SNIPS); 117 118 postFirstBlog(config, space); 119 120 config.set(Configuration.APP_PERM_WEBLOGSPING, ping); 121 config.set(Configuration.APP_PERM_NOTIFICATION, notify); 122 123 ConfigurationManager configManager = ConfigurationManager.getInstance(); 124 configManager.addConfiguration(appOid, config); 125 126 storeConfiguration(config, space); 128 129 } catch (Exception e) { 130 appManager.removeApplication(appOid); 131 config.getFileStore(appOid).delete(); 132 e.printStackTrace(); 133 throw e; 134 } 135 136 return appOid; 137 } 138 139 public static void createConfigSnipFromFile(String name, String file, SnipSpace space) throws IOException { 140 String content = getResourceAsString(InitializeDatabase.class.getResourceAsStream(file)); 141 createConfigSnip(name, content, space); 142 } 143 144 public static Snip createConfigSnip(String name, String content, SnipSpace space) { 145 Snip snip = space.create(name, content); 146 snip.getPermissions().add(Permissions.EDIT_SNIP, Roles.ADMIN); 147 snip.getPermissions().add(Permissions.ATTACH_TO_SNIP, Roles.ADMIN); 148 snip.getLabels().addLabel(new RenderEngineLabel("RenderEngine", "org.snipsnap.render.PlainTextRenderEngine")); 149 space.systemStore(snip); 150 return snip; 151 } 152 153 private static void postFirstBlog(Configuration config, SnipSpace space) throws IOException { 154 message("posting initial weblog entry"); 155 String weblogPost = getResourceAsString(getLocalizedResource("i18n.welcome", "blog", config.getLocale())); 156 String title = weblogPost.substring(0, weblogPost.indexOf('\n')); 157 weblogPost = weblogPost.substring(weblogPost.indexOf('\n') + 1); 158 space.getBlog().post(weblogPost, title); 159 } 160 161 public final static String LOGO_FILE = "_logofile"; 162 public final static String LOGO_FILE_TYPE = "_logofiletype"; 163 164 private static void storeConfiguration(Configuration config, SnipSpace space) throws IOException { 165 String logoFileName = config.get(LOGO_FILE); 166 String logoFileType = config.get(LOGO_FILE_TYPE); 167 config.getProperties().remove(LOGO_FILE); 168 config.getProperties().remove(LOGO_FILE_TYPE); 169 170 message("creating configuration snip '" + Configuration.SNIPSNAP_CONFIG + "'"); 171 config.setConfigured("true"); 172 ByteArrayOutputStream configStream = new ByteArrayOutputStream(); 173 config.store(configStream); 174 Snip configSnip = createConfigSnip(Configuration.SNIPSNAP_CONFIG, 175 new String (configStream.toString("UTF-8")), 176 space); 177 String logo = config.getLogo(); 178 if (logoFileName != null && !"".equals(logoFileName)) { 179 File logoFile = new File(logoFileName); 180 File relativeFileLocation = new File(new File(Configuration.SNIPSNAP_CONFIG), logo); 181 new File(config.getFilePath(), Configuration.SNIPSNAP_CONFIG).mkdirs(); 182 File attFile = new File(config.getFilePath(), relativeFileLocation.getPath()); 183 FileInputStream in = new FileInputStream(logoFile); 184 FileOutputStream out = new FileOutputStream(attFile); 185 byte[] buf = new byte[4096]; 186 int length = 0; 187 while ((length = in.read(buf)) != -1) { 188 out.write(buf, 0, length); 189 } 190 out.close(); 191 in.close(); 192 configSnip.getAttachments().addAttachment(logo, logoFileType, attFile.length(), 193 relativeFileLocation.getPath()); 194 space.store(configSnip); 195 } 196 } 197 198 private static User createAdministrator(Configuration config) { 199 message("creating administrator account and snip"); 201 UserManager um = UserManagerFactory.getInstance(); 202 User admin = um.load(config.getAdminLogin()); 203 if (admin != null) { 204 message("overriding administrator: " + admin); 205 um.remove(admin); 206 } 207 admin = um.create(config.getAdminLogin(), config.getAdminPassword(), config.getAdminEmail()); 208 admin.getRoles().add(Roles.ADMIN); 209 admin.getRoles().add(Roles.EDITOR); 210 um.store(admin); 211 212 config.setAdminPassword(admin.getPasswd()); 214 215 Application.get().setUser(admin); 217 HomePage.create(config.getAdminLogin()); 218 219 return admin; 220 } 221 222 223 private static String getResourceAsString(InputStream is) throws IOException { 224 BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8")); 225 StringBuffer contents = new StringBuffer (); 226 String line = null; 227 while ((line = in.readLine()) != null) { 228 contents.append(line).append("\n"); 229 } 230 return contents.toString(); 231 } 232 233 241 private static InputStream getLocalizedResource(String resource, String ext, Locale locale) { 242 InputStream is = findLocalizedResource(resource, ext, locale); 243 if (is == null) { 244 is = getResource(resource, null, null, null, ext); 245 } 246 return is; 247 } 248 249 255 private static InputStream findLocalizedResource(String base, String ext, Locale locale) { 256 String language = locale.getLanguage(); 257 String country = locale.getCountry(); 258 String variant = locale.getVariant(); 259 260 InputStream is = null; 261 if ((is = getResource(base, language, country, variant, ext)) != null) { 262 return is; 263 } else if ((is = getResource(base, language, country, null, ext)) != null) { 264 return is; 265 } else if ((is = getResource(base, language, null, null, ext)) != null) { 266 return is; 267 } 268 return getResource(base, "en", null, null, ext); 269 } 270 271 278 private static InputStream getResource(String base, String language, String country, String variant, String ext) { 279 String file = "/" + base.replace('.', '/') + 280 (language != null ? "_" + language : "") + 281 (country != null ? "_" + country : "") + 282 (variant != null ? "_" + variant : "") + 283 "." + ext; 284 return InitializeDatabase.class.getResourceAsStream(file); 285 } 286 } 287 | Popular Tags |