You are viewing a plain text version of this content. The canonical link for it is here.
Posted to nuvem-commits@incubator.apache.org by lr...@apache.org on 2010/09/22 01:53:52 UTC
svn commit: r999723 - in /incubator/nuvem/trunk: store-appengine-webapp/
store-appengine-webapp/src/ store-appengine-webapp/src/services/
store-appengine-webapp/war/ store-appengine-webapp/war/WEB-INF/
store-assets/src/main/java/services/
Author: lresende
Date: Wed Sep 22 01:53:52 2010
New Revision: 999723
URL: http://svn.apache.org/viewvc?rev=999723&view=rev
Log:
Refactoring ShoppingCart to use a reference to a DocumentService component to allow easily rewiring different storage utility components
Added:
incubator/nuvem/trunk/store-appengine-webapp/src/services/
incubator/nuvem/trunk/store-appengine-webapp/src/services/CachedShoppingCartManager.java (with props)
Removed:
incubator/nuvem/trunk/store-appengine-webapp/src/Workaround.java
Modified:
incubator/nuvem/trunk/store-appengine-webapp/pom.xml
incubator/nuvem/trunk/store-appengine-webapp/war/WEB-INF/appengine-web.xml
incubator/nuvem/trunk/store-appengine-webapp/war/store.composite
incubator/nuvem/trunk/store-assets/src/main/java/services/ShoppingCartImpl.java
incubator/nuvem/trunk/store-assets/src/main/java/services/ShoppingCartManager.java
Modified: incubator/nuvem/trunk/store-appengine-webapp/pom.xml
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/store-appengine-webapp/pom.xml?rev=999723&r1=999722&r2=999723&view=diff
==============================================================================
--- incubator/nuvem/trunk/store-appengine-webapp/pom.xml (original)
+++ incubator/nuvem/trunk/store-appengine-webapp/pom.xml Wed Sep 22 01:53:52 2010
@@ -7,15 +7,15 @@
* 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.
+ * under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>
@@ -50,6 +50,12 @@
</dependency>
<dependency>
+ <groupId>org.apache.nuvem</groupId>
+ <artifactId>nuvem-cloud-standalone</artifactId>
+ <version>${pom.version}</version>
+ </dependency>
+
+ <dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-sca-api</artifactId>
<version>${tuscany.version}</version>
Added: incubator/nuvem/trunk/store-appengine-webapp/src/services/CachedShoppingCartManager.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/store-appengine-webapp/src/services/CachedShoppingCartManager.java?rev=999723&view=auto
==============================================================================
--- incubator/nuvem/trunk/store-appengine-webapp/src/services/CachedShoppingCartManager.java (added)
+++ incubator/nuvem/trunk/store-appengine-webapp/src/services/CachedShoppingCartManager.java Wed Sep 22 01:53:52 2010
@@ -0,0 +1,192 @@
+/*
+ * 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 services;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import net.sf.jsr107cache.Cache;
+import net.sf.jsr107cache.CacheManager;
+
+import org.apache.nuvem.cloud.user.User;
+import org.apache.nuvem.cloud.user.UserService;
+import org.apache.tuscany.sca.data.collection.Entry;
+import org.apache.tuscany.sca.data.collection.NotFoundException;
+import org.oasisopen.sca.annotation.Init;
+import org.oasisopen.sca.annotation.Reference;
+import org.oasisopen.sca.annotation.Scope;
+import org.oasisopen.sca.annotation.Service;
+
+@Scope("COMPOSITE")
+@Service(ShoppingCart.class)
+public class CachedShoppingCartManager implements ShoppingCart {
+ private static final Logger log = Logger.getLogger(CachedShoppingCartManager.class.getName());
+ private static String ANONYMOUS = "anonymous";
+
+ private Cache cache;
+
+ @Init
+ public void init() {
+ try {
+ cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
+ }catch(Throwable t) {
+ log.log(Level.SEVERE, "Error initialize cache + " + t.getMessage());
+ }
+ }
+
+ @Reference
+ private UserService userService;
+
+ public Entry<String, Item>[] getAll() {
+ Map<String, Item> cart = getUserItems();
+
+ Entry<String, Item>[] entries = new Entry[cart.size()];
+ int i = 0;
+ for (Map.Entry<String, Item> e: cart.entrySet()) {
+ entries[i++] = new Entry<String, Item>(e.getKey(), e.getValue());
+ }
+ return entries;
+ }
+
+ public Item get(String key) throws NotFoundException {
+ Map<String, Item> cart = getUserItems();
+
+ Item item = cart.get(key);
+ if (item == null) {
+ throw new NotFoundException(key);
+ } else {
+ return item;
+ }
+ }
+
+ public String post(String key, Item item) {
+ Map<String, Item> cart = getUserItems();
+
+ if (key == null || key.isEmpty()) {
+ key = this.generateItemKey();
+ }
+
+ cart.put(key, item);
+ cache.put(getCartKey(), cart);
+ return key;
+ }
+
+ public void put(String key, Item item) throws NotFoundException {
+ Map<String, Item> cart = getUserItems();
+
+ if (!cart.containsKey(key)) {
+ throw new NotFoundException(key);
+ }
+ cart.put(key, item);
+ cache.put(getCartKey(), cart);
+ }
+
+ public void delete(String key) throws NotFoundException {
+ if (key == null || key.isEmpty()) {
+ cache.remove(getCartKey());
+ } else {
+ Map<String, Item> cart = getUserItems();
+
+ Item item = cart.remove(key);
+ if (item == null) {
+ throw new NotFoundException(key);
+ }
+ cache.put(getCartKey(), cart);
+ }
+ }
+
+ public Entry<String, Item>[] query(String queryString) {
+ throw new UnsupportedOperationException("Operation not supported !");
+ }
+
+ public String getTotal() {
+ double total = 0;
+ String currencySymbol = "";
+ if (cache.containsKey(getCartKey())) {
+ Map<String, Item> cart = getUserItems();
+ if (!cart.isEmpty()) {
+ Item item = cart.values().iterator().next();
+ currencySymbol = item.getCurrencySymbol();
+ }
+ for (Item item : cart.values()) {
+ total += item.getPrice();
+ }
+
+
+ }
+ return currencySymbol + String.valueOf(total);
+ }
+
+ /**
+ * Utility functions
+ */
+ /*
+ private ShoppingCart getUserShoppingCart() {
+ String key = getCartKey();
+ ShoppingCart userCart = (ShoppingCart) cache.get(key);
+ if(userCart == null) {
+ userCart = new ShoppingCartImpl();
+ cache.put(key, userCart);
+ }
+ return userCart;
+ }*/
+
+ private String getUserId() {
+ String userId = null;
+ if(userService != null) {
+ try {
+ User user = userService.getCurrentUser();
+ userId = user.getUserId();
+ } catch(Exception e) {
+ //ignore
+ e.printStackTrace();
+ }
+ }
+ if(userId == null || userId.length() == 0) {
+ userId = ANONYMOUS;
+ }
+ return userId;
+ }
+ private String getCartKey() {
+ String cartKey = "cart-" + this.getUserId();
+ return cartKey;
+ }
+ private String generateItemKey() {
+ String itemKey = getCartKey() + "-item-" + UUID.randomUUID().toString();
+ return itemKey;
+ }
+ private Map<String, Item> getUserItems() {
+ String userCartKey = getCartKey();
+ HashMap<String, Item> cartMap;
+
+ if(! cache.containsKey(userCartKey)) {
+ cartMap = new HashMap<String, Item>();
+ cache.put(userCartKey, cartMap);
+ } else {
+ cartMap = (HashMap<String, Item>) cache.get(userCartKey);
+ }
+
+ return cartMap;
+ }
+}
Propchange: incubator/nuvem/trunk/store-appengine-webapp/src/services/CachedShoppingCartManager.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/nuvem/trunk/store-appengine-webapp/src/services/CachedShoppingCartManager.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/nuvem/trunk/store-appengine-webapp/war/WEB-INF/appengine-web.xml
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/store-appengine-webapp/war/WEB-INF/appengine-web.xml?rev=999723&r1=999722&r2=999723&view=diff
==============================================================================
--- incubator/nuvem/trunk/store-appengine-webapp/war/WEB-INF/appengine-web.xml (original)
+++ incubator/nuvem/trunk/store-appengine-webapp/war/WEB-INF/appengine-web.xml Wed Sep 22 01:53:52 2010
@@ -7,23 +7,23 @@
* 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.
+ * under the License.
-->
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
- <application>tuscany-store</application>
- <version>6</version>
-
+ <application>tuscany-test</application>
+ <version>1</version>
+
<!-- Configure java.util.logging -->
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
-
+
</appengine-web-app>
\ No newline at end of file
Modified: incubator/nuvem/trunk/store-appengine-webapp/war/store.composite
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/store-appengine-webapp/war/store.composite?rev=999723&r1=999722&r2=999723&view=diff
==============================================================================
--- incubator/nuvem/trunk/store-appengine-webapp/war/store.composite (original)
+++ incubator/nuvem/trunk/store-appengine-webapp/war/store.composite Wed Sep 22 01:53:52 2010
@@ -19,6 +19,7 @@
-->
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:s="http://store"
targetNamespace="http://store"
name="store">
@@ -48,6 +49,12 @@
<reference name="userService" target="UserService">
<binding.sca name="local"/>
</reference>
+ <reference name="documentService" target="DocumentService" />
+ </component>
+
+ <component name="DocumentService">
+ <implementation.java class="org.apache.nuvem.cloud.data.impl.MemcacheDocumentServiceImpl"/>
+ <!-- implementation.java class="org.apache.nuvem.cloud.data.impl.MapDocumentServiceImpl" /-->
</component>
<component name="UserService">
Modified: incubator/nuvem/trunk/store-assets/src/main/java/services/ShoppingCartImpl.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/store-assets/src/main/java/services/ShoppingCartImpl.java?rev=999723&r1=999722&r2=999723&view=diff
==============================================================================
--- incubator/nuvem/trunk/store-assets/src/main/java/services/ShoppingCartImpl.java (original)
+++ incubator/nuvem/trunk/store-assets/src/main/java/services/ShoppingCartImpl.java Wed Sep 22 01:53:52 2010
@@ -28,13 +28,18 @@ import java.util.logging.Logger;
import org.apache.tuscany.sca.data.collection.Entry;
import org.apache.tuscany.sca.data.collection.NotFoundException;
+import org.oasisopen.sca.annotation.Reference;
import org.oasisopen.sca.annotation.Scope;
+import org.apache.nuvem.cloud.data.DocumentService;
+
@Scope("COMPOSITE")
public class ShoppingCartImpl implements ShoppingCart {
private static final Logger log = Logger.getLogger(ShoppingCartImpl.class.getName());
- //private DatastoreDocumentServiceImpl cart = new DatastoreDocumentServiceImpl();
+ @Reference
+ private DocumentService docService;
+
private Map<String, Item> cart = new HashMap<String, Item>();
public Entry<String, Item>[] getAll() {
Modified: incubator/nuvem/trunk/store-assets/src/main/java/services/ShoppingCartManager.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/store-assets/src/main/java/services/ShoppingCartManager.java?rev=999723&r1=999722&r2=999723&view=diff
==============================================================================
--- incubator/nuvem/trunk/store-assets/src/main/java/services/ShoppingCartManager.java (original)
+++ incubator/nuvem/trunk/store-assets/src/main/java/services/ShoppingCartManager.java Wed Sep 22 01:53:52 2010
@@ -24,6 +24,7 @@ import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
+import org.apache.nuvem.cloud.data.DocumentService;
import org.apache.nuvem.cloud.user.User;
import org.apache.nuvem.cloud.user.UserService;
import org.apache.tuscany.sca.data.collection.Entry;
@@ -36,55 +37,112 @@ public class ShoppingCartManager impleme
private static final Logger log = Logger.getLogger(ShoppingCartManager.class.getName());
private static String ANONYMOUS = "anonymous";
- private Map<String, ShoppingCart> carts = new HashMap<String, ShoppingCart>();
+ @Reference
+ private DocumentService documentService;
@Reference
private UserService userService;
public Entry<String, Item>[] getAll() {
- return getUserShoppingCart().getAll();
+ Map<String, Item> cart = getUserShoppingCart();
+
+ Entry<String, Item>[] entries = new Entry[cart.size()];
+ int i = 0;
+ for (Map.Entry<String, Item> e: cart.entrySet()) {
+ entries[i++] = new Entry<String, Item>(e.getKey(), e.getValue());
+ }
+ return entries;
}
public Item get(String key) throws NotFoundException {
- return getUserShoppingCart().get(key);
+ Map<String, Item> cart = getUserShoppingCart();
+
+ Item item = cart.get(key);
+ if (item == null) {
+ throw new NotFoundException(key);
+ } else {
+ return item;
+ }
}
public String post(String key, Item item) {
+ Map<String, Item> cart = getUserShoppingCart();
+
if (key == null || key.isEmpty()) {
key = this.generateItemKey();
}
- return getUserShoppingCart().post(key, item);
+
+ //add to the cart map
+ cart.put(key, item);
+ //add back to the store
+ documentService.post(getCartKey(), cart);
+ return key;
}
public void put(String key, Item item) throws NotFoundException {
- getUserShoppingCart().put(key,item);
- }
+ Map<String, Item> cart = getUserShoppingCart();
- public Entry<String, Item>[] query(String queryString) {
- return getUserShoppingCart().query(queryString);
+ if (!cart.containsKey(key)) {
+ throw new NotFoundException(key);
+ }
+ //add to the cart map
+ cart.put(key, item);
+ //add back to the store
+ documentService.put(getCartKey(), cart);
}
public void delete(String key) throws NotFoundException {
- this.getUserShoppingCart().delete(key);
+ if (key == null || key.isEmpty()) {
+ documentService.delete(getCartKey());
+ } else {
+ Map<String, Item> cart = getUserShoppingCart();
+
+ Item item = cart.remove(key);
+ if (item == null) {
+ throw new NotFoundException(key);
+ }
+ documentService.put(getCartKey(), cart);
+ }
+ }
+
+ public Entry<String, Item>[] query(String queryString) {
+ throw new UnsupportedOperationException("Operation not supported !");
}
public String getTotal() {
- return this.getUserShoppingCart().getTotal();
+ double total = 0;
+ String currencySymbol = "";
+
+
+ Map<String, Item> cart = getUserShoppingCart();
+ if (!cart.isEmpty()) {
+ Item item = cart.values().iterator().next();
+ currencySymbol = item.getCurrencySymbol();
+ }
+ for (Item item : cart.values()) {
+ total += item.getPrice();
+ }
+
+ return currencySymbol + String.valueOf(total);
}
/**
* Utility functions
*/
- private ShoppingCart getUserShoppingCart() {
- String key = getCartKey();
- ShoppingCart userCart = carts.get(key);
- if(userCart == null) {
- userCart = new ShoppingCartImpl();
- carts.put(key, userCart);
- }
- return userCart;
- }
+ private Map<String, Item> getUserShoppingCart() {
+ String userCartKey = getCartKey();
+ HashMap<String, Item> cart;
+
+ try {
+ cart = (HashMap<String, Item>) documentService.get(userCartKey);
+ } catch (NotFoundException e) {
+ cart = new HashMap<String, Item>();
+ documentService.post(userCartKey, cart);
+ }
+
+ return cart;
+ }
private String getUserId() {
String userId = null;
if(userService != null) {