[jboss-svn-commits] JBL Code SVN: r5453 - in labs/shotoku/trunk: shotoku-feeds/src/java/org/jboss/shotoku/feeds shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/exceptions shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/service shotoku-feeds/src/java/org/jboss/shotoku/feeds/data shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/special shotoku-feeds/src/java/org/jboss/shotoku/feeds/exceptions shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter shotoku-feeds/src/java/org/jboss/shotoku/feeds/service shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools shotoku-tags/src/java/org/jboss/shotoku/tags shotoku-tags/src/java/org/jboss/shotoku/tags/service shotoku-tags/src/java/org/jboss/shotoku/tags/test
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Aug 3 15:47:45 EDT 2006
Author: adamw
Date: 2006-08-03 15:47:37 -0400 (Thu, 03 Aug 2006)
New Revision: 5453
Added:
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsService.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/exceptions/
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/exceptions/CommentableFeedDoesNotExistException.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/exceptions/CommentsNotAvialableException.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/exceptions/UnauthorizedToCommentException.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/service/
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/service/CommentsServiceImpl.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/service/CommentsServiceLocal.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/exceptions/
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/exceptions/FeedDoesNotExistException.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/exceptions/FeedsException.java
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/ExceptionsInterceptor.java
Removed:
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/CommentsService.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsNotAvialableException.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/FeedDoesNotExistException.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/UnauthorizedToCommentException.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceImpl.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceLocal.java
Modified:
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsService.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsServlet.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsServlet.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/CommentableFeed.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/ShotokuCommentableFeed.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/special/CommentFeed.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/FeedsViewPortlet.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/AbstractFeedGetter.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/ViewFeedGetter.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/ViewFeedGetterWrapper.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AggregatedFeedTagHandler.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsTools.java
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/AbstractTag.java
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/test/TagsServiceMultiThreadTest.java
labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/test/TagsTestServlet.java
Log:
http://jira.jboss.com/jira/browse/JBSHOTOKU-38
Deleted: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/CommentsService.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/CommentsService.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/CommentsService.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -1,28 +0,0 @@
-package org.jboss.shotoku.feeds;
-
-import org.jboss.shotoku.feeds.comments.FeedDoesNotExistException;
-import org.jboss.shotoku.feeds.comments.CommentsNotAvialableException;
-import org.jboss.shotoku.feeds.comments.UnauthorizedToCommentException;
-import org.jboss.shotoku.service.AdministratedService;
-import org.jboss.shotoku.Node;
-import org.jboss.shotoku.Directory;
-import org.jboss.shotoku.exceptions.ResourceDoesNotExist;
-
-/**
- * @author Adam Warski (adamw at aster.pl)
- */
-public interface CommentsService extends AdministratedService {
- public void addComment(String feedId, String feedName, String feedElement,
- String feedType, String username, String title,
- String content, String userIp)
- throws FeedDoesNotExistException, CommentsNotAvialableException,
- UnauthorizedToCommentException;
-
- public String getCommentFeedLink(String feedName,
- String feedElement, String feedType);
-
- public String getCommentFeedName(String feedName, String feedElement);
-
- public Directory getCommentsDirectory(Node commentedNode)
- throws ResourceDoesNotExist;
-}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -39,6 +39,7 @@
import org.jboss.shotoku.feeds.tag.FeedTagHandler;
import org.jboss.shotoku.feeds.tag.FeedTagHandlerManager;
import org.jboss.shotoku.feeds.tools.FeedsConstants;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
import org.jboss.shotoku.tools.Pair;
import org.jboss.shotoku.tools.Tools;
import org.w3c.dom.Node;
@@ -286,13 +287,22 @@
* @param type Type of the feed to get.
* @return Feed with the given name and type.
*/
- public NormalFeed getNormalFeed(String name, String type) {
+ public NormalFeed getNormalFeed(String name, String type)
+ throws FeedDoesNotExistException {
Map<String, NormalFeed> feedTypes = feeds.get(name);
if (feedTypes == null) {
- return null;
+ throw new FeedDoesNotExistException("Feed " + name +
+ " does not exist.");
}
- return feedTypes.get(type);
+ NormalFeed feed = feedTypes.get(type);
+
+ if (feed == null) {
+ throw new FeedDoesNotExistException("Feed " + name +
+ " exists, but not with type " + type + ".");
+ }
+
+ return feed;
}
/**
@@ -301,13 +311,22 @@
* @param type Type of the feed to get.
* @return Commentable feed with the given name and type.
*/
- public CommentableFeed getCommentableFeed(String name, String type) {
+ public CommentableFeed getCommentableFeed(String name, String type)
+ throws FeedDoesNotExistException {
Map<String, CommentableFeed> feedTypes = commentableFeeds.get(name);
if (feedTypes == null) {
- return null;
+ throw new FeedDoesNotExistException("Commentable feed " + name +
+ " does not exist.");
}
- return feedTypes.get(type);
+ CommentableFeed feed = feedTypes.get(type);
+
+ if (feed == null) {
+ throw new FeedDoesNotExistException("Commentable feed " + name +
+ " exists, but not with type " + type + ".");
+ }
+
+ return feed;
}
/**
@@ -316,9 +335,11 @@
* @param type Type of the feed to get.
* @return Feed with the given name and type.
*/
- public Feed getFeed(String name, String type) {
- Feed ret = getNormalFeed(name, type);
- if (ret == null) {
+ public Feed getFeed(String name, String type)
+ throws FeedDoesNotExistException {
+ try {
+ return getNormalFeed(name, type);
+ } catch (FeedDoesNotExistException e) {
// Checking for a special feed.
for (String feedNamePrefix : specialFeeds.keySet()) {
if (name.startsWith(feedNamePrefix)) {
@@ -326,15 +347,15 @@
if (unconcreteFeed != null) {
return unconcreteFeed.concretizeFeed(name);
} else {
- return null;
+ throw new FeedDoesNotExistException("Feed " +
+ name + " exists, but not with type " +
+ type + ".");
}
}
}
- return null;
+ throw e;
}
-
- return ret;
}
/**
@@ -353,11 +374,11 @@
* Releases all feeds - deletes temporary files which contain them.
*/
public void release() {
- for (Map<String, NormalFeed> feedTypes : feeds.values()) {
- for (NormalFeed feed : feedTypes.values()) {
- feed.release();
- }
- }
+ for (Map<String, NormalFeed> feedTypes : feeds.values()) {
+ for (NormalFeed feed : feedTypes.values()) {
+ feed.release();
+ }
+ }
}
public String getId() {
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsService.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsService.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsService.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -3,13 +3,16 @@
import org.jboss.shotoku.service.AdministratedService;
import org.jboss.shotoku.feeds.data.Feed;
import org.jboss.shotoku.feeds.data.CommentableFeed;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
/**
* @author Adam Warski (adamw at aster.pl)
*/
public interface FeedsService extends AdministratedService {
public String getProperty(String id, String name);
- public Feed getFeed(String id, String name, String type);
+ public Feed getFeed(String id, String name, String type)
+ throws FeedDoesNotExistException;
public CommentableFeed getCommentableFeed(String id, String name,
- String type);
+ String type)
+ throws FeedDoesNotExistException;
}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsServlet.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsServlet.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsServlet.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -28,7 +28,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.jboss.shotoku.feeds.data.Feed;
import org.jboss.shotoku.feeds.tools.FeedsTools;
public class FeedsServlet extends HttpServlet {
@@ -41,7 +40,7 @@
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
+ throws ServletException, IOException {
String requestURI = request.getRequestURI();
// Getting rid of the /war-name/.
@@ -62,16 +61,10 @@
String type = requestURI.substring(lastSlash+1);
String name = requestURI.substring(0, lastSlash);
- Feed feed = FeedsTools.getService().getFeed("default", name, type);
-
- if (feed == null) {
- invalidRequest(response, "Feed not found");
- return;
- }
-
try {
- feed.write(response, request);
- } catch (IOException e) {
+ FeedsTools.getService().getFeed("default", name,
+ type).write(response, request);
+ } catch (Exception e) {
invalidRequest(response, e.getMessage());
}
}
Deleted: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsNotAvialableException.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsNotAvialableException.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsNotAvialableException.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -1,21 +0,0 @@
-package org.jboss.shotoku.feeds.comments;
-
-/**
- * @author Adam Warski (adamw at aster.pl)
- */
-public class CommentsNotAvialableException extends Exception {
- public CommentsNotAvialableException() {
- }
-
- public CommentsNotAvialableException(String message) {
- super(message);
- }
-
- public CommentsNotAvialableException(String message, Throwable cause) {
- super(message, cause);
- }
-
- public CommentsNotAvialableException(Throwable cause) {
- super(cause);
- }
-}
Copied: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsService.java (from rev 5432, labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/CommentsService.java)
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/CommentsService.java 2006-08-03 13:47:45 UTC (rev 5432)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsService.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -0,0 +1,28 @@
+package org.jboss.shotoku.feeds.comments;
+
+import org.jboss.shotoku.feeds.comments.exceptions.CommentableFeedDoesNotExistException;
+import org.jboss.shotoku.feeds.comments.exceptions.CommentsNotAvialableException;
+import org.jboss.shotoku.feeds.comments.exceptions.UnauthorizedToCommentException;
+import org.jboss.shotoku.service.AdministratedService;
+import org.jboss.shotoku.Node;
+import org.jboss.shotoku.Directory;
+import org.jboss.shotoku.exceptions.ResourceDoesNotExist;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+public interface CommentsService extends AdministratedService {
+ public void addComment(String feedId, String feedName, String feedElement,
+ String feedType, String username, String title,
+ String content, String userIp)
+ throws CommentableFeedDoesNotExistException, CommentsNotAvialableException,
+ UnauthorizedToCommentException;
+
+ public String getCommentFeedLink(String feedName,
+ String feedElement, String feedType);
+
+ public String getCommentFeedName(String feedName, String feedElement);
+
+ public Directory getCommentsDirectory(Node commentedNode)
+ throws ResourceDoesNotExist;
+}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsServlet.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsServlet.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsServlet.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -7,6 +7,7 @@
import org.jboss.shotoku.tools.Tools;
import org.jboss.shotoku.feeds.tools.FeedsTools;
import org.jboss.shotoku.feeds.data.model.FeedEntries;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@@ -28,9 +29,13 @@
protected void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
- FeedEntries fe =
- FeedsTools.getService().getFeed(
- "default", "kosmos", "rss2").getFeedEntries();
+ FeedEntries fe;
+ try {
+ fe = FeedsTools.getService().getFeed(
+ "default", "kosmos", "rss2").getFeedEntries();
+ } catch (FeedDoesNotExistException e) {
+ throw new IOException(e.getMessage());
+ }
System.out.println("fe.getTitle() = " + fe.getTitle());
System.out.println("fe.getEntries().size() = " +
fe.getEntries().size());
Deleted: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/FeedDoesNotExistException.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/FeedDoesNotExistException.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/FeedDoesNotExistException.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -1,21 +0,0 @@
-package org.jboss.shotoku.feeds.comments;
-
-/**
- * @author Adam Warski (adamw at aster.pl)
- */
-public class FeedDoesNotExistException extends Exception {
- public FeedDoesNotExistException() {
- }
-
- public FeedDoesNotExistException(String message) {
- super(message);
- }
-
- public FeedDoesNotExistException(String message, Throwable cause) {
- super(message, cause);
- }
-
- public FeedDoesNotExistException(Throwable cause) {
- super(cause);
- }
-}
Deleted: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/UnauthorizedToCommentException.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/UnauthorizedToCommentException.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/UnauthorizedToCommentException.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -1,21 +0,0 @@
-package org.jboss.shotoku.feeds.comments;
-
-/**
- * @author Adam Warski (adamw at aster.pl)
- */
-public class UnauthorizedToCommentException extends Exception {
- public UnauthorizedToCommentException() {
- }
-
- public UnauthorizedToCommentException(String message) {
- super(message);
- }
-
- public UnauthorizedToCommentException(String message, Throwable cause) {
- super(message, cause);
- }
-
- public UnauthorizedToCommentException(Throwable cause) {
- super(cause);
- }
-}
Copied: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/exceptions/CommentableFeedDoesNotExistException.java (from rev 5372, labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/FeedDoesNotExistException.java)
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/FeedDoesNotExistException.java 2006-07-31 22:05:07 UTC (rev 5372)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/exceptions/CommentableFeedDoesNotExistException.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -0,0 +1,25 @@
+package org.jboss.shotoku.feeds.comments.exceptions;
+
+import org.jboss.shotoku.feeds.exceptions.FeedsException;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+public class CommentableFeedDoesNotExistException
+ extends FeedDoesNotExistException {
+ public CommentableFeedDoesNotExistException() {
+ }
+
+ public CommentableFeedDoesNotExistException(String message) {
+ super(message);
+ }
+
+ public CommentableFeedDoesNotExistException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public CommentableFeedDoesNotExistException(Throwable cause) {
+ super(cause);
+ }
+}
Copied: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/exceptions/CommentsNotAvialableException.java (from rev 5372, labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsNotAvialableException.java)
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/CommentsNotAvialableException.java 2006-07-31 22:05:07 UTC (rev 5372)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/exceptions/CommentsNotAvialableException.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -0,0 +1,23 @@
+package org.jboss.shotoku.feeds.comments.exceptions;
+
+import org.jboss.shotoku.feeds.exceptions.FeedsException;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+public class CommentsNotAvialableException extends FeedsException {
+ public CommentsNotAvialableException() {
+ }
+
+ public CommentsNotAvialableException(String message) {
+ super(message);
+ }
+
+ public CommentsNotAvialableException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public CommentsNotAvialableException(Throwable cause) {
+ super(cause);
+ }
+}
Copied: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/exceptions/UnauthorizedToCommentException.java (from rev 5372, labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/UnauthorizedToCommentException.java)
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/UnauthorizedToCommentException.java 2006-07-31 22:05:07 UTC (rev 5372)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/exceptions/UnauthorizedToCommentException.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -0,0 +1,23 @@
+package org.jboss.shotoku.feeds.comments.exceptions;
+
+import org.jboss.shotoku.feeds.exceptions.FeedsException;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+public class UnauthorizedToCommentException extends FeedsException {
+ public UnauthorizedToCommentException() {
+ }
+
+ public UnauthorizedToCommentException(String message) {
+ super(message);
+ }
+
+ public UnauthorizedToCommentException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public UnauthorizedToCommentException(Throwable cause) {
+ super(cause);
+ }
+}
Copied: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/service/CommentsServiceImpl.java (from rev 5432, labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceImpl.java)
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceImpl.java 2006-08-03 13:47:45 UTC (rev 5432)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/service/CommentsServiceImpl.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -0,0 +1,322 @@
+package org.jboss.shotoku.feeds.comments.service;
+
+import org.jboss.annotation.ejb.Service;
+import org.jboss.annotation.ejb.Management;
+import org.jboss.annotation.ejb.Depends;
+import org.jboss.shotoku.feeds.tools.FeedsConstants;
+import org.jboss.shotoku.feeds.tools.FeedsTools;
+import org.jboss.shotoku.feeds.tools.NodesFeedsProperties;
+import org.jboss.shotoku.feeds.comments.CommentsService;
+import org.jboss.shotoku.feeds.comments.exceptions.UnauthorizedToCommentException;
+import org.jboss.shotoku.feeds.comments.CommentEntity;
+import org.jboss.shotoku.feeds.comments.exceptions.CommentsNotAvialableException;
+import org.jboss.shotoku.feeds.comments.exceptions.CommentableFeedDoesNotExistException;
+import org.jboss.shotoku.feeds.data.CommentableFeed;
+import org.jboss.shotoku.feeds.data.ShotokuCommentableFeed;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
+import org.jboss.shotoku.user.tools.UserConstants;
+import org.jboss.shotoku.tools.Constants;
+import org.jboss.shotoku.tools.Tools;
+import org.jboss.shotoku.service.AdministratedServiceImpl;
+import org.jboss.shotoku.service.AdministratedService;
+import org.jboss.shotoku.Node;
+import org.jboss.shotoku.Directory;
+import org.jboss.shotoku.ContentManager;
+import org.jboss.shotoku.exceptions.NameFormatException;
+import org.jboss.shotoku.exceptions.ResourceAlreadyExists;
+import org.jboss.shotoku.exceptions.ResourceDoesNotExist;
+import org.apache.log4j.Logger;
+
+import javax.ejb.Local;
+import javax.persistence.PersistenceUnit;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityManager;
+import javax.transaction.UserTransaction;
+import javax.naming.InitialContext;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+ at Service(objectName = FeedsConstants.COMMENTS_SERVICE_NAME)
+ at Local(CommentsServiceLocal.class)
+ at Management(CommentsService.class)
+ at Depends({UserConstants.USER_SERVICE_NAME, Constants.SHOTOKU_SERVICE_NAME,
+ FeedsConstants.FEEDS_SERVICE_NAME})
+public class CommentsServiceImpl extends AdministratedServiceImpl
+ implements CommentsService, CommentsServiceLocal {
+ private static final Logger log = Logger.getLogger(AdministratedService.class);
+
+ /*
+ * CommentsService implementation.
+ */
+
+ @SuppressWarnings({"UNUSED_SYMBOL"})
+ @PersistenceUnit
+ private EntityManagerFactory emf;
+
+ public void addComment(String feedId, String feedName, String feedElement,
+ String feedType, String username,
+ String title, String content, String userIp)
+ throws CommentableFeedDoesNotExistException, CommentsNotAvialableException,
+ UnauthorizedToCommentException {
+ System.out.println("ADD COMMENT:");
+ System.out.println("feedId = " + feedId);
+ System.out.println("feedName = " + feedName);
+ System.out.println("feedType = " + feedType);
+ System.out.println("feedElement = " + feedElement);
+ System.out.println("username = " + username);
+ System.out.println("title = " + title);
+ System.out.println("content = " + content);
+
+ // Getting the feed and checking if it exists.
+ CommentableFeed feed;
+ try {
+ feed = FeedsTools.getService().getCommentableFeed(
+ feedId, feedName, feedType);
+ } catch (FeedDoesNotExistException e) {
+ throw new CommentableFeedDoesNotExistException(e.getMessage());
+ }
+
+ // Checking if comments are enabled for this feed.
+ if (!feed.getCommentsEnabled()) {
+ throw new CommentsNotAvialableException();
+ }
+
+ // Checking if the user is authorised to add comments.
+ if (!feed.getUserCanAddComments(username)) {
+ throw new UnauthorizedToCommentException();
+ }
+
+ // Checking if the commented item exists in the content manager.
+ if (!feed.getFeedEntryExists(feedElement)) {
+ throw new CommentableFeedDoesNotExistException("Invalid feed element.");
+ }
+
+ // Saving the comment for later addition to Shotoku.
+ EntityManager em = emf.createEntityManager();
+ try {
+ em.persist(new CommentEntity(feedId, feedName, feedType,
+ feedElement, username, title, content));
+ } finally {
+ em.close();
+ }
+ }
+
+ public String getCommentFeedName(String feedName, String feedElement) {
+ return "comments/" + feedName + "/" + Tools.encodeURL(feedElement);
+ }
+
+ public String getCommentFeedLink(String feedName,
+ String feedElement, String feedType) {
+ return "/feeds/" + getCommentFeedName(feedName, feedElement) +
+ "/" + feedType;
+ }
+
+ private String getCommentsDirectoryName(Node commentedNode) {
+ return commentedNode.getName() + "_comments";
+ }
+
+ public Directory getCommentsDirectory(Node commentedNode)
+ throws ResourceDoesNotExist {
+ return commentedNode.getParent().getDirectory(
+ getCommentsDirectoryName(commentedNode));
+ }
+
+ /*
+ * Update method.
+ */
+
+ /**
+ * Creates a node in the given directory, in which a comment can
+ * be placed. Effectively, crates a new "fresh" node, which
+ * doesn't yet exist.
+ * @param commentDir Directory in which the node shold be created.
+ * @param number Number from which will be appended to the end of
+ * the node name. If a node with this name already exists, the next
+ * number will be tried, and so on.
+ * @return A "fresh" node.
+ */
+ private Node createCommentNode(Directory commentDir, int number)
+ throws NameFormatException {
+ try {
+ return commentDir.newNode("c" + System.currentTimeMillis()
+ + "_" + number);
+ } catch (ResourceAlreadyExists e) {
+ return createCommentNode(commentDir, number + 1);
+ }
+ }
+
+ private Node createCommentNode(Directory commentDir)
+ throws NameFormatException {
+ return createCommentNode(commentDir, 0);
+ }
+
+ private void removeCommentEntity(EntityManager em, CommentEntity ce)
+ throws Throwable {
+ UserTransaction tran = null;
+ try {
+ InitialContext jndiCntx = new InitialContext();
+ tran = (UserTransaction) jndiCntx.lookup("UserTransaction");
+
+ tran.begin();
+ em.joinTransaction();
+ em.remove(ce);
+ tran.commit();
+ } catch (Throwable t) {
+ if (tran != null) {
+ tran.rollback();
+ }
+
+ throw t;
+ }
+ }
+
+ public void update() {
+ // Adding any (possible) comments.
+ EntityManager updateThreadEm = emf.createEntityManager();
+
+ try {
+ //noinspection unchecked
+ List<CommentEntity> result = updateThreadEm.createQuery(
+ "FROM CommentEntity").getResultList();
+
+ List<CommentEntity> failedComments = new ArrayList<CommentEntity>();
+ for (CommentEntity ce : result) {
+ try {
+ CommentableFeed feed = FeedsTools.getService().
+ getCommentableFeed(ce.getFeedId(),
+ ce.getFeedName(), ce.getFeedType());
+
+ // Checking if the feed exists.
+ if (feed == null || !feed.getCommentsEnabled() ||
+ !(feed instanceof ShotokuCommentableFeed)) {
+ failedComments.add(ce);
+ continue;
+ }
+
+ ShotokuCommentableFeed scf = (ShotokuCommentableFeed) feed;
+
+ Node commentedElement = scf.getNodeForFeedElement(
+ ce.getFeedElement());
+
+ // Checking if the commented element exists.
+ if (commentedElement == null) {
+ failedComments.add(ce);
+ continue;
+ }
+
+ // Adding the comment.
+
+ // Getting/ creating the comment directory.
+ Directory commentDir;
+ try {
+ commentDir = getCommentsDirectory(commentedElement);
+ } catch (ResourceDoesNotExist e) {
+ try {
+ commentDir = commentedElement.getParent().
+ newDirectory(getCommentsDirectoryName(
+ commentedElement));
+ } catch (ResourceAlreadyExists e1) {
+ log.warn("A node which does exist and doesn't.", e1);
+ failedComments.add(ce);
+ continue;
+ }
+ }
+
+ // Creating the comment node.
+ Node commentNode = createCommentNode(commentDir);
+ commentNode.setContent(ce.getContent());
+ commentNode.setProperty(NodesFeedsProperties.TITLE,
+ ce.getTitle());
+ commentNode.setProperty(NodesFeedsProperties.AUTHOR,
+ ce.getUsername());
+ if (feed.getCommentsModerated()) {
+ commentNode.setProperty(
+ NodesFeedsProperties.ACCEPTED_COMMENT_NAME,
+ NodesFeedsProperties.NOTACCEPTED_COMMENT_VALUE);
+ } else {
+ // If the feed is not moderated, the entry gets
+ // automatically accepted.
+ commentNode.setProperty(
+ NodesFeedsProperties.ACCEPTED_COMMENT_NAME,
+ NodesFeedsProperties.ACCEPTED_COMMENT_VALUE);
+ }
+
+ // Saving.
+ commentNode.getContentManager().save("Adding a comment",
+ commentNode, commentDir);
+ } catch (Throwable t) {
+ log.warn("Exception when trying to add a comment.", t);
+ failedComments.add(ce);
+ }
+
+ try {
+ removeCommentEntity(updateThreadEm, ce);
+ } catch (Throwable t) {
+ log.warn("Exception when trying to remove a comment entity.", t);
+ }
+ }
+
+ // Logging information about comments, which addition failed.
+ for (CommentEntity ce : failedComments) {
+ log.warn("Failed to add comment: " +
+ ce.getFeedId() + " :: " + ce.getFeedName() + " :: " +
+ ce.getFeedType() + " :: " + ce.getFeedElement() + " :: " +
+ ce.getTitle() + " :: " + ce.getContent() + " :: " +
+ ce.getUsername());
+
+ try {
+ removeCommentEntity(updateThreadEm, ce);
+ } catch (Throwable t) {
+ log.warn("Exception when trying to remove a comment entity.", t);
+ }
+ }
+ } finally {
+ updateThreadEm.close();
+ }
+ }
+
+ /*
+ * Service management.
+ */
+ public void create() throws Exception {
+ super.create();
+
+ setTimerInterval(ContentManager.getProperty(
+ FeedsConstants.PROPERTY_INTERVAL, 10000));
+
+ super.afterCreate();
+ }
+
+ public void start() throws Exception {
+ super.start();
+ startUpdateThread();
+ super.afterStart();
+ }
+
+ public void destroy() {
+
+ }
+
+ /*
+ * Administrated service implementation.
+ */
+
+ public String getServiceId() {
+ return "ShotokuCommentsService";
+ }
+
+ public String getServiceName() {
+ return "Comments service";
+ }
+
+ public String getServiceDescription() {
+ return "Shotoku comments service.";
+ }
+
+ public AdministratedService getServiceInstance() {
+ return FeedsTools.getCommentsService();
+ }
+}
Copied: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/service/CommentsServiceLocal.java (from rev 5372, labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceLocal.java)
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceLocal.java 2006-07-31 22:05:07 UTC (rev 5372)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/comments/service/CommentsServiceLocal.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -0,0 +1,7 @@
+package org.jboss.shotoku.feeds.comments.service;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+public interface CommentsServiceLocal {
+}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/CommentableFeed.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/CommentableFeed.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/CommentableFeed.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -1,11 +1,10 @@
package org.jboss.shotoku.feeds.data;
-import org.jboss.shotoku.feeds.comments.UnauthorizedToCommentException;
-import org.jboss.shotoku.feeds.comments.CommentsNotAvialableException;
-import org.jboss.shotoku.feeds.comments.FeedDoesNotExistException;
+import org.jboss.shotoku.feeds.comments.exceptions.UnauthorizedToCommentException;
+import org.jboss.shotoku.feeds.comments.exceptions.CommentsNotAvialableException;
+import org.jboss.shotoku.feeds.comments.exceptions.CommentableFeedDoesNotExistException;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
-import java.util.List;
-
/**
* @author Adam Warski (adamw at aster.pl)
*/
@@ -59,14 +58,14 @@
* @param title
* @param content
* @param userIp
- * @throws UnauthorizedToCommentException
- * @throws CommentsNotAvialableException
- * @throws FeedDoesNotExistException
+ * @throws org.jboss.shotoku.feeds.comments.exceptions.UnauthorizedToCommentException
+ * @throws org.jboss.shotoku.feeds.comments.exceptions.CommentsNotAvialableException
+ * @throws org.jboss.shotoku.feeds.comments.exceptions.CommentableFeedDoesNotExistException
*/
public void addComment(String feedEntry, String username, String title,
String content, String userIp)
throws UnauthorizedToCommentException,
- CommentsNotAvialableException, FeedDoesNotExistException;
+ CommentsNotAvialableException, CommentableFeedDoesNotExistException;
/**
* Gets a feed of comments to a specific feed element.
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/ShotokuCommentableFeed.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/ShotokuCommentableFeed.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/ShotokuCommentableFeed.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -7,9 +7,10 @@
import org.jboss.shotoku.feeds.data.model.FeedEntries;
import org.jboss.shotoku.feeds.tools.FeedsTools;
import org.jboss.shotoku.feeds.tools.FeedDefAttributes;
-import org.jboss.shotoku.feeds.comments.UnauthorizedToCommentException;
-import org.jboss.shotoku.feeds.comments.CommentsNotAvialableException;
-import org.jboss.shotoku.feeds.comments.FeedDoesNotExistException;
+import org.jboss.shotoku.feeds.comments.exceptions.UnauthorizedToCommentException;
+import org.jboss.shotoku.feeds.comments.exceptions.CommentsNotAvialableException;
+import org.jboss.shotoku.feeds.comments.exceptions.CommentableFeedDoesNotExistException;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
import org.jboss.shotoku.tools.Tools;
import javax.servlet.http.HttpServletRequest;
@@ -92,7 +93,7 @@
}
private String prepareFeedElement(String feedElement)
- throws FeedDoesNotExistException {
+ throws CommentableFeedDoesNotExistException {
// It may be possible that the feed element is a full URL,
// so we have to cut off the unnecessary part.
if (Tools.isOutsideLink(feedElement)) {
@@ -102,7 +103,8 @@
i = feedElement.indexOf('/');
feedElement = feedElement.substring(i+1);
} catch (IndexOutOfBoundsException e) {
- throw new FeedDoesNotExistException("Invalid feed element.");
+ throw new CommentableFeedDoesNotExistException(
+ "Invalid feed element: " + feedElement + ".");
}
}
@@ -112,7 +114,7 @@
public void addComment(String feedElement, String username, String title,
String content, String userIp)
throws UnauthorizedToCommentException,
- CommentsNotAvialableException, FeedDoesNotExistException {
+ CommentsNotAvialableException, CommentableFeedDoesNotExistException {
FeedsTools.getCommentsService().addComment(nf.getId(),
nf.getName(), prepareFeedElement(feedElement), nf.getType(),
username, title, content, userIp);
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/special/CommentFeed.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/special/CommentFeed.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/data/special/CommentFeed.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -19,6 +19,7 @@
import org.jboss.shotoku.feeds.tools.NodesFeedsProperties;
import org.jboss.shotoku.feeds.tools.FeedDefAttributes;
import org.jboss.shotoku.feeds.tools.VelocityContextAttributes;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
import org.jboss.shotoku.tools.Tools;
import javax.servlet.http.HttpServletRequest;
@@ -74,11 +75,14 @@
String feedName = info.substring(0, lastSlash);
String feedElement = Tools.decodeURL(info.substring(lastSlash+1));
- CommentableFeed feed = FeedsTools.getService().getCommentableFeed(
- getId(), feedName, getType());
+ CommentableFeed feed;
+ try {
+ feed = FeedsTools.getService().getCommentableFeed(
+ getId(), feedName, getType());
+ } catch (FeedDoesNotExistException e) {
+ throw new IOException(e.getMessage());
+ }
- check(feed != null);
- //noinspection ConstantConditions
check(feed.getCommentsEnabled());
check(feed instanceof ShotokuCommentableFeed);
Added: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/exceptions/FeedDoesNotExistException.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/exceptions/FeedDoesNotExistException.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/exceptions/FeedDoesNotExistException.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -0,0 +1,21 @@
+package org.jboss.shotoku.feeds.exceptions;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+public class FeedDoesNotExistException extends FeedsException {
+ public FeedDoesNotExistException() {
+ }
+
+ public FeedDoesNotExistException(String message) {
+ super(message);
+ }
+
+ public FeedDoesNotExistException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public FeedDoesNotExistException(Throwable cause) {
+ super(cause);
+ }
+}
Added: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/exceptions/FeedsException.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/exceptions/FeedsException.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/exceptions/FeedsException.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -0,0 +1,21 @@
+package org.jboss.shotoku.feeds.exceptions;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+public abstract class FeedsException extends Exception {
+ public FeedsException() {
+ }
+
+ public FeedsException(String message) {
+ super(message);
+ }
+
+ public FeedsException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public FeedsException(Throwable cause) {
+ super(cause);
+ }
+}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/FeedsViewPortlet.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/FeedsViewPortlet.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/FeedsViewPortlet.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -6,11 +6,13 @@
import org.jboss.shotoku.feeds.tools.FeedsTools;
import org.jboss.shotoku.feeds.tools.FeedsConstants;
import org.jboss.shotoku.feeds.FeedsDescriptor;
-import org.jboss.shotoku.feeds.comments.UnauthorizedToCommentException;
-import org.jboss.shotoku.feeds.comments.CommentsNotAvialableException;
-import org.jboss.shotoku.feeds.comments.FeedDoesNotExistException;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
+import org.jboss.shotoku.feeds.comments.exceptions.UnauthorizedToCommentException;
+import org.jboss.shotoku.feeds.comments.exceptions.CommentsNotAvialableException;
+import org.jboss.shotoku.feeds.comments.exceptions.CommentableFeedDoesNotExistException;
import org.jboss.shotoku.feeds.data.Feed;
import org.jboss.shotoku.feeds.data.CommentableFeed;
+import org.jboss.shotoku.feeds.data.DummyFeed;
import org.jboss.shotoku.feeds.data.model.FeedEntry;
import org.apache.log4j.Logger;
@@ -19,11 +21,7 @@
import java.util.List;
import java.util.ArrayList;
-import static org.jboss.shotoku.feeds.tools.FeedDefAttributes.EntriesExpandMode;
-import org.jboss.shotoku.tools.Constants;
import org.jboss.shotoku.tools.Tools;
-import org.jboss.shotoku.tags.tools.UserTools;
-import org.jboss.shotoku.user.UserNotFoundException;
/**
* @author Adam Warski (adamw at aster.pl)
@@ -40,10 +38,10 @@
private static final String JSP_SINGLE = JSP_BASE_PATH + "single.jsp";
/*
- * Names of session attributes where display info is held.
+ * Names of (session) attributes where state info is held.
*/
- private static final String BEGIN_ATTR = "begin";
- private static final String END_ATTR = "end";
+ private static final String BEGIN = "begin";
+ private static final String END = "end";
private static final String SHOW_GUID = "showGuid";
private static final String SHOW_ALL = "showAll";
private static final String SHOW_SINGLE = "showSingle";
@@ -118,11 +116,11 @@
private int getEntriesBegin(String feedGuid, PortletRequest request) {
Integer begin = ((Integer) getSessionAttribute(request, feedGuid,
- BEGIN_ATTR));
+ BEGIN));
if (begin == null) {
// Setting to default
- setSessionAttribute(request, feedGuid, BEGIN_ATTR, 0);
+ setSessionAttribute(request, feedGuid, BEGIN, 0);
begin = 0;
}
@@ -132,7 +130,7 @@
private int setEntriesBegin(String feedGuid, PortletRequest request,
int newBegin) {
if (newBegin < 0) { newBegin = 0; }
- setSessionAttribute(request, feedGuid, BEGIN_ATTR, newBegin);
+ setSessionAttribute(request, feedGuid, BEGIN, newBegin);
return newBegin;
}
@@ -172,7 +170,9 @@
}
/*
- * Action-processing functions.
+ * -----------------
+ * ACTION-PROCESSING
+ * -----------------
*/
private void processActionView(Feed feed, ActionRequest request) {
@@ -264,7 +264,7 @@
"comments.");
} catch (CommentsNotAvialableException e) {
addMessage(feed, request, "You cannot add comments to this feed.");
- } catch (FeedDoesNotExistException e) {
+ } catch (CommentableFeedDoesNotExistException e) {
addMessage(feed, request, "The feed or entry which you want to " +
"comment does not exist");
}
@@ -283,7 +283,13 @@
}
// If action name is correct, getting the currently displayed feed.
- Feed feed = vfg.getFeed(getId(), request);
+ Feed feed;
+ try {
+ feed = vfg.getFeed(getId(), request);
+ } catch (FeedDoesNotExistException e) {
+ // Doing nothing.
+ return;
+ }
// Clearing any previous messages.
clearMessages(feed, request);
@@ -295,28 +301,47 @@
case NEXT: processActionNext(feed, request); break;
case PREVIOUS: processActionPrevious(feed, request); break;
case ADD_COMMENT:
- CommentableFeed cFeed = vfg.getCommentableFeed(getId(),
- request);
- if (cFeed != null) { processActionAddComment(cFeed, request); }
- break;
+ try {
+ processActionAddComment(vfg.getCommentableFeed(getId(),
+ request), request);
+ } catch (FeedDoesNotExistException e) {
+ // Doing nothing.
+ }
}
}
+ /*
+ * ---------
+ * RENDERING
+ * ---------
+ */
+
public void doView(RenderRequest request,
RenderResponse response)
throws PortletException, IOException {
// Getting the feed to display.
- Feed feed = vfg.getFeed(getId(), request);
+ Feed feed;
+ try {
+ feed = vfg.getFeed(getId(), request);
+ } catch (FeedDoesNotExistException e) {
+ feed = new DummyFeed();
+ }
- // Checking if the feed is commentable. If so, setting the
- // necessary parameters.
- CommentableFeed cFeed = vfg.getCommentableFeed(getId(), request);
- if (cFeed != null && cFeed.getCommentsEnabled()) {
- request.setAttribute(COMMENTABLE, true);
+ CommentableFeed cFeed;
+ try {
+ // Checking if the feed is commentable. If so, setting the
+ // necessary parameters.
+ cFeed = vfg.getCommentableFeed(getId(), request);
+ if (cFeed.getCommentsEnabled()) {
+ request.setAttribute(COMMENTABLE, true);
- if (cFeed.getUserCanAddComments(request.getRemoteUser())) {
- request.setAttribute(COMMENTABLE_BY_USER, true);
+ if (cFeed.getUserCanAddComments(request.getRemoteUser())) {
+ request.setAttribute(COMMENTABLE_BY_USER, true);
+ }
}
+ } catch (FeedDoesNotExistException e) {
+ // If the feed is not commentable, no actions are necessary.
+ cFeed = null;
}
// Copying messages.
@@ -359,7 +384,7 @@
}
} else {
// 2. Otherwise, we have to get them from the URL.
-
+ // TODO
}
}
@@ -374,8 +399,8 @@
// Setting entries boundaries.
int begin = getEntriesBegin(feed.getGuid(), request);
- request.setAttribute(BEGIN_ATTR, begin);
- request.setAttribute(END_ATTR, begin + feed.getEntriesVisible() - 1);
+ request.setAttribute(BEGIN, begin);
+ request.setAttribute(END, begin + feed.getEntriesVisible() - 1);
if (feed.getEntriesExpanded()) {
// Expanding all entries.
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/AbstractFeedGetter.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/AbstractFeedGetter.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/AbstractFeedGetter.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -3,6 +3,7 @@
import org.jboss.shotoku.feeds.data.CommentableFeed;
import org.jboss.shotoku.feeds.data.Feed;
import org.jboss.shotoku.feeds.tools.FeedsTools;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
import javax.portlet.PortletRequest;
@@ -11,12 +12,14 @@
*/
public abstract class AbstractFeedGetter implements ViewFeedGetter {
public CommentableFeed getCommentableFeed(String id,
- PortletRequest request) {
+ PortletRequest request)
+ throws FeedDoesNotExistException {
return FeedsTools.getService().getCommentableFeed(id,
getName(request), getType(request));
}
- public Feed getFeed(String id, PortletRequest request) {
+ public Feed getFeed(String id, PortletRequest request)
+ throws FeedDoesNotExistException {
return FeedsTools.getService().getFeed(id, getName(request),
getType(request));
}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/ViewFeedGetter.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/ViewFeedGetter.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/ViewFeedGetter.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -2,6 +2,7 @@
import org.jboss.shotoku.feeds.data.Feed;
import org.jboss.shotoku.feeds.data.CommentableFeed;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
import javax.portlet.PortletRequest;
@@ -14,7 +15,7 @@
public String getEntry(PortletRequest request);
public Integer getFrom(PortletRequest request);
- public Feed getFeed(String id, PortletRequest request);
+ public Feed getFeed(String id, PortletRequest request) throws FeedDoesNotExistException;
public CommentableFeed getCommentableFeed(String id,
- PortletRequest request);
+ PortletRequest request) throws FeedDoesNotExistException;
}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/ViewFeedGetterWrapper.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/ViewFeedGetterWrapper.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/portlet/getter/ViewFeedGetterWrapper.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -3,6 +3,7 @@
import org.jboss.shotoku.feeds.data.Feed;
import org.jboss.shotoku.feeds.data.CommentableFeed;
import org.jboss.shotoku.feeds.data.DummyFeed;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
import javax.portlet.PortletRequest;
@@ -32,12 +33,13 @@
return vfg.getFrom(request);
}
- public Feed getFeed(String id, PortletRequest request) {
- Feed feed = vfg.getFeed(id, request);
- return feed == null ? new DummyFeed() : feed;
+ public Feed getFeed(String id, PortletRequest request)
+ throws FeedDoesNotExistException {
+ return vfg.getFeed(id, request);
}
- public CommentableFeed getCommentableFeed(String id, PortletRequest request) {
+ public CommentableFeed getCommentableFeed(String id, PortletRequest request)
+ throws FeedDoesNotExistException {
return vfg.getCommentableFeed(id, request);
}
}
Deleted: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceImpl.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceImpl.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceImpl.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -1,324 +0,0 @@
-package org.jboss.shotoku.feeds.service;
-
-import org.jboss.annotation.ejb.Service;
-import org.jboss.annotation.ejb.Management;
-import org.jboss.annotation.ejb.Depends;
-import org.jboss.shotoku.feeds.tools.FeedsConstants;
-import org.jboss.shotoku.feeds.tools.FeedsTools;
-import org.jboss.shotoku.feeds.tools.NodesFeedsProperties;
-import org.jboss.shotoku.feeds.CommentsService;
-import org.jboss.shotoku.feeds.comments.FeedDoesNotExistException;
-import org.jboss.shotoku.feeds.comments.CommentsNotAvialableException;
-import org.jboss.shotoku.feeds.comments.UnauthorizedToCommentException;
-import org.jboss.shotoku.feeds.comments.CommentEntity;
-import org.jboss.shotoku.feeds.data.CommentableFeed;
-import org.jboss.shotoku.feeds.data.ShotokuCommentableFeed;
-import org.jboss.shotoku.user.tools.UserConstants;
-import org.jboss.shotoku.user.UserNotFoundException;
-import org.jboss.shotoku.tools.Constants;
-import org.jboss.shotoku.tools.Tools;
-import org.jboss.shotoku.service.AdministratedServiceImpl;
-import org.jboss.shotoku.service.AdministratedService;
-import org.jboss.shotoku.tags.tools.UserTools;
-import org.jboss.shotoku.Node;
-import org.jboss.shotoku.Directory;
-import org.jboss.shotoku.ContentManager;
-import org.jboss.shotoku.exceptions.NameFormatException;
-import org.jboss.shotoku.exceptions.ResourceAlreadyExists;
-import org.jboss.shotoku.exceptions.ResourceDoesNotExist;
-import org.apache.log4j.Logger;
-
-import javax.ejb.Local;
-import javax.persistence.PersistenceUnit;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.EntityManager;
-import javax.transaction.UserTransaction;
-import javax.naming.InitialContext;
-import java.util.List;
-import java.util.ArrayList;
-
-/**
- * @author Adam Warski (adamw at aster.pl)
- */
- at Service(objectName = FeedsConstants.COMMENTS_SERVICE_NAME)
- at Local(CommentsServiceLocal.class)
- at Management(CommentsService.class)
- at Depends({UserConstants.USER_SERVICE_NAME, Constants.SHOTOKU_SERVICE_NAME,
- FeedsConstants.FEEDS_SERVICE_NAME})
-public class CommentsServiceImpl extends AdministratedServiceImpl
- implements CommentsService, CommentsServiceLocal {
- private static final Logger log = Logger.getLogger(AdministratedService.class);
-
- /*
- * CommentsService implementation.
- */
-
- @SuppressWarnings({"UNUSED_SYMBOL"})
- @PersistenceUnit
- private EntityManagerFactory emf;
-
- public void addComment(String feedId, String feedName, String feedElement,
- String feedType, String username,
- String title, String content, String userIp)
- throws FeedDoesNotExistException, CommentsNotAvialableException,
- UnauthorizedToCommentException {
- System.out.println("ADD COMMENT:");
- System.out.println("feedId = " + feedId);
- System.out.println("feedName = " + feedName);
- System.out.println("feedType = " + feedType);
- System.out.println("feedElement = " + feedElement);
- System.out.println("username = " + username);
- System.out.println("title = " + title);
- System.out.println("content = " + content);
-
- // Getting the feed and checking if it exists.
- CommentableFeed feed = FeedsTools.getService().getCommentableFeed(
- feedId, feedName, feedType);
-
- if (feed == null) {
- throw new FeedDoesNotExistException();
- }
-
- // Checking if comments are enabled for this feed.
- if (!feed.getCommentsEnabled()) {
- throw new CommentsNotAvialableException();
- }
-
- // Checking if there is a user logged in and if it is a valid user.
- try {
- UserTools.getService().getUser(username);
- } catch (UserNotFoundException e) {
- throw new UnauthorizedToCommentException(e);
- }
-
- // Checking if the commented item exists in the content manager.
- if (!feed.getFeedEntryExists(feedElement)) {
- throw new FeedDoesNotExistException("Invalid feed element.");
- }
-
- // Saving the comment for later addition to Shotoku.
- EntityManager em = emf.createEntityManager();
- try {
- em.persist(new CommentEntity(feedId, feedName, feedType,
- feedElement, username, title, content));
- } finally {
- em.close();
- }
- }
-
- public String getCommentFeedName(String feedName, String feedElement) {
- return "comments/" + feedName + "/" + Tools.encodeURL(feedElement);
- }
-
- public String getCommentFeedLink(String feedName,
- String feedElement, String feedType) {
- return "/feeds/" + getCommentFeedName(feedName, feedElement) +
- "/" + feedType;
- }
-
- private String getCommentsDirectoryName(Node commentedNode) {
- return commentedNode.getName() + "_comments";
- }
-
- public Directory getCommentsDirectory(Node commentedNode)
- throws ResourceDoesNotExist {
- return commentedNode.getParent().getDirectory(
- getCommentsDirectoryName(commentedNode));
- }
-
- /*
- * Update method.
- */
-
- /**
- * Creates a node in the given directory, in which a comment can
- * be placed. Effectively, crates a new "fresh" node, which
- * doesn't yet exist.
- * @param commentDir Directory in which the node shold be created.
- * @param number Number from which will be appended to the end of
- * the node name. If a node with this name already exists, the next
- * number will be tried, and so on.
- * @return A "fresh" node.
- */
- private Node createCommentNode(Directory commentDir, int number)
- throws NameFormatException {
- try {
- return commentDir.newNode("c" + System.currentTimeMillis()
- + "_" + number);
- } catch (ResourceAlreadyExists e) {
- return createCommentNode(commentDir, number + 1);
- }
- }
-
- private Node createCommentNode(Directory commentDir)
- throws NameFormatException {
- return createCommentNode(commentDir, 0);
- }
-
- private void removeCommentEntity(EntityManager em, CommentEntity ce)
- throws Throwable {
- UserTransaction tran = null;
- try {
- InitialContext jndiCntx = new InitialContext();
- tran = (UserTransaction) jndiCntx.lookup("UserTransaction");
-
- tran.begin();
- em.joinTransaction();
- em.remove(ce);
- tran.commit();
- } catch (Throwable t) {
- if (tran != null) {
- tran.rollback();
- }
-
- throw t;
- }
- }
-
- public void update() {
- // Adding any (possible) comments.
- EntityManager updateThreadEm = emf.createEntityManager();
-
- try {
- //noinspection unchecked
- List<CommentEntity> result = updateThreadEm.createQuery(
- "FROM CommentEntity").getResultList();
-
- List<CommentEntity> failedComments = new ArrayList<CommentEntity>();
- for (CommentEntity ce : result) {
- try {
- CommentableFeed feed = FeedsTools.getService().
- getCommentableFeed(ce.getFeedId(),
- ce.getFeedName(), ce.getFeedType());
-
- // Checking if the feed exists.
- if (feed == null || !feed.getCommentsEnabled() ||
- !(feed instanceof ShotokuCommentableFeed)) {
- failedComments.add(ce);
- continue;
- }
-
- ShotokuCommentableFeed scf = (ShotokuCommentableFeed) feed;
-
- Node commentedElement = scf.getNodeForFeedElement(
- ce.getFeedElement());
-
- // Checking if the commented element exists.
- if (commentedElement == null) {
- failedComments.add(ce);
- continue;
- }
-
- // Adding the comment.
-
- // Getting/ creating the comment directory.
- Directory commentDir;
- try {
- commentDir = getCommentsDirectory(commentedElement);
- } catch (ResourceDoesNotExist e) {
- try {
- commentDir = commentedElement.getParent().
- newDirectory(getCommentsDirectoryName(
- commentedElement));
- } catch (ResourceAlreadyExists e1) {
- log.warn("A node which does exist and doesn't.", e1);
- failedComments.add(ce);
- continue;
- }
- }
-
- // Creating the comment node.
- Node commentNode = createCommentNode(commentDir);
- commentNode.setContent(ce.getContent());
- commentNode.setProperty(NodesFeedsProperties.TITLE,
- ce.getTitle());
- commentNode.setProperty(NodesFeedsProperties.AUTHOR,
- ce.getUsername());
- if (feed.getCommentsModerated()) {
- commentNode.setProperty(
- NodesFeedsProperties.ACCEPTED_COMMENT_NAME,
- NodesFeedsProperties.NOTACCEPTED_COMMENT_VALUE);
- } else {
- // If the feed is not moderated, the entry gets
- // automatically accepted.
- commentNode.setProperty(
- NodesFeedsProperties.ACCEPTED_COMMENT_NAME,
- NodesFeedsProperties.ACCEPTED_COMMENT_VALUE);
- }
-
- // Saving.
- commentNode.getContentManager().save("Adding a comment",
- commentNode, commentDir);
- } catch (Throwable t) {
- log.warn("Exception when trying to add a comment.", t);
- failedComments.add(ce);
- }
-
- try {
- removeCommentEntity(updateThreadEm, ce);
- } catch (Throwable t) {
- log.warn("Exception when trying to remove a comment entity.", t);
- }
- }
-
- // Logging information about comments, which addition failed.
- for (CommentEntity ce : failedComments) {
- log.warn("Failed to add comment: " +
- ce.getFeedId() + " :: " + ce.getFeedName() + " :: " +
- ce.getFeedType() + " :: " + ce.getFeedElement() + " :: " +
- ce.getTitle() + " :: " + ce.getContent() + " :: " +
- ce.getUsername());
-
- try {
- removeCommentEntity(updateThreadEm, ce);
- } catch (Throwable t) {
- log.warn("Exception when trying to remove a comment entity.", t);
- }
- }
- } finally {
- updateThreadEm.close();
- }
- }
-
- /*
- * Service management.
- */
- public void create() throws Exception {
- super.create();
-
- setTimerInterval(ContentManager.getProperty(
- FeedsConstants.PROPERTY_INTERVAL, 10000));
-
- super.afterCreate();
- }
-
- public void start() throws Exception {
- super.start();
- startUpdateThread();
- super.afterStart();
- }
-
- public void destroy() {
-
- }
-
- /*
- * Administrated service implementation.
- */
-
- public String getServiceId() {
- return "ShotokuCommentsService";
- }
-
- public String getServiceName() {
- return "Comments service";
- }
-
- public String getServiceDescription() {
- return "Shotoku comments service.";
- }
-
- public AdministratedService getServiceInstance() {
- return FeedsTools.getCommentsService();
- }
-}
Deleted: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceLocal.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceLocal.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/CommentsServiceLocal.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -1,7 +0,0 @@
-package org.jboss.shotoku.feeds.service;
-
-/**
- * @author Adam Warski (adamw at aster.pl)
- */
-public interface CommentsServiceLocal {
-}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -6,6 +6,7 @@
import org.jboss.shotoku.feeds.FeedsService;
import org.jboss.shotoku.feeds.FeedsDescriptor;
import org.jboss.shotoku.feeds.FeedsConfigurationWatcher;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
import org.jboss.shotoku.feeds.data.Feed;
import org.jboss.shotoku.feeds.data.CommentableFeed;
import org.jboss.shotoku.feeds.tools.FeedsConstants;
@@ -57,12 +58,14 @@
return getFeedsDescriptor(id).getProperty(name);
}
- public Feed getFeed(String id, String name, String type) {
+ public Feed getFeed(String id, String name, String type)
+ throws FeedDoesNotExistException {
return getFeedsDescriptor(id).getFeed(name, type);
}
public CommentableFeed getCommentableFeed(String id, String name,
- String type) {
+ String type)
+ throws FeedDoesNotExistException {
return getFeedsDescriptor(id).getCommentableFeed(name, type);
}
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AggregatedFeedTagHandler.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AggregatedFeedTagHandler.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tag/AggregatedFeedTagHandler.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -5,6 +5,7 @@
import org.jboss.shotoku.feeds.data.NormalFeed;
import org.jboss.shotoku.feeds.FeedFactory;
import org.jboss.shotoku.feeds.FeedsDescriptor;
+import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
import org.jboss.shotoku.feeds.tools.FeedDefAttributes;
import org.jboss.shotoku.feeds.variables.Substitution;
import org.jboss.shotoku.tools.Tools;
@@ -51,18 +52,27 @@
for (Substitution s : substitutions) {
String name = s.make(nameAttr);
- NormalFeed f = fd.getNormalFeed(name, feedType);
-
- if (f == null) {
+ NormalFeed f;
+ try {
+ f = fd.getNormalFeed(name, feedType);
+ } catch (FeedDoesNotExistException e) {
// Trying to generate the feed.
if (!fd.generateFeed(name)) {
- log.error("Cannot find all aggregated feeds or feeds definitions " +
- "cycle for feed " + feedName + ".");
+ log.error("Cannot find all aggregated feeds or " +
+ "feeds definitions cycle for feed " +
+ feedName + " (failed to get feed: " +
+ name + ").");
return false;
}
// The feeds have been successfully generated.
- f = fd.getNormalFeed(name, feedType);
+ try {
+ f = fd.getNormalFeed(name, feedType);
+ } catch (FeedDoesNotExistException e1) {
+ log.error("Feed generation return true, yet the " +
+ "feed " + name + " does not exist");
+ return false;
+ }
}
toAggregate.add(f);
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsTools.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsTools.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/tools/FeedsTools.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -4,9 +4,9 @@
import org.apache.log4j.Logger;
import org.jboss.shotoku.feeds.FeedsDescriptor;
import org.jboss.shotoku.feeds.FeedsService;
-import org.jboss.shotoku.feeds.CommentsService;
+import org.jboss.shotoku.feeds.comments.CommentsService;
import org.jboss.shotoku.feeds.service.FeedsServiceImpl;
-import org.jboss.shotoku.feeds.service.CommentsServiceImpl;
+import org.jboss.shotoku.feeds.comments.service.CommentsServiceImpl;
import org.jboss.shotoku.ContentManager;
import org.jboss.shotoku.tools.Tools;
import org.jboss.mx.util.MBeanProxyExt;
Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/AbstractTag.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/AbstractTag.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/AbstractTag.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -78,4 +78,9 @@
public Integer getFeedVisits() {
return TagTools.getService().getFeedVisits(FeedType.TAGS, getName());
}
+
+ public String toString() {
+ return "(" + getName() + ", " + getAuthor() + ", " +
+ getResourceId() + ")";
+ }
}
Added: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/ExceptionsInterceptor.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/ExceptionsInterceptor.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/ExceptionsInterceptor.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -0,0 +1,23 @@
+package org.jboss.shotoku.tags.service;
+
+import org.jboss.shotoku.tags.exceptions.TagAddException;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+/**
+ * @author Adam Warski (adamw at aster.pl)
+ */
+public class ExceptionsInterceptor {
+ @AroundInvoke
+ public Object catchExceptionsInterceptor(
+ InvocationContext invocation) throws Exception {
+ try {
+ System.out.println("INTERCEPTOR");
+ return invocation.proceed();
+ } catch (Exception e) {
+ System.out.println("Exception caught (2): " + e.getMessage());
+ throw new TagAddException(e);
+ }
+ }
+}
Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/service/TagServiceImpl.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -28,6 +28,7 @@
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
import javax.transaction.SystemException;
+import javax.interceptor.Interceptors;
import org.apache.log4j.Logger;
import org.jboss.annotation.ejb.Management;
@@ -59,647 +60,648 @@
@Management(TagService.class)
@Depends(Constants.SHOTOKU_SERVICE_NAME)
public class TagServiceImpl extends AdministratedServiceImpl implements
- TagService, TagServiceLocal {
- private static final Logger log = Logger
- .getLogger(AdministratedService.class);
+ TagService, TagServiceLocal {
+ private static final Logger log = Logger
+ .getLogger(AdministratedService.class);
- private boolean syncOn;
+ private boolean syncOn;
- private ContentManager cm;
+ private ContentManager cm;
- @SuppressWarnings( { "UNUSED_SYMBOL" })
- @PersistenceUnit
- private EntityManagerFactory emf;
+ @SuppressWarnings({ "UNUSED_SYMBOL" })
+ @PersistenceUnit
+ private EntityManagerFactory emf;
- /*
- * Service lifecycle management.
- */
+ /*
+ * Service lifecycle management.
+ */
- public void create() throws Exception {
- super.create();
+ public void create() throws Exception {
+ super.create();
- setTimerInterval(ContentManager
- .getProperty(
- org.jboss.shotoku.tags.tools.Constants.PROPERTY_INTERVAL,
- 10000));
+ setTimerInterval(ContentManager
+ .getProperty(
+ org.jboss.shotoku.tags.tools.Constants.PROPERTY_INTERVAL,
+ 10000));
- syncOn = ContentManager.getProperty(
- org.jboss.shotoku.tags.tools.Constants.PROPERTY_SYNC, 0) == 1;
- if (syncOn) {
- cm = ContentManager.getContentManager(ContentManager.getProperty(
- org.jboss.shotoku.tags.tools.Constants.PROPERTY_CMID,
- Constants.DEFAULT_ID), ContentManager.getProperty(
- org.jboss.shotoku.tags.tools.Constants.PROPERTY_CMDIR, ""));
- }
+ syncOn = ContentManager.getProperty(
+ org.jboss.shotoku.tags.tools.Constants.PROPERTY_SYNC, 0) == 1;
+ if (syncOn) {
+ cm = ContentManager.getContentManager(ContentManager.getProperty(
+ org.jboss.shotoku.tags.tools.Constants.PROPERTY_CMID,
+ Constants.DEFAULT_ID), ContentManager.getProperty(
+ org.jboss.shotoku.tags.tools.Constants.PROPERTY_CMDIR, ""));
+ }
- super.afterCreate();
- }
+ super.afterCreate();
+ }
- public void start() throws Exception {
- super.start();
- startUpdateThread();
- log.info("Tag service started with update interval: "
- + getTimerInterval());
- }
+ public void start() throws Exception {
+ super.start();
+ startUpdateThread();
+ log.info("Tag service started with update interval: "
+ + getTimerInterval());
+ }
- public void destroy() {
+ public void destroy() {
- }
+ }
- /*
- * Timer-handling functions.
- */
+ /*
+ * Timer-handling functions.
+ */
- public void update() {
- if (!syncOn) {
- // Synchronization is turned off.
- return;
- }
+ public void update() {
+ if (!syncOn) {
+ // Synchronization is turned off.
+ return;
+ }
- EntityManager em = emf.createEntityManager();
+ EntityManager em = emf.createEntityManager();
- try {
- // Performing synchronization (tags -> shotoku).
- // noinspection unchecked
- List<TagEntity> result = em.createQuery(
- "FROM TagEntity " + "WHERE synced = 0 OR synced IS NULL")
- .getResultList();
+ try {
+ // Performing synchronization (tags -> shotoku).
+ // noinspection unchecked
+ List<TagEntity> result = em.createQuery(
+ "FROM TagEntity " + "WHERE synced = 0 OR synced IS NULL")
+ .getResultList();
- if (result.size() == 0) {
- return;
- }
+ if (result.size() == 0) {
+ return;
+ }
- Set<Resource> toSave = new HashSet<Resource>();
- for (TagEntity te : result) {
- if (org.jboss.shotoku.tags.tools.Constants.SHOTOKU_TAG
- .equals(te.getType())) {
- try {
- Node n = cm.getNode(te.getResourceId());
- n.setProperty(te.getShotokuPropReprName(), te
- .getShotokuPropReprValue());
- toSave.add(n);
- } catch (ResourceDoesNotExist e) {
- log
- .warn(
- "Unable to synchronize tag with Shotoku resource, "
- + "the tagged resource does not exist.",
- e);
- }
- }
- }
+ Set<Resource> toSave = new HashSet<Resource>();
+ for (TagEntity te : result) {
+ if (org.jboss.shotoku.tags.tools.Constants.SHOTOKU_TAG
+ .equals(te.getType())) {
+ try {
+ Node n = cm.getNode(te.getResourceId());
+ n.setProperty(te.getShotokuPropReprName(), te
+ .getShotokuPropReprValue());
+ toSave.add(n);
+ } catch (ResourceDoesNotExist e) {
+ log
+ .warn(
+ "Unable to synchronize tag with Shotoku resource, "
+ + "the tagged resource does not exist.",
+ e);
+ }
+ }
+ }
- try {
- cm.save("", toSave);
- } catch (SaveException e) {
- log.warn(e);
- return;
- }
+ try {
+ cm.save("", toSave);
+ } catch (SaveException e) {
+ log.warn(e);
+ return;
+ }
- UserTransaction tran = null;
- try {
- InitialContext jndiCntx = new InitialContext();
- tran = (UserTransaction) jndiCntx.lookup("UserTransaction");
+ UserTransaction tran = null;
+ try {
+ InitialContext jndiCntx = new InitialContext();
+ tran = (UserTransaction) jndiCntx.lookup("UserTransaction");
- tran.begin();
- em.joinTransaction();
+ tran.begin();
+ em.joinTransaction();
- for (TagEntity te : result) {
- te.setSynced(true);
- }
+ for (TagEntity te : result) {
+ te.setSynced(true);
+ }
- tran.commit();
- } catch (Throwable t) {
- if (tran != null) {
- try {
- tran.rollback();
- } catch (SystemException e) {
- // Oh well ...
- }
- }
+ tran.commit();
+ } catch (Throwable t) {
+ if (tran != null) {
+ try {
+ tran.rollback();
+ } catch (SystemException e) {
+ // Oh well ...
+ }
+ }
- log.warn("Unable to synchronize tags.", t);
- }
- } finally {
- em.close();
- }
- }
+ log.warn("Unable to synchronize tags.", t);
+ }
+ } finally {
+ em.close();
+ }
+ }
- public String getServiceId() {
- return "ShotokuTagService";
- }
+ public String getServiceId() {
+ return "ShotokuTagService";
+ }
- public String getServiceName() {
- return "Tag service";
- }
+ public String getServiceName() {
+ return "Tag service";
+ }
- public String getServiceDescription() {
- return "Shotoku tag service";
- }
+ public String getServiceDescription() {
+ return "Shotoku tag service";
+ }
- public AdministratedService getServiceInstance() {
- return TagTools.getService();
- }
+ public AdministratedService getServiceInstance() {
+ return TagTools.getService();
+ }
- /*
- * TagService implementation.
- */
+ /*
+ * TagService implementation.
+ */
- /**
- * MIN_SIMILARITY - minimal similarity for checking relatedTags needed
- */
- private double MIN_SIMILARITY = 0.7d;
+ /**
+ * MIN_SIMILARITY - minimal similarity for checking relatedTags needed
+ */
+ private double MIN_SIMILARITY = 0.7d;
- private TagEntity getTagEntity(Tag t) {
- TagEntity te = new TagEntity();
+ private TagEntity getTagEntity(Tag t) {
+ TagEntity te = new TagEntity();
- te.setAuthor(t.getAuthor());
- te.setData(t.getData());
- te.setDateCreated(t.getDateCreated());
- te.setName(t.getName());
- te.setResourceId(t.getResourceId());
- te.setType(t.getType());
+ te.setAuthor(t.getAuthor());
+ te.setData(t.getData());
+ te.setDateCreated(t.getDateCreated());
+ te.setName(t.getName());
+ te.setResourceId(t.getResourceId());
+ te.setType(t.getType());
- return te;
- }
+ return te;
+ }
- public void addTag(Tag t) throws TagAddException {
- EntityManager manager = emf.createEntityManager();
- try {
- manager.persist(getTagEntity(t).normalizeName());
- } catch (Throwable e) {
- throw new TagAddException(e);
- } finally {
- manager.close();
- }
- }
+ @Interceptors(ExceptionsInterceptor.class)
+ public void addTag(Tag t) throws TagAddException {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ manager.persist(getTagEntity(t).normalizeName());
+ } catch (Exception e) {
+ throw new TagAddException(e);
+ } finally {
+ manager.close();
+ }
+ }
- public void deleteTag(Tag t) throws TagDeleteException {
- EntityManager manager = emf.createEntityManager();
- try {
- manager.remove(getTagEntity(t));
- } catch (Throwable e) {
- throw new TagDeleteException(e);
- } finally {
- manager.close();
- }
- }
+ public void deleteTag(Tag t) throws TagDeleteException {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ manager.remove(getTagEntity(t));
+ } catch (Throwable e) {
+ throw new TagDeleteException(e);
+ } finally {
+ manager.close();
+ }
+ }
- private void sortTagsByDate(List<Tag> tags) {
- Collections.sort(tags, new Comparator<Tag>() {
- public int compare(Tag o1, Tag o2) {
- return o1.getDateCreated().compareTo(o2.getDateCreated());
- }
- });
- }
+ private void sortTagsByDate(List<Tag> tags) {
+ Collections.sort(tags, new Comparator<Tag>() {
+ public int compare(Tag o1, Tag o2) {
+ return o1.getDateCreated().compareTo(o2.getDateCreated());
+ }
+ });
+ }
- public List<Tag> getTags(String resourceId) throws TagGetException {
- EntityManager manager = emf.createEntityManager();
- try {
- // noinspection unchecked
- List<TagEntity> result = manager
- .createQuery(
- "from TagEntity where resourceId = :resourceId order by dateCreated desc")
- .setParameter("resourceId", resourceId).getResultList();
+ public List<Tag> getTags(String resourceId) throws TagGetException {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ // noinspection unchecked
+ List<TagEntity> result = manager
+ .createQuery(
+ "from TagEntity where resourceId = :resourceId order by dateCreated desc")
+ .setParameter("resourceId", resourceId).getResultList();
- List<Tag> ret = new ArrayList<Tag>();
- for (TagEntity te : result) {
- ret.add(te.getTag());
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (TagEntity te : result) {
+ ret.add(te.getTag());
+ }
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- } finally {
- manager.close();
- }
- }
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ } finally {
+ manager.close();
+ }
+ }
- public Tag getTag(String tagName) throws TagGetException {
- EntityManager manager = emf.createEntityManager();
- try {
- // noinspection unchecked
- TagEntity result = (TagEntity) manager.createQuery(
- "from TagEntity where name = :name").setParameter("name",
- tagName).getSingleResult();
+ public Tag getTag(String tagName) throws TagGetException {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ // noinspection unchecked
+ TagEntity result = (TagEntity) manager.createQuery(
+ "from TagEntity where name = :name").setParameter("name",
+ tagName).getSingleResult();
- if (result == null) {
- return null;
- }
+ if (result == null) {
+ return null;
+ }
- return result.getTag();
- } catch (Throwable e) {
- throw new TagGetException(e);
- } finally {
- manager.close();
- }
- }
+ return result.getTag();
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ } finally {
+ manager.close();
+ }
+ }
- public List<Tag> getTags(List<String> tagNames) throws TagGetException {
- EntityManager manager = emf.createEntityManager();
- try {
- StringBuffer querySb = new StringBuffer("FROM TagEntity WHERE ");
- int i = 0;
- for (Iterator iter = tagNames.iterator(); iter.hasNext();) {
- querySb.append("name").append(" = :name").append(i);
+ public List<Tag> getTags(List<String> tagNames) throws TagGetException {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ StringBuffer querySb = new StringBuffer("FROM TagEntity WHERE ");
+ int i = 0;
+ for (Iterator iter = tagNames.iterator(); iter.hasNext();) {
+ querySb.append("name").append(" = :name").append(i);
- iter.next();
+ iter.next();
- if (iter.hasNext()) {
- querySb.append(" OR ");
- }
+ if (iter.hasNext()) {
+ querySb.append(" OR ");
+ }
- i++;
- }
+ i++;
+ }
- Query query = manager.createQuery(querySb.toString());
- i = 0;
- for (String tagName : tagNames) {
- query.setParameter("name" + i++, tagName);
- }
+ Query query = manager.createQuery(querySb.toString());
+ i = 0;
+ for (String tagName : tagNames) {
+ query.setParameter("name" + i++, tagName);
+ }
- // noinspection unchecked
- List<TagEntity> result = query.getResultList();
+ // noinspection unchecked
+ List<TagEntity> result = query.getResultList();
- List<Tag> ret = new ArrayList<Tag>();
- for (TagEntity te : result) {
- ret.add(te.getTag());
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (TagEntity te : result) {
+ ret.add(te.getTag());
+ }
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- } finally {
- manager.close();
- }
- }
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ } finally {
+ manager.close();
+ }
+ }
- public List<Tag> getUniqueTags(List<String> tagNames)
- throws TagGetException {
- EntityManager manager = emf.createEntityManager();
- try {
- StringBuffer querySb = new StringBuffer(
- "SELECT name, resourceId, min(dateCreated) "
- + "FROM TagEntity WHERE ");
- int i = 0;
- for (Iterator iter = tagNames.iterator(); iter.hasNext();) {
- querySb.append("name").append(" = :name").append(i);
+ public List<Tag> getUniqueTags(List<String> tagNames)
+ throws TagGetException {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ StringBuffer querySb = new StringBuffer(
+ "SELECT name, resourceId, min(dateCreated) "
+ + "FROM TagEntity WHERE ");
+ int i = 0;
+ for (Iterator iter = tagNames.iterator(); iter.hasNext();) {
+ querySb.append("name").append(" = :name").append(i);
- iter.next();
+ iter.next();
- if (iter.hasNext()) {
- querySb.append(" OR ");
- }
+ if (iter.hasNext()) {
+ querySb.append(" OR ");
+ }
- i++;
- }
+ i++;
+ }
- querySb.append(" GROUP BY name, resourceId");
+ querySb.append(" GROUP BY name, resourceId");
- Query query = manager.createQuery(querySb.toString());
- i = 0;
- for (String tagName : tagNames) {
- query.setParameter("name" + i++, tagName);
- }
+ Query query = manager.createQuery(querySb.toString());
+ i = 0;
+ for (String tagName : tagNames) {
+ query.setParameter("name" + i++, tagName);
+ }
- // noinspection unchecked
- List<Object[]> result = query.getResultList();
+ // noinspection unchecked
+ List<Object[]> result = query.getResultList();
- List<Tag> ret = new ArrayList<Tag>();
- for (Object[] o : result) {
- ret.add(new DummyTag((String) o[0], null, (String) o[1], null,
- (Date) o[2]));
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (Object[] o : result) {
+ ret.add(new DummyTag((String) o[0], null, (String) o[1], null,
+ (Date) o[2]));
+ }
- sortTagsByDate(ret);
+ sortTagsByDate(ret);
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- } finally {
- manager.close();
- }
- }
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ } finally {
+ manager.close();
+ }
+ }
- @SuppressWarnings( { "unchecked" })
- public List<Tag> getRelatedTags(List<Tag> relateTo) throws TagGetException {
- List<Tag> ret = new ArrayList<Tag>();
+ @SuppressWarnings( { "unchecked" })
+ public List<Tag> getRelatedTags(List<Tag> relateTo) throws TagGetException {
+ List<Tag> ret = new ArrayList<Tag>();
- Map<String, List<Tag>> otherResources = new HashMap<String, List<Tag>>();
+ Map<String, List<Tag>> otherResources = new HashMap<String, List<Tag>>();
- EntityManager manager = emf.createEntityManager();
- try {
- for (Tag relatedTag : relateTo) {
- List<TagEntity> result = manager.createQuery(
- "from TagEntity where name = :name").setParameter(
- "name", relatedTag.getName()).getResultList();
+ EntityManager manager = emf.createEntityManager();
+ try {
+ for (Tag relatedTag : relateTo) {
+ List<TagEntity> result = manager.createQuery(
+ "from TagEntity where name = :name").setParameter(
+ "name", relatedTag.getName()).getResultList();
- for (TagEntity otherTag : result) {
- if (!otherResources.containsKey(otherTag.getResourceId())) {
- otherResources.put(otherTag.getResourceId(),
- getTags(otherTag.getResourceId()));
- }
- }
- }
+ for (TagEntity otherTag : result) {
+ if (!otherResources.containsKey(otherTag.getResourceId())) {
+ otherResources.put(otherTag.getResourceId(),
+ getTags(otherTag.getResourceId()));
+ }
+ }
+ }
- for (List<Tag> tagList : otherResources.values()) {
- if (checkSimilarity(relateTo, tagList) >= MIN_SIMILARITY) {
- ret.addAll(tagList);
- }
- }
+ for (List<Tag> tagList : otherResources.values()) {
+ if (checkSimilarity(relateTo, tagList) >= MIN_SIMILARITY) {
+ ret.addAll(tagList);
+ }
+ }
- // don't return "relateTo" members
- List<Tag> endRet = new ArrayList<Tag>(ret);
- for (Tag tag : ret) {
- if (tagListContainsTag(tag, relateTo)) {
- endRet.remove(tag);
- }
- }
+ // don't return "relateTo" members
+ List<Tag> endRet = new ArrayList<Tag>(ret);
+ for (Tag tag : ret) {
+ if (tagListContainsTag(tag, relateTo)) {
+ endRet.remove(tag);
+ }
+ }
- return endRet;
- } finally {
- manager.close();
- }
- }
+ return endRet;
+ } finally {
+ manager.close();
+ }
+ }
- private boolean tagListContainsTag(Tag tag, List<Tag> listToCheck) {
- for (Tag tag2 : listToCheck) {
- if (tag.getName().equals(tag2.getName())) {
- return true;
- }
- }
+ private boolean tagListContainsTag(Tag tag, List<Tag> listToCheck) {
+ for (Tag tag2 : listToCheck) {
+ if (tag.getName().equals(tag2.getName())) {
+ return true;
+ }
+ }
- return false;
- }
+ return false;
+ }
- private double checkSimilarity(List<Tag> givenTags, List<Tag> listToCheck) {
- double ret = 0;
+ private double checkSimilarity(List<Tag> givenTags, List<Tag> listToCheck) {
+ double ret = 0;
- for (Tag tag : givenTags) {
- if (tagListContainsTag(tag, listToCheck)) {
- ret++;
- }
- }
+ for (Tag tag : givenTags) {
+ if (tagListContainsTag(tag, listToCheck)) {
+ ret++;
+ }
+ }
- return ret / (double) givenTags.size();
- }
+ return ret / (double) givenTags.size();
+ }
- public List<Tag> getTagsByAuthor(String author) throws TagGetException {
- EntityManager manager = emf.createEntityManager();
- try {
- // noinspection unchecked
- List<TagEntity> result = manager
- .createQuery(
- "from TagEntity where author = :author order by dateCreated desc")
- .setParameter("author", author).getResultList();
+ public List<Tag> getTagsByAuthor(String author) throws TagGetException {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ // noinspection unchecked
+ List<TagEntity> result = manager
+ .createQuery(
+ "from TagEntity where author = :author order by dateCreated desc")
+ .setParameter("author", author).getResultList();
- List<Tag> ret = new ArrayList<Tag>();
- for (TagEntity te : result) {
- ret.add(te.getTag());
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (TagEntity te : result) {
+ ret.add(te.getTag());
+ }
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- } finally {
- manager.close();
- }
- }
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ } finally {
+ manager.close();
+ }
+ }
- public List<Tag> getUniqueTagsByAuthor(String author)
- throws TagGetException {
- EntityManager manager = emf.createEntityManager();
- try {
- // noinspection unchecked
- List<Object[]> result = manager.createQuery(
- "SELECT name, min(dateCreated) FROM TagEntity "
- + "WHERE author = :author " + "GROUP BY name") // +
- // "ORDER BY mdc DESC")
- .setParameter("author", author).getResultList();
+ public List<Tag> getUniqueTagsByAuthor(String author)
+ throws TagGetException {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ // noinspection unchecked
+ List<Object[]> result = manager.createQuery(
+ "SELECT name, min(dateCreated) FROM TagEntity "
+ + "WHERE author = :author " + "GROUP BY name") // +
+ // "ORDER BY mdc DESC")
+ .setParameter("author", author).getResultList();
- List<Tag> ret = new ArrayList<Tag>();
- for (Object[] o : result) {
- ret.add(new DummyTag((String) o[0], author, null, null,
- (Date) o[1]));
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (Object[] o : result) {
+ ret.add(new DummyTag((String) o[0], author, null, null,
+ (Date) o[1]));
+ }
- sortTagsByDate(ret);
+ sortTagsByDate(ret);
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- } finally {
- manager.close();
- }
- }
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ } finally {
+ manager.close();
+ }
+ }
- public List<Tag> getTags(String name, String author) throws TagGetException {
- EntityManager manager = emf.createEntityManager();
- try {
- // noinspection unchecked
- List<TagEntity> result = (List<TagEntity>) manager.createQuery(
- "FROM TagEntity WHERE name = :name AND "
- + "author = :author").setParameter("name", name)
- .setParameter("author", author).getResultList();
+ public List<Tag> getTags(String name, String author) throws TagGetException {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ // noinspection unchecked
+ List<TagEntity> result = (List<TagEntity>) manager.createQuery(
+ "FROM TagEntity WHERE name = :name AND "
+ + "author = :author").setParameter("name", name)
+ .setParameter("author", author).getResultList();
- if (result == null) {
- return null;
- }
+ if (result == null) {
+ return null;
+ }
- List<Tag> ret = new ArrayList<Tag>();
- for (TagEntity te : result) {
- ret.add(te.getTag());
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (TagEntity te : result) {
+ ret.add(te.getTag());
+ }
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- } finally {
- manager.close();
- }
- }
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ } finally {
+ manager.close();
+ }
+ }
- public List<Tag> getUniqueTags(String resourceId) throws TagGetException {
- EntityManager manager = emf.createEntityManager();
- try {
- // noinspection unchecked
- List<Object[]> result = manager.createQuery(
- "SELECT name, min(dateCreated) FROM TagEntity "
- + "WHERE resourceId = :resourceId "
- + "GROUP BY name") // +
- // "ORDER BY min(dateCreated) DESC")
- .setParameter("resourceId", resourceId).getResultList();
+ public List<Tag> getUniqueTags(String resourceId) throws TagGetException {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ // noinspection unchecked
+ List<Object[]> result = manager.createQuery(
+ "SELECT name, min(dateCreated) FROM TagEntity "
+ + "WHERE resourceId = :resourceId "
+ + "GROUP BY name") // +
+ // "ORDER BY min(dateCreated) DESC")
+ .setParameter("resourceId", resourceId).getResultList();
- List<Tag> ret = new ArrayList<Tag>();
- for (Object[] o : result) {
- ret.add(new DummyTag((String) o[0], null, resourceId, null,
- (Date) o[1]));
- }
+ List<Tag> ret = new ArrayList<Tag>();
+ for (Object[] o : result) {
+ ret.add(new DummyTag((String) o[0], null, resourceId, null,
+ (Date) o[1]));
+ }
- sortTagsByDate(ret);
+ sortTagsByDate(ret);
- return ret;
- } catch (Throwable e) {
- throw new TagGetException(e);
- } finally {
- manager.close();
- }
- }
+ return ret;
+ } catch (Throwable e) {
+ throw new TagGetException(e);
+ } finally {
+ manager.close();
+ }
+ }
- public String getFeedLink(FeedType feedType, String data, String type) {
- return "/feeds/tag/" + feedType.toString() + "/"
- + Tools.encodeURL(data) + "/" + type;
- }
+ public String getFeedLink(FeedType feedType, String data, String type) {
+ return "/feeds/tag/" + feedType.toString() + "/"
+ + Tools.encodeURL(data) + "/" + type;
+ }
- public String getFeedLink(FeedType feedType, List<String> dataList,
- String type) {
- StringBuffer sb = new StringBuffer();
+ public String getFeedLink(FeedType feedType, List<String> dataList,
+ String type) {
+ StringBuffer sb = new StringBuffer();
- for (Iterator<String> iter = dataList.iterator(); iter.hasNext();) {
- sb.append(iter.next());
- if (iter.hasNext()) {
- sb.append("+");
- }
- }
+ for (Iterator<String> iter = dataList.iterator(); iter.hasNext();) {
+ sb.append(iter.next());
+ if (iter.hasNext()) {
+ sb.append("+");
+ }
+ }
- return "/feeds/tag/" + feedType.toString() + "/"
- + Tools.encodeURL(sb.toString()) + "/" + type;
- }
+ return "/feeds/tag/" + feedType.toString() + "/"
+ + Tools.encodeURL(sb.toString()) + "/" + type;
+ }
- public List<String> getAllTagsNames(String resourceId)
- throws TagGetException {
- EntityManager manager = emf.createEntityManager();
- // noinspection unchecked
- return (List<String>) manager
- .createQuery(
- "SELECT DISTINCT name FROM TagEntity WHERE resourceId = :resourceId")
- .setParameter("resourceId", resourceId).getResultList();
- }
+ public List<String> getAllTagsNames(String resourceId)
+ throws TagGetException {
+ EntityManager manager = emf.createEntityManager();
+ // noinspection unchecked
+ return (List<String>) manager
+ .createQuery(
+ "SELECT DISTINCT name FROM TagEntity WHERE resourceId = :resourceId")
+ .setParameter("resourceId", resourceId).getResultList();
+ }
- public List<Tag> getAllTags() throws TagGetException {
- EntityManager manager = emf.createEntityManager();
- try {
- List<Tag> result = new ArrayList<Tag>();
- // noinspection unchecked
- List<TagEntity> tagEnitites = manager.createQuery("from TagEntity")
- .getResultList();
+ public List<Tag> getAllTags() throws TagGetException {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ List<Tag> result = new ArrayList<Tag>();
+ // noinspection unchecked
+ List<TagEntity> tagEnitites = manager.createQuery("from TagEntity")
+ .getResultList();
- for (TagEntity tagEnt : tagEnitites) {
- result.add(tagEnt.getTag());
- }
+ for (TagEntity tagEnt : tagEnitites) {
+ result.add(tagEnt.getTag());
+ }
- return result;
- } finally {
- manager.close();
- }
- }
+ return result;
+ } finally {
+ manager.close();
+ }
+ }
- public List<Tag> searchTags(String tag, String author, String keyword) {
- List<Tag> searchResult = new ArrayList<Tag>();
+ public List<Tag> searchTags(String tag, String author, String keyword) {
+ List<Tag> searchResult = new ArrayList<Tag>();
- log.debug("'" + tag + "' '" + author + "' '" + keyword + "'");
+ log.debug("'" + tag + "' '" + author + "' '" + keyword + "'");
- TagSearch tagSearch = new TagSearch(tag, author, keyword);
+ TagSearch tagSearch = new TagSearch(tag, author, keyword);
- try {
- List<Tag> allTags = getAllTags();
+ try {
+ List<Tag> allTags = getAllTags();
- for (Tag nextTag : allTags) {
- if (tagSearch.matches(nextTag)) {
- searchResult.add(nextTag);
- }
- }
- } catch (TagGetException e) {
- e.printStackTrace();
- }
+ for (Tag nextTag : allTags) {
+ if (tagSearch.matches(nextTag)) {
+ searchResult.add(nextTag);
+ }
+ }
+ } catch (TagGetException e) {
+ e.printStackTrace();
+ }
- return searchResult;
- }
+ return searchResult;
+ }
- public int getFeedHits(FeedType feedType, String data) {
- EntityManager manager = emf.createEntityManager();
- try {
- HitsEntity ce = manager.find(HitsEntity.class, new HitsIdEntity(
- data, feedType.toString()));
+ public int getFeedHits(FeedType feedType, String data) {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ HitsEntity ce = manager.find(HitsEntity.class, new HitsIdEntity(
+ data, feedType.toString()));
- return ce == null ? 0 : ce.getCount();
- } finally {
- manager.close();
- }
- }
+ return ce == null ? 0 : ce.getCount();
+ } finally {
+ manager.close();
+ }
+ }
- public int getFeedVisits(FeedType feedType, String data) {
- EntityManager manager = emf.createEntityManager();
- try {
- VisitsEntity ve = manager.find(VisitsEntity.class,
- new VisitsIdEntity(data, feedType.toString()));
+ public int getFeedVisits(FeedType feedType, String data) {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ VisitsEntity ve = manager.find(VisitsEntity.class,
+ new VisitsIdEntity(data, feedType.toString()));
- return ve == null ? 0 : ve.getCount();
- } finally {
- manager.close();
- }
- }
+ return ve == null ? 0 : ve.getCount();
+ } finally {
+ manager.close();
+ }
+ }
- public void increaseFeedCounters(FeedType feedType, String data,
- String ipAddress) {
- EntityManager manager = emf.createEntityManager();
- try {
- // 1. Increasing the hit counter.
- HitsEntity ce = manager.find(HitsEntity.class, new HitsIdEntity(
- data, feedType.toString()));
+ public void increaseFeedCounters(FeedType feedType, String data,
+ String ipAddress) {
+ EntityManager manager = emf.createEntityManager();
+ try {
+ // 1. Increasing the hit counter.
+ HitsEntity ce = manager.find(HitsEntity.class, new HitsIdEntity(
+ data, feedType.toString()));
- if (ce == null) {
- manager.persist(new HitsEntity(new HitsIdEntity(data, feedType
- .toString()), 1));
- } else {
- ce.setCount(ce.getCount() + 1);
- manager.flush();
- }
+ if (ce == null) {
+ manager.persist(new HitsEntity(new HitsIdEntity(data, feedType
+ .toString()), 1));
+ } else {
+ ce.setCount(ce.getCount() + 1);
+ manager.flush();
+ }
- // 2. Increasing the visits counter.
- VisitsIpsEntity dbVie = manager
- .find(VisitsIpsEntity.class, new VisitsIpsIdEntity(data,
- feedType.toString(), ipAddress));
+ // 2. Increasing the visits counter.
+ VisitsIpsEntity dbVie = manager
+ .find(VisitsIpsEntity.class, new VisitsIpsIdEntity(data,
+ feedType.toString(), ipAddress));
- if (dbVie == null) {
- // Storing the new ip address.
- try {
- manager.persist(new VisitsIpsEntity(data, feedType
- .toString(), ipAddress));
- } catch (Exception e) {
- // Somebody must have already added it.
- return;
- }
+ if (dbVie == null) {
+ // Storing the new ip address.
+ try {
+ manager.persist(new VisitsIpsEntity(data, feedType
+ .toString(), ipAddress));
+ } catch (Exception e) {
+ // Somebody must have already added it.
+ return;
+ }
- // Increasing the counter.
- VisitsEntity ve = manager.find(VisitsEntity.class,
- new VisitsIdEntity(data, feedType.toString()));
+ // Increasing the counter.
+ VisitsEntity ve = manager.find(VisitsEntity.class,
+ new VisitsIdEntity(data, feedType.toString()));
- if (ve == null) {
- manager.persist(new VisitsEntity(new VisitsIdEntity(data,
- feedType.toString()), 1));
- } else {
- ve.setCount(ve.getCount() + 1);
- manager.flush();
- }
- }
- } finally {
- manager.close();
- }
- }
+ if (ve == null) {
+ manager.persist(new VisitsEntity(new VisitsIdEntity(data,
+ feedType.toString()), 1));
+ } else {
+ ve.setCount(ve.getCount() + 1);
+ manager.flush();
+ }
+ }
+ } finally {
+ manager.close();
+ }
+ }
- public Integer getAllSubscribers(String author) throws TagGetException {
- Integer visits = 0;
+ public Integer getAllSubscribers(String author) throws TagGetException {
+ Integer visits = 0;
- List<Tag> tagsByAuthor = getTagsByAuthor(author);
+ List<Tag> tagsByAuthor = getTagsByAuthor(author);
- for (Tag tag : tagsByAuthor) {
- visits += getFeedVisits(FeedType.AUTHOR_TAG, tag.getName() + "+"
- + tag.getAuthor());
- }
+ for (Tag tag : tagsByAuthor) {
+ visits += getFeedVisits(FeedType.AUTHOR_TAG, tag.getName() + "+"
+ + tag.getAuthor());
+ }
- visits += getFeedVisits(FeedType.AUTHOR_ALL, author);
- visits += getFeedVisits(FeedType.AUTHOR_UNIQUE, author);
+ visits += getFeedVisits(FeedType.AUTHOR_ALL, author);
+ visits += getFeedVisits(FeedType.AUTHOR_UNIQUE, author);
- return visits;
- }
+ return visits;
+ }
}
Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/test/TagsServiceMultiThreadTest.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/test/TagsServiceMultiThreadTest.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/test/TagsServiceMultiThreadTest.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -1,8 +1,110 @@
package org.jboss.shotoku.tags.test;
+import org.jboss.shotoku.tags.tools.TagTools;
+import org.jboss.shotoku.tags.ShotokuTag;
+import org.jboss.shotoku.tags.Tag;
+
+import java.util.Random;
+import java.util.Date;
+
/**
* @author Adam Warski (adamw at aster.pl)
*/
public class TagsServiceMultiThreadTest {
+ private void start(final int iterations) {
+ new Thread() {
+ private String[] tagNames = {
+ "tag0", "tag1", "tag2", "tag3", "tag4", "tag5" };
+ private String[] authors = { "admin", "adamw" };
+
+ private String[] resIds = {
+ "default/members/kosmos/freezone/kosmonauts.html",
+ "default/members/kosmos/freezone/contribution.html",
+ "default/members/kosmos/freezone/compatibility.html"
+ };
+
+ private Random random = new Random();
+
+ private int getRandom(int begin, int end) {
+ return random.nextInt(end-begin) + begin;
+ }
+
+ private int getRandom(int end) {
+ return random.nextInt(end);
+ }
+
+ public void run() {
+ int exceptions = 0;
+ for (int i=0; i<iterations; i++) {
+ try {
+ switch (getRandom(13)) {
+ case 0:
+ TagTools.getService().getTag(
+ tagNames[getRandom(tagNames.length)]);
+ break;
+ case 1:
+ case 9:
+ case 10:
+ case 11:
+ case 12:
+ Tag t = new ShotokuTag(
+ tagNames[getRandom(tagNames.length)],
+ authors[getRandom(authors.length)],
+ resIds[getRandom(resIds.length)],
+ null,
+ new Date());
+ System.out.println("Adding tag: " + t);
+ TagTools.getService().addTag(t);
+ break;
+ case 2:
+ TagTools.getService().getAllTags();
+ break;
+ case 3:
+ TagTools.getService().getTagsByAuthor(
+ authors[getRandom(authors.length)]);
+ break;
+ case 4:
+ TagTools.getService().getTags(
+ resIds[getRandom(resIds.length)]);
+ break;
+ case 5:
+ TagTools.getService().getTags(
+ tagNames[getRandom(tagNames.length)],
+ authors[getRandom(authors.length)]);
+ break;
+ case 6:
+ TagTools.getService().getUniqueTags(
+ resIds[getRandom(resIds.length)]);
+ break;
+ case 7:
+ TagTools.getService().deleteTag(
+ TagTools.getService().getTag(
+ tagNames[getRandom(
+ tagNames.length)]));
+ break;
+ case 8:
+ TagTools.getService().getUniqueTagsByAuthor(
+ authors[getRandom(authors.length)]);
+
+ }
+ //TagTools.getService().
+ } catch (Exception e) {
+ System.out.println("Exception caught (3): " +
+ e.getMessage());
+ exceptions++;
+ }
+ }
+
+ System.out.println("Done " + iterations + " operations, "
+ + exceptions + " of them threm exceptions.");
+ }
+ }.start();
+ }
+
+ public void start(int threads, int iterations) {
+ for (int i = 0; i<threads; i++) {
+ start(iterations);
+ }
+ }
}
Modified: labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/test/TagsTestServlet.java
===================================================================
--- labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/test/TagsTestServlet.java 2006-08-03 19:37:32 UTC (rev 5452)
+++ labs/shotoku/trunk/shotoku-tags/src/java/org/jboss/shotoku/tags/test/TagsTestServlet.java 2006-08-03 19:47:37 UTC (rev 5453)
@@ -15,6 +15,7 @@
import java.util.Calendar;
import java.util.List;
import java.util.ArrayList;
+import java.util.Date;
/**
* @author Adam Warski (adamw at aster.pl)
@@ -22,10 +23,30 @@
public class TagsTestServlet extends HttpServlet {
static int i = 2;
- protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
+ protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException {
httpServletResponse.setContentType("text/html");
- List<String> params = new ArrayList<String>();
+ try {
+ Tag t = new ShotokuTag(
+ "tag0",
+ "adamw",
+ "default/members/kosmos/freezone/kosmonauts.html",
+ null,
+ new Date());
+ System.out.println("Adding tag: " + t);
+ TagTools.getService().addTag(t);
+ } catch (Exception e) {
+ System.out.println("EXCEPTION!");
+ e.printStackTrace();
+ }
+ /*int threads = Integer.parseInt(request.getParameter("threads"));
+ int iterations = Integer.parseInt(request.getParameter("iterations"));
+
+ httpServletResponse.getWriter().println("Starting test ...");
+ new TagsServiceMultiThreadTest().start(threads, iterations);
+ httpServletResponse.getWriter().println("Done."); */
+
+ /*List<String> params = new ArrayList<String>();
params.add("adamw");
params.add("default/members/default/freezone/welcome.html");
httpServletResponse.getWriter().println(
@@ -35,7 +56,7 @@
httpServletResponse.getWriter().println(
TagTools.getService().getFeedLink(FeedType.RESOURCE,
"default/members/default/freezone/welcome.html",
- "rss2"));
+ "rss2")); */
/*httpServletResponse.getWriter().println(TagTools.getService().getServiceName());
More information about the jboss-svn-commits
mailing list