Commit 9ce9f362 authored by Gwenael Cattez's avatar Gwenael Cattez

Refactoring on ResourceUtil component

parent fc966ed6
......@@ -119,6 +119,15 @@ public interface FractalUtilItf
*/
public boolean isSCAComponent(Component component);
/**
* Check is a fractal interface is a controller interface
* ie if it's a server interface named *-controller
*
* @param itf fractal interface
* @return true if fractal interface is a controller interface, false otherwise
*/
public boolean isFractalControllerInterface(Interface itf);
/**
* Get a reference to the fractal component designated by id.
*
......@@ -163,9 +172,8 @@ public interface FractalUtilItf
*
* @param owner the component to introspect
* @return a list of all subComponents
* @throws NoSuchInterfaceException
*/
public List<Component> getSubComponents(Component parent) throws NoSuchInterfaceException ;
public List<Component> getSubComponents(Component parent);
/**
* Get a reference to the fractal Component named subComponentName owner by fractal Component parent
......
......@@ -353,15 +353,7 @@ public class RemoteScaDomainImpl implements RemoteScaDomain
logEntering("getSubComponents", componentId);
org.objectweb.fractal.api.Component parent = this.getFcComponent(componentId);
List<org.objectweb.fractal.api.Component> subComponents;
try
{
subComponents = fractalUtil.getSubComponents(parent);
} catch (NoSuchInterfaceException e)
{
throw new MyWebApplicationException("The component " + componentId + " is a primitive component so it have no subcomponents");
}
List<org.objectweb.fractal.api.Component> subComponents = fractalUtil.getSubComponents(parent);
Collection<Component> children = new ArrayList<Component>();
Component child;
for (org.objectweb.fractal.api.Component subComponent : subComponents)
......
......@@ -337,6 +337,16 @@ public class FractalUtilImpl implements FractalUtilItf
}
}
/**
* @see org.ow2.frascati.remote.introspection.util.FractalUtilItf#isFractalControllerInterface(org.objectweb.fractal.api.Interface)
*/
public boolean isFractalControllerInterface(Interface itf)
{
InterfaceType itfType=(InterfaceType) itf.getFcItfType();
String interfaceName=itf.getFcItfName();
return (!itfType.isFcClientItf())&&(interfaceName.endsWith("-controller"));
}
/**
* Get the SCAPropertyController related to component
*
......@@ -416,15 +426,19 @@ public class FractalUtilImpl implements FractalUtilItf
* @return a list of all subComponents
* @throws NoSuchInterfaceException
*/
public List<Component> getSubComponents(Component parent) throws NoSuchInterfaceException
public List<Component> getSubComponents(Component parent)
{
logEntering("getSubComponents", parent);
ContentController parentContentController = Fractal.getContentController(parent);
if (parentContentController == null)
List<Component> subComponents=new ArrayList<Component>();
try
{
ContentController parentContentController = Fractal.getContentController(parent);
subComponents = Arrays.asList(parentContentController.getFcSubComponents());
}
catch(NoSuchInterfaceException noSuchInterfaceException)
{
throw new NoSuchControllerDescriptorException("Can not find ContentController for parent component");
logger.fine("no content controller found for component "+this.getComponentName(parent));
}
List<Component> subComponents = Arrays.asList(parentContentController.getFcSubComponents());
logExiting("getSubComponent", subComponents);
return subComponents;
}
......
......@@ -65,7 +65,7 @@ import org.ow2.frascati.tinfi.api.control.SCAPropertyController;
* Utility class used to convert objects from Fractal API to REST (SCA)
* resources.
*
* @author Christophe Demarey
* @author Gwenael Cattez
*/
public class ResourceUtilImpl implements ResourceUtilItf
{
......@@ -85,29 +85,15 @@ public class ResourceUtilImpl implements ResourceUtilItf
public Component getComponentResource(org.objectweb.fractal.api.Component comp)
{
Component compResource = new Component();
try
{
String name = Fractal.getNameController(comp).getFcName();
compResource.setName(name);
} catch (NoSuchInterfaceException e)
{
logger.warning("Cannot find a name controller on " + comp);
}
try
String componentName=fractalUtil.getComponentName(comp);
compResource.setName(componentName);
String componentStatus=fractalUtil.getComponentStatus(comp);
if (componentStatus.equals(ComponentStatus.STARTED.value()))
{
String status = Fractal.getLifeCycleController(comp).getFcState();
if (status.compareTo(LifeCycleController.STARTED) == 0)
{
compResource.setStatus(ComponentStatus.STARTED);
} else
{
compResource.setStatus(ComponentStatus.STOPPED);
}
} catch (NoSuchInterfaceException e)
{
logger.warning("Cannot find a lifecycle controller on " + comp);
compResource.setStatus(ComponentStatus.STARTED);
} else
{
compResource.setStatus(ComponentStatus.STOPPED);
}
return compResource;
}
......@@ -117,189 +103,125 @@ public class ResourceUtilImpl implements ResourceUtilItf
*/
public Component getFullComponentResource(org.objectweb.fractal.api.Component comp)
{
Component compResource = null;
if (comp != null)
if(comp==null)
{
compResource = getComponentResource(comp);
NameController componentNameController = null;
// Sub components => use RemoteScaDomainImpl.getSubComponents ?
try
{
componentNameController = Fractal.getNameController(comp);
ContentController contentController = Fractal.getContentController(comp);
for (org.objectweb.fractal.api.Component child : contentController.getFcSubComponents())
{
NameController childNameController = null;
try
{
childNameController = Fractal.getNameController(child);
child.getFcInterface(FractalUtilImpl.SCA_COMPONENT_CONTEXT_CTRL_ITF_NAME);
compResource.getComponents().add(getComponentResource(child));
} catch (NoSuchInterfaceException e)
{
/*
* The sca-component-controller cannot be found! This
* component is not a SCA component => Nothing to do!
*/
if (childNameController != null)
{
logger.fine("sca-component-controller cannot be found for component " + childNameController.getFcName()
+ ", this component must not be a SCA component");
}
}
}
} catch (NoSuchInterfaceException e)
logger.fine("getFullComponentResource null component");
return null;
}
Component compResource = this.getComponentResource(comp);
List<org.objectweb.fractal.api.Component> subComponents=fractalUtil.getSubComponents(comp);
for(org.objectweb.fractal.api.Component subComponent : subComponents)
{
if(fractalUtil.isSCAComponent(subComponent))
{
// No sub-components
if (componentNameController != null)
{
// log.info("no sub-component found for component " +
// componentNameController.getFcName());
}
compResource.getComponents().add(getComponentResource(subComponent));
}
// Get SCA Services // Get SCA References
getScaInterfaces(comp, compResource);
// Get SCA properties
addPropertiesResource(comp, compResource.getProperties());
}
// Get SCA Services // Get SCA References
this.addPortResources(comp, compResource);
// Get SCA properties
this.addPropertiesResource(comp, compResource.getProperties());
return compResource;
}
private Port getPortFromCollectionInterfaceType(InterfaceType itfType)
private void addPortResources(org.objectweb.fractal.api.Component comp, Component compResource)
{
Port port = new Port();
port.setName(itfType.getFcItfName());
String signature = itfType.getFcItfSignature();
if (itfType.isFcOptionalItf())
{
port.setMutiplicity(Multiplicity._0N);
} else
{
port.setMutiplicity(Multiplicity._1N);
}
org.ow2.frascati.remote.introspection.resources.Interface rItf = new org.ow2.frascati.remote.introspection.resources.Interface();
rItf.setClazz(signature);
List<org.ow2.frascati.remote.introspection.resources.Method> methods = rItf.getMethods();
List<org.ow2.frascati.remote.introspection.resources.Parameter> parameters;
org.ow2.frascati.remote.introspection.resources.Parameter parameter;
int index;
try
logger.fine("addPortResources "+fractalUtil.getComponentName(comp));
Interface itf= null;
InterfaceType itfType=null;
for(Object itfObject : comp.getFcInterfaces())
{
Class<?> interfaceClass = itfType.getClass().getClassLoader().loadClass(signature);
java.lang.reflect.Method[] jMethods = interfaceClass.getMethods();
for (java.lang.reflect.Method m : jMethods)
if(comp==itfObject)
{
org.ow2.frascati.remote.introspection.resources.Method method = new Method();
method.setName(m.getName());
parameters = method.getParameters();
index = 0;
for (Class<?> param : m.getParameterTypes())
{
parameter = new Parameter();
parameter.setName("Parameter" + index);
parameter.setType(param.getName());
parameters.add(parameter);
index++;
}
methods.add(method);
continue;
}
} catch (ClassNotFoundException e)
{
throw new MyWebApplicationException("interface : " + itfType.getFcItfName() + ", can't load class for signature " + signature);
}
// SCA Interface
port.setImplementedInterface(rItf);
// SCA intents
return port;
}
private void getScaInterfaces(org.objectweb.fractal.api.Component comp, Component compResource)
{
// Introspect component interfaces.
for (Object itfObject : comp.getFcInterfaces())
{
if (!comp.equals(itfObject))
itf = (Interface) itfObject;
logger.fine("interface "+itf.getFcItfName());
itfType = (InterfaceType) itf.getFcItfType();
if(itfType.isFcCollectionItf())
{
Interface itf = (Interface) itfObject;
InterfaceType itfType = (InterfaceType) itf.getFcItfType();
/*
* if the interface is a multiple reference we check that it is
* really binded
*/
if (itfType.isFcCollectionItf())
{
try
{
BindingController compBindingController = Fractal.getBindingController(comp);
if (compBindingController.lookupFc(itf.getFcItfName()) == null)
{
continue;
}
compResource.getReferences().add(getPortResource(itf));
} catch (NoSuchInterfaceException e)
{
continue;
}
} else if (itfType.isFcClientItf())
{
compResource.getReferences().add(getPortResource(itf));
} else
Object serverObject=fractalUtil.getServerInterface(itf);
if(serverObject==null)
{
if (!itf.getFcItfName().endsWith("controller"))
{
compResource.getServices().add(getPortResource(itf));
}
/**if the fractal interface is multiple but not bound it's the multiple reference fractal definition*/
logger.fine("interface "+itf.getFcItfName()+" is not bounded collection");
continue;
}
}
}
// Add the collection interfaces.
ComponentType compType = (ComponentType) comp.getFcType();
boolean referenceAlreadyFound;
for (InterfaceType itfType : compType.getFcInterfaceTypes())
{
if (itfType.isFcCollectionItf() && itfType.isFcClientItf())
if(itfType.isFcClientItf())
{
// Check the collection is not already in the references
referenceAlreadyFound = false;
for (Port reference : compResource.getReferences())
{
if (reference.getName().equals(itfType.getFcItfName()))
{
referenceAlreadyFound = true;
}
}
if (!referenceAlreadyFound)
logger.fine("add interface "+itf.getFcItfName()+" to reference resources");
compResource.getReferences().add(getPortResource(itf));
} else
{
if(!fractalUtil.isFractalControllerInterface(itf))
{
compResource.getReferences().add(getPortFromCollectionInterfaceType(itfType));
logger.fine("add interface "+itf.getFcItfName()+" to service resources");
compResource.getServices().add(getPortResource(itf));
}
}
}
//TODO not bounded reference multiple
// // Add the collection interfaces.TODO remove this
// ComponentType compType = (ComponentType) comp.getFcType();
// boolean referenceAlreadyFound;
// for (InterfaceType compInterfaceType : compType.getFcInterfaceTypes())
// {
// if (compInterfaceType.isFcCollectionItf() && compInterfaceType.isFcClientItf())
// {
// // Check the collection is not already in the references
// referenceAlreadyFound = false;
// for (Port reference : compResource.getReferences())
// {
// if (reference.getName().equals(itfType.getFcItfName()))
// {
// referenceAlreadyFound = true;
// }
// }
// if (!referenceAlreadyFound)
// {
// compResource.getReferences().add(getPortResource(itfType));
// }
// }
// }
}
/**
* @see org.ow2.frascati.remote.introspection.util.ResourceUtilItf#getPortResource(org.objectweb.fractal.api.Interface)
*/
public Port getPortResource(org.objectweb.fractal.api.Interface itf)
{
logger.fine("getPortResource "+fractalUtil.getComponentName(itf.getFcItfOwner())+"/"+itf.getFcItfName());
Port port = new Port();
port.setName(itf.getFcItfName());
InterfaceType itfType=(InterfaceType) itf.getFcItfType();
Port port=this.getPortResource(itfType, itf.getClass().getClassLoader());
InterfaceType itfType = ((InterfaceType) itf.getFcItfType());
// SCA bindings
addBindingsResource(itf, port.getBindings());
return port;
}
private Port getPortResource(InterfaceType itfType)
{
return this.getPortResource(itfType, itfType.getClass().getClassLoader());
}
private Port getPortResource(InterfaceType itfType, ClassLoader classloader)
{
Port port = new Port();
port.setName(itfType.getFcItfName());
String signature = itfType.getFcItfSignature();
if (itfType.isFcClientItf())
{
if (itfType.isFcOptionalItf())
......@@ -325,51 +247,53 @@ public class ResourceUtilImpl implements ResourceUtilItf
}
org.ow2.frascati.remote.introspection.resources.Interface rItf = new org.ow2.frascati.remote.introspection.resources.Interface();
String signature = itfType.getFcItfSignature();
rItf.setClazz(signature);
List<org.ow2.frascati.remote.introspection.resources.Method> methods = rItf.getMethods();
port.setImplementedInterface(rItf);
addMethodsResource(signature, classloader, rItf.getMethods());
return port;
}
/**
* @param signature
* @param classLoader
* @param methods
*/
private void addMethodsResource(String signature, ClassLoader classLoader, List<Method> methods)
{
Class<?> interfaceClass=null;
try
{
interfaceClass = classLoader.loadClass(signature);
}
catch (ClassNotFoundException e)
{
logger.warning("can't load class for signature " + signature);
return;
}
Method method;
List<org.ow2.frascati.remote.introspection.resources.Parameter> parameters;
org.ow2.frascati.remote.introspection.resources.Parameter parameter;
int index;
try
for (java.lang.reflect.Method m : interfaceClass.getMethods())
{
Class<?> interfaceClass = itf.getClass().getClassLoader().loadClass(signature);
java.lang.reflect.Method[] jMethods = interfaceClass.getMethods();
for (java.lang.reflect.Method m : jMethods)
method = new Method();
method.setName(m.getName());
parameters = method.getParameters();
index = 0;
for (Class<?> param : m.getParameterTypes())
{
org.ow2.frascati.remote.introspection.resources.Method method = new Method();
method.setName(m.getName());
parameters = method.getParameters();
index = 0;
for (Class<?> param : m.getParameterTypes())
{
parameter = new Parameter();
parameter.setName("Parameter" + index);
parameter.setType(param.getName());
parameters.add(parameter);
index++;
}
methods.add(method);
parameter = new Parameter();
parameter.setName("Parameter" + index);
parameter.setType(param.getName());
parameters.add(parameter);
index++;
}
} catch (ClassNotFoundException e)
{
throw new MyWebApplicationException("interface : " + itf.getFcItfName() + ", can't load class for signature " + signature);
methods.add(method);
}
// SCA Interface
port.setImplementedInterface(rItf);
// SCA bindings
addBindingsResource(itf, port.getBindings());
// SCA intents
return port;
}
/**
* Get SCA bindings defined on a SCA service or reference.
*
......
......@@ -72,7 +72,8 @@ import org.ow2.frascati.util.FrascatiException;
* Order of the Class in the suite depending of the dependencies
*/
@RunWith(Suite.class)
@SuiteClasses({DeploymentTest.class, FractalUtilTest.class,SCAModelUtilTest.class,BindingManagerTest.class,ResourceUtilTest.class})
//@SuiteClasses({DeploymentTest.class, FractalUtilTest.class,SCAModelUtilTest.class,BindingManagerTest.class,ResourceUtilTest.class})
@SuiteClasses({ResourceUtilTest.class})
public class IntrospectionTestSuite
{
/** REST binding URI */
......
......@@ -42,7 +42,7 @@ import org.ow2.frascati.remote.introspection.binding.processor.BindingProcessorI
import org.ow2.frascati.remote.introspection.binding.processor.BindingProcessorTest;
import org.ow2.frascati.remote.introspection.binding.processor.JMSBindingProcessorTest;
import org.ow2.frascati.remote.introspection.binding.processor.JSONRPCBindingProcessorTest;
import org.ow2.frascati.remote.introspection.binding.processor.RESTBindingProcessorTest;
import org.ow2.frascati.remote.introspection.binding.processor.RestBindingProcessorTest;
import org.ow2.frascati.remote.introspection.binding.processor.SCABindingProcessorTest;
import org.ow2.frascati.remote.introspection.binding.processor.UPnPBindingProcessorTest;
import org.ow2.frascati.remote.introspection.binding.processor.WSBindingProcessorTest;
......@@ -183,7 +183,7 @@ public class BindingManagerTest
@Test
public void restBindingProcessor() throws NoSuchInterfaceException, UnsupportedBindingComponentException, UnsupportedBindingException
{
RESTBindingProcessorTest restBindingProcessorTest=new RESTBindingProcessorTest();
RestBindingProcessorTest restBindingProcessorTest=new RestBindingProcessorTest();
restBindingProcessorTest.initTest(fractalUtil);
this.getBindinProcessor(restBindingProcessorTest);
this.getClientBindingBundle(restBindingProcessorTest);
......
......@@ -35,7 +35,7 @@ import org.ow2.frascati.remote.introspection.resources.BindingKind;
/**
*
*/
public class RESTBindingProcessorTest extends BindingProcessorTest
public class RestBindingProcessorTest extends BindingProcessorTest
{
/**
......@@ -43,7 +43,7 @@ public class RESTBindingProcessorTest extends BindingProcessorTest
* @param serverInterfacePath
* @param clientInterfacePath
*/
public RESTBindingProcessorTest()
public RestBindingProcessorTest()
{
super(BindingKind.REST, "counter-server/server/counter","counter-client/client/counter");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment