You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2008/03/12 20:08:31 UTC

svn commit: r636457 - in /openejb/trunk/openejb3/examples/expanded-env-entries/src: main/java/org/superbiz/enventries/ main/resources/META-INF/ test/java/org/superbiz/enventries/

Author: dblevins
Date: Wed Mar 12 12:08:26 2008
New Revision: 636457

URL: http://svn.apache.org/viewvc?rev=636457&view=rev
Log:
Reworked the example to be more interesting and use more aspects of the feature

Added:
    openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Pickup.java
    openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/PickupEditor.java
Modified:
    openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Stratocaster.java
    openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/StratocasterImpl.java
    openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Style.java
    openejb/trunk/openejb3/examples/expanded-env-entries/src/main/resources/META-INF/env-entries.properties
    openejb/trunk/openejb3/examples/expanded-env-entries/src/test/java/org/superbiz/enventries/StratocasterTest.java

Added: openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Pickup.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Pickup.java?rev=636457&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Pickup.java (added)
+++ openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Pickup.java Wed Mar 12 12:08:26 2008
@@ -0,0 +1,35 @@
+/**
+ * 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.superbiz.enventries;
+
+
+//START SNIPPET: code
+import java.beans.PropertyEditorManager;
+
+public enum Pickup {
+
+    HUMBUCKER,
+    SINGLE_COIL;
+
+    // Here's the little magic where we register the PickupEditor
+    // which knows how to create this object from a string.
+    // You can add any of your own Property Editors in the same way.
+    static {
+        PropertyEditorManager.registerEditor(Pickup.class, PickupEditor.class);
+    }
+}
+//END SNIPPET: code

Added: openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/PickupEditor.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/PickupEditor.java?rev=636457&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/PickupEditor.java (added)
+++ openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/PickupEditor.java Wed Mar 12 12:08:26 2008
@@ -0,0 +1,41 @@
+/**
+ * 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.superbiz.enventries;
+
+/**
+ * With a java.beans.PropertyEditor, you can go way beyond the built-in
+ * types that OpenEJB supports and can extend dependency injection to
+ * just about anywhere.
+ *
+ * In the world of electric guitars, two types of pickups are used: humbucking, and single-coil.
+ * Guitarists often refer to their guitars as HSS, meaning a guitar with 1 humbucker and
+ * 2 single coil pickups, and so on.  This little PropertyEditor supports that shorthand notation.
+ *
+ * @version $Revision$ $Date$
+ */
+//START SNIPPET: code
+public class PickupEditor extends java.beans.PropertyEditorSupport {
+
+    public void setAsText(String text) throws IllegalArgumentException {
+        text = text.trim();
+
+        if (text.equalsIgnoreCase("H")) setValue(Pickup.HUMBUCKER);
+        else if (text.equalsIgnoreCase("S")) setValue(Pickup.SINGLE_COIL);
+        else throw new IllegalStateException("H and S are the only supported Pickup aliases");
+    }
+}
+//END SNIPPET: code

Modified: openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Stratocaster.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Stratocaster.java?rev=636457&r1=636456&r2=636457&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Stratocaster.java (original)
+++ openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Stratocaster.java Wed Mar 12 12:08:26 2008
@@ -27,20 +27,17 @@
 /**
  * @version $Rev$ $Date$
  */
+//START SNIPPET: code
 public interface Stratocaster {
-    Class getMyClass();
+    Date getDateCreated();
 
-    Date getMyDate();
+    float getStringGuage(String string);
 
-    Map<String, File> getMyFiles();
+    List<Pickup> getPickups();
 
-    InetAddress getMyInetAddress();
+    Style getStyle();
 
-    List getMyList();
+    File getCertificateOfAuthenticity();
 
-    Map getMyMap();
-
-    List<URI> getMyURIs();
-
-    URL getMyURL();
 }
+//END SNIPPET: code

Modified: openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/StratocasterImpl.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/StratocasterImpl.java?rev=636457&r1=636456&r2=636457&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/StratocasterImpl.java (original)
+++ openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/StratocasterImpl.java Wed Mar 12 12:08:26 2008
@@ -30,65 +30,51 @@
  * In addition to the standard env-entry types (String, Integer, Long, Short, Byte, Boolean, Double, Float, Character)
  * OpenEJB supports many other types.
  *
- * This bean has just about every flavor of env-entry OpenEJB supports.
  */
+//START SNIPPET: code
 @Stateless
 public class StratocasterImpl implements Stratocaster {
 
-    @Resource(name = "myClass")
-    private Class myClass;
 
-    @Resource(name = "myDate")
-    private Date myDate;
+    @Resource(name = "pickups")
+    private List<Pickup> pickups;
 
-    @Resource(name = "myFiles")
-    private Map<String, File> myFiles;
+    @Resource(name = "style")
+    private Style style;
 
-    @Resource(name = "myInetAddress")
-    private InetAddress myInetAddress;
+    @Resource(name = "dateCreated")
+    private Date dateCreated;
 
-    @Resource(name = "myList")
-    private List myList;
+    @Resource(name = "guitarStringGuages")
+    private Map<String, Float> guitarStringGuages;
 
-    @Resource(name = "myMap")
-    private Map myMap;
+    @Resource(name = "certificateOfAuthenticity")
+    private File certificateOfAuthenticity;
 
-    @Resource(name = "myURIs")
-    private List<URI> myURIs;
-
-    @Resource(name = "myURL")
-    private URL myURL;
-
-    public Class getMyClass() {
-        return myClass;
+    public Date getDateCreated() {
+        return dateCreated;
     }
 
-    public Date getMyDate() {
-        return myDate;
+    /**
+     * Gets the guage of the electric guitar strings
+     * used in this guitar.
+     * @param string
+     * @return
+     */
+    public float getStringGuage(String string){
+        return guitarStringGuages.get(string);
     }
 
-    public Map<String, File> getMyFiles() {
-        return myFiles;
+    public List<Pickup> getPickups() {
+        return pickups;
     }
 
-    public InetAddress getMyInetAddress() {
-        return myInetAddress;
+    public Style getStyle() {
+        return style;
     }
 
-    public List getMyList() {
-        return myList;
+    public File getCertificateOfAuthenticity() {
+        return certificateOfAuthenticity;
     }
-
-    public Map getMyMap() {
-        return myMap;
-    }
-
-    public List<URI> getMyURIs() {
-        return myURIs;
-    }
-
-    public URL getMyURL() {
-        return myURL;
-    }
-
 }
+//END SNIPPET: code

Modified: openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Style.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Style.java?rev=636457&r1=636456&r2=636457&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Style.java (original)
+++ openejb/trunk/openejb3/examples/expanded-env-entries/src/main/java/org/superbiz/enventries/Style.java Wed Mar 12 12:08:26 2008
@@ -19,5 +19,12 @@
 /**
  * @version $Revision$ $Date$
  */
+//START SNIPPET: code
 public enum Style {
+
+    STANDARD,
+    DELUX,
+    VINTAGE;
+
 }
+//END SNIPPET: code

Modified: openejb/trunk/openejb3/examples/expanded-env-entries/src/main/resources/META-INF/env-entries.properties
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/expanded-env-entries/src/main/resources/META-INF/env-entries.properties?rev=636457&r1=636456&r2=636457&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/expanded-env-entries/src/main/resources/META-INF/env-entries.properties (original)
+++ openejb/trunk/openejb3/examples/expanded-env-entries/src/main/resources/META-INF/env-entries.properties Wed Mar 12 12:08:26 2008
@@ -1,8 +1,5 @@
-myClass = org.superbiz.enventries.Stratocaster
-myDate = 1954-03-01
-myFiles = history=/tmp/play-history.txt\nartists=/tmp/famous-artists.txt
-myInetAddress = localhost
-myList = [Stevie Ray Vaughan, Eric Johnson, Mark Knopfler, Buddy Guy]
-myMap = color=3-Color Sunburst\nneck=maple\nfretboard=African rosewood\npickups=Texas Special
-myURIs = game://guitarheroII/?mode=expert,game://guitarheroIII/?guitar=wireless
-myURL = http://www.fender.com/
+guitarStringGuages=E1=0.052\nA=0.042\nD=0.030\nG=0.017\nB=0.013\nE=0.010
+certificateOfAuthenticity=/tmp/strat-certificate.txt
+dateCreated=1962-03-01
+pickups=S,S,S
+style=VINTAGE
\ No newline at end of file

Modified: openejb/trunk/openejb3/examples/expanded-env-entries/src/test/java/org/superbiz/enventries/StratocasterTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/expanded-env-entries/src/test/java/org/superbiz/enventries/StratocasterTest.java?rev=636457&r1=636456&r2=636457&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/expanded-env-entries/src/test/java/org/superbiz/enventries/StratocasterTest.java (original)
+++ openejb/trunk/openejb3/examples/expanded-env-entries/src/test/java/org/superbiz/enventries/StratocasterTest.java Wed Mar 12 12:08:26 2008
@@ -16,6 +16,8 @@
  */
 package org.superbiz.enventries;
 
+import static java.util.Arrays.asList;
+
 import junit.framework.TestCase;
 
 import javax.naming.InitialContext;
@@ -28,9 +30,10 @@
 import java.util.List;
 import java.util.Map;
 import java.util.LinkedHashMap;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Arrays;
 import java.io.File;
 import java.net.URI;
 import java.net.URL;
@@ -40,6 +43,7 @@
 /**
  * @version $Rev$ $Date$
  */
+//START SNIPPET: code
 public class StratocasterTest extends TestCase {
 
     private InitialContext initialContext;
@@ -52,43 +56,28 @@
     }
 
     public void test() throws Exception {
-        Stratocaster stratocaster = (Stratocaster) initialContext.lookup("StratocasterImplLocal");
+        Stratocaster strat = (Stratocaster) initialContext.lookup("StratocasterImplLocal");
+
 
+        Date date = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US).parse("Mar 1, 1962");
+        assertEquals("Strat.getDateCreated()", date, strat.getDateCreated());
 
-        assertEquals("Stratocaster.getMyClass()", Stratocaster.class, stratocaster.getMyClass());
+        List<Pickup> pickups = asList(Pickup.SINGLE_COIL, Pickup.SINGLE_COIL, Pickup.SINGLE_COIL);
+        assertEquals("Strat.getPickups()", pickups, strat.getPickups());
 
-        Date date = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US).parse("Mar 1, 1954");
-        assertEquals("Stratocaster.getMyDate()", date, stratocaster.getMyDate());
+        assertEquals("Strat.getStyle()", Style.VINTAGE, strat.getStyle());
 
-        Map<String, File> files = new HashMap<String, File>();
-        files.put("history", new File("/tmp/play-history.txt"));
-        files.put("artists", new File("/tmp/famous-artists.txt"));
-        assertEquals("Stratocaster.getMyFiles()", files, stratocaster.getMyFiles());
-
-        InetAddress host = InetAddress.getByName("localhost");
-        assertEquals("Stratocaster.getMyInetAddress()", host, stratocaster.getMyInetAddress());
-
-        List<String> list = new LinkedList<String>();
-        list.add("Stevie Ray Vaughan");
-        list.add("Eric Johnson");
-        list.add("Mark Knopfler");
-        list.add("Buddy Guy");
-        assertEquals("Stratocaster.getMyList()", list, stratocaster.getMyList());
-
-        Map<String,String> map = new LinkedHashMap<String,String>();
-        map.put("color", "3-Color Sunburst");
-        map.put("neck", "maple");
-        map.put("fretboard", "African rosewood");
-        map.put("pickups", "Texas Special");
-        assertEquals("Stratocaster.getMyMap()", map, stratocaster.getMyMap());
-
-        List<URI> uris = new ArrayList<URI>();
-        uris.add(new URI("game://guitarheroII/?mode=expert"));
-        uris.add(new URI("game://guitarheroIII/?guitar=wireless"));
-        assertEquals("Stratocaster.getMyURIs()", uris, stratocaster.getMyURIs());
+        assertEquals("Strat.getStringGuage(\"E1\")", 0.052F, strat.getStringGuage("E1"));
+        assertEquals("Strat.getStringGuage(\"A\")", 0.042F, strat.getStringGuage("A"));
+        assertEquals("Strat.getStringGuage(\"D\")", 0.030F, strat.getStringGuage("D"));
+        assertEquals("Strat.getStringGuage(\"G\")", 0.017F, strat.getStringGuage("G"));
+        assertEquals("Strat.getStringGuage(\"B\")", 0.013F, strat.getStringGuage("B"));
+        assertEquals("Strat.getStringGuage(\"E\")", 0.010F, strat.getStringGuage("E"));
 
-        assertEquals("Stratocaster.getMyURL()", new URL("http://www.fender.com/"), stratocaster.getMyURL());
+        File file = new File("/tmp/strat-certificate.txt");
+        assertEquals("Strat.getCertificateOfAuthenticity()", file, strat.getCertificateOfAuthenticity());
 
 
     }
 }
+//END SNIPPET: code