Commit e2e26ea5 authored by Lionel Seinturier's avatar Lionel Seinturier
Browse files

COMP mode: Check that the content class implements the server interfaces.

parent 9b79c886
......@@ -54,8 +54,8 @@ implements SCAContentController {
public void setFcContent( Object content )
throws IllegalLifeCycleException, ContentInstantiationException {
Type type = _this_weaveableC.getFcType();
Class<?> cl = content.getClass();
checkType(type,cl);
Class<?> c = content.getClass();
checkType(type,c);
_super_setFcContent(content);
}
......
......@@ -34,4 +34,5 @@ import org.objectweb.fractal.julia.BasicControllerTrait
*/
trait SCAContentControllerTrait extends BasicControllerTrait
with RequestContextSCAContentTrait with SCABasicContentControllerTrait
with LifeCycleSCAContentTrait with SCAExtendedContentController
with TypeCheckSCAContentTrait with LifeCycleSCAContentTrait
with SCAExtendedContentController
/***
* OW2 FraSCAti Tinfi
* Copyright (C) 2012 INRIA, USTL
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact: frascati@ow2.org
*
* Author: Lionel Seinturier
*/
package org.ow2.frascati.tinfi.control.content
import org.oasisopen.sca.RequestContext
import org.objectweb.fractal.julia.UseComponentTrait
import org.objectweb.fractal.api.Type
import org.objectweb.fractal.api.`type`.ComponentType
import org.ow2.frascati.tinfi.api.control.ContentInstantiationException
/**
* Request context management.
*
* @author Lionel <Lionel.Seinturier@univ-lille1.fr>
* @since 1.4.5
*/
trait TypeCheckSCAContentTrait extends SCABasicContentControllerTrait
with UseComponentTrait {
// -------------------------------------------------------------------------
// Implementation of the SCAContentController interface
// -------------------------------------------------------------------------
override def setFcContentClass( c: Class[_] ) = {
val typ = weaveableC.getFcType
checkType(typ,c)
super.setFcContentClass(c)
}
override def setFcContent( content: Object ) = {
val typ = weaveableC.getFcType
val c = content.getClass
checkType(typ,c)
super.setFcContent(content)
}
// -------------------------------------------------------------------------
// Implementation specific
// -------------------------------------------------------------------------
private def checkType( typ: Type, contentClass: Class[_] ) = {
/*
* Check that the content class implements the server interfaces.
*/
val ct = typ.asInstanceOf[ComponentType]
val its = ct.getFcInterfaceTypes
its.foreach( it => {
var continue = false
// Skip client interfaces
if( it.isFcClientItf ) {
continue = true
}
// Skip control interfaces
val name = it.getFcItfName
if( name.equals("component") || name.endsWith("-controller") ) {
continue = true
}
if( !continue ) {
/*
* Workaround since Object.class.getClassLoader() returns null.
* Object.class as a content class is not a priori a useful case,
* but it uses it in test cases, and anyway the workaround is needed
* if Object.class is used elsewhere.
*/
val signature = it.getFcItfSignature
if( contentClass.equals(classOf[Object]) ) {
val msg =
"Object.class is not adequate since the content class "+
"should implement "+signature;
throw new ContentInstantiationException(msg)
}
// Check that the class implements the server interface
val cl = load(contentClass,signature)
if( ! cl.isAssignableFrom(contentClass) ) {
val msg = contentClass+" should implement "+signature
throw new ContentInstantiationException(msg)
}
}
})
}
/**
* Load the class whose name is specified with the class loader of the
* specified class.
*
* @param main the class used to retrieve the class loader
* @param name the name of the class to load
* @throws ContentInstantiationException if the class cannot be loader
*/
private def load( main: Class[_], name: String ) : Class[_] = {
val loader = main.getClassLoader
try {
return loader.loadClass(name);
} catch {
case cnfe : ClassNotFoundException =>
throw new ContentInstantiationException(cnfe)
}
}
}
......@@ -137,6 +137,32 @@ public class SCAContentControllerTestCase {
catch( IllegalLifeCycleException ilce ) {}
}
/**
* Test that when the content class of <code>scaPrimitive</code> components
* is changed, the new class implements the server interfaces.
*
* @since 1.4.5
*/
@Test
public void testSetContentClassImplementsServices()
throws IllegalLifeCycleException, NoSuchInterfaceException {
// Start and invoke the component once
Fractal.getLifeCycleController(client).startFc();
r.getContentClass();
// Reconfigure the content class
Fractal.getLifeCycleController(client).stopFc();
SCAContentController scacc = (SCAContentController)
client.getFcInterface(SCAContentController.NAME);
try {
scacc.setFcContentClass(Object.class);
String msg = "Class must implement server interfaces";
Assert.fail(msg);
}
catch( ContentInstantiationException cie ) {}
}
/**
* @since 1.4.3
*/
......@@ -219,4 +245,27 @@ public class SCAContentControllerTestCase {
}
catch( ContentInstantiationException cie ) {}
}
/**
* @since 1.4.5
*/
@Test
public void testSetContentImplementsServices()
throws IllegalLifeCycleException, NoSuchInterfaceException {
// Check that the component is instantiated with the regular content instance
String value = r.getProp();
Assert.assertEquals(SCAContentControllerImpl1.DEFAULT,value);
Fractal.getLifeCycleController(client).stopFc();
// Associate the ad hoc content instance with the component
SCAContentController scacc = (SCAContentController)
client.getFcInterface(SCAContentController.NAME);
try {
scacc.setFcContent(new Object());
String msg = "Object must implement services";
Assert.fail(msg);
}
catch( ContentInstantiationException cie ) {}
}
}
......@@ -157,9 +157,7 @@ public class SCAContentControllerTestCase {
client.getFcInterface(SCAContentController.NAME);
try {
scacc.setFcContentClass(Object.class);
String msg =
"Classes that do not implement server interfaces should not "+
"be allowed";
String msg = "Class must implement server interfaces";
Assert.fail(msg);
}
catch( ContentInstantiationException cie ) {}
......@@ -265,9 +263,7 @@ public class SCAContentControllerTestCase {
client.getFcInterface(SCAContentController.NAME);
try {
scacc.setFcContent(new Object());
String msg =
"Objects that do not implement server interfaces should not "+
"be allowed";
String msg = "Object must implement services";
Assert.fail(msg);
}
catch( ContentInstantiationException cie ) {}
......
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