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