tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcombell...@apache.org
Subject svn commit: r786996 - in /tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main: java/scatours/blog/ java/scatours/blog/feed/ resources/
Date Sun, 21 Jun 2009 11:48:02 GMT
Author: mcombellack
Date: Sun Jun 21 11:48:02 2009
New Revision: 786996

URL: http://svn.apache.org/viewvc?rev=786996&view=rev
Log:
Updated the application so that it uses the Tuscany Data API for creating the blog feeds so
that we can add both an Atom and RSS feed onto the same Java component implementation.
For completeness, added implementations that extend the Collections interfaces for the Atom
and RSS bindings to show it can be done that way too but the implementation code cannot support
both bindings at the same time

Added:
    tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/BlogPost.java
    tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/AtomBlogFeedImpl.java
    tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BaseBlogFeedImpl.java
    tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/GenericBlogFeedImpl.java
    tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/RSSBlogFeedImpl.java
Removed:
    tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BlogFeedImpl.java
Modified:
    tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/resources/blog-feed.composite

Added: tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/BlogPost.java
URL: http://svn.apache.org/viewvc/tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/BlogPost.java?rev=786996&view=auto
==============================================================================
--- tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/BlogPost.java
(added)
+++ tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/BlogPost.java
Sun Jun 21 11:48:02 2009
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.blog;
+
+import java.util.Date;
+
+/**
+ * Bean for blog posts.
+ */
+public class BlogPost {
+
+    private final String author;
+    private final String title;
+    private final String content;
+    private final Date updated;
+    private final String link;
+    private final String related;
+
+    public BlogPost(String author, String title, String content, Date updated, String link,
String related) {
+        this.author = author;
+        this.title = title;
+        this.content = content;
+        this.updated = updated;
+        this.link = link;
+        this.related = related;
+    }
+
+    public String getAuthor() {
+        return author;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public Date getUpdated() {
+        return updated;
+    }
+
+    public String getLink() {
+        return link;
+    }
+
+    public String getRelated() {
+        return related;
+    }
+}

Added: tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/AtomBlogFeedImpl.java
URL: http://svn.apache.org/viewvc/tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/AtomBlogFeedImpl.java?rev=786996&view=auto
==============================================================================
--- tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/AtomBlogFeedImpl.java
(added)
+++ tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/AtomBlogFeedImpl.java
Sun Jun 21 11:48:02 2009
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.blog.feed;
+
+import java.util.List;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.model.Feed;
+import org.apache.tuscany.sca.binding.atom.collection.NotFoundException;
+
+import scatours.blog.BlogPost;
+
+/**
+ * An Atom feed that implements the org.apache.tuscany.sca.binding.atom.collection.Collection
+ * interface and uses the Atom APIs to construct the Atom feed.
+ */
+public class AtomBlogFeedImpl extends BaseBlogFeedImpl implements org.apache.tuscany.sca.binding.atom.collection.Collection
{
+
+    /**
+     * Gets an Atom feed containing all the blog posts.
+     * 
+     * @return An Atom feed containing all the blog posts.
+     */
+    public Feed getFeed() {
+        // Create SCA Tours blog Atom feed
+        final Factory factory = Abdera.getNewFactory();
+        final Feed feed = factory.newFeed();
+        feed.setTitle(FEED_TITLE);
+        feed.setSubtitle(FEED_DESCRIPTION);
+        feed.addAuthor(FEED_AUTHOR);
+
+        // Get all blog posts and convert to Atom entries
+        final List<BlogPost> blogEntries = getAllBlogPosts();
+        for (BlogPost blogEntry : blogEntries) {
+            final Entry entry = factory.newEntry();
+            entry.setId(nextBlogID());
+            entry.addAuthor(blogEntry.getAuthor());
+            entry.setTitle(blogEntry.getTitle());
+            entry.setContentAsHtml(blogEntry.getContent());
+            entry.setUpdated(blogEntry.getUpdated());
+            entry.addLink(blogEntry.getLink());
+            feed.addEntry(entry);
+        }
+
+        return feed;
+    }
+
+    /**
+     * Query the feed.
+     * 
+     * @param query The query
+     * @return Always returns null as method not implemented
+     */
+    public Feed query(String query) {
+        // Not implemented
+        return null;
+    }
+
+    /**
+     * Posts a new entry to the blog.
+     * 
+     * @param entry The new entry
+     * @return Always returns null as method not implemented
+     */
+    public Entry post(Entry entry) {
+        // Not implemented
+        return null;
+    }
+
+    /**
+     * Gets the specified entry from the blog.
+     * 
+     * @param id ID of the entry to get
+     * @return Not used
+     * @throws NotFoundException Always thrown as method not implemented
+     */
+    public Entry get(String id) throws NotFoundException {
+        // Not implemented
+        throw new NotFoundException("You are not allowed to update entries");
+    }
+
+    /**
+     * Updates the specified entry on the blog.
+     * 
+     * @param id ID of the entry to update
+     * @param entry The new entry
+     * @throws NotFoundException Always thrown as method not implemented
+     */
+    public void put(String id, Entry entry) throws NotFoundException {
+        // Not implemented
+        throw new NotFoundException("You are not allowed to update entries");
+    }
+
+    /**
+     * Deletes the specified entry from the blog.
+     * 
+     * @param id ID of the entry to delete
+     * @throws NotFoundException Always thrown as method not implemented
+     */
+    public void delete(String id) throws NotFoundException {
+        // Not implemented
+        throw new NotFoundException("You are not allowed to delete entries");
+    }
+}

Added: tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BaseBlogFeedImpl.java
URL: http://svn.apache.org/viewvc/tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BaseBlogFeedImpl.java?rev=786996&view=auto
==============================================================================
--- tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BaseBlogFeedImpl.java
(added)
+++ tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/BaseBlogFeedImpl.java
Sun Jun 21 11:48:02 2009
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.blog.feed;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import scatours.blog.BlogPost;
+
+/**
+ * Base class for all blog feeds that provides the common methods
+ * that are shared by all the blog feed implementations.
+ */
+public abstract class BaseBlogFeedImpl {
+
+    /**
+     * Title of the blog.feed.
+     */
+    protected static final String FEED_TITLE = "Tuscany SCA Tours Blog Feed";
+
+    /**
+     * Description of the blog feed.
+     */
+    protected static final String FEED_DESCRIPTION = "Feed contianing the latest blog posts
from Tuscany SCA Tours";
+
+    /**
+     * Author of the blog feed.
+     */
+    protected static final String FEED_AUTHOR = "SCA Tours CEO";
+
+    /**
+     * Used to generate unique IDs for the blog entries.
+     */
+    protected static final AtomicInteger ID_GEN = new AtomicInteger();
+
+    /**
+     * Generates the next blog entry ID.
+     * 
+     * @return Next blog entry ID
+     */
+    protected String nextBlogID() {
+        return Integer.toString(ID_GEN.incrementAndGet());
+    }
+
+    /**
+     * Retrieves a list of all blog posts.
+     * 
+     * @return A list of all blog posts.
+     */
+    public List<BlogPost> getAllBlogPosts() {
+        // Note: To keep things simple, we will just hard code a sample post.
+        // A proper implementation would load all blog posts from some resource
+        // such as files or a database.
+        List<BlogPost> blogEntries = new ArrayList<BlogPost>();
+
+        // Create a sample entry
+        final BlogPost samplePost = new BlogPost(
+             FEED_AUTHOR, 
+             "Apache Tuscany in Action book features SCA Tours",
+            "We are famous as SCA Tours has been featured in the Apache Tuscany in Action
book published by Manning",
+            new Date(),
+            "http://www.manning.com/laws/",
+            null);
+
+        // Add sample post to the list of posts
+        blogEntries.add(samplePost);
+
+        return blogEntries;
+    }
+}

Added: tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/GenericBlogFeedImpl.java
URL: http://svn.apache.org/viewvc/tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/GenericBlogFeedImpl.java?rev=786996&view=auto
==============================================================================
--- tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/GenericBlogFeedImpl.java
(added)
+++ tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/GenericBlogFeedImpl.java
Sun Jun 21 11:48:02 2009
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.blog.feed;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.data.collection.Entry;
+import org.apache.tuscany.sca.data.collection.Item;
+
+import scatours.blog.BlogPost;
+
+/**
+ * Implementation of the blog feed that uses the Tuscany Data API so
+ * that it is independent of any Feed APIs such as Atom and RSS.  
+ */
+public class GenericBlogFeedImpl extends BaseBlogFeedImpl {
+
+    /**
+     * Implementation of the getAll() method from the Tuscany API
+     * that will return all of the blog posts as generic Tuscany
+     * feed items.
+     * 
+     * @return All blog entries
+     */
+    public Entry<Object, Object>[] getAll() {
+        final List<BlogPost> posts = getAllBlogPosts();
+
+        final Entry<Object, Object>[] entries = new Entry[posts.size()];
+        int i = 0;
+        for (BlogPost post : posts) {
+            entries[i++] = convertBlogPostToFeedItem(post);
+        }
+
+        return entries;
+    }
+
+    /**
+     * Converts a blog post to a Tuscany API feed item.
+     * 
+     * @param post The blog post to convert
+     * @return The blog post as a Tuscany API feed item
+     */
+    private Entry<Object, Object> convertBlogPostToFeedItem(BlogPost post) {
+        // Convert Blog entry into an Item
+        final Item item = new Item(
+                        post.getTitle(), 
+                        post.getContent(), 
+                        post.getLink(), 
+                        post.getRelated(), 
+                        post.getUpdated());
+
+        // Add item to entry 
+        final Entry<Object, Object> entry = new Entry<Object, Object>(
+                        nextBlogID(), item);
+
+        return entry;
+    }
+}

Added: tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/RSSBlogFeedImpl.java
URL: http://svn.apache.org/viewvc/tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/RSSBlogFeedImpl.java?rev=786996&view=auto
==============================================================================
--- tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/RSSBlogFeedImpl.java
(added)
+++ tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/java/scatours/blog/feed/RSSBlogFeedImpl.java
Sun Jun 21 11:48:02 2009
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.blog.feed;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.binding.rss.collection.NotFoundException;
+
+import scatours.blog.BlogPost;
+
+import com.sun.syndication.feed.synd.SyndContent;
+import com.sun.syndication.feed.synd.SyndContentImpl;
+import com.sun.syndication.feed.synd.SyndEntry;
+import com.sun.syndication.feed.synd.SyndEntryImpl;
+import com.sun.syndication.feed.synd.SyndFeed;
+import com.sun.syndication.feed.synd.SyndFeedImpl;
+
+/**
+ * An RSS feed that implements the org.apache.tuscany.sca.binding.rss.collection.Collection
+ * interface and uses the RSS APIs to construct the RSS feed.
+ */
+public class RSSBlogFeedImpl extends BaseBlogFeedImpl implements org.apache.tuscany.sca.binding.rss.collection.Collection
{
+
+    /**
+     * Gets an RSS feed containing all the blog posts.
+     * 
+     * @return An RSS feed containing all the blog posts.
+     */
+    public SyndFeed getFeed() {
+        // Create SCA Tours blog RSS feed
+        SyndFeed feed = new SyndFeedImpl();
+        feed.setTitle(FEED_TITLE);
+        feed.setDescription(FEED_DESCRIPTION);
+        feed.setAuthor(FEED_AUTHOR);
+
+        // Get all blog posts and convert to RSS entries
+        final List<BlogPost> blogEntries = getAllBlogPosts();
+        for (BlogPost blogEntry : blogEntries) {
+            SyndEntry entry = new SyndEntryImpl();
+            entry.setUri(nextBlogID());
+            entry.setAuthor(blogEntry.getAuthor());
+            entry.setTitle(blogEntry.getTitle());
+            
+            SyndContent content = new SyndContentImpl();
+            content.setType("text");
+            content.setValue(blogEntry.getContent());
+
+            entry.setPublishedDate(blogEntry.getUpdated());
+            entry.setLink(blogEntry.getLink());
+
+            feed.getEntries().add(entry);
+        }
+
+        return feed;
+    }
+
+    /**
+     * Query the feed.
+     * 
+     * @param query The query
+     * @return Always returns null as method not implemented
+     */
+    public SyndFeed query(String query) {
+        // Not implemented
+        return null;
+    }
+
+    public SyndEntry get(String id) throws NotFoundException {
+        // Not implemented
+        return null;
+    }
+
+    public List<SyndEntry> getAll() throws NotFoundException {
+        // Not implemented
+        return null;
+    }
+}

Modified: tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/resources/blog-feed.composite
URL: http://svn.apache.org/viewvc/tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/resources/blog-feed.composite?rev=786996&r1=786995&r2=786996&view=diff
==============================================================================
--- tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/resources/blog-feed.composite
(original)
+++ tuscany/sandbox/travelsample/contributions/blog-feed-contribution/src/main/resources/blog-feed.composite
Sun Jun 21 11:48:02 2009
@@ -22,16 +22,38 @@
     targetNamespace="http://goodvaluetrips.com/"
     name="blog-feed">
 
-    <service name="BlogRSS" promote="Blog">
-        <tuscany:binding.atom uri="http://localhost:8090/BlogRSS"/>
+
+    <!-- Example that shows using the Tuscany Data APIs to create a feed that -->
+    <!-- is independent of the Feed API -->
+    <service name="BlogAtom" promote="BlogFeed">
+        <tuscany:binding.atom uri="http://localhost:8090/BlogAtom"/>
+    </service>
+
+    <service name="BlogRSS" promote="BlogFeed">
+        <tuscany:binding.rss uri="http://localhost:8090/BlogRSS"/>
     </service>
 
-    <service name="BlogAtom" promote="Blog">
-        <tuscany:binding.atom  uri="http://localhost:8090/BlogAtom"/>
+    <component name="BlogFeed">
+        <implementation.java class="scatours.blog.feed.GenericBlogFeedImpl"/>
+    </component>
+
+    <!-- Example that shows using the Atom Collections API to create a feed that -->
+    <!-- uses the Atom APIs -->
+    <service name="BlogAtomAPIs" promote="BlogAtom">
+        <tuscany:binding.atom uri="http://localhost:8090/BlogAtomAPIs"/>
     </service>
 
-    <component name="Blog">
-        <implementation.java class="scatours.blog.feed.BlogFeedImpl"/>
+    <component name="BlogAtom">
+        <implementation.java class="scatours.blog.feed.AtomBlogFeedImpl"/>
     </component>
 
+    <!-- Example that shows using the RSS Collections API to create a feed that -->
+    <!-- uses the RSS APIs -->
+    <service name="BlogRSSAPIs" promote="BlogRSS">
+        <tuscany:binding.rss uri="http://localhost:8090/BlogRSSAPIs"/>
+    </service>
+
+    <component name="BlogRSS">
+        <implementation.java class="scatours.blog.feed.RSSBlogFeedImpl"/>
+    </component>
 </composite>



Mime
View raw message