Clover coverage report - Maven Clover report
Coverage timestamp: Tue Sep 16 2008 01:16:37 EEST
file stats: LOC: 177   Methods: 6
NCLOC: 118   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
IDReference.java 0% 0% 0% 0%
coverage
 1    /*
 2    wsmo4j - a WSMO API and Reference Implementation
 3   
 4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
 5    University of Innsbruck, Austria
 6   
 7    This library is free software; you can redistribute it and/or modify it under
 8    the terms of the GNU Lesser General Public License as published by the Free
 9    Software Foundation; either version 2.1 of the License, or (at your option)
 10    any later version.
 11    This library is distributed in the hope that it will be useful, but WITHOUT
 12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 13    FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 14    details.
 15    You should have received a copy of the GNU Lesser General Public License along
 16    with this library; if not, write to the Free Software Foundation, Inc.,
 17    59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 18    */
 19   
 20    package com.ontotext.wsmo4j.factory;
 21   
 22    import java.lang.reflect.Method;
 23    import java.lang.reflect.InvocationHandler;
 24    import java.lang.reflect.Proxy;
 25    import java.util.*;
 26   
 27    import org.omwg.ontology.*;
 28    import org.wsmo.common.Identifier;
 29    import org.wsmo.common.Entity;
 30    import org.wsmo.common.exception.SynchronisationException;
 31   
 32    import org.wsmo.factory.Factory;
 33   
 34    /**
 35    * <p>Title: </p>
 36    * <p>Description: </p>
 37    * <p>Copyright: Copyright (c) 2004-2005</p>
 38    * <p>Company: Ontotext Lab., Sirma AI</p>
 39    * @author unascribed
 40    * @version 1.0
 41    */
 42   
 43    public class IDReference implements InvocationHandler {
 44    private Identifier handle;
 45   
 46    private Entity ref = null;
 47   
 48    private LinkedList invokeQueue;
 49   
 50    private Class[] supportedInterfaces;
 51   
 52  0 private IDReference(Identifier handle, Class[] ifaces) {
 53  0 this.handle = handle;
 54  0 this.supportedInterfaces = ifaces;
 55  0 invokeQueue = new LinkedList();
 56    }
 57   
 58  0 public boolean isResolved() {
 59  0 return ref != null;
 60    }
 61   
 62  0 Class[] getSupportedInterfaces() {
 63  0 return supportedInterfaces;
 64    }
 65   
 66  0 void setDelegate(Entity delegate) {
 67  0 if (ref != null
 68    && 0 == delegate.getIdentifier().toString().compareTo(
 69    ref.getIdentifier().toString()))
 70  0 return;
 71  0 if (delegate instanceof Proxy)
 72  0 throw new RuntimeException("cannot set a Proxy as Delegate!");
 73  0 ref = delegate;
 74  0 for (Iterator i = invokeQueue.iterator(); i.hasNext();) {
 75  0 try {
 76  0 Object[] args = (Object[]) i.next();
 77  0 invoke(args[0], (Method) args[1], (Object[]) args[2]);
 78    }
 79    catch (Throwable t) {
 80  0 throw new RuntimeException("Could not apply the proxy call history!", t);
 81    }
 82    }
 83  0 invokeQueue.clear();
 84    }
 85   
 86  0 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 87    // handle Entity.getIdentifier();
 88  0 if (0 == method.getName().compareTo("getIdentifier"))
 89  0 return handle;
 90    // route since we have a deleagte already set
 91  0 if (ref != null) {
 92  0 return method.invoke(ref, args);
 93    }
 94  0 if (0 == method.getName().compareTo("hashCode")) {
 95  0 return method.invoke(handle, args);
 96    }
 97  0 if (0 == method.getName().compareTo("equals")) {
 98  0 if (args[0] instanceof Proxy) {
 99  0 Object temp = Proxy.getInvocationHandler(args[0]);
 100  0 if (temp instanceof IDReference) {
 101  0 IDReference invocHandler = (IDReference) temp;
 102  0 if (invocHandler.getSupportedInterfaces() == getSupportedInterfaces()
 103    && invocHandler.handle.equals(handle)) {
 104  0 return Boolean.TRUE;
 105    }
 106    }
 107    }
 108  0 return Boolean.FALSE;
 109    }
 110    // handle Object's public methods 'hashCode', 'toString', equals
 111  0 if (method.getDeclaringClass().equals(Object.class)) {
 112    // if (ref != null)
 113    // return method.invoke(ref, args);
 114  0 return method.invoke(handle, args);
 115    }
 116   
 117    // try to resolve it
 118  0 Entity temp = null;
 119  0 for (int i = 0; i < supportedInterfaces.length; i++) {
 120  0 temp = Factory.getLocatorManager().lookup(handle, supportedInterfaces[i]);
 121  0 if (temp != null) {
 122  0 setDelegate(temp);
 123  0 return method.invoke(ref, args);
 124    }
 125    }
 126   
 127    // if not resolved try to work with the proxy
 128  0 if (method.getReturnType() == void.class) {
 129  0 invokeQueue.add(new Object[] { proxy, method, args });
 130  0 return null;
 131    }
 132  0 if (method.getReturnType() == Set.class) {
 133  0 return new LinkedHashSet();
 134    }
 135  0 if (method.getReturnType() == List.class) {
 136  0 return new LinkedList();
 137    }
 138  0 if (method.getReturnType() == Map.class) {
 139  0 return new HashMap();
 140    }
 141  0 if (method.getReturnType() == Ontology.class) {
 142  0 return null;
 143    }
 144  0 throw new SynchronisationException(handle.toString());
 145    }
 146   
 147  0 static Object createNewReference(Identifier id, Class[] ifaces) {
 148    // @todo: some runtime checks to ensure that there is no impl already created
 149    // @todo: some threadsafty should be added to avoid multiple refs with single id to be
 150    // created across the active treads
 151  0 Object obj = null;
 152    // ClassLoader loader = Thread.currentThread().getContextClassLoader();
 153  0 ClassLoader loader = Identifier.class.getClassLoader();
 154  0 try {
 155  0 obj = Proxy.newProxyInstance(loader, ifaces, new IDReference(id, ifaces));
 156    }
 157    catch (RuntimeException ia) {
 158  0 loader = null;
 159    }
 160   
 161  0 if (obj == null && loader == null)
 162  0 loader = Thread.currentThread().getContextClassLoader();
 163  0 try {
 164  0 obj = Proxy.newProxyInstance(loader, ifaces, new IDReference(id, ifaces));
 165    }
 166    catch (RuntimeException ia) {
 167  0 loader = null;
 168    }
 169   
 170  0 if (obj == null && loader == null)
 171  0 loader = ClassLoader.getSystemClassLoader();
 172   
 173  0 obj = Proxy.newProxyInstance(loader, ifaces, new IDReference(id, ifaces));
 174   
 175  0 return obj;
 176    }
 177    }