Renaming the AServerInfo class to ServerConnector and spliting it into three separate classes.
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 15 Nov 2008 06:53:43 +0100
changeset 29260bb8519cb2d
parent 291 9cbb8364c4ae
child 293 4feb55fa3c91
Renaming the AServerInfo class to ServerConnector and spliting it into three separate classes.
samples/aserverinfo/build.xml
samples/aserverinfo/nbproject/build-impl.xml
samples/aserverinfo/nbproject/genfiles.properties
samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java
samples/aserverinfo/src/org/apidesign/aserverinfo/cummulativefactory/ServerConnector.java
samples/aserverinfo/src/org/apidesign/aserverinfo/factories/ServerConnector.java
samples/aserverinfo/src/org/apidesign/aserverinfo/magicalbagfactory/ServerConnector.java
samples/aserverinfo/src/org/apidesign/aserverinfo/spi/NameProvider.java
samples/aserverinfo/src/org/apidesign/aserverinfo/spi/ResetHandler.java
samples/aserverinfo/src/org/apidesign/aserverinfo/spi/ShutdownHandler.java
samples/aserverinfo/src/org/apidesign/aserverinfo/spi/URLProvider.java
samples/aserverinfo/test/org/apidesign/aserverinfo/AServerInfoTest.java
samples/aserverinfo/test/org/apidesign/aserverinfo/test/ServerConnectorTest.java
     1.1 --- a/samples/aserverinfo/build.xml	Sun Nov 09 16:20:01 2008 +0100
     1.2 +++ b/samples/aserverinfo/build.xml	Sat Nov 15 06:53:43 2008 +0100
     1.3 @@ -2,6 +2,11 @@
     1.4  <!-- You may freely edit this file. See commented blocks below for -->
     1.5  <!-- some examples of how to customize the build. -->
     1.6  <!-- (If you delete it and reopen the project it will be recreated.) -->
     1.7 +<!-- By default, only the Clean and Build commands use this build script. -->
     1.8 +<!-- Commands such as Run, Debug, and Test only use this build script if -->
     1.9 +<!-- the Compile on Save feature is turned off for the project. -->
    1.10 +<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
    1.11 +<!-- in the project's Project Properties dialog box.-->
    1.12  <project name="aserverinfo" default="default" basedir=".">
    1.13      <description>Builds, tests, and runs the project aserverinfo.</description>
    1.14      <import file="nbproject/build-impl.xml"/>
     2.1 --- a/samples/aserverinfo/nbproject/build-impl.xml	Sun Nov 09 16:20:01 2008 +0100
     2.2 +++ b/samples/aserverinfo/nbproject/build-impl.xml	Sat Nov 15 06:53:43 2008 +0100
     2.3 @@ -19,7 +19,7 @@
     2.4    - cleanup
     2.5  
     2.6          -->
     2.7 -<project xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1" xmlns:j2seproject2="http://www.netbeans.org/ns/j2se-project/2" xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3" xmlns:jaxrpc="http://www.netbeans.org/ns/j2se-project/jax-rpc" basedir=".." default="default" name="aserverinfo-impl">
     2.8 +<project xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1" xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3" xmlns:jaxrpc="http://www.netbeans.org/ns/j2se-project/jax-rpc" basedir=".." default="default" name="aserverinfo-impl">
     2.9      <target depends="test,jar,javadoc" description="Build and test whole project." name="default"/>
    2.10      <!-- 
    2.11                  ======================
    2.12 @@ -218,13 +218,13 @@
    2.13              </sequential>
    2.14          </macrodef>
    2.15      </target>
    2.16 -    <target name="-init-macrodef-nbjpda">
    2.17 +    <target depends="-init-debug-args" name="-init-macrodef-nbjpda">
    2.18          <macrodef name="nbjpdastart" uri="http://www.netbeans.org/ns/j2se-project/1">
    2.19              <attribute default="${main.class}" name="name"/>
    2.20              <attribute default="${debug.classpath}" name="classpath"/>
    2.21              <attribute default="" name="stopclassname"/>
    2.22              <sequential>
    2.23 -                <nbjpdastart addressproperty="jpda.address" name="@{name}" stopclassname="@{stopclassname}" transport="dt_socket">
    2.24 +                <nbjpdastart addressproperty="jpda.address" name="@{name}" stopclassname="@{stopclassname}" transport="${debug-transport}">
    2.25                      <classpath>
    2.26                          <path path="@{classpath}"/>
    2.27                      </classpath>
    2.28 @@ -235,7 +235,9 @@
    2.29              <attribute default="${build.classes.dir}" name="dir"/>
    2.30              <sequential>
    2.31                  <nbjpdareload>
    2.32 -                    <fileset dir="@{dir}" includes="${fix.includes}*.class"/>
    2.33 +                    <fileset dir="@{dir}" includes="${fix.classes}">
    2.34 +                        <include name="${fix.includes}*.class"/>
    2.35 +                    </fileset>
    2.36                  </nbjpdareload>
    2.37              </sequential>
    2.38          </macrodef>
    2.39 @@ -253,6 +255,12 @@
    2.40          <condition else="-Xdebug" property="debug-args-line" value="-Xdebug -Xnoagent -Djava.compiler=none">
    2.41              <istrue value="${have-jdk-older-than-1.4}"/>
    2.42          </condition>
    2.43 +        <condition else="dt_socket" property="debug-transport-by-os" value="dt_shmem">
    2.44 +            <os family="windows"/>
    2.45 +        </condition>
    2.46 +        <condition else="${debug-transport-by-os}" property="debug-transport" value="${debug.transport}">
    2.47 +            <isset property="debug.transport"/>
    2.48 +        </condition>
    2.49      </target>
    2.50      <target depends="-init-debug-args" name="-init-macrodef-debug">
    2.51          <macrodef name="debug" uri="http://www.netbeans.org/ns/j2se-project/3">
    2.52 @@ -262,7 +270,7 @@
    2.53              <sequential>
    2.54                  <java classname="@{classname}" dir="${work.dir}" fork="true">
    2.55                      <jvmarg line="${debug-args-line}"/>
    2.56 -                    <jvmarg value="-Xrunjdwp:transport=dt_socket,address=${jpda.address}"/>
    2.57 +                    <jvmarg value="-Xrunjdwp:transport=${debug-transport},address=${jpda.address}"/>
    2.58                      <jvmarg line="${run.jvmargs}"/>
    2.59                      <classpath>
    2.60                          <path path="@{classpath}"/>
    2.61 @@ -279,12 +287,13 @@
    2.62      <target name="-init-macrodef-java">
    2.63          <macrodef name="java" uri="http://www.netbeans.org/ns/j2se-project/1">
    2.64              <attribute default="${main.class}" name="classname"/>
    2.65 +            <attribute default="${run.classpath}" name="classpath"/>
    2.66              <element name="customize" optional="true"/>
    2.67              <sequential>
    2.68                  <java classname="@{classname}" dir="${work.dir}" fork="true">
    2.69                      <jvmarg line="${run.jvmargs}"/>
    2.70                      <classpath>
    2.71 -                        <path path="${run.classpath}"/>
    2.72 +                        <path path="@{classpath}"/>
    2.73                      </classpath>
    2.74                      <syspropertyset>
    2.75                          <propertyref prefix="run-sys-prop."/>
    2.76 @@ -309,6 +318,13 @@
    2.77                  ===================
    2.78              -->
    2.79      <target depends="init" name="deps-jar" unless="no.deps"/>
    2.80 +    <target depends="init,-check-automatic-build,-clean-after-automatic-build" name="-verify-automatic-build"/>
    2.81 +    <target depends="init" name="-check-automatic-build">
    2.82 +        <available file="${build.classes.dir}/.netbeans_automatic_build" property="netbeans.automatic.build"/>
    2.83 +    </target>
    2.84 +    <target depends="init" if="netbeans.automatic.build" name="-clean-after-automatic-build">
    2.85 +        <antcall target="clean"/>
    2.86 +    </target>
    2.87      <target depends="init,deps-jar" name="-pre-pre-compile">
    2.88          <mkdir dir="${build.classes.dir}"/>
    2.89      </target>
    2.90 @@ -329,7 +345,7 @@
    2.91          <!-- Empty placeholder for easier customization. -->
    2.92          <!-- You can override this target in the ../build.xml file. -->
    2.93      </target>
    2.94 -    <target depends="init,deps-jar,-pre-pre-compile,-pre-compile,-do-compile,-post-compile" description="Compile project." name="compile"/>
    2.95 +    <target depends="init,deps-jar,-verify-automatic-build,-pre-pre-compile,-pre-compile,-do-compile,-post-compile" description="Compile project." name="compile"/>
    2.96      <target name="-pre-compile-single">
    2.97          <!-- Empty placeholder for easier customization. -->
    2.98          <!-- You can override this target in the ../build.xml file. -->
    2.99 @@ -343,7 +359,7 @@
   2.100          <!-- Empty placeholder for easier customization. -->
   2.101          <!-- You can override this target in the ../build.xml file. -->
   2.102      </target>
   2.103 -    <target depends="init,deps-jar,-pre-pre-compile,-pre-compile-single,-do-compile-single,-post-compile-single" name="compile-single"/>
   2.104 +    <target depends="init,deps-jar,-verify-automatic-build,-pre-pre-compile,-pre-compile-single,-do-compile-single,-post-compile-single" name="compile-single"/>
   2.105      <!--
   2.106                  ====================
   2.107                  JAR BUILDING SECTION
   2.108 @@ -427,6 +443,10 @@
   2.109          <fail unless="run.class">Must select one file in the IDE or set run.class</fail>
   2.110          <j2seproject1:java classname="${run.class}"/>
   2.111      </target>
   2.112 +    <target depends="init,-do-not-recompile,compile-test-single" name="run-test-with-main">
   2.113 +        <fail unless="run.class">Must select one file in the IDE or set run.class</fail>
   2.114 +        <j2seproject1:java classname="${run.class}" classpath="${run.test.classpath}"/>
   2.115 +    </target>
   2.116      <!--
   2.117                  =================
   2.118                  DEBUGGING SECTION
   2.119 @@ -435,6 +455,9 @@
   2.120      <target depends="init" if="netbeans.home" name="-debug-start-debugger">
   2.121          <j2seproject1:nbjpdastart name="${debug.class}"/>
   2.122      </target>
   2.123 +    <target depends="init" if="netbeans.home" name="-debug-start-debugger-main-test">
   2.124 +        <j2seproject1:nbjpdastart classpath="${debug.test.classpath}" name="${debug.class}"/>
   2.125 +    </target>
   2.126      <target depends="init,compile" name="-debug-start-debuggee">
   2.127          <j2seproject3:debug>
   2.128              <customize>
   2.129 @@ -452,6 +475,11 @@
   2.130          <j2seproject3:debug classname="${debug.class}"/>
   2.131      </target>
   2.132      <target depends="init,-do-not-recompile,compile-single,-debug-start-debugger,-debug-start-debuggee-single" if="netbeans.home" name="debug-single"/>
   2.133 +    <target depends="init,compile-test-single" if="netbeans.home" name="-debug-start-debuggee-main-test">
   2.134 +        <fail unless="debug.class">Must select one file in the IDE or set debug.class</fail>
   2.135 +        <j2seproject3:debug classname="${debug.class}" classpath="${debug.test.classpath}"/>
   2.136 +    </target>
   2.137 +    <target depends="init,-do-not-recompile,compile-test-single,-debug-start-debugger-main-test,-debug-start-debuggee-main-test" if="netbeans.home" name="debug-test-with-main"/>
   2.138      <target depends="init" name="-pre-debug-fix">
   2.139          <fail unless="fix.includes">Must set fix.includes</fail>
   2.140          <property name="javac.includes" value="${fix.includes}.java"/>
     3.1 --- a/samples/aserverinfo/nbproject/genfiles.properties	Sun Nov 09 16:20:01 2008 +0100
     3.2 +++ b/samples/aserverinfo/nbproject/genfiles.properties	Sat Nov 15 06:53:43 2008 +0100
     3.3 @@ -1,8 +1,8 @@
     3.4  build.xml.data.CRC32=c82747ea
     3.5 -build.xml.script.CRC32=68215f8a
     3.6 -build.xml.stylesheet.CRC32=be360661
     3.7 +build.xml.script.CRC32=aee7ca48
     3.8 +build.xml.stylesheet.CRC32=958a1d3e
     3.9  # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
    3.10  # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
    3.11  nbproject/build-impl.xml.data.CRC32=c82747ea
    3.12 -nbproject/build-impl.xml.script.CRC32=b22c6fb6
    3.13 -nbproject/build-impl.xml.stylesheet.CRC32=f1d9da08
    3.14 +nbproject/build-impl.xml.script.CRC32=1033418a
    3.15 +nbproject/build-impl.xml.stylesheet.CRC32=84d9cdb5
     4.1 --- a/samples/aserverinfo/src/org/apidesign/aserverinfo/AServerInfo.java	Sun Nov 09 16:20:01 2008 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,142 +0,0 @@
     4.4 -package org.apidesign.aserverinfo;
     4.5 -
     4.6 -import java.net.URL;
     4.7 -import org.openide.util.Lookup;
     4.8 -
     4.9 -// BEGIN: aserverinfo.api
    4.10 -public final class AServerInfo {
    4.11 -    public String getName() {
    4.12 -        return name == null ? "noname" : name.getName();
    4.13 -    }
    4.14 -    
    4.15 -    public URL getURL() {
    4.16 -        return url == null ? null : url.getURL();
    4.17 -    }
    4.18 -    
    4.19 -    public void reset() {
    4.20 -        if (reset != null) {
    4.21 -            reset.reset();
    4.22 -        }
    4.23 -    }
    4.24 -    
    4.25 -    public void shutdown() {
    4.26 -        if (shutdown != null) {
    4.27 -            shutdown.shutdown();
    4.28 -        }
    4.29 -    }
    4.30 -// FINISH: aserverinfo.api
    4.31 -
    4.32 -    //
    4.33 -    // private part
    4.34 -    //
    4.35 -    
    4.36 -    private final NameProvider name;
    4.37 -    private final URLProvider url;
    4.38 -    private final ResetHandler reset;
    4.39 -    private final ShutdownHandler shutdown;
    4.40 -
    4.41 -    private AServerInfo(
    4.42 -        NameProvider name, URLProvider url,
    4.43 -        ResetHandler reset, ShutdownHandler shutdown
    4.44 -    ) {
    4.45 -        this.name = name;
    4.46 -        this.url = url;
    4.47 -        this.reset = reset;
    4.48 -        this.shutdown = shutdown;
    4.49 -    }
    4.50 -
    4.51 -    //
    4.52 -    // factories
    4.53 -    //
    4.54 -
    4.55 -    // BEGIN: aserverinfo.create
    4.56 -    public interface NameProvider {
    4.57 -        public String getName();
    4.58 -    }
    4.59 -    public interface URLProvider {
    4.60 -        public URL getURL();
    4.61 -    }
    4.62 -    public interface ResetHandler {
    4.63 -        public void reset();
    4.64 -    }
    4.65 -    
    4.66 -    public static AServerInfo create(final Lookup interfaces) 
    4.67 -    // END: aserverinfo.create
    4.68 -    {
    4.69 -        NameProvider nameP = new NameProvider() {
    4.70 -            public String getName() {
    4.71 -                NameProvider p = interfaces.lookup(NameProvider.class);
    4.72 -                return p == null ? "noname" : p.getName();
    4.73 -            }
    4.74 -        };
    4.75 -        URLProvider urlP = new URLProvider() {
    4.76 -            public URL getURL() {
    4.77 -                URLProvider p = interfaces.lookup(URLProvider.class);
    4.78 -                return p == null ? null : p.getURL();
    4.79 -            }
    4.80 -        };
    4.81 -        ResetHandler resetP = new ResetHandler() {
    4.82 -            public void reset() {
    4.83 -                ResetHandler h = interfaces.lookup(ResetHandler.class);
    4.84 -                if (h != null) {
    4.85 -                    h.reset();
    4.86 -                }
    4.87 -            }
    4.88 -        };
    4.89 -        
    4.90 -        return new AServerInfo(nameP, urlP, resetP, null);
    4.91 -    }
    4.92 -    
    4.93 -    // BEGIN: aserverinfo.regularcreate
    4.94 -    public static AServerInfo create(
    4.95 -        NameProvider nameProvider, 
    4.96 -        URLProvider urlProvider, 
    4.97 -        ResetHandler reset
    4.98 -    )
    4.99 -    // END: aserverinfo.regularcreate
   4.100 -    {
   4.101 -        return new AServerInfo(nameProvider, urlProvider, reset, null);
   4.102 -    }
   4.103 -    
   4.104 -    // BEGIN: aserverinfo.regularcreate.withshutdown
   4.105 -    /** @since 2.0 */
   4.106 -    public interface ShutdownHandler {
   4.107 -        public void shutdown();
   4.108 -    }
   4.109 -    
   4.110 -    /** @since 2.0 */
   4.111 -    public static AServerInfo create(
   4.112 -        NameProvider nameProvider, 
   4.113 -        URLProvider urlProvider, 
   4.114 -        ResetHandler reset, 
   4.115 -        ShutdownHandler shutdown
   4.116 -    )
   4.117 -    // END: aserverinfo.regularcreate.withshutdown
   4.118 -    {
   4.119 -        return new AServerInfo(nameProvider, urlProvider, reset, shutdown);
   4.120 -    }
   4.121 -
   4.122 -    //
   4.123 -    // cumulative factory methods
   4.124 -    //
   4.125 -    
   4.126 -    // BEGIN: aserverinfo.cumulative.factory
   4.127 -    public static AServerInfo empty() {
   4.128 -        return new AServerInfo(null, null, null, null);
   4.129 -    }
   4.130 -    
   4.131 -    public final AServerInfo nameProvider(final NameProvider np) {
   4.132 -        return new AServerInfo(np, this.url, this.reset, this.shutdown);
   4.133 -    }
   4.134 -    // END: aserverinfo.cumulative.factory
   4.135 -
   4.136 -    public final AServerInfo urlProvider(final URLProvider up) {
   4.137 -        return new AServerInfo(this.name, up, this.reset, this.shutdown);
   4.138 -    }
   4.139 -    public final AServerInfo reset(final ResetHandler h) {
   4.140 -        return new AServerInfo(this.name, this.url, h, this.shutdown);
   4.141 -    }
   4.142 -    public final AServerInfo shutdown(final ShutdownHandler s) {
   4.143 -        return new AServerInfo(this.name, this.url, this.reset, s);
   4.144 -    }
   4.145 -}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/cummulativefactory/ServerConnector.java	Sat Nov 15 06:53:43 2008 +0100
     5.3 @@ -0,0 +1,88 @@
     5.4 +package org.apidesign.aserverinfo.cummulativefactory;
     5.5 +
     5.6 +import org.apidesign.aserverinfo.spi.NameProvider;
     5.7 +import org.apidesign.aserverinfo.spi.ResetHandler;
     5.8 +import org.apidesign.aserverinfo.spi.URLProvider;
     5.9 +import java.net.URL;
    5.10 +import org.apidesign.aserverinfo.spi.ShutdownHandler;
    5.11 +
    5.12 +// BEGIN: aserverinfo.api
    5.13 +/** A class to connect to server, identify it and manipulate with
    5.14 + * it state. The <a href="http://apidesign.org">Practical API Design</a>
    5.15 + * book used to call it AServerInfo.
    5.16 + * <p>
    5.17 + */
    5.18 +public final class ServerConnector {
    5.19 +    public String getName() {
    5.20 +        return name == null ? "noname" : name.getName();
    5.21 +    }
    5.22 +    
    5.23 +    public URL getURL() {
    5.24 +        return url == null ? null : url.getURL();
    5.25 +    }
    5.26 +    
    5.27 +    public void reset() {
    5.28 +        if (reset != null) {
    5.29 +            reset.reset();
    5.30 +        }
    5.31 +    }
    5.32 +
    5.33 +    /** Additional method for API clients not available from first version.
    5.34 +     * @since 2.0
    5.35 +     */
    5.36 +    public void shutdown() {
    5.37 +        if (shutdown != null) {
    5.38 +            shutdown.shutdown();
    5.39 +        }
    5.40 +    }
    5.41 +// FINISH: aserverinfo.api
    5.42 +
    5.43 +    //
    5.44 +    // cumulative factory methods
    5.45 +    //
    5.46 +    
    5.47 +    // BEGIN: aserverinfo.cumulative.factory
    5.48 +    public static ServerConnector empty() {
    5.49 +        return new ServerConnector(null, null, null, null);
    5.50 +    }
    5.51 +    
    5.52 +    public final ServerConnector nameProvider(NameProvider np) {
    5.53 +        return new ServerConnector(np, this.url, this.reset, this.shutdown);
    5.54 +    }
    5.55 +
    5.56 +    public final ServerConnector urlProvider(URLProvider up) {
    5.57 +        return new ServerConnector(this.name, up, this.reset, this.shutdown);
    5.58 +    }
    5.59 +    public final ServerConnector reset(ResetHandler h) {
    5.60 +        return new ServerConnector(this.name, this.url, h, this.shutdown);
    5.61 +    }
    5.62 +    /** All one needs to do when there is a need to add new
    5.63 +     * style of creation is to add new <em>clonning</em> method.
    5.64 +     * @param handler
    5.65 +     * @return
    5.66 +     * @since 2.0
    5.67 +     */
    5.68 +    public final ServerConnector shutdown(ShutdownHandler handler) {
    5.69 +        return new ServerConnector(this.name, this.url, this.reset, handler);
    5.70 +    }
    5.71 +    // END: aserverinfo.cumulative.factory
    5.72 +
    5.73 +    //
    5.74 +    // private part
    5.75 +    //
    5.76 +
    5.77 +    private final NameProvider name;
    5.78 +    private final URLProvider url;
    5.79 +    private final ResetHandler reset;
    5.80 +    private final ShutdownHandler shutdown;
    5.81 +
    5.82 +    private ServerConnector(
    5.83 +        NameProvider name, URLProvider url,
    5.84 +        ResetHandler reset, ShutdownHandler shutdown
    5.85 +    ) {
    5.86 +        this.name = name;
    5.87 +        this.url = url;
    5.88 +        this.reset = reset;
    5.89 +        this.shutdown = shutdown;
    5.90 +    }
    5.91 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/factories/ServerConnector.java	Sat Nov 15 06:53:43 2008 +0100
     6.3 @@ -0,0 +1,84 @@
     6.4 +package org.apidesign.aserverinfo.factories;
     6.5 +
     6.6 +import org.apidesign.aserverinfo.spi.ShutdownHandler;
     6.7 +import java.net.URL;
     6.8 +import org.apidesign.aserverinfo.spi.NameProvider;
     6.9 +import org.apidesign.aserverinfo.spi.ResetHandler;
    6.10 +import org.apidesign.aserverinfo.spi.URLProvider;
    6.11 +
    6.12 +/** A class to connect to server, identify it and manipulate with
    6.13 + * it state. The <a href="http://apidesign.org">Practical API Design</a>
    6.14 + * book used to call it AServerInfo.
    6.15 + * <p>
    6.16 + */
    6.17 +public final class ServerConnector {
    6.18 +    public String getName() {
    6.19 +        return name == null ? "noname" : name.getName();
    6.20 +    }
    6.21 +
    6.22 +    public URL getURL() {
    6.23 +        return url == null ? null : url.getURL();
    6.24 +    }
    6.25 +
    6.26 +    public void reset() {
    6.27 +        if (reset != null) {
    6.28 +            reset.reset();
    6.29 +        }
    6.30 +    }
    6.31 +
    6.32 +    /** Additional method for API clients not available from first version.
    6.33 +     * @since 2.0
    6.34 +     */
    6.35 +    public void shutdown() {
    6.36 +        if (shutdown != null) {
    6.37 +            shutdown.shutdown();
    6.38 +        }
    6.39 +    }
    6.40 +
    6.41 +    //
    6.42 +    // factories
    6.43 +    //
    6.44 +    
    6.45 +    // BEGIN: aserverinfo.regularcreate
    6.46 +    public static ServerConnector create(
    6.47 +        NameProvider nameProvider, 
    6.48 +        URLProvider urlProvider, 
    6.49 +        ResetHandler reset
    6.50 +    )
    6.51 +    // END: aserverinfo.regularcreate
    6.52 +    {
    6.53 +        return new ServerConnector(nameProvider, urlProvider, reset, null);
    6.54 +    }
    6.55 +    
    6.56 +    /** @since 2.0 */
    6.57 +    public static ServerConnector create(
    6.58 +        NameProvider nameProvider, 
    6.59 +        URLProvider urlProvider, 
    6.60 +        ResetHandler reset, 
    6.61 +        ShutdownHandler shutdown
    6.62 +    )
    6.63 +    // END: aserverinfo.regularcreate.withshutdown
    6.64 +    {
    6.65 +        return new ServerConnector(nameProvider, urlProvider, reset, shutdown);
    6.66 +    }
    6.67 +
    6.68 +    //
    6.69 +    // private part
    6.70 +    //
    6.71 +
    6.72 +    private final NameProvider name;
    6.73 +    private final URLProvider url;
    6.74 +    private final ResetHandler reset;
    6.75 +    private final ShutdownHandler shutdown;
    6.76 +
    6.77 +    private ServerConnector(
    6.78 +        NameProvider name, URLProvider url,
    6.79 +        ResetHandler reset, ShutdownHandler shutdown
    6.80 +    ) {
    6.81 +        this.name = name;
    6.82 +        this.url = url;
    6.83 +        this.reset = reset;
    6.84 +        this.shutdown = shutdown;
    6.85 +    }
    6.86 +
    6.87 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/magicalbagfactory/ServerConnector.java	Sat Nov 15 06:53:43 2008 +0100
     7.3 @@ -0,0 +1,93 @@
     7.4 +package org.apidesign.aserverinfo.magicalbagfactory;
     7.5 +
     7.6 +import java.net.URL;
     7.7 +import org.apidesign.aserverinfo.spi.NameProvider;
     7.8 +import org.apidesign.aserverinfo.spi.ResetHandler;
     7.9 +import org.apidesign.aserverinfo.spi.ShutdownHandler;
    7.10 +import org.apidesign.aserverinfo.spi.URLProvider;
    7.11 +import org.openide.util.Lookup;
    7.12 +
    7.13 +/** A class to connect to server, identify it and manipulate with
    7.14 + * it state. The <a href="http://apidesign.org">Practical API Design</a>
    7.15 + * book used to call it AServerInfo.
    7.16 + * <p>
    7.17 + */
    7.18 +public final class ServerConnector {
    7.19 +    public String getName() {
    7.20 +        return name == null ? "noname" : name.getName();
    7.21 +    }
    7.22 +
    7.23 +    public URL getURL() {
    7.24 +        return url == null ? null : url.getURL();
    7.25 +    }
    7.26 +
    7.27 +    public void reset() {
    7.28 +        if (reset != null) {
    7.29 +            reset.reset();
    7.30 +        }
    7.31 +    }
    7.32 +
    7.33 +    /** Additional method for API clients not available from first version.
    7.34 +     * @since 2.0
    7.35 +     */
    7.36 +    public void shutdown() {
    7.37 +        if (shutdown != null) {
    7.38 +            shutdown.shutdown();
    7.39 +        }
    7.40 +    }
    7.41 +
    7.42 +    //
    7.43 +    // private part
    7.44 +    //
    7.45 +    
    7.46 +    private final NameProvider name;
    7.47 +    private final URLProvider url;
    7.48 +    private final ResetHandler reset;
    7.49 +    private final ShutdownHandler shutdown;
    7.50 +
    7.51 +    private ServerConnector(
    7.52 +        NameProvider name, URLProvider url,
    7.53 +        ResetHandler reset, ShutdownHandler shutdown
    7.54 +    ) {
    7.55 +        this.name = name;
    7.56 +        this.url = url;
    7.57 +        this.reset = reset;
    7.58 +        this.shutdown = shutdown;
    7.59 +    }
    7.60 +
    7.61 +    // BEGIN: aserverinfo.magicalbag.create
    7.62 +    public static ServerConnector create(final Lookup interfaces) {
    7.63 +        NameProvider nameP = new NameProvider() {
    7.64 +            public String getName() {
    7.65 +                NameProvider p = interfaces.lookup(NameProvider.class);
    7.66 +                return p == null ? "noname" : p.getName();
    7.67 +            }
    7.68 +        };
    7.69 +        URLProvider urlP = new URLProvider() {
    7.70 +            public URL getURL() {
    7.71 +                URLProvider p = interfaces.lookup(URLProvider.class);
    7.72 +                return p == null ? null : p.getURL();
    7.73 +            }
    7.74 +        };
    7.75 +        ResetHandler resetP = new ResetHandler() {
    7.76 +            public void reset() {
    7.77 +                ResetHandler h = interfaces.lookup(ResetHandler.class);
    7.78 +                if (h != null) {
    7.79 +                    h.reset();
    7.80 +                }
    7.81 +            }
    7.82 +        };
    7.83 +        // code present since version 2.0
    7.84 +        ShutdownHandler shutH = new ShutdownHandler() {
    7.85 +            public void shutdown() {
    7.86 +                ShutdownHandler h = interfaces.lookup(ShutdownHandler.class);
    7.87 +                if (h != null) {
    7.88 +                    h.shutdown();
    7.89 +                }
    7.90 +            }
    7.91 +        };
    7.92 +        
    7.93 +        return new ServerConnector(nameP, urlP, resetP, shutH);
    7.94 +    }
    7.95 +    // END: aserverinfo.magicalbag.create
    7.96 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/spi/NameProvider.java	Sat Nov 15 06:53:43 2008 +0100
     8.3 @@ -0,0 +1,6 @@
     8.4 +package org.apidesign.aserverinfo.spi;
     8.5 +
     8.6 +public interface NameProvider {
     8.7 +
     8.8 +    public String getName();
     8.9 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/spi/ResetHandler.java	Sat Nov 15 06:53:43 2008 +0100
     9.3 @@ -0,0 +1,6 @@
     9.4 +package org.apidesign.aserverinfo.spi;
     9.5 +
     9.6 +public interface ResetHandler {
     9.7 +
     9.8 +    public void reset();
     9.9 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/spi/ShutdownHandler.java	Sat Nov 15 06:53:43 2008 +0100
    10.3 @@ -0,0 +1,15 @@
    10.4 +package org.apidesign.aserverinfo.spi;
    10.5 +
    10.6 +/**
    10.7 + * Additional interface descrabing capability of a server connection
    10.8 + * introduced in subsequent version of the API to emulate evolution
    10.9 + * issues with various implementation of creational patterns.
   10.10 + *
   10.11 + * @since 2.0
   10.12 + * @see org.apidesign.aserverinfo.cloningfactory.ServerConnector
   10.13 + * @see org.apidesign.aserverinfo.factories.ServerConnector
   10.14 + * @see org.apidesign.aserverinfo.magicalbagfactory.ServerConnector
   10.15 + */
   10.16 +public interface ShutdownHandler {
   10.17 +    public void shutdown();
   10.18 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/samples/aserverinfo/src/org/apidesign/aserverinfo/spi/URLProvider.java	Sat Nov 15 06:53:43 2008 +0100
    11.3 @@ -0,0 +1,8 @@
    11.4 +package org.apidesign.aserverinfo.spi;
    11.5 +
    11.6 +import java.net.URL;
    11.7 +
    11.8 +public interface URLProvider {
    11.9 +
   11.10 +    public URL getURL();
   11.11 +}
    12.1 --- a/samples/aserverinfo/test/org/apidesign/aserverinfo/AServerInfoTest.java	Sun Nov 09 16:20:01 2008 +0100
    12.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.3 @@ -1,85 +0,0 @@
    12.4 -package org.apidesign.aserverinfo;
    12.5 -
    12.6 -import java.net.MalformedURLException;
    12.7 -import java.net.URL;
    12.8 -import org.junit.After;
    12.9 -import org.junit.Before;
   12.10 -import org.junit.Test;
   12.11 -import static org.junit.Assert.*;
   12.12 -import org.openide.util.Exceptions;
   12.13 -
   12.14 -public class AServerInfoTest {
   12.15 -
   12.16 -    public AServerInfoTest() {
   12.17 -    }
   12.18 -
   12.19 -    @Before
   12.20 -    public void setUp() {
   12.21 -    }
   12.22 -
   12.23 -    @After
   12.24 -    public void tearDown() {
   12.25 -    }
   12.26 -
   12.27 -    @Test
   12.28 -    public void showUseOfCumulativeFactory() throws Exception {
   12.29 -        Prov p = new Prov();
   12.30 -        AServerInfo.NameProvider np = p;
   12.31 -        AServerInfo.URLProvider up = p;
   12.32 -        AServerInfo.ResetHandler res = p;
   12.33 -        AServerInfo inf;
   12.34 -        
   12.35 -        // BEGIN: aserverinfo.cumulative.creation
   12.36 -        inf = AServerInfo.empty().nameProvider(np).urlProvider(up).reset(res);
   12.37 -        // END: aserverinfo.cumulative.creation
   12.38 -        
   12.39 -        assertEquals("API Design Server", inf.getName());
   12.40 -        assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
   12.41 -        inf.reset();
   12.42 -        assertEquals("Once reset", 1, p.resets);
   12.43 -        
   12.44 -    }
   12.45 -    
   12.46 -    @Test
   12.47 -    public void showVerboseUseOfCumulativeFactory() throws Exception {
   12.48 -        Prov prov = new Prov();
   12.49 -        AServerInfo info;
   12.50 -        
   12.51 -        // BEGIN: aserverinfo.cumulative.creation.verbose
   12.52 -        AServerInfo empty = AServerInfo.empty();
   12.53 -        AServerInfo name = empty.nameProvider(prov);
   12.54 -        AServerInfo urlAndName = name.urlProvider(prov);
   12.55 -        info = urlAndName.reset(prov);
   12.56 -        // END: aserverinfo.cumulative.creation.verbose
   12.57 -        
   12.58 -        assertEquals("API Design Server", info.getName());
   12.59 -        assertEquals("http://www.apidesign.org", info.getURL().toExternalForm());
   12.60 -        info.reset();
   12.61 -        assertEquals("Once reset", 1, prov.resets);
   12.62 -        
   12.63 -    }
   12.64 -    
   12.65 -    
   12.66 -    private static class Prov implements AServerInfo.NameProvider, AServerInfo.URLProvider, AServerInfo.ResetHandler {
   12.67 -        int resets;
   12.68 -
   12.69 -        public String getName() {
   12.70 -            return "API Design Server";
   12.71 -        }
   12.72 -
   12.73 -        public URL getURL() {
   12.74 -            try {
   12.75 -                return new URL("http://www.apidesign.org");
   12.76 -            } catch (MalformedURLException ex) {
   12.77 -                Exceptions.printStackTrace(ex);
   12.78 -                return null;
   12.79 -            }
   12.80 -        }
   12.81 -
   12.82 -        public void reset() {
   12.83 -            resets++;
   12.84 -        }
   12.85 -
   12.86 -    }
   12.87 -        
   12.88 -}
   12.89 \ No newline at end of file
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/samples/aserverinfo/test/org/apidesign/aserverinfo/test/ServerConnectorTest.java	Sat Nov 15 06:53:43 2008 +0100
    13.3 @@ -0,0 +1,89 @@
    13.4 +package org.apidesign.aserverinfo.test;
    13.5 +
    13.6 +import java.net.MalformedURLException;
    13.7 +import java.net.URL;
    13.8 +import org.apidesign.aserverinfo.cummulativefactory.ServerConnector;
    13.9 +import org.apidesign.aserverinfo.spi.NameProvider;
   13.10 +import org.apidesign.aserverinfo.spi.ResetHandler;
   13.11 +import org.apidesign.aserverinfo.spi.URLProvider;
   13.12 +import org.junit.After;
   13.13 +import org.junit.Before;
   13.14 +import org.junit.Test;
   13.15 +import static org.junit.Assert.*;
   13.16 +import org.openide.util.Exceptions;
   13.17 +
   13.18 +public class ServerConnectorTest {
   13.19 +
   13.20 +    public ServerConnectorTest() {
   13.21 +    }
   13.22 +
   13.23 +    @Before
   13.24 +    public void setUp() {
   13.25 +    }
   13.26 +
   13.27 +    @After
   13.28 +    public void tearDown() {
   13.29 +    }
   13.30 +
   13.31 +    @Test
   13.32 +    public void showUseOfCumulativeFactory() throws Exception {
   13.33 +        Prov p = new Prov();
   13.34 +        NameProvider np = p;
   13.35 +        URLProvider up = p;
   13.36 +        ResetHandler res = p;
   13.37 +        ServerConnector inf;
   13.38 +        
   13.39 +        // BEGIN: ServerConnector.cumulative.creation
   13.40 +        inf = ServerConnector.empty().nameProvider(np).urlProvider(up).reset(res);
   13.41 +        // END: ServerConnector.cumulative.creation
   13.42 +        
   13.43 +        assertEquals("API Design Server", inf.getName());
   13.44 +        assertEquals("http://www.apidesign.org", inf.getURL().toExternalForm());
   13.45 +        inf.reset();
   13.46 +        assertEquals("Once reset", 1, p.resets);
   13.47 +        
   13.48 +    }
   13.49 +    
   13.50 +    @Test
   13.51 +    public void showVerboseUseOfCumulativeFactory() throws Exception {
   13.52 +        Prov prov = new Prov();
   13.53 +        ServerConnector info;
   13.54 +        
   13.55 +        // BEGIN: ServerConnector.cumulative.creation.verbose
   13.56 +        ServerConnector empty = ServerConnector.empty();
   13.57 +        ServerConnector name = empty.nameProvider(prov);
   13.58 +        ServerConnector urlAndName = name.urlProvider(prov);
   13.59 +        info = urlAndName.reset(prov);
   13.60 +        // END: ServerConnector.cumulative.creation.verbose
   13.61 +        
   13.62 +        assertEquals("API Design Server", info.getName());
   13.63 +        assertEquals("http://www.apidesign.org", info.getURL().toExternalForm());
   13.64 +        info.reset();
   13.65 +        assertEquals("Once reset", 1, prov.resets);
   13.66 +        
   13.67 +    }
   13.68 +    
   13.69 +    
   13.70 +    private static class Prov implements NameProvider, URLProvider, ResetHandler {
   13.71 +        int resets;
   13.72 +
   13.73 +        public String getName() {
   13.74 +            return "API Design Server";
   13.75 +        }
   13.76 +
   13.77 +        public URL getURL() {
   13.78 +            try {
   13.79 +                return new URL("http://www.apidesign.org");
   13.80 +            } catch (MalformedURLException ex) {
   13.81 +                Exceptions.printStackTrace(ex);
   13.82 +                return null;
   13.83 +            }
   13.84 +        }
   13.85 +
   13.86 +        public void reset() {
   13.87 +            resets++;
   13.88 +        }
   13.89 +
   13.90 +    }
   13.91 +        
   13.92 +}
   13.93 \ No newline at end of file