From 07b7dfd1a70ba222b899d9813f8c449dbf3b785f Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 11 Nov 2009 23:07:28 +0000 Subject: Moving 1.x branches git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835124 13f79535-47bb-0310-9956-ffa450edef68 --- .../samples/store/src/main/java/launch/Launch.java | 34 ++++++ .../store/src/main/java/services/Catalog.java | 27 +++++ .../store/src/main/java/services/CatalogImpl.java | 49 +++++++++ .../src/main/java/services/CurrencyConverter.java | 29 +++++ .../main/java/services/CurrencyConverterImpl.java | 46 ++++++++ .../src/main/java/services/ShoppingCartImpl.java | 105 ++++++++++++++++++ .../samples/store/src/main/java/util/CreateDB.java | 97 +++++++++++++++++ .../store/src/main/resources/store.composite | 59 ++++++++++ .../store/src/main/resources/ufservices/store.html | 119 +++++++++++++++++++++ 9 files changed, 565 insertions(+) create mode 100644 sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/launch/Launch.java create mode 100644 sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/Catalog.java create mode 100644 sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CatalogImpl.java create mode 100644 sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CurrencyConverter.java create mode 100644 sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CurrencyConverterImpl.java create mode 100644 sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/ShoppingCartImpl.java create mode 100644 sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/util/CreateDB.java create mode 100644 sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/resources/store.composite create mode 100644 sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/resources/ufservices/store.html (limited to 'sca-java-1.x/branches/sca-java-1.1/samples/store/src/main') diff --git a/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/launch/Launch.java b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/launch/Launch.java new file mode 100644 index 0000000000..801697abea --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/launch/Launch.java @@ -0,0 +1,34 @@ +/* + * 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 launch; + +import org.apache.tuscany.sca.host.embedded.SCADomain; + +public class Launch { + public static void main(String[] args) throws Exception { + System.out.println("Starting ..."); + SCADomain scaDomain = SCADomain.newInstance("store.composite"); + System.out.println("store.composite ready for big business !!!"); + System.in.read(); + System.out.println("Stopping ..."); + scaDomain.close(); + System.out.println(); + } +} diff --git a/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/Catalog.java b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/Catalog.java new file mode 100644 index 0000000000..9f784b0848 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/Catalog.java @@ -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 services; + +import org.osoa.sca.annotations.Remotable; + +@Remotable +public interface Catalog { + String[] get(); +} diff --git a/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CatalogImpl.java b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CatalogImpl.java new file mode 100644 index 0000000000..fd44c66bf6 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CatalogImpl.java @@ -0,0 +1,49 @@ +/* + * 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.ArrayList; +import java.util.List; + +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Property; +import org.osoa.sca.annotations.Reference; + +public class CatalogImpl implements Catalog { + @Property + public String currencyCode = "USD"; + @Reference + public CurrencyConverter currencyConverter; + private List catalog = new ArrayList(); + + @Init + public void init() { + String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode); + catalog.add("Apple - " + currencySymbol + currencyConverter.getConversion("USD", currencyCode, 2.99f)); + catalog.add("Orange - " + currencySymbol + currencyConverter.getConversion("USD", currencyCode, 3.55f)); + catalog.add("Pear - " + currencySymbol + currencyConverter.getConversion("USD", currencyCode, 1.55f)); + } + + public String[] get() { + String[] catalogArray = new String[catalog.size()]; + catalog.toArray(catalogArray); + return catalogArray; + } +} diff --git a/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CurrencyConverter.java b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CurrencyConverter.java new file mode 100644 index 0000000000..8b0f70907d --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CurrencyConverter.java @@ -0,0 +1,29 @@ +/* + * 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 org.osoa.sca.annotations.Remotable; + +@Remotable +public interface CurrencyConverter { + public float getConversion(String fromCurrenycCode, String toCurrencyCode, float amount); + + public String getCurrencySymbol(String currencyCode); +} diff --git a/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CurrencyConverterImpl.java b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CurrencyConverterImpl.java new file mode 100644 index 0000000000..56b5a64c8a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/CurrencyConverterImpl.java @@ -0,0 +1,46 @@ +/* + * 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 org.osoa.sca.annotations.Property; + +public class CurrencyConverterImpl implements CurrencyConverter { + @Property + public Float rate = 0.7256f; + + public float getConversion(String fromCurrencyCode, String toCurrencyCode, float amount) { + if (fromCurrencyCode.equals(toCurrencyCode)) + return amount; + + if (toCurrencyCode.equals("USD")) + return amount / rate; + else if (toCurrencyCode.equals("EUR")) + return amount * rate; + return 0; + } + + public String getCurrencySymbol(String currencyCode) { + if (currencyCode.equals("USD")) + return "$ "; + else if (currencyCode.equals("EUR")) + return "€ "; + return "?"; + } +} diff --git a/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/ShoppingCartImpl.java b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/ShoppingCartImpl.java new file mode 100644 index 0000000000..41e9a9eaa3 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/services/ShoppingCartImpl.java @@ -0,0 +1,105 @@ +/* + * 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.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.apache.tuscany.sca.binding.feed.collection.Collection; +import org.apache.tuscany.sca.binding.feed.collection.NotFoundException; + +import com.sun.syndication.feed.atom.Content; +import com.sun.syndication.feed.atom.Entry; +import com.sun.syndication.feed.atom.Feed; +import com.sun.syndication.feed.atom.Link; + +public class ShoppingCartImpl implements Collection { + + private static Map cart = new HashMap(); + + public Feed getFeed() { + Feed feed = new Feed(); + feed.setTitle("shopping cart"); + Content subtitle = new Content(); + subtitle.setValue("Total : " + getTotal()); + feed.setSubtitle(subtitle); + feed.getEntries().addAll(cart.values()); + return feed; + } + + public Entry get(String id) throws NotFoundException { + return cart.get(id); + } + + public Entry post(Entry entry) { + System.out.println("post" + entry); + String id = "cart-" + UUID.randomUUID().toString(); + entry.setId(id); + + Link link = new Link(); + link.setRel("edit"); + link.setHref(id); + entry.getOtherLinks().add(link); + link = new Link(); + link.setRel("alternate"); + link.setHref(id); + entry.getAlternateLinks().add(link); + + entry.setCreated(new Date()); + + cart.put(id, entry); + return entry; + } + + public void put(String id, Entry entry) throws NotFoundException { + entry.setUpdated(new Date()); + cart.put(id, entry); + } + + public void delete(String id) throws NotFoundException { + if (id.equals("")) + cart.clear(); + else + cart.remove(id); + } + + private String getTotal() { + float total = 0; + String currencySymbol = ""; + if (!cart.isEmpty()) { + String item = ((Content)cart.values().iterator().next().getContents().get(0)).getValue(); + currencySymbol = item.substring(item.indexOf("-") + 2, item.indexOf("-") + 3); + } + for (Entry entry : cart.values()) { + String item = ((Content)entry.getContents().get(0)).getValue(); + + int index = item.length()-1; + char digit; + while ((digit = item.charAt(index)) == '.' || Character.isDigit(digit)) { + index--; + } + + total += Float.valueOf(item.substring(index)); + } + return currencySymbol + String.valueOf(total); + } +} diff --git a/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/util/CreateDB.java b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/util/CreateDB.java new file mode 100644 index 0000000000..b8177ee25b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/java/util/CreateDB.java @@ -0,0 +1,97 @@ +/* + * 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 util; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +public class CreateDB { + + public static void main(String[] args) { + System.out.println("Creating database ..."); + + Connection connection = null; + PreparedStatement preparedStatement = null; + + try { + //initialize driver and register it with DriverManager + Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); + + //connect and create the db if not present + connection = DriverManager.getConnection( + "jdbc:derby:target/store_db;create=true", + "", + ""); + + + try { + preparedStatement = connection.prepareStatement("DROP TABLE CATALOG"); + preparedStatement.execute(); + }catch(Exception e) { + //ignore to avoid erros when db is being created from scratch + } + + + preparedStatement = connection.prepareStatement("CREATE TABLE CATALOG(" + + "id NUMERIC(5 , 0) NOT NULL," + + "product_name VARCHAR(30)," + + "currency_code CHAR(3)," + + "price REAL," + + "primary key (id)" + + ")"); + preparedStatement.execute(); + + preparedStatement = connection.prepareStatement("INSERT INTO CATALOG VALUES(0,'Apple', 'USD', 2.99)"); + preparedStatement.execute(); + + preparedStatement = connection.prepareStatement("INSERT INTO CATALOG VALUES(1,'Orange', 'USD', 3.55)"); + preparedStatement.execute(); + + preparedStatement = connection.prepareStatement("INSERT INTO CATALOG VALUES(2,'Pear', 'USD', 1.55)"); + preparedStatement.execute(); + + System.out.println("Done !"); + + } catch (SQLException ex) { + ex.printStackTrace(); + }catch (ClassNotFoundException ex) { + ex.printStackTrace(); + } finally { + if (preparedStatement!=null) { + try { + preparedStatement.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + + if (connection!=null) { + try { + connection.close(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/resources/store.composite b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/resources/store.composite new file mode 100644 index 0000000000..a2933104b5 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/resources/store.composite @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + USD + + + + + + + + + + + + + + + + 0.7256 + + + diff --git a/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/resources/ufservices/store.html b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/resources/ufservices/store.html new file mode 100644 index 0000000000..abef20a494 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.1/samples/store/src/main/resources/ufservices/store.html @@ -0,0 +1,119 @@ + + + +Store + + + + + + + + +

Store

+
+

Catalog

+
+
+
+ +
+ +
+ +

Your Shopping Cart

+
+
+
+
+
+ + + (feed) +
+
+ + -- cgit v1.2.3