Commit f498788d authored by Liang Ding's avatar Liang Ding

#11755

需要拉 latke 1.0.10-SNAPSHOT 进行构建,latke 1.0.10 预计本周五发布
parent 1caf8219
......@@ -18,9 +18,8 @@ package org.b3log.solo.processor.renderer;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.File;
import java.io.IOException;
import org.b3log.latke.logging.Level;
import javax.servlet.ServletContext;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.servlet.HTTPRequestContext;
import org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer;
......@@ -32,7 +31,7 @@ import org.b3log.solo.SoloServletListener;
* renderer for administrator console and initialization rendering.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.1, Nov 17, 2013
* @version 1.0.1.1, Apr 15, 2014
* @since 0.4.1
*/
public final class ConsoleRenderer extends AbstractFreeMarkerRenderer {
......@@ -50,13 +49,10 @@ public final class ConsoleRenderer extends AbstractFreeMarkerRenderer {
static {
TEMPLATE_CFG = new Configuration();
TEMPLATE_CFG.setDefaultEncoding("UTF-8");
try {
final String webRootPath = SoloServletListener.getWebRoot();
TEMPLATE_CFG.setDirectoryForTemplateLoading(new File(webRootPath));
} catch (final IOException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
}
final ServletContext servletContext = SoloServletListener.getServletContext();
TEMPLATE_CFG.setServletContextForTemplateLoading(servletContext, "");
}
@Override
......
......@@ -16,13 +16,12 @@
package org.b3log.solo.service;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.inject.Inject;
import javax.servlet.ServletContext;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
......@@ -62,7 +61,7 @@ import org.json.JSONObject;
* B3log Solo initialization service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.1.7, Jun 28, 2013
* @version 1.0.2.7, Apr 15, 2014
* @since 0.4.0
*/
@Service
......@@ -168,24 +167,24 @@ public class InitService {
/**
* Initializes B3log Solo.
*
*
* <p>
* Initializes the followings in sequence:
* <ol>
* <li>Statistic.</li>
* <li>Preference.</li>
* <li>Administrator.</li>
* </ol>
* <ol>
* <li>Statistic.</li>
* <li>Preference.</li>
* <li>Administrator.</li>
* </ol>
* </p>
*
*
* <p>
* We will try to initialize B3log Solo 3 times at most.
* We will try to initialize B3log Solo 3 times at most.
* </p>
*
*
* <p>
* Posts "Hello World!" article and its comment while B3log Solo initialized.
* Posts "Hello World!" article and its comment while B3log Solo initialized.
* </p>
*
*
* @param requestJSONObject the specified request json object, for example,
* <pre>
* {
......@@ -194,6 +193,7 @@ public class InitService {
* "userPassword": "", // Unhashed
* }
* </pre>
*
* @throws ServiceException service exception
*/
public void init(final JSONObject requestJSONObject) throws ServiceException {
......@@ -323,7 +323,7 @@ public class InitService {
/**
* Adds the specified "Hello World" article.
*
*
* @param article the specified "Hello World" article
* @return generated article id
* @throws RepositoryException repository exception
......@@ -380,6 +380,7 @@ public class InitService {
* ....
* }
* </pre>
*
* @throws RepositoryException repository exception
*/
public void archiveDate(final JSONObject article) throws RepositoryException {
......@@ -457,7 +458,7 @@ public class InitService {
}
/**
* Initializes administrator with the specified request json object, and
* Initializes administrator with the specified request json object, and
* then logins it.
*
* @param requestJSONObject the specified request json object, for example,
......@@ -468,6 +469,7 @@ public class InitService {
* "userPassowrd": "" // Unhashed
* }
* </pre>
*
* @throws Exception exception
*/
private void initAdmin(final JSONObject requestJSONObject) throws Exception {
......@@ -513,7 +515,7 @@ public class InitService {
/**
* Initializes reply notification template.
*
*
* @throws Exception exception
*/
private void initReplyNotificationTemplate() throws Exception {
......@@ -574,7 +576,7 @@ public class InitService {
ret.put(Skin.SKIN_DIR_NAME, skinDirName);
final String skinName = Skins.getSkinName(skinDirName);
final String skinName = Latkes.getSkinName(skinDirName);
ret.put(Skin.SKIN_NAME, skinName);
......@@ -586,7 +588,7 @@ public class InitService {
skinArray.put(skin);
final String name = Skins.getSkinName(dirName);
final String name = Latkes.getSkinName(dirName);
skin.put(Skin.SKIN_NAME, name);
skin.put(Skin.SKIN_DIR_NAME, dirName);
......@@ -594,15 +596,9 @@ public class InitService {
ret.put(Skin.SKINS, skinArray.toString());
try {
final String webRootPath = SoloServletListener.getWebRoot();
final String skinPath = webRootPath + Skin.SKINS + "/" + skinDirName;
final ServletContext servletContext = SoloServletListener.getServletContext();
Templates.MAIN_CFG.setDirectoryForTemplateLoading(new File(skinPath));
} catch (final IOException e) {
LOGGER.log(Level.ERROR, "Loads skins error!", e);
throw new IllegalStateException(e);
}
Templates.MAIN_CFG.setServletContextForTemplateLoading(servletContext, skinDirName);
TimeZones.setTimeZone(INIT_TIME_ZONE_ID);
......@@ -616,7 +612,7 @@ public class InitService {
/**
* Sets archive date article repository with the specified archive date article repository.
*
*
* @param archiveDateArticleRepository the specified archive date article repository
*/
public void setArchiveDateArticleRepository(final ArchiveDateArticleRepository archiveDateArticleRepository) {
......@@ -625,7 +621,7 @@ public class InitService {
/**
* Sets archive date repository with the specified archive date repository.
*
*
* @param archiveDateRepository the specified archive date repository
*/
public void setArchiveDateRepository(final ArchiveDateRepository archiveDateRepository) {
......@@ -634,7 +630,7 @@ public class InitService {
/**
* Sets the article repository with the specified article repository.
*
*
* @param articleRepository the specified article repository
*/
public void setArticleRepository(final ArticleRepository articleRepository) {
......@@ -643,7 +639,7 @@ public class InitService {
/**
* Sets the user repository with the specified user repository.
*
*
* @param userRepository the specified user repository
*/
public void setUserRepository(final UserRepository userRepository) {
......@@ -652,7 +648,7 @@ public class InitService {
/**
* Sets the preference repository with the specified preference repository.
*
*
* @param preferenceRepository the specified preference repository
*/
public void setPreferenceRepository(final PreferenceRepository preferenceRepository) {
......@@ -661,7 +657,7 @@ public class InitService {
/**
* Sets the statistic repository with the specified statistic repository.
*
*
* @param statisticRepository the specified statistic repository
*/
public void setStatisticRepository(final StatisticRepository statisticRepository) {
......@@ -670,7 +666,7 @@ public class InitService {
/**
* Sets the tag repository with the specified tag repository.
*
*
* @param tagRepository the specified tag repository
*/
public void setTagRepository(final TagRepository tagRepository) {
......@@ -679,7 +675,7 @@ public class InitService {
/**
* Sets the tag article repository with the specified tag article repository.
*
*
* @param tagArticleRepository the specified tag article repository
*/
public void setTagArticleRepository(final TagArticleRepository tagArticleRepository) {
......@@ -688,7 +684,7 @@ public class InitService {
/**
* Sets the comment repository with the specified comment repository.
*
*
* @param commentRepository the specified comment repository
*/
public void setCommentRepository(final CommentRepository commentRepository) {
......@@ -697,16 +693,16 @@ public class InitService {
/**
* Sets the language service with the specified language service.
*
*
* @param langPropsService the specified language service
*/
public void setLangPropsService(final LangPropsService langPropsService) {
this.langPropsService = langPropsService;
}
/**
* Sets the plugin manager with the specified plugin manager.
*
*
* @param pluginManager the specified plugin manager
*/
public void setPluginManager(final PluginManager pluginManager) {
......
......@@ -16,20 +16,17 @@
package org.b3log.solo.service;
import java.io.IOException;
import org.b3log.latke.service.LangPropsService;
import org.b3log.solo.util.TimeZones;
import org.b3log.solo.util.Skins;
import org.json.JSONException;
import java.io.File;
import java.util.Iterator;
import java.util.Locale;
import java.util.Set;
import javax.inject.Inject;
import javax.servlet.ServletContext;
import org.b3log.latke.Latkes;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.Transaction;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.service.annotation.Service;
......@@ -48,7 +45,6 @@ import static org.b3log.solo.model.Skin.SKINS;
import static org.b3log.solo.model.Skin.SKIN_DIR_NAME;
import static org.b3log.solo.model.Skin.SKIN_NAME;
import static org.b3log.solo.util.Skins.getSkinDirNames;
import static org.b3log.solo.util.Skins.getSkinName;
import static org.b3log.solo.util.Skins.setDirectoryForTemplateLoading;
......@@ -56,7 +52,7 @@ import static org.b3log.solo.util.Skins.setDirectoryForTemplateLoading;
* Preference management service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.7, Jul 18, 2013
* @version 1.0.1.7, Apr 15, 2014
* @since 0.4.0
*/
@Service
......@@ -81,7 +77,7 @@ public class PreferenceMgmtService {
/**
* Loads skins for the specified preference and initializes templates loading.
*
*
* <p>
* If the skins directory has been changed, persists the change into preference.
* </p>
......@@ -101,7 +97,7 @@ public class PreferenceMgmtService {
for (final String dirName : skinDirNames) {
final JSONObject skin = new JSONObject();
final String name = getSkinName(dirName);
final String name = Latkes.getSkinName(dirName);
if (null == name) {
LOGGER.log(Level.WARN, "The directory[{0}] does not contain any skin, ignored it", dirName);
......@@ -159,7 +155,7 @@ public class PreferenceMgmtService {
/**
* Updates the reply notification template with the specified reply notification template.
*
*
* @param replyNotificationTemplate the specified reply notification template
* @throws ServiceException service exception
*/
......@@ -201,7 +197,7 @@ public class PreferenceMgmtService {
try {
final String skinDirName = preference.getString(Skin.SKIN_DIR_NAME);
final String skinName = Skins.getSkinName(skinDirName);
final String skinName = Latkes.getSkinName(skinDirName);
preference.put(Skin.SKIN_NAME, skinName);
final Set<String> skinDirNames = Skins.getSkinDirNames();
......@@ -212,15 +208,11 @@ public class PreferenceMgmtService {
skinArray.put(skin);
final String name = Skins.getSkinName(dirName);
final String name = Latkes.getSkinName(dirName);
skin.put(Skin.SKIN_NAME, name);
skin.put(Skin.SKIN_DIR_NAME, dirName);
}
final String webRootPath = SoloServletListener.getWebRoot();
final String skinPath = webRootPath + Skin.SKINS + "/" + skinDirName;
LOGGER.log(Level.DEBUG, "Skin path[{0}]", skinPath);
preference.put(Skin.SKINS, skinArray.toString());
......@@ -250,23 +242,14 @@ public class PreferenceMgmtService {
transaction.commit();
Templates.MAIN_CFG.setDirectoryForTemplateLoading(new File(skinPath));
} catch (final JSONException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates preference failed", e);
throw new ServiceException(langPropsService.get("updateFailLabel"));
} catch (final RepositoryException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates preference failed", e);
throw new ServiceException(langPropsService.get("updateFailLabel"));
} catch (final IOException e) {
final ServletContext servletContext = SoloServletListener.getServletContext();
Templates.MAIN_CFG.setServletContextForTemplateLoading(servletContext, skinDirName);
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates preference failed", e);
throw new ServiceException(langPropsService.get("updateFailLabel"));
}
......@@ -276,7 +259,7 @@ public class PreferenceMgmtService {
/**
* Sets the preference repository with the specified preference repository.
*
*
* @param preferenceRepository the specified preference repository
*/
public void setPreferenceRepository(final PreferenceRepository preferenceRepository) {
......@@ -285,7 +268,7 @@ public class PreferenceMgmtService {
/**
* Sets the language service with the specified language service.
*
*
* @param langPropsService the specified language service
*/
public void setLangPropsService(final LangPropsService langPropsService) {
......
......@@ -16,16 +16,14 @@
package org.b3log.solo.util;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.b3log.latke.Keys;
import javax.servlet.ServletContext;
import org.b3log.latke.Latkes;
import org.b3log.latke.ioc.LatkeBeanManager;
import org.b3log.latke.ioc.Lifecycle;
......@@ -38,14 +36,13 @@ import org.b3log.latke.util.Locales;
import org.b3log.latke.util.Stopwatchs;
import org.b3log.latke.util.freemarker.Templates;
import org.b3log.solo.SoloServletListener;
import static org.b3log.solo.model.Skin.*;
/**
* Skin utilities.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.2.6, Jun 12, 2012
* @version 1.0.3.6, Apr 15, 2014
* @since 0.3.1
*/
public final class Skins {
......@@ -66,13 +63,13 @@ public final class Skins {
private Skins() {}
/**
* Fills the specified data model with the current skink's (WebRoot/skins/${skinName}/lang/lang_xx_XX.properties) and
* Fills the specified data model with the current skink's (WebRoot/skins/${skinName}/lang/lang_xx_XX.properties) and
* core language (WebRoot/WEB-INF/classes/lang_xx_XX.properties) configurations.
*
*
* @param localeString the specified locale string
* @param currentSkinDirName the specified current skin directory name
* @param dataModel the specified data model
* @throws ServiceException service exception
* @throws ServiceException service exception
*/
public static void fillLangs(final String localeString, final String currentSkinDirName, final Map<String, Object> dataModel)
throws ServiceException {
......@@ -88,17 +85,16 @@ public final class Skins {
LOGGER.log(Level.INFO, "Loading skin [dirName={0}, locale={1}]", new Object[] {currentSkinDirName, localeString});
langs = new HashMap<String, String>();
final String webRootPath = SoloServletListener.getWebRoot();
final String language = Locales.getLanguage(localeString);
final String country = Locales.getCountry(localeString);
final ServletContext servletContext = SoloServletListener.getServletContext();
final InputStream inputStream = servletContext.getResourceAsStream(
"/skins/" + currentSkinDirName + "/lang/lang_" + language + '_' + country + ".properties");
final Properties props = new Properties();
props.load(
new FileReader(
webRootPath + "skins" + File.separator + currentSkinDirName + File.separator + Keys.LANGUAGE + File.separator
+ Keys.LANGUAGE + '_' + language + '_' + country + ".properties"));
props.load(inputStream);
final Set<Object> keys = props.keySet();
for (final Object key : keys) {
......@@ -111,7 +107,7 @@ public final class Skins {
}
dataModel.putAll(langs); // Fills the current skin's language configurations
// Fills the core language configurations
final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
final LangPropsService langPropsService = beanManager.getReference(LangPropsServiceImpl.class);
......@@ -128,109 +124,45 @@ public final class Skins {
/**
* Sets the directory for template loading with the specified skin directory
* name, and sets the directory for mobile request template loading.
*
*
* @param skinDirName the specified skin directory name
*/
public static void setDirectoryForTemplateLoading(final String skinDirName) {
try {
final String webRootPath = SoloServletListener.getWebRoot();
final String skinPath = webRootPath + SKINS + File.separator + skinDirName;
final ServletContext servletContext = SoloServletListener.getServletContext();
Templates.MAIN_CFG.setDirectoryForTemplateLoading(new File(skinPath));
Templates.MOBILE_CFG.setDirectoryForTemplateLoading(new File(webRootPath + SKINS + File.separator + "mobile"));
} catch (final IOException e) {
LOGGER.log(Level.ERROR, "Loads skins error!", e);
throw new IllegalStateException(e);
}
Templates.MAIN_CFG.setServletContextForTemplateLoading(servletContext, "/skins/" + skinDirName);
Templates.MOBILE_CFG.setServletContextForTemplateLoading(servletContext, "/skins/mobile");
}
/**
* Gets all skin directory names. Scans the
* {@linkplain SoloServletListener#getWebRoot() Web root}/skins/ directory,
* Gets all skin directory names. Scans the /skins/ directory,
* using the subdirectory of it as the skin directory name, for example,
* <pre>
* ${Web root}/skins/
* <b>default</b>/
* <b>mobile</b>/
* <b>classic</b>/
* </pre>
* Skips files that name starts with . and {@linkplain File#isHidden()
* hidden} files.
* </pre>.
*
* @return a set of skin name, returns an empty set if not found
*/
public static Set<String> getSkinDirNames() {
final String webRootPath = SoloServletListener.getWebRoot();
final File skins = new File(webRootPath + "skins" + File.separator);
final File[] skinDirs = skins.listFiles(new FileFilter() {
@Override
public boolean accept(final File file) {
return file.isDirectory() && !file.getName().startsWith(".");
}
});
final ServletContext servletContext = SoloServletListener.getServletContext();
final Set<String> ret = new HashSet<String>();
if (null == skinDirs) {
LOGGER.error("Skin directory is null");
return ret;
}
@SuppressWarnings("unchecked")
final Set<String> resourcePaths = servletContext.getResourcePaths("/skins");
for (int i = 0; i < skinDirs.length; i++) {
final File file = skinDirs[i];
for (final String path : resourcePaths) {
if (path.startsWith(".")) {
continue;
}
ret.add(file.getName());
ret.add(path.substring("/skins".length() + 1, path.length() - 1));
}
return ret;
}
/**
* Gets the skin name for the specified skin directory name. The skin name
* was configured in skin.properties file({@code name} as the key) under
* skin directory specified by the given skin directory name.
*
* @param skinDirName the given skin directory name
* @return skin name, returns {@code null} if not found or error occurs
* @see #getSkinDirNames()
*/
public static String getSkinName(final String skinDirName) {
final String webRootPath = SoloServletListener.getWebRoot();
final File skins = new File(webRootPath + "skins" + File.separator);
final File[] skinDirs = skins.listFiles(new FileFilter() {
@Override
public boolean accept(final File pathname) {
return pathname.isDirectory() && pathname.getName().equals(skinDirName) ? true : false;
}
});
if (null == skinDirs) {
LOGGER.error("Skin directory is null");
return null;
}
if (1 != skinDirs.length) {
LOGGER.log(Level.ERROR, "Skin directory count[{0}]", skinDirs.length);
return null;
}
try {
final Properties ret = new Properties();
final String skinPropsPath = skinDirs[0].getPath() + File.separator + "skin.properties";
ret.load(new FileReader(skinPropsPath));
return ret.getProperty("name");
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Read skin configuration error[msg={0}]", e.getMessage());
return null;
}
}
}
......@@ -29,7 +29,7 @@
<properties>
<servlet.version>2.5</servlet.version>
<slf4j.version>1.7.5</slf4j.version>
<org.b3log.latke.version>1.0.8</org.b3log.latke.version>
<org.b3log.latke.version>1.0.10-SNAPSHOT</org.b3log.latke.version>
<maven-gae-plugin.version>0.9.0</maven-gae-plugin.version>
<gae.version>1.8.1.1</gae.version>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment