hlship      2004/06/05 19:14:30

  Modified:    library/src/descriptor/META-INF hivemodule.sdl
               src/documentation/content/xdocs site.xml
  Added:       library/src/test/hivemind/test/lib ToString.java
                        TestDefaultImplementationBuilder.java
                        DefaultImplementationBuilder.sdl ValueHolder.java
               library/src/documentation/content/xdocs/hivemind-lib
                        PlaceholderBuilder.xml
                        DefaultImplementationBuilder.xml
               library/src/java/org/apache/hivemind/lib
                        DefaultImplementationBuilder.java
               library/src/java/org/apache/hivemind/lib/impl
                        DefaultImplementationBuilderImpl.java
                        PlaceholderBuilder.java
  Log:
  Add new services to the library for creating default/placeholder 
implementations of services.
  
  Revision  Changes    Path
  1.1                  
jakarta-hivemind/library/src/test/hivemind/test/lib/ToString.java
  
  Index: ToString.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed 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 hivemind.test.lib;
  
  /**
   * Used with [EMAIL PROTECTED] 
hivemind.test.lib.TestDefaultImplementationBuilder}.
   *
   * @author Howard Lewis Ship
   */
  public interface ToString
  {
        /**
         * Expect this to return null, since it's part of the interface.
         */
        public String toString();
  }
  
  
  
  1.1                  
jakarta-hivemind/library/src/test/hivemind/test/lib/TestDefaultImplementationBuilder.java
  
  Index: TestDefaultImplementationBuilder.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed 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 hivemind.test.lib;
  
  import org.apache.hivemind.ApplicationRuntimeException;
  import org.apache.hivemind.impl.DefaultClassResolver;
  import org.apache.hivemind.internal.Module;
  import org.apache.hivemind.internal.RegistryInfrastructure;
  import org.apache.hivemind.lib.DefaultImplementationBuilder;
  import org.apache.hivemind.lib.impl.PlaceholderBuilder;
  import org.apache.hivemind.lib.impl.DefaultImplementationBuilderImpl;
  import org.apache.hivemind.service.impl.ClassFactoryImpl;
  import org.apache.hivemind.test.HiveMindTestCase;
  import org.easymock.MockControl;
  
  /**
   * Tests for [EMAIL PROTECTED] 
org.apache.hivemind.lib.impl.DefaultImplementationBuilderImpl} and
   * [EMAIL PROTECTED] org.apache.hivemind.lib.impl.PlaceholderBuilder}.
   *
   * @author Howard Lewis Ship
   */
  public class TestDefaultImplementationBuilder extends HiveMindTestCase
  {
      private DefaultImplementationBuilder _builder;
  
      private MockControl _moduleControl;
      private Module _module;
  
      protected void setUp() throws Exception
      {
          DefaultImplementationBuilderImpl bi = new 
DefaultImplementationBuilderImpl();
  
          bi.setClassFactory(new ClassFactoryImpl());
  
          _builder = bi;
      }
  
      private void setupModule()
      {
          _moduleControl = MockControl.createStrictControl(Module.class);
          _module = (Module) _moduleControl.getMock();
  
          _module.getModuleId();
          _moduleControl.setReturnValue("some.id");
  
          _module.getClassResolver();
          _moduleControl.setReturnValue(new DefaultClassResolver());
  
          _moduleControl.replay();
      }
  
      private Object create(Class interfaceType)
      {
          setupModule();
  
          return _builder.buildDefaultImplementation(interfaceType, _module);
      }
  
      public void testSimple()
      {
          Runnable r = (Runnable) create(Runnable.class);
  
          r.run();
  
          assertEquals("<Default implementation of interface 
java.lang.Runnable>", r.toString());
  
          _moduleControl.verify();
      }
  
      public void testComplex()
      {
          ValueHolder vh = (ValueHolder) create(ValueHolder.class);
  
          assertNull(vh.getStringValue());
          assertEquals(false, vh.getBooleanValue());
          assertEquals(0, vh.getIntValue());
  
          _moduleControl.verify();
      }
  
      public void testToStringInInterface()
      {
          ToString ts = (ToString) create(ToString.class);
  
          assertNull(ts.toString());
  
          _moduleControl.verify();
      }
  
      public void testCache()
      {
          Runnable r1 = (Runnable) create(Runnable.class);
          Runnable r2 = (Runnable) 
_builder.buildDefaultImplementation(Runnable.class, _module);
  
          assertSame(r1, r2);
  
          _moduleControl.verify();
      }
  
      public void testNotInterface()
      {
          try
          {
              create(String.class);
              unreachable();
          }
          catch (ApplicationRuntimeException ex)
          {
              assertExceptionSubstring(ex, "Class java.lang.String is not an 
interface.");
          }
      }
  
      public void testModuleDescriptor() throws Exception
      {
          RegistryInfrastructure r = 
buildFrameworkRegistry("DefaultImplementationBuilder.sdl");
  
          DefaultImplementationBuilder dib =
              (DefaultImplementationBuilder) r.getService(
                  "hivemind.lib.DefaultImplementationBuilder",
                  DefaultImplementationBuilder.class);
  
          setupModule();
  
          Runnable o = (Runnable) 
dib.buildDefaultImplementation(Runnable.class, _module);
  
          o.run();
  
          _moduleControl.verify();
      }
  
      public void testPlaceholderBuilderSimulated() throws Exception
      {
          PlaceholderBuilder db = new PlaceholderBuilder();
  
          db.setBuilder(_builder);
  
          setupModule();
  
          Runnable r =
              (Runnable) db.createCoreServiceImplementation("foo", 
Runnable.class, _module, null);
  
          r.run();
  
          _moduleControl.verify();
      }
  
      public void testPlaceholderBuilder() throws Exception
      {
          RegistryInfrastructure r = 
buildFrameworkRegistry("DefaultImplementationBuilder.sdl");
  
          Runnable o = (Runnable) r.getService("hivemind.test.lib.Runnable", 
Runnable.class);
  
          o.run();
      }
  }
  
  
  
  1.1                  
jakarta-hivemind/library/src/test/hivemind/test/lib/DefaultImplementationBuilder.sdl
  
  Index: DefaultImplementationBuilder.sdl
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed 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.
  
  module (id=hivemind.test.lib version="1.0.0")
  {
    service-point (id=Runnable interface=java.lang.Runnable)
    {
      invoke-factory (service-id=hivemind.lib.PlaceholderBuilder)
    }
  }
  
  
  1.1                  
jakarta-hivemind/library/src/test/hivemind/test/lib/ValueHolder.java
  
  Index: ValueHolder.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed 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 hivemind.test.lib;
  
  /**
   * Used with [EMAIL PROTECTED] 
hivemind.test.lib.TestDefaultImplementationBuilder}.
   *
   * @author Howard Lewis Ship
   */
  public interface ValueHolder
  {
        public boolean getBooleanValue();
        public int getIntValue();
        public String getStringValue();
  }
  
  
  
  1.1                  
jakarta-hivemind/library/src/documentation/content/xdocs/hivemind-lib/PlaceholderBuilder.xml
  
  Index: PlaceholderBuilder.xml
  ===================================================================
  <?xml version="1.0"?>
  <!-- 
     Copyright 2004 The Apache Software Foundation
  
     Licensed 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.
  -->
  <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.2//EN"
        "./dtd/document-v12.dtd" [
        <!ENTITY projectroot '../'>
        <!ENTITY % common-links SYSTEM "../links.ent">
        %common-links;
        ]>
  <document>
        <header>
                <title>hivemind.lib.PlaceholderBuilder Service</title>
        </header>
        <body>
      <p>The <link 
href="&hivedoc;/service/hivemind.lib.PlaceholderBuilder.html"> 
        PlaceholderBuilder</link> service is a service implementation factory 
        that uses the <link 
        
href="site:hivemind.lib.DefaultImplementationBuilder">DefaultImplementationBuilder</link>
 
        service to create placeholder implementations for services. 
Placeholders 
        do nothing at all.</p>
        </body>
  </document>
  
  
  
  1.1                  
jakarta-hivemind/library/src/documentation/content/xdocs/hivemind-lib/DefaultImplementationBuilder.xml
  
  Index: DefaultImplementationBuilder.xml
  ===================================================================
  <?xml version="1.0"?>
  <!-- 
     Copyright 2004 The Apache Software Foundation
  
     Licensed 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.
  -->
  <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.2//EN"
        "./dtd/document-v12.dtd" [
        <!ENTITY projectroot '../'>
        <!ENTITY % common-links SYSTEM "../links.ent">
        %common-links;
        ]>
  <document>
        <header>
                <title>hivemind.lib.DefaultImplementationBuilder Service</title>
        </header>
        <body>
      <p>The <link 
        
href="&hivedoc;/service/hivemind.lib.DefaultImplementationBuilder.html"> 
        DefaultImplementationBuilder</link> service is used to create default 
        implementations of interfaces. As described in the <link 
        href="&apiroot-lib;/DefaultImplementationBuilder.html"> service 
interface 
        JavaDoc</link>, methods return null, 0 or false (depending on return 
        type) and otherwise do nothing. </p>
        </body>
  </document>
  
  
  
  1.1                  
jakarta-hivemind/library/src/java/org/apache/hivemind/lib/DefaultImplementationBuilder.java
  
  Index: DefaultImplementationBuilder.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed 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.hivemind.lib;
  
  import org.apache.hivemind.internal.Module;
  
  /**
   * Builds a default implementation of an interface. The fabricated class has 
no-op
   * implementions for each method in the interface.  Non-void methods return 
null,
   * false or zero.  A cached, shared instance of the empty implementation is 
kept and will
   * be returned on future invocations.
   *
   * @author Howard Lewis Ship
   */
  public interface DefaultImplementationBuilder
  {
      /**
       * Builds a default implementation of the indicated interface, 
instantiates and returns it.
       * Results are cached for later re-use.
       * 
       * @throws org.apache.hivemind.ApplicationRuntimeException if 
interfaceType is not an interface.
       */
      public Object buildDefaultImplementation(Class interfaceType, Module 
module);
  }
  
  
  
  1.6       +34 -2     
jakarta-hivemind/library/src/descriptor/META-INF/hivemodule.sdl
  
  Index: hivemodule.sdl
  ===================================================================
  RCS file: 
/home/cvs/jakarta-hivemind/library/src/descriptor/META-INF/hivemodule.sdl,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- hivemodule.sdl    1 Jun 2004 14:17:30 -0000       1.5
  +++ hivemodule.sdl    6 Jun 2004 02:14:30 -0000       1.6
  @@ -12,8 +12,6 @@
   // See the License for the specific language governing permissions and
   // limitations under the License.
   
  -// $Id$
  -
   module (id=hivemind.lib version="1.0.0")
   {
        description { "Standard library for HiveMind, providing commonly used 
services." }
  @@ -167,5 +165,39 @@
          }
          
          create-instance 
(class=org.apache.hivemind.lib.impl.SpringBeanFactoryHolderImpl)
  +     }
  +     
  +     service-point (id=DefaultImplementationBuilder 
interface=org.apache.hivemind.lib.DefaultImplementationBuilder)
  +     {
  +       description
  +       {
  +         "A service which can create a default implementation of an 
arbitrary interface. The instance returned does "
  +         "nothing. Returned instances are cached and re-used."
  +       }
  +       
  +       invoke-factory (service-id=hivemind.BuilderFactory)
  +       {
  +         construct 
(class=org.apache.hivemind.lib.impl.DefaultImplementationBuilderImpl)
  +         {
  +           set-service (property=classFactory 
service-id=hivemind.ClassFactory)
  +         }
  +       }
  +     }
  +     
  +     service-point (id=PlaceholderBuilder 
interface=org.apache.hivemind.ServiceImplementationFactory)
  +     {
  +       description
  +       {
  +         "A service implementation factory that builds a default 
implementation of a service. The service implementation "
  +         "is a placeholder that does nothing."
  +       }
  +       
  +       invoke-factory (service-id=hivemind.BuilderFactory)
  +       {
  +         construct (class=org.apache.hivemind.lib.impl.PlaceholderBuilder)
  +         {
  +           set-service (property=builder 
service-id=DefaultImplementationBuilder)
  +         }
  +       }
        }
   }
  
  
  
  1.13      +2 -0      jakarta-hivemind/src/documentation/content/xdocs/site.xml
  
  Index: site.xml
  ===================================================================
  RCS file: 
/home/cvs/jakarta-hivemind/src/documentation/content/xdocs/site.xml,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- site.xml  5 Jun 2004 19:36:37 -0000       1.12
  +++ site.xml  6 Jun 2004 02:14:30 -0000       1.13
  @@ -104,8 +104,10 @@
                <index href="index.html"/>
                
                <services label="Services">
  +      <hivemind.lib.DefaultImplementationBuilder 
label="DefaultImplementationBuilder" href="DefaultImplementationBuilder.html"/>
                        <hivemind.lib.EJBProxyFactory label="EJBProxyFactory" 
href="EJBProxyFactory.html"/>
                        <hivemind.lib.NameLookup label="NameLookup" 
href="NameLookup.html"/>
  +      <hivemind.lib.PlaceholderBuilder label="PlaceholderBuilder" 
href="PlaceholderBuilder.html"/>      
                        <hivemind.lib.RemoteExceptionCoordinator 
label="RemoteExceptionCoordinator" href="RemoteExceptionCoordinator.html"/>
                        <hivemind.lib.SpringLookupFactory 
label="SpringLookupFactory" href="SpringLookupFactory.html"/>
                </services>     
  
  
  
  1.1                  
jakarta-hivemind/library/src/java/org/apache/hivemind/lib/impl/DefaultImplementationBuilderImpl.java
  
  Index: DefaultImplementationBuilderImpl.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed 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.hivemind.lib.impl;
  
  import java.lang.reflect.Method;
  import java.lang.reflect.Modifier;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.Map;
  
  import org.apache.hivemind.ApplicationRuntimeException;
  import org.apache.hivemind.impl.BaseLocatable;
  import org.apache.hivemind.internal.Module;
  import org.apache.hivemind.lib.DefaultImplementationBuilder;
  import org.apache.hivemind.service.ClassFab;
  import org.apache.hivemind.service.ClassFabUtils;
  import org.apache.hivemind.service.ClassFactory;
  
  /**
   * Implemenation of [EMAIL PROTECTED] 
org.apache.hivemind.lib.DefaultImplementationBuilder}.
   *
   * @author Howard Lewis Ship
   */
  public class DefaultImplementationBuilderImpl
      extends BaseLocatable
      implements DefaultImplementationBuilder
  {
      private Map _instances = Collections.synchronizedMap(new HashMap());
  
      private ClassFactory _classFactory;
  
      public Object buildDefaultImplementation(Class interfaceType, Module 
module)
      {
          Object result = _instances.get(interfaceType);
  
          if (result == null)
          {
              result = create(interfaceType, module);
              _instances.put(interfaceType, result);
          }
  
          return result;
      }
  
      private Object create(Class interfaceType, Module module)
      {
          Class defaultClass = createClass(interfaceType, module);
  
          try
          {
              return defaultClass.newInstance();
          }
          catch (Exception ex)
          {
              throw new ApplicationRuntimeException(
                  "Unable to create default implementation of interface "
                      + interfaceType.getName()
                      + ":"
                      + ex.getMessage(),
                  ex);
          }
      }
  
      private Class createClass(Class interfaceType, Module module)
      {
          if (!interfaceType.isInterface())
              throw new ApplicationRuntimeException(
                  "Class " + interfaceType.getName() + " is not an interface.");
  
          String name = ClassFabUtils.generateClassName("DefaultImpl");
  
          ClassFab cf = _classFactory.newClass(name, Object.class, module);
  
          cf.addInterface(interfaceType);
  
          boolean toString = false;
  
          Method[] methods = interfaceType.getMethods();
  
          for (int i = 0; i < methods.length; i++)
          {
              Method m = methods[i];
  
              toString |= ClassFabUtils.isToString(m);
  
              addMethod(cf, m);
          }
  
          if (!toString)
              ClassFabUtils.addToStringMethod(
                  cf,
                  "<Default implementation of interface " + 
interfaceType.getName() + ">");
  
          return cf.createClass();
      }
  
      private void addMethod(ClassFab cf, Method m)
      {
          StringBuffer body = new StringBuffer("{ ");
  
          Class returnType = m.getReturnType();
  
          if (returnType != void.class)
          {
              body.append("return ");
  
              if (returnType.isPrimitive())
              {
                  if (returnType == boolean.class)
                      body.append("false");
                  else
                      body.append("0");
              }
              else
              {
                  body.append("null");
              }
  
              body.append(";");
          }
  
          body.append(" }");
  
          cf.addMethod(
              Modifier.PUBLIC,
              m.getName(),
              returnType,
              m.getParameterTypes(),
              m.getExceptionTypes(),
              body.toString());
      }
      
      public void setClassFactory(ClassFactory factory)
      {
          _classFactory = factory;
      }
  
  }
  
  
  
  1.1                  
jakarta-hivemind/library/src/java/org/apache/hivemind/lib/impl/PlaceholderBuilder.java
  
  Index: PlaceholderBuilder.java
  ===================================================================
  //  Copyright 2004 The Apache Software Foundation
  //
  // Licensed 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.hivemind.lib.impl;
  
  import java.util.List;
  
  import org.apache.hivemind.ServiceImplementationFactory;
  import org.apache.hivemind.impl.BaseLocatable;
  import org.apache.hivemind.internal.Module;
  import org.apache.hivemind.lib.DefaultImplementationBuilder;
  
  /**
   * Wrapper around [EMAIL PROTECTED] 
org.apache.hivemind.lib.DefaultImplementationBuilder} that is used
   * to create default implementations of services.
   *
   * @author Howard Lewis Ship
   */
  public class PlaceholderBuilder extends BaseLocatable implements 
ServiceImplementationFactory
  {
      private DefaultImplementationBuilder _builder;
  
      public Object createCoreServiceImplementation(
          String serviceId,
          Class serviceInterface,
          Module invokingModule,
          List parameters)
      {
          return _builder.buildDefaultImplementation(serviceInterface, 
invokingModule);
      }
  
      public void setBuilder(DefaultImplementationBuilder builder)
      {
          _builder = builder;
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to