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
......@@ -194,6 +193,7 @@ public class InitService {
* "userPassword": "", // Unhashed
* }
* </pre>
*
* @throws ServiceException service exception
*/
public void init(final JSONObject requestJSONObject) throws ServiceException {
......@@ -380,6 +380,7 @@ public class InitService {
* ....
* }
* </pre>
*
* @throws RepositoryException repository exception
*/
public void archiveDate(final JSONObject article) throws RepositoryException {
......@@ -468,6 +469,7 @@ public class InitService {
* "userPassowrd": "" // Unhashed
* }
* </pre>
*
* @throws Exception exception
*/
private void initAdmin(final JSONObject requestJSONObject) 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);
......
......@@ -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
......@@ -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);
......@@ -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"));
}
......
......@@ -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 {
......@@ -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) {
......@@ -132,105 +128,41 @@ public final class Skins {
* @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;
Templates.MAIN_CFG.setDirectoryForTemplateLoading(new File(skinPath));
final ServletContext servletContext = SoloServletListener.getServletContext();
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");
@SuppressWarnings("unchecked")
final Set<String> resourcePaths = servletContext.getResourcePaths("/skins");
return ret;
for (final String path : resourcePaths) {
if (path.startsWith(".")) {
continue;
}
for (int i = 0; i < skinDirs.length; i++) {
final File file = skinDirs[i];
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