You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by hl...@apache.org on 2003/08/06 20:50:50 UTC

cvs commit: jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/impl ModuleImpl.java SchemaElement.java

hlship      2003/08/06 11:50:50

  Modified:    hivemind/src/java/org/apache/commons/hivemind Registry.java
               hivemind/src/java/org/apache/commons/hivemind/service/impl
                        AbstractServiceInterceptorFactory.java
               hivemind/xdocs services.xml
               hivemind/src/test/hivemind/test/services TestServices.java
               hivemind/src/META-INF hivemodule.xml
               hivemind/src/java/org/apache/commons/hivemind/impl
                        ModuleImpl.java SchemaElement.java
  Added:       hivemind/src/test/hivemind/test/services/impl
                        ToStringImpl.java
               hivemind/src/test/hivemind/test/services ToString.java
                        ToString.xml
  Log:
  Add support for Interceptors to provide a meaningful toString() method (if toString() is not part of the service interface).
  
  Revision  Changes    Path
  1.5       +6 -4      jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/Registry.java
  
  Index: Registry.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/Registry.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Registry.java	31 Jul 2003 21:06:25 -0000	1.4
  +++ Registry.java	6 Aug 2003 18:50:49 -0000	1.5
  @@ -73,15 +73,17 @@
    * extension point elements}.
    * 
    * <p>
  - * A secondary function for the registry is to convert variable references
  - * to strings.  A variable reference is of the form:
  + * A secondary function for the registry is to convert symbols
  + * to strings.  A symbol is of the form:
    * 
    * <blockquote>
    * <code>${<i>name</i>}</code>
    * </blockquote>
    * 
    * <p>
  - * The name is any string, and may contain punctuation and dashes.
  + * The name is any string, and may contain punctuation and dashes.  Symbols
  + * appear in the XML contributed to extension points, as well as in the XML
  + * parameters to service factories.
    * 
    *
    * @author Howard Lewis Ship
  
  
  
  1.1                  jakarta-commons-sandbox/hivemind/src/test/hivemind/test/services/impl/ToStringImpl.java
  
  Index: ToStringImpl.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package hivemind.test.services.impl;
  
  import hivemind.test.services.ToString;
  
  /**
   * Used to test interaction between interceptors and toString().
   *
   * @author Howard Lewis Ship
   * @version $Id: ToStringImpl.java,v 1.1 2003/08/06 18:50:49 hlship Exp $
   */
  public class ToStringImpl implements ToString
  {
      public String toString()
      {
          return "ToStringImpl of toString()";
  
      }
  }
  
  
  1.6       +49 -8     jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/AbstractServiceInterceptorFactory.java
  
  Index: AbstractServiceInterceptorFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/AbstractServiceInterceptorFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- AbstractServiceInterceptorFactory.java	5 Aug 2003 22:06:58 -0000	1.5
  +++ AbstractServiceInterceptorFactory.java	6 Aug 2003 18:50:49 -0000	1.6
  @@ -82,7 +82,6 @@
   {
       private ClassFactory _factory;
       private String _extensionId;
  -    private String _baseName;
       private static int _uid;
   
       public void createInterceptor(InterceptorStack stack)
  @@ -98,7 +97,7 @@
   
           createInfrastructure(serviceInterfaceClass, classFab);
   
  -        createMethods(serviceInterfaceClass, classFab);
  +        createMethods(stack, classFab);
   
           Class interceptorClass = classFab.createClass();
   
  @@ -137,7 +136,7 @@
           _extensionId = point.getExtensionPointId();
   
           _factory =
  -            (ClassFactory)point.getModule().getRegistry().getService(
  +            (ClassFactory) point.getModule().getRegistry().getService(
                   HiveMind.CLASS_FACTORY_SERVICE_ID,
                   ClassFactory.class);
       }
  @@ -203,21 +202,63 @@
   
       }
   
  -    private void createMethods(Class serviceInterfaceClass, ClassFab fab)
  +    private void createMethods(InterceptorStack stack, ClassFab fab)
       {
  -        Method[] methods = serviceInterfaceClass.getMethods();
  +        boolean toString = false;
  +        Method[] methods = stack.getServiceInterface().getMethods();
   
  -        int count = methods.length;
  -        for (int i = 0; i < count; i++)
  +        for (int i = 0; i < methods.length; i++)
           {
               Method m = methods[i];
  +
               createMethod(
                   fab,
                   m.getName(),
                   m.getReturnType(),
                   m.getParameterTypes(),
                   m.getExceptionTypes());
  +
  +            toString |= isToString(m);
           }
  +
  +        if (!toString)
  +            createToString(stack, fab);
  +    }
  +
  +    /**
  +     * Returns true if the method is the standard toString() method.
  +     * Very few interfaces will ever include this method as part of
  +     * the interface, but we have to be sure.
  +     */
  +    private boolean isToString(Method method)
  +    {
  +        if (!method.getName().equals("toString"))
  +            return false;
  +
  +        if (method.getParameterTypes().length > 0)
  +            return false;
  +
  +        return method.getReturnType().equals(String.class);
  +    }
  +
  +    /**
  +     * Creates a toString() method that identify the interceptor service id,
  +     * the intercepted service id, and the service interface class name).
  +     */
  +    private void createToString(InterceptorStack stack, ClassFab fab)
  +    {
  +        StringBuffer body = new StringBuffer("return \"<Interceptor: ");
  +
  +        body.append(_extensionId);
  +        body.append(" for ");
  +        body.append(stack.getServiceExtensionPoint().getExtensionPointId());
  +        body.append("(");
  +        body.append(stack.getServiceInterface().getName());
  +        body.append(")>");
  +
  +        body.append("\";");
  +
  +        fab.addMethod("toString", String.class, null, null, body.toString());
       }
   
       /**
  
  
  
  1.13      +23 -1     jakarta-commons-sandbox/hivemind/xdocs/services.xml
  
  Index: services.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/xdocs/services.xml,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- services.xml	1 Aug 2003 14:21:38 -0000	1.12
  +++ services.xml	6 Aug 2003 18:50:50 -0000	1.13
  @@ -148,6 +148,28 @@
   				was given a lower order than "Performance" so that the time for security checks would be included in the
   				"Performance" analysis.
   				</p>
  +				
  +				<p>
  +				Interceptors implement the <code>toString()</code>	method to provide a useful identification for the interceptor,
  +				for example:
  +				<br/>
  +				<code>
  +				&lt;Interceptor: hivemind.LoggingInterceptor for com.myco.MyService(com.myco.MyServiceInterface)&gt;
  +				</code>
  +				</p>
  +				
  +				<p>
  +				This string identifies the interceptor service factory (hivemind.LoggingInterceptor),
  +				the service extension point (com.myco.MyService) and the service interface
  +				(com.myco.MyServiceInterface).
  +					</p>
  +					
  +					<p>
  +					If <code>toString()</code>	 is part of the service interface (really, a very rare
  +					case), then the interceptor <em>does not</em> override the service implementation's
  +					implementation.
  +					</p>
  +				
   			</subsection>
   		</section>
   		<section name="A short example">
  
  
  
  1.16      +28 -1     jakarta-commons-sandbox/hivemind/src/test/hivemind/test/services/TestServices.java
  
  Index: TestServices.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/test/hivemind/test/services/TestServices.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- TestServices.java	5 Aug 2003 00:50:36 -0000	1.15
  +++ TestServices.java	6 Aug 2003 18:50:50 -0000	1.16
  @@ -138,6 +138,10 @@
           assertEquals(19, s.add(11, 8));
   
           assertEquals(2, CountFactory.getCount());
  +
  +        assertEquals(
  +            "<Interceptor: hivemind.test.services.CountFactory for hivemind.test.services.Simple(hivemind.test.services.SimpleService)>",
  +            s.toString());
       }
   
       public void testInterceptorSort() throws Exception
  @@ -349,5 +353,28 @@
   
           assertSame(ss, c.getSimpleService());
           assertSame(nl, c.getNameLookup());
  +    }
  +
  +    /**
  +     * Test that checks that interceptors don't override toString() if toString()
  +     * is part of the service interface.
  +     */
  +    public void testToString() throws Exception
  +    {
  +        Registry r = buildRegistry("ToString.xml");
  +
  +        ToString ts = (ToString) r.getService("hivemind.test.services.ToString", ToString.class);
  +
  +        interceptLogging("hivemind.test");
  +
  +        assertEquals("ToStringImpl of toString()", ts.toString());
  +
  +        List events = getInterceptedLogEvents();
  +        checkLoggingEvent("hivemind.test.services.ToString", "BEGIN toString\\(\\)", events);
  +        checkLoggingEvent(
  +            "hivemind.test.services.ToString",
  +            "END toString\\(\\) \\[ToStringImpl of toString\\(\\)\\]",
  +            events);
  +
       }
   }
  
  
  
  1.1                  jakarta-commons-sandbox/hivemind/src/test/hivemind/test/services/ToString.java
  
  Index: ToString.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package hivemind.test.services;
  
  /**
   * Used with test that ensures the interceptor does not override the toString() method,
   * if the method is explicitly part of the service interface.
   *
   * @author Howard Lewis Ship
   * @version $Id: ToString.java,v 1.1 2003/08/06 18:50:50 hlship Exp $
   */
  public interface ToString
  {
  	/**
  	 * An odd thing to put into a service interface, but if the service implementation
  	 * needs to implement this, then so be it.
  	 */
  	public String toString();
  }
  
  
  
  1.1                  jakarta-commons-sandbox/hivemind/src/test/hivemind/test/services/ToString.xml
  
  Index: ToString.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <!-- $Id: ToString.xml,v 1.1 2003/08/06 18:50:50 hlship Exp $ -->
  <module id="hivemind.test.services" version="1.0.0">
  	<service id="ToString" interface="hivemind.test.services.ToString">
  	  <create-instance class="hivemind.test.services.impl.ToStringImpl"/>
  	  <interceptor service-id="hivemind.LoggingInterceptor"/>
  	</service>
  </module>
  
  
  1.12      +6 -2      jakarta-commons-sandbox/hivemind/src/META-INF/hivemodule.xml
  
  Index: hivemodule.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/META-INF/hivemodule.xml,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- hivemodule.xml	5 Aug 2003 15:41:44 -0000	1.11
  +++ hivemodule.xml	6 Aug 2003 18:50:50 -0000	1.12
  @@ -47,7 +47,11 @@
   	</service>
   	
   		<service id="LoggingInterceptor" interface="org.apache.commons.hivemind.ServiceInterceptorFactory">
  -		<description>An interceptor factory for adding method-level logging to a service.</description>
  +		<description>
  +			An interceptor factory for adding method-level logging to a service. 
  +			Logging occurs at level DEBUG and uses the service id as the logger. 
  +		  Method entry (with parameters) and method exit (with return value) are logged,
  +		  as are any checked exceptions.</description>
   		<create-instance class="org.apache.commons.hivemind.service.impl.LoggingInterceptorFactory"/>
   	</service>
   	
  
  
  
  1.3       +1 -5      jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/impl/ModuleImpl.java
  
  Index: ModuleImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/impl/ModuleImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ModuleImpl.java	5 Aug 2003 17:16:29 -0000	1.2
  +++ ModuleImpl.java	6 Aug 2003 18:50:50 -0000	1.3
  @@ -67,8 +67,6 @@
   import org.apache.commons.hivemind.Registry;
   import org.apache.commons.hivemind.ServiceExtensionPoint;
   import org.apache.commons.lang.builder.ToStringBuilder;
  -import org.apache.commons.logging.Log;
  -import org.apache.commons.logging.LogFactory;
   import org.apache.tapestry.ApplicationRuntimeException;
   import org.apache.tapestry.IMessages;
   import org.apache.tapestry.IResourceResolver;
  @@ -82,8 +80,6 @@
    */
   public class ModuleImpl extends BaseLocatable implements Module
   {
  -    private static final Log LOG = LogFactory.getLog(ModuleImpl.class);
  -
       private String _moduleId;
       private Registry _registry;
       private IResourceResolver _resolver;
  
  
  
  1.2       +1 -5      jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/impl/SchemaElement.java
  
  Index: SchemaElement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/impl/SchemaElement.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SchemaElement.java	29 Jul 2003 22:20:35 -0000	1.1
  +++ SchemaElement.java	6 Aug 2003 18:50:50 -0000	1.2
  @@ -72,8 +72,6 @@
   import org.apache.commons.hivemind.schema.ElementModel;
   import org.apache.commons.hivemind.schema.Rule;
   import org.apache.commons.hivemind.schema.SchemaProcessor;
  -import org.apache.commons.logging.Log;
  -import org.apache.commons.logging.LogFactory;
   import org.apache.tapestry.ApplicationRuntimeException;
   
   /**
  @@ -86,8 +84,6 @@
   
   class SchemaElement
   {
  -    private static final Log LOG = LogFactory.getLog(SchemaElement.class);
  -
       private ElementModel _model;
       private List _requiredAttributes;
       private Set _knownAttributes;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org