Clover coverage report - Code Coverage for hivemind-lib release 1.0-beta-2
Coverage timestamp: Sun Aug 1 2004 14:04:17 EDT
file stats: LOC: 277   Methods: 12
NCLOC: 188   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
EJBProxyFactory.java 87.5% 98.8% 100% 98.1%
coverage coverage
 1   
 //  Copyright 2004 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.hivemind.lib.impl;
 16   
 
 17   
 import java.lang.reflect.Constructor;
 18   
 import java.lang.reflect.Method;
 19   
 import java.lang.reflect.Modifier;
 20   
 import java.rmi.RemoteException;
 21   
 import java.util.List;
 22   
 
 23   
 import org.apache.hivemind.ApplicationRuntimeException;
 24   
 import org.apache.hivemind.HiveMind;
 25   
 import org.apache.hivemind.ServiceImplementationFactory;
 26   
 import org.apache.hivemind.impl.BaseLocatable;
 27   
 import org.apache.hivemind.internal.Module;
 28   
 import org.apache.hivemind.lib.NameLookup;
 29   
 import org.apache.hivemind.lib.RemoteExceptionCoordinator;
 30   
 import org.apache.hivemind.service.BodyBuilder;
 31   
 import org.apache.hivemind.service.ClassFab;
 32   
 import org.apache.hivemind.service.ClassFabUtils;
 33   
 import org.apache.hivemind.service.ClassFactory;
 34   
 import org.apache.hivemind.service.MethodSignature;
 35   
 
 36   
 /**
 37   
  * An implementation of {@link org.apache.hivemind.ServiceImplementationFactory}
 38   
  * that can create a proxy to a stateless session EJB.  Using this factory, it is
 39   
  * easy to create a HiveMind service wrapper around the actual EJB.
 40   
  * 
 41   
  * <p>
 42   
  * The parameters for the factory are used to identify the JNDI name of the
 43   
  * session EJB's home interface.
 44   
  * 
 45   
  * 
 46   
  *
 47   
  * @author Howard Lewis Ship
 48   
  */
 49   
 public class EJBProxyFactory extends BaseLocatable implements ServiceImplementationFactory
 50   
 {
 51   
     /**
 52   
      * SEP for the EJBProxyFactory.
 53   
      */
 54   
     private String _pointId;
 55   
     private NameLookup _nameLookup;
 56   
     private RemoteExceptionCoordinator _coordinator;
 57   
     private ClassFactory _classFactory;
 58   
 
 59  3
     public Object createCoreServiceImplementation(
 60   
         String serviceId,
 61   
         Class serviceInterface,
 62   
         Module invokingModule,
 63   
         List parameters)
 64   
     {
 65  3
         HiveMind.checkFactoryParameterCount(_pointId, parameters, 1);
 66   
 
 67  3
         EJBProxyParameters proxyParameters = (EJBProxyParameters) parameters.get(0);
 68  3
         String jndiName = proxyParameters.getJndiName();
 69  3
         String homeInterfaceClassName = proxyParameters.getHomeInterfaceClassName();
 70   
 
 71   
         // The service interface is the remote interface.
 72   
 
 73  3
         Class homeInterface = invokingModule.getClassResolver().findClass(homeInterfaceClassName);
 74   
 
 75  3
         String proxyClassName = ClassFabUtils.generateClassName("EJBProxy");
 76   
 
 77  3
         ClassFab classFab =
 78   
             _classFactory.newClass(proxyClassName, AbstractEJBProxy.class, invokingModule);
 79   
 
 80  3
         classFab.addInterface(serviceInterface);
 81   
 
 82  3
         classFab.addField("_remote", serviceInterface);
 83   
 
 84  3
         addClearCachedMethod(classFab);
 85   
 
 86  3
         addLookupMethod(classFab, homeInterface, serviceInterface, jndiName);
 87   
 
 88  3
         addServiceMethods(classFab, serviceInterface, serviceId, jndiName);
 89   
 
 90  3
         addConstructor(classFab);
 91   
 
 92  3
         Class proxyClass = classFab.createClass();
 93   
 
 94  3
         return invokeConstructor(proxyClass, proxyParameters.getNameLookup(_nameLookup));
 95   
     }
 96   
 
 97  3
     private void addClearCachedMethod(ClassFab classFab)
 98   
     {
 99  3
         classFab.addMethod(
 100   
             Modifier.PROTECTED,
 101   
             new MethodSignature(void.class, "_clearCachedReferences", null, null),
 102   
             "_remote = null;");
 103   
     }
 104   
 
 105  3
     private void addLookupMethod(
 106   
         ClassFab classFab,
 107   
         Class homeInterface,
 108   
         Class remoteInterface,
 109   
         String jndiName)
 110   
     {
 111  3
         String homeInterfaceName = homeInterface.getName();
 112   
 
 113  3
         BodyBuilder builder = new BodyBuilder();
 114   
 
 115  3
         builder.begin();
 116   
 
 117  3
         builder.addln("if (_remote != null)");
 118  3
         builder.addln("  return _remote;");
 119   
 
 120  3
         builder.add(homeInterfaceName);
 121  3
         builder.add(" home = (");
 122  3
         builder.add(homeInterfaceName);
 123  3
         builder.add(") _lookup(");
 124  3
         builder.addQuoted(jndiName);
 125  3
         builder.addln(");");
 126   
 
 127  3
         builder.add("try");
 128  3
         builder.begin();
 129  3
         builder.add("_remote = home.create();");
 130  3
         builder.end();
 131  3
         builder.add("catch (javax.ejb.CreateException ex)");
 132  3
         builder.begin();
 133  3
         builder.add("throw new java.rmi.RemoteException(ex.getMessage(), ex);");
 134  3
         builder.end();
 135   
 
 136  3
         builder.add("return _remote;");
 137   
 
 138  3
         builder.end();
 139   
 
 140  3
         classFab.addMethod(
 141   
             Modifier.SYNCHRONIZED + Modifier.PRIVATE,
 142   
             new MethodSignature(
 143   
                 remoteInterface,
 144   
                 "_lookupRemote",
 145   
                 null,
 146   
                 new Class[] { RemoteException.class }),
 147   
             builder.toString());
 148   
 
 149   
     }
 150   
 
 151  3
     private void addServiceMethods(
 152   
         ClassFab classFab,
 153   
         Class serviceInterface,
 154   
         String serviceId,
 155   
         String jndiName)
 156   
     {
 157  3
         Method[] methods = serviceInterface.getMethods();
 158  3
         boolean toString = false;
 159   
 
 160  3
         for (int i = 0; i < methods.length; i++)
 161   
         {
 162  18
             Method m = methods[i];
 163   
 
 164  18
             addServiceMethod(classFab, m);
 165   
 
 166  18
             toString |= ClassFabUtils.isToString(m);
 167   
         }
 168   
 
 169  3
         if (!toString)
 170  3
             addToStringMethod(classFab, serviceInterface, serviceId, jndiName);
 171   
     }
 172   
 
 173  18
     private void addServiceMethod(ClassFab classFab, Method m)
 174   
     {
 175  18
         String methodName = m.getName();
 176   
 
 177  18
         boolean isVoid = m.getReturnType().equals(Void.TYPE);
 178   
 
 179  18
         BodyBuilder builder = new BodyBuilder();
 180   
 
 181  18
         builder.begin();
 182   
 
 183  18
         builder.addln("boolean first = true;");
 184  18
         builder.add("while (true)");
 185  18
         builder.begin();
 186   
 
 187  18
         builder.add("try");
 188  18
         builder.begin();
 189   
 
 190  18
         if (!isVoid)
 191  15
             builder.add("return ");
 192   
 
 193  18
         builder.add("_lookupRemote().");
 194  18
         builder.add(methodName);
 195  18
         builder.addln("($$);");
 196   
 
 197  18
         if (isVoid)
 198  3
             builder.addln("return;");
 199   
 
 200  18
         builder.end(); // try
 201   
 
 202  18
         builder.add("catch (java.rmi.RemoteException ex)");
 203  18
         builder.begin();
 204   
 
 205  18
         builder.addln("if (first)");
 206  18
         builder.begin();
 207   
 
 208  18
         builder.addln("_handleRemoteException(ex);");
 209  18
         builder.addln("first = false;");
 210   
 
 211  18
         builder.end(); // if
 212  18
         builder.addln("else");
 213  18
         builder.add("  throw ex;");
 214  18
         builder.end(); // catch
 215  18
         builder.end(); // while
 216  18
         builder.end();
 217   
 
 218  18
         classFab.addMethod(Modifier.PUBLIC, new MethodSignature(m), builder.toString());
 219   
     }
 220   
 
 221  3
     private void addToStringMethod(
 222   
         ClassFab classFab,
 223   
         Class serviceInterface,
 224   
         String serviceId,
 225   
         String jndiName)
 226   
     {
 227  3
         ClassFabUtils.addToStringMethod(
 228   
             classFab,
 229   
             ImplMessages.ejbProxyDescription(serviceId, serviceInterface, jndiName));
 230   
     }
 231   
 
 232  3
     private void addConstructor(ClassFab classFab)
 233   
     {
 234  3
         classFab.addConstructor(
 235   
             new Class[] { NameLookup.class, RemoteExceptionCoordinator.class },
 236   
             null,
 237   
             "super($1, $2);");
 238   
     }
 239   
 
 240  3
     private Object invokeConstructor(Class proxyClass, NameLookup nameLookup)
 241   
     {
 242  3
         try
 243   
         {
 244  3
             Constructor c =
 245   
                 proxyClass.getConstructor(
 246   
                     new Class[] { NameLookup.class, RemoteExceptionCoordinator.class });
 247   
 
 248  3
             return c.newInstance(new Object[] { nameLookup, _coordinator });
 249   
         }
 250   
         catch (Exception ex)
 251   
         {
 252  0
             throw new ApplicationRuntimeException(ex);
 253   
         }
 254   
     }
 255   
 
 256  3
     public void setPointId(String string)
 257   
     {
 258  3
         _pointId = string;
 259   
     }
 260   
 
 261  3
     public void setClassFactory(ClassFactory factory)
 262   
     {
 263  3
         _classFactory = factory;
 264   
     }
 265   
 
 266  3
     public void setCoordinator(RemoteExceptionCoordinator coordinator)
 267   
     {
 268  3
         _coordinator = coordinator;
 269   
     }
 270   
 
 271  3
     public void setNameLookup(NameLookup lookup)
 272   
     {
 273  3
         _nameLookup = lookup;
 274   
     }
 275   
 
 276   
 }
 277