Skip to content

Commit

Permalink
IoC 化
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jun 26, 2013
1 parent 44b5c8d commit ddebc54
Show file tree
Hide file tree
Showing 52 changed files with 677 additions and 705 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ public final class SymphonyServletListener extends AbstractServletListener {
public void contextInitialized(final ServletContextEvent servletContextEvent) {
Stopwatchs.start("Context Initialized");

// For CloudFoundry
// System.out.println(System.getenv("VCAP_SERVICES"));
// LOGGER.info(System.getenv("VCAP_SERVICES"));

super.contextInitialized(servletContextEvent);

Skins.loadSkin();
Expand Down Expand Up @@ -110,7 +106,7 @@ public void sessionDestroyed(final HttpSessionEvent httpSessionEvent) {
final JSONObject user = (JSONObject) userObj;

try {
UserMgmtService.getInstance().updateOnlineStatus(user.optString(Keys.OBJECT_ID), false);
new UserMgmtService().updateOnlineStatus(user.optString(Keys.OBJECT_ID), false);
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, "Changes user online from [true] to [false] failed", e);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/b3log/symphony/api/BroadcastProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.b3log.symphony.api;

import java.util.List;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.b3log.latke.servlet.HTTPRequestContext;
Expand Down Expand Up @@ -44,7 +45,8 @@ public final class BroadcastProcessor {
/**
* Article query service.
*/
private ArticleQueryService articleQueryService = ArticleQueryService.getInstance();
@Inject
private ArticleQueryService articleQueryService;

/**
* Broadcasts fetch size.
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/b3log/symphony/api/NewsProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.b3log.symphony.api;

import java.util.List;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.b3log.latke.servlet.HTTPRequestContext;
Expand Down Expand Up @@ -44,7 +45,9 @@ public final class NewsProcessor {
/**
* Article query service.
*/
private ArticleQueryService articleQueryService = ArticleQueryService.getInstance();
@Inject
private ArticleQueryService articleQueryService;

/**
* News fetch size.
*/
Expand All @@ -63,7 +66,7 @@ public void getNews(final HTTPRequestContext context, final HttpServletRequest r
throws Exception {
final JSONRenderer renderer = new JSONRenderer().setJSONP(true);
context.setRenderer(renderer);

renderer.setCallback(request.getParameter("callback"));

final JSONObject ret = new JSONObject();
Expand Down
34 changes: 26 additions & 8 deletions src/main/java/org/b3log/symphony/dev/InitProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.util.List;
import java.util.Random;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.b3log.latke.Keys;
Expand Down Expand Up @@ -62,6 +63,30 @@ public class InitProcessor {
*/
private static final int ARTICLE_GENERATE_NUM = 49;

/**
* Option repository.
*/
@Inject
private OptionRepository optionRepository;

/**
* Article management service.
*/
@Inject
private ArticleMgmtService articleMgmtService;

/**
* User management service.
*/
@Inject
private UserMgmtService userMgmtService;

/**
* User query service.
*/
@Inject
private UserQueryService userQueryService;

/**
* Generates tables.
*
Expand All @@ -83,7 +108,6 @@ public void genTables(final HTTPRequestContext context, final HttpServletRequest
}

// Init stat.
final OptionRepository optionRepository = OptionRepository.getInstance();
final Transaction transaction = optionRepository.beginTransaction();

JSONObject option = new JSONObject();
Expand Down Expand Up @@ -119,8 +143,7 @@ public void genTables(final HTTPRequestContext context, final HttpServletRequest
transaction.commit();

// Init admin
final UserMgmtService userMgmtService = UserMgmtService.getInstance();
JSONObject admin = new JSONObject();
final JSONObject admin = new JSONObject();
admin.put(User.USER_EMAIL, UserExt.DEFAULT_ADMIN_EMAIL);
admin.put(User.USER_NAME, UserExt.DEFAULT_ADMIN_NAME);
admin.put(User.USER_PASSWORD, MD5.hash("test"));
Expand All @@ -143,8 +166,6 @@ public void genTables(final HTTPRequestContext context, final HttpServletRequest
userMgmtService.addUser(defaultCommenter);

// Hello World!
admin = UserQueryService.getInstance().getAdmin();
final ArticleMgmtService articleMgmtService = ArticleMgmtService.getInstance();
final JSONObject article = new JSONObject();
article.put(Article.ARTICLE_TITLE, "B3log 社区上线 ♥");
article.put(Article.ARTICLE_TAGS, "B3log, Java, Q&A, B3log Announcement");
Expand Down Expand Up @@ -174,10 +195,7 @@ public void genTables(final HTTPRequestContext context, final HttpServletRequest
public void genArticles(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response)
throws IOException {
try {
final ArticleMgmtService articleMgmtService = ArticleMgmtService.getInstance();
final UserQueryService userQueryService = UserQueryService.getInstance();
final JSONObject admin = userQueryService.getAdmin();
final OptionRepository optionRepository = OptionRepository.getInstance();
final JSONObject articleCntOption = optionRepository.get(Option.ID_C_STATISTIC_ARTICLE_COUNT);
final int start = articleCntOption.optInt(Option.OPTION_VALUE) + 1;
final int end = start + ARTICLE_GENERATE_NUM;
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/b3log/symphony/event/ArticleNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.b3log.latke.event.AbstractEventListener;
import org.b3log.latke.event.Event;
import org.b3log.latke.event.EventException;
import org.b3log.latke.ioc.LatkeBeanManager;
import org.b3log.latke.ioc.Lifecycle;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.User;
Expand Down Expand Up @@ -51,10 +53,7 @@ public final class ArticleNotifier extends AbstractEventListener<JSONObject> {
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(ArticleNotifier.class.getName());
/**
* User query service.
*/
private UserQueryService userQueryService = UserQueryService.getInstance();

/**
* URL fetch service.
*/
Expand All @@ -66,6 +65,10 @@ public void action(final Event<JSONObject> event) throws EventException {
LOGGER.log(Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]",
new Object[]{event.getType(), data, ArticleNotifier.class.getName()});


final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
final UserQueryService userQueryService = beanManager.getReference(UserQueryService.class);

try {
final JSONObject originalArticle = data.getJSONObject(Article.ARTICLE);

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/b3log/symphony/event/CommentNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.b3log.latke.event.AbstractEventListener;
import org.b3log.latke.event.Event;
import org.b3log.latke.event.EventException;
import org.b3log.latke.ioc.LatkeBeanManager;
import org.b3log.latke.ioc.Lifecycle;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.User;
Expand Down Expand Up @@ -55,11 +57,6 @@ public final class CommentNotifier extends AbstractEventListener<JSONObject> {
*/
private static final Logger LOGGER = Logger.getLogger(CommentNotifier.class.getName());

/**
* User query service.
*/
private UserQueryService userQueryService = UserQueryService.getInstance();

/**
* URL fetch service.
*/
Expand All @@ -71,6 +68,9 @@ public void action(final Event<JSONObject> event) throws EventException {
LOGGER.log(Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]",
new Object[]{event.getType(), data, CommentNotifier.class.getName()});

final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
final UserQueryService userQueryService = beanManager.getReference(UserQueryService.class);

try {
final JSONObject originalArticle = data.getJSONObject(Article.ARTICLE);
final JSONObject originalComment = data.getJSONObject(Comment.COMMENT);
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/org/b3log/symphony/event/solo/ArticleSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.b3log.latke.event.AbstractEventListener;
import org.b3log.latke.event.Event;
import org.b3log.latke.event.EventException;
import org.b3log.latke.ioc.LatkeBeanManager;
import org.b3log.latke.ioc.Lifecycle;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.servlet.HTTPRequestMethod;
Expand Down Expand Up @@ -47,10 +49,7 @@ public final class ArticleSender extends AbstractEventListener<JSONObject> {
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(ArticleSender.class.getName());
/**
* User query service.
*/
private UserQueryService userQueryService = UserQueryService.getInstance();

/**
* URL fetch service.
*/
Expand All @@ -60,7 +59,7 @@ public final class ArticleSender extends AbstractEventListener<JSONObject> {
public void action(final Event<JSONObject> event) throws EventException {
final JSONObject data = event.getData();
LOGGER.log(Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]",
new Object[]{event.getType(), data, ArticleSender.class.getName()});
new Object[]{event.getType(), data, ArticleSender.class.getName()});
try {
if (data.optBoolean(Common.FROM_CLIENT)) {
return;
Expand All @@ -71,7 +70,10 @@ public void action(final Event<JSONObject> event) throws EventException {
if (!originalArticle.optBoolean(Article.ARTICLE_SYNC_TO_CLIENT)) {
return;
}


final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
final UserQueryService userQueryService = beanManager.getReference(UserQueryService.class);

final String authorId = originalArticle.optString(Article.ARTICLE_AUTHOR_ID);
final JSONObject author = userQueryService.getUser(authorId);
final String clientURL = author.optString(UserExt.USER_B3_CLIENT_ADD_ARTICLE_URL);
Expand All @@ -85,11 +87,11 @@ public void action(final Event<JSONObject> event) throws EventException {
httpRequest.setRequestMethod(HTTPRequestMethod.POST);
final JSONObject requestJSONObject = new JSONObject();
final JSONObject article = new JSONObject(originalArticle, new String[]{
Article.ARTICLE_CONTENT,
Article.ARTICLE_TAGS,
Article.ARTICLE_TITLE,
Keys.OBJECT_ID
});
Article.ARTICLE_CONTENT,
Article.ARTICLE_TAGS,
Article.ARTICLE_TITLE,
Keys.OBJECT_ID
});

article.put(UserExt.USER_B3_KEY, author.optString(UserExt.USER_B3_KEY));
article.put(Article.ARTICLE_EDITOR_TYPE, "CodeMirror-Markdown");
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/org/b3log/symphony/event/solo/ArticleUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.b3log.latke.event.AbstractEventListener;
import org.b3log.latke.event.Event;
import org.b3log.latke.event.EventException;
import org.b3log.latke.ioc.LatkeBeanManager;
import org.b3log.latke.ioc.Lifecycle;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.servlet.HTTPRequestMethod;
Expand Down Expand Up @@ -47,10 +49,7 @@ public final class ArticleUpdater extends AbstractEventListener<JSONObject> {
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(ArticleUpdater.class.getName());
/**
* User query service.
*/
private UserQueryService userQueryService = UserQueryService.getInstance();

/**
* URL fetch service.
*/
Expand All @@ -60,7 +59,7 @@ public final class ArticleUpdater extends AbstractEventListener<JSONObject> {
public void action(final Event<JSONObject> event) throws EventException {
final JSONObject data = event.getData();
LOGGER.log(Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]",
new Object[]{event.getType(), data, ArticleUpdater.class.getName()});
new Object[]{event.getType(), data, ArticleUpdater.class.getName()});
try {
if (data.optBoolean(Common.FROM_CLIENT)) {
return;
Expand All @@ -72,6 +71,10 @@ public void action(final Event<JSONObject> event) throws EventException {
return;
}

final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
final UserQueryService userQueryService = beanManager.getReference(UserQueryService.class);


final String authorId = originalArticle.optString(Article.ARTICLE_AUTHOR_ID);
final JSONObject author = userQueryService.getUser(authorId);
final String clientURL = author.optString(UserExt.USER_B3_CLIENT_UPDATE_ARTICLE_URL);
Expand All @@ -85,10 +88,9 @@ public void action(final Event<JSONObject> event) throws EventException {
httpRequest.setRequestMethod(HTTPRequestMethod.PUT);
final JSONObject requestJSONObject = new JSONObject();
final JSONObject article = new JSONObject(originalArticle, new String[]{
Article.ARTICLE_CONTENT,
Article.ARTICLE_TAGS,
Article.ARTICLE_TITLE,
});
Article.ARTICLE_CONTENT,
Article.ARTICLE_TAGS,
Article.ARTICLE_TITLE});

article.put(Keys.OBJECT_ID, originalArticle.optString(Article.ARTICLE_CLIENT_ARTICLE_ID));
article.put(UserExt.USER_B3_KEY, author.optString(UserExt.USER_B3_KEY));
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/org/b3log/symphony/event/solo/CommentSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.b3log.latke.event.AbstractEventListener;
import org.b3log.latke.event.Event;
import org.b3log.latke.event.EventException;
import org.b3log.latke.ioc.LatkeBeanManager;
import org.b3log.latke.ioc.Lifecycle;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.User;
Expand Down Expand Up @@ -49,10 +51,7 @@ public final class CommentSender extends AbstractEventListener<JSONObject> {
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(CommentSender.class.getName());
/**
* User query service.
*/
private UserQueryService userQueryService = UserQueryService.getInstance();

/**
* URL fetch service.
*/
Expand All @@ -62,7 +61,7 @@ public final class CommentSender extends AbstractEventListener<JSONObject> {
public void action(final Event<JSONObject> event) throws EventException {
final JSONObject data = event.getData();
LOGGER.log(Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]",
new Object[]{event.getType(), data, CommentSender.class.getName()});
new Object[]{event.getType(), data, CommentSender.class.getName()});
try {
if (data.optBoolean(Common.FROM_CLIENT)) {
return;
Expand All @@ -74,6 +73,9 @@ public void action(final Event<JSONObject> event) throws EventException {
return;
}

final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
final UserQueryService userQueryService = beanManager.getReference(UserQueryService.class);

final String authorId = originalArticle.optString(Article.ARTICLE_AUTHOR_ID);
final JSONObject author = userQueryService.getUser(authorId);
final String clientURL = author.optString(UserExt.USER_B3_CLIENT_ADD_COMMENT_URL);
Expand All @@ -91,10 +93,10 @@ public void action(final Event<JSONObject> event) throws EventException {
httpRequest.setRequestMethod(HTTPRequestMethod.PUT);
final JSONObject requestJSONObject = new JSONObject();
final JSONObject comment = new JSONObject(originalComment, new String[]{
Comment.COMMENT_AUTHOR_EMAIL,
Comment.COMMENT_CONTENT,
Keys.OBJECT_ID
});
Comment.COMMENT_AUTHOR_EMAIL,
Comment.COMMENT_CONTENT,
Keys.OBJECT_ID
});

comment.put(Comment.COMMENT_T_AUTHOR_NAME, commenter.optString(User.USER_NAME));
comment.put(UserExt.USER_B3_KEY, author.optString(UserExt.USER_B3_KEY));
Expand Down
Loading

0 comments on commit ddebc54

Please sign in to comment.