You are viewing a plain text version of this content. The canonical link for it is here.
Posted to photark-commits@incubator.apache.org by lr...@apache.org on 2010/06/29 03:00:47 UTC
svn commit: r958808 - in /incubator/photark/branches/photark-rest:
photark-jcr/ photark-jcr/src/main/java/org/apache/photark/services/jcr/
photark-jcr/src/test/java/org/apache/photark/services/jcr/
photark-jcr/src/test/resources/ photark/ photark/src/m...
Author: lresende
Date: Tue Jun 29 03:00:47 2010
New Revision: 958808
URL: http://svn.apache.org/viewvc?rev=958808&view=rev
Log:
PHOTARK-51 - Applying patch from Luis Fernando that provides support for managing album subscriptions and it's storage within the JCR repository
Added:
incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRSubscriptionCollection.java (with props)
incubator/photark/branches/photark-rest/photark-jcr/src/test/java/org/apache/photark/services/jcr/JCRSubscriptionCollectionTestCase.java (with props)
incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Subscription.java (with props)
incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/services/SubscriptionCollection.java (with props)
Modified:
incubator/photark/branches/photark-rest/photark-jcr/pom.xml
incubator/photark/branches/photark-rest/photark-jcr/src/test/resources/gallery.composite
incubator/photark/branches/photark-rest/photark/pom.xml
Modified: incubator/photark/branches/photark-rest/photark-jcr/pom.xml
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark-jcr/pom.xml?rev=958808&r1=958807&r2=958808&view=diff
==============================================================================
--- incubator/photark/branches/photark-rest/photark-jcr/pom.xml (original)
+++ incubator/photark/branches/photark-rest/photark-jcr/pom.xml Tue Jun 29 03:00:47 2010
@@ -110,7 +110,21 @@
<version>2.5</version>
<scope>provided</scope>
</dependency>
-
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-node-api</artifactId>
+ <version>${tuscany.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-node-impl</artifactId>
+ <version>${tuscany.version}</version>
+ <scope>test</scope>
+ </dependency>
+
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Added: incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRSubscriptionCollection.java
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRSubscriptionCollection.java?rev=958808&view=auto
==============================================================================
--- incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRSubscriptionCollection.java (added)
+++ incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRSubscriptionCollection.java Tue Jun 29 03:00:47 2010
@@ -0,0 +1,237 @@
+/*
+ * 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 org.apache.photark.services.jcr;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.photark.Subscription;
+import org.apache.photark.services.SubscriptionCollection;
+import org.apache.tuscany.sca.data.collection.Entry;
+import org.apache.tuscany.sca.data.collection.NotFoundException;
+import org.oasisopen.sca.annotation.Destroy;
+import org.oasisopen.sca.annotation.Init;
+import org.oasisopen.sca.annotation.Reference;
+import org.oasisopen.sca.annotation.Scope;
+import org.oasisopen.sca.annotation.Service;
+
+/**
+ * Collection representing all subscriptions either local (e.g fileSystem) or remote (e.g Flickr, Picasa, etc)
+ * This also serves as a intermediary cache, as it removes the requirement to read from the repository every single time
+ *
+ * @version $Rev$ $Date$
+ */
+@Scope("COMPOSITE")
+@Service(SubscriptionCollection.class)
+public class JCRSubscriptionCollection implements SubscriptionCollection {
+ private static final Logger logger = Logger.getLogger(JCRSubscriptionCollection.class.getName());
+
+ private JCRRepositoryManager repositoryManager;
+
+ private Map<String, Subscription> subscriptions = new HashMap<String, Subscription>();
+
+ public JCRSubscriptionCollection() {
+
+ }
+
+ @Reference(name="repositoryManager")
+ protected void setRepositoryManager(JCRRepositoryManager repositoryManager) {
+ this.repositoryManager = repositoryManager;
+ }
+
+ @Init
+ public void init() {
+ try {
+ Session session = repositoryManager.getSession();
+ Node rootNode = getSubscriptionRootNode(session);
+ NodeIterator subscriptionNodes = rootNode.getNodes();
+ while (subscriptionNodes.hasNext()) {
+ Node subscriptionNode = subscriptionNodes.nextNode();
+ if (subscriptionNode.getPath().equals("/jcr:system")) {
+ continue;
+ }
+
+ Subscription subscription = fromNode(subscriptionNode);
+ if (!subscriptions.containsKey(subscription.getTitle())) {
+ subscriptions.put(subscription.getTitle(),subscription);
+ }
+ }
+
+ } catch (Exception e) {
+ logger.log(Level.FINE,"Error initializing Subscription Collection: " + e.getMessage(), e);
+ e.printStackTrace();
+ }
+ }
+
+ @Destroy
+ public void destroy() {
+
+ }
+
+ public Entry<String, Subscription>[] getAll() {
+ Entry<String, Subscription>[] entries = new Entry[subscriptions.size()];
+ int i = 0;
+ for (Map.Entry<String, Subscription> e: subscriptions.entrySet()) {
+ entries[i++] = new Entry<String, Subscription>(e.getKey(), e.getValue());
+ }
+ return entries;
+ }
+
+ public Subscription get(String key) throws NotFoundException {
+ Subscription subscription = subscriptions.get(key);
+ if (subscription == null) {
+ throw new NotFoundException(key);
+ } else {
+ return subscription;
+ }
+
+ }
+
+ public String post(String key, Subscription subscription) {
+ if (subscription.getTitle() == null && subscription.getTitle().length() == 0) {
+ key = "subscription-" + UUID.randomUUID().toString();
+ subscription.setTitle(key);
+ }
+
+ try {
+ Session session = repositoryManager.getSession();
+ Node rootNode = getSubscriptionRootNode(session);
+ if (rootNode.hasNode(key)) {
+ logger.info("This subscription is already in gallery");
+ return key;
+ }
+
+ // save the new subscription to the jcr
+ Node subscriptionNode = rootNode.addNode(key);
+ fromSubscription(subscriptionNode, subscription);
+ session.save();
+
+ // add the new subscription to the subscription Map
+ subscriptions.put(key, subscription);
+ } catch (RepositoryException e) {
+ logger.log(Level.FINE,"Error adding new subscription : " + e.getMessage(), e);
+ e.printStackTrace();
+ }
+
+ return key;
+ }
+
+ public void put(String key, Subscription subscription) throws NotFoundException {
+ try {
+ Session session = repositoryManager.getSession();
+ Node rootNode = getSubscriptionRootNode(session);
+ if (! rootNode.hasNode(key)) {
+ throw new NotFoundException("Subscription not found:" + key);
+ }
+
+ // save the subscription to the jcr
+ Node subscriptionNode = rootNode.getNode(key);
+ fromSubscription(subscriptionNode, subscription);
+ session.save();
+
+ // update the new subscription to the subscription Map
+ subscriptions.put(key, subscription);
+ } catch (RepositoryException e) {
+ logger.log(Level.FINE,"Error updating new subscription : " + e.getMessage(), e);
+ e.printStackTrace();
+ }
+ }
+
+ public void delete(String key) throws NotFoundException {
+ try {
+ Session session = repositoryManager.getSession();
+ Node rootNode = getSubscriptionRootNode(session);
+ if (! rootNode.hasNode(key)) {
+ throw new NotFoundException("Subscription not found:" + key);
+ }
+
+ // remove the subscription from the jcr
+ Node subscriptionNode = rootNode.getNode(key);
+ subscriptionNode.remove();
+ session.save();
+
+ subscriptions.remove(key);
+
+ } catch (RepositoryException e) {
+ logger.log(Level.FINE,"Error removing new subscription : " + e.getMessage(), e);
+ e.printStackTrace();
+ }
+ }
+
+ public Entry<String, Subscription>[] query(String query) {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+
+ /**
+ * Utility methods
+ */
+
+
+
+ /**
+ *
+ * @return
+ * @throws RepositoryException
+ */
+ private Node getSubscriptionRootNode(Session session) throws RepositoryException {
+ Node rootNode = null;
+ try {
+ rootNode = session.getRootNode().getNode("subscriptions");
+ } catch (javax.jcr.PathNotFoundException pnf) {
+ rootNode = session.getRootNode().addNode("subscriptions");
+ }
+
+ return rootNode;
+ }
+
+ private Subscription fromNode(Node subscriptionNode) {
+ Subscription subscription = null;
+ try {
+ subscription = new Subscription();
+ subscription.setTitle(subscriptionNode.getProperty("title").getValue().toString());
+ subscription.setType(subscriptionNode.getProperty("type").getValue().toString());
+ subscription.setUrl(subscriptionNode.getProperty("url").getValue().toString());
+ } catch(Exception e) {
+ logger.log(Level.WARNING, "Can't read subscription node :" + e.getMessage(), e);
+ }
+
+ return subscription;
+ }
+
+ private void fromSubscription(Node subscriptionNode, Subscription subscription) {
+ try {
+ subscriptionNode.setProperty("title", subscription.getTitle());
+ subscriptionNode.setProperty("type", subscription.getType());
+ subscriptionNode.setProperty("url", subscription.getUrl());
+ } catch(Exception e) {
+ logger.log(Level.WARNING, "Can't save subscription node :" + e.getMessage(), e);
+ }
+ }
+
+}
Propchange: incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRSubscriptionCollection.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/photark/branches/photark-rest/photark-jcr/src/main/java/org/apache/photark/services/jcr/JCRSubscriptionCollection.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/photark/branches/photark-rest/photark-jcr/src/test/java/org/apache/photark/services/jcr/JCRSubscriptionCollectionTestCase.java
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark-jcr/src/test/java/org/apache/photark/services/jcr/JCRSubscriptionCollectionTestCase.java?rev=958808&view=auto
==============================================================================
--- incubator/photark/branches/photark-rest/photark-jcr/src/test/java/org/apache/photark/services/jcr/JCRSubscriptionCollectionTestCase.java (added)
+++ incubator/photark/branches/photark-rest/photark-jcr/src/test/java/org/apache/photark/services/jcr/JCRSubscriptionCollectionTestCase.java Tue Jun 29 03:00:47 2010
@@ -0,0 +1,92 @@
+/*
+ * 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 org.apache.photark.services.jcr;
+
+import junit.framework.Assert;
+
+import org.apache.photark.Subscription;
+import org.apache.photark.services.SubscriptionCollection;
+import org.apache.tuscany.sca.data.collection.NotFoundException;
+import org.apache.tuscany.sca.node.Contribution;
+import org.apache.tuscany.sca.node.ContributionLocationHelper;
+import org.apache.tuscany.sca.node.Node;
+import org.apache.tuscany.sca.node.NodeFactory;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class JCRSubscriptionCollectionTestCase {
+ private static Node node;
+ private static SubscriptionCollection subscriptions;
+
+ @BeforeClass
+ public static void BeforeClass() {
+ try {
+ String contribution = ContributionLocationHelper.getContributionLocation("gallery.composite");
+ node = NodeFactory.newInstance().createNode("gallery.composite", new Contribution("gallery", contribution));
+ node.start();
+
+ subscriptions = node.getService(SubscriptionCollection.class, "SubscriptionComponent");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ @AfterClass
+ public static void AfterClass() {
+ node.stop();
+ }
+
+ @Test
+ public void testAddSubscription() throws Exception {
+ Subscription subscription = new Subscription();
+ subscription.setTitle("Test 01");
+ subscription.setType("flickr");
+ subscription.setUrl("http://api.flickr.com/services/feeds/photos_public.gne?id=24662369@N07&lang=en-us&format=atom");
+
+ subscriptions.post(subscription.getTitle(), subscription);
+
+ subscriptions.getAll();
+ }
+
+ @Test
+ public void testUpdateSubscription() throws Exception {
+ Subscription subscription = new Subscription();
+ subscription.setTitle("Test 02");
+ subscription.setType("flickr");
+ subscription.setUrl("http://localhost/xxx");
+
+ try {
+ subscriptions.get(subscription.getTitle());
+ } catch(NotFoundException nf) {
+ subscriptions.delete(subscription.getTitle());
+ }
+
+ subscriptions.post(subscription.getTitle(), subscription);
+
+ subscription.setUrl("http://api.flickr.com/services/feeds/photos_public.gne?id=24662369@N07&lang=en-us&format=atom");
+
+ subscriptions.post(subscription.getTitle(), subscription);
+
+ Subscription subscritionRead = subscriptions.get(subscription.getTitle());
+
+ Assert.assertEquals(subscription.getTitle(), subscritionRead.getTitle());
+ }
+}
Propchange: incubator/photark/branches/photark-rest/photark-jcr/src/test/java/org/apache/photark/services/jcr/JCRSubscriptionCollectionTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/photark/branches/photark-rest/photark-jcr/src/test/java/org/apache/photark/services/jcr/JCRSubscriptionCollectionTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/photark/branches/photark-rest/photark-jcr/src/test/resources/gallery.composite
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark-jcr/src/test/resources/gallery.composite?rev=958808&r1=958807&r2=958808&view=diff
==============================================================================
--- incubator/photark/branches/photark-rest/photark-jcr/src/test/resources/gallery.composite (original)
+++ incubator/photark/branches/photark-rest/photark-jcr/src/test/resources/gallery.composite Tue Jun 29 03:00:47 2010
@@ -21,8 +21,20 @@
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
targetNamespace="http://org.apache.photoark"
name="gallery">
-
- <component name="Gallery">
+
+ <!-- Component responsible for providing JCR Management Support -->
+ <component name="RepositoryManager">
+ <implementation.java class="org.apache.photark.services.jcr.JCRRepositoryManager"/>
+ <property name="repositoryHome">photark</property>
+ </component>
+
+ <component name="SubscriptionComponent">
+ <implementation.java class="org.apache.photark.services.jcr.JCRSubscriptionCollection"/>
+ <reference name="repositoryManager" target="RepositoryManager"/>
+ </component>
+
+ <!--
+ <component name="GalleryComponent">
<implementation.java class="org.apache.photark.services.jcr.JCRGallery"/>
<property name="galleryRoot">gallery-root</property>
<service name="GalleryService">
@@ -31,4 +43,5 @@
</tuscany:binding.rest>
</service>
</component>
+ -->
</composite>
Modified: incubator/photark/branches/photark-rest/photark/pom.xml
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark/pom.xml?rev=958808&r1=958807&r2=958808&view=diff
==============================================================================
--- incubator/photark/branches/photark-rest/photark/pom.xml (original)
+++ incubator/photark/branches/photark-rest/photark/pom.xml Tue Jun 29 03:00:47 2010
@@ -77,14 +77,14 @@
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-node-impl</artifactId>
- <version>2.0-SNAPSHOT</version>
+ <version>${tuscany.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-host-jetty</artifactId>
- <version>2.0-SNAPSHOT</version>
+ <version>${tuscany.version}</version>
<scope>test</scope>
</dependency>
Added: incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Subscription.java
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Subscription.java?rev=958808&view=auto
==============================================================================
--- incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Subscription.java (added)
+++ incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Subscription.java Tue Jun 29 03:00:47 2010
@@ -0,0 +1,93 @@
+/*
+ * 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 org.apache.photark;
+
+/**
+ * Model representing a subscription where images are loded from
+ * Subscriptions can be local (e.g fileSystem) or remote (e.g Flickr, Picasa, etc)
+ *
+ * @version $Rev$ $Date$
+ */
+public class Subscription {
+ private String title;
+ private String type;
+ private String url;
+
+ /**
+ * Get subscription title
+ *
+ * @return the subscription title
+ */
+ public String getTitle() {
+ return title;
+ }
+
+ /**
+ * Set subscription title
+ *
+ * @param title the subscription title
+ */
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ /**
+ * Get subscription type
+ *
+ * @return the subscription title
+ */
+ public String getType() {
+ return type;
+ }
+
+ /**
+ * Set subscription type
+ *
+ * @param type the subscription type
+ */
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ /**
+ * Get subscription url (e.g Local filesystem or remote Flickr Album)
+ *
+ * @return the subscription url
+ */
+ public String getUrl() {
+ return url;
+ }
+
+ /**
+ * Set subscription url (e.g Local filesystem or remote Flickr Album)
+ *
+ * @param url the subscription url
+ */
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ @Override
+ public String toString() {
+ return "Subscription [title=" + title + ", type=" + type + ", url=" + url + "]";
+ }
+
+
+}
Propchange: incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Subscription.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/Subscription.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/services/SubscriptionCollection.java
URL: http://svn.apache.org/viewvc/incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/services/SubscriptionCollection.java?rev=958808&view=auto
==============================================================================
--- incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/services/SubscriptionCollection.java (added)
+++ incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/services/SubscriptionCollection.java Tue Jun 29 03:00:47 2010
@@ -0,0 +1,27 @@
+/*
+ * 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 org.apache.photark.services;
+
+import org.apache.photark.Subscription;
+import org.apache.tuscany.sca.data.collection.Collection;
+
+public interface SubscriptionCollection extends Collection<String, Subscription>{
+
+}
Propchange: incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/services/SubscriptionCollection.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/photark/branches/photark-rest/photark/src/main/java/org/apache/photark/services/SubscriptionCollection.java
------------------------------------------------------------------------------
svn:keywords = Rev Date