Mail Archive Home | fractal-commits List | December 2006 Index
| <-- Date Index --> | <-- Thread Index --> |
Date: Monday, December 18, 2006 @ 22:12:24
Author: seintur
Path: /cvsroot/fractal/julia/src/org/objectweb/fractal/juliak
Added: factory/AbstractGenericFactoryImpl.java
factory/CompBasedMembraneGenericFactoryImpl.java
Modified: BootstrapComponentImpl.java Juliak.java
membrane/backend/MembraneCompiler.java
Removed: factory/GenericFactoryImpl.java
Splitting the code of the component factory in two classes:
- one with code specific to component-based membranes,
- another, general one, independant of any design choice concerning the
implementation of membranes (may be objects instead of components).
--------------------------------------------------+
BootstrapComponentImpl.java | 4
Juliak.java | 4
factory/AbstractGenericFactoryImpl.java | 565 ++++++++++++
factory/CompBasedMembraneGenericFactoryImpl.java | 475 ++++++++++
factory/GenericFactoryImpl.java | 925 ---------------------
membrane/backend/MembraneCompiler.java | 6
6 files changed, 1047 insertions(+), 932 deletions(-)
Index: julia/src/org/objectweb/fractal/juliak/BootstrapComponentImpl.java
diff -u
julia/src/org/objectweb/fractal/juliak/BootstrapComponentImpl.java:1.4
julia/src/org/objectweb/fractal/juliak/BootstrapComponentImpl.java:1.5
--- julia/src/org/objectweb/fractal/juliak/BootstrapComponentImpl.java:1.4
Fri Nov 10 21:06:38 2006
+++ julia/src/org/objectweb/fractal/juliak/BootstrapComponentImpl.java Mon
Dec 18 22:12:24 2006
@@ -30,7 +30,7 @@
import org.objectweb.fractal.api.type.ComponentType;
import org.objectweb.fractal.api.type.InterfaceType;
import org.objectweb.fractal.api.type.TypeFactory;
-import org.objectweb.fractal.juliak.factory.GenericFactoryImpl;
+import org.objectweb.fractal.juliak.factory.AbstractGenericFactoryImpl;
import org.objectweb.fractal.juliak.factory.TypeFactoryImpl;
@@ -71,7 +71,7 @@
// Implementation of the GenericFactory interface
// -----------------------------------------------------------------
- private GenericFactory gf = GenericFactoryImpl.get();
+ private GenericFactory gf = AbstractGenericFactoryImpl.get();
public Component newFcInstance(
Type type, Object controllerDesc, Object contentDesc)
Index: julia/src/org/objectweb/fractal/juliak/Juliak.java
diff -u julia/src/org/objectweb/fractal/juliak/Juliak.java:1.5
julia/src/org/objectweb/fractal/juliak/Juliak.java:1.6
--- julia/src/org/objectweb/fractal/juliak/Juliak.java:1.5 Fri Nov 10
21:06:38 2006
+++ julia/src/org/objectweb/fractal/juliak/Juliak.java Mon Dec 18 22:12:24
2006
@@ -33,7 +33,7 @@
import org.objectweb.fractal.api.type.TypeFactory;
import org.objectweb.fractal.julia.type.BasicComponentType;
import org.objectweb.fractal.julia.type.BasicInterfaceType;
-import org.objectweb.fractal.juliak.factory.GenericFactoryImpl;
+import org.objectweb.fractal.juliak.factory.AbstractGenericFactoryImpl;
/**
@@ -125,7 +125,7 @@
return bootstrap;
// Create the bootstrap component
- GenericFactory gf = GenericFactoryImpl.get();
+ GenericFactory gf = AbstractGenericFactoryImpl.get();
bootstrap =
gf.newFcInstance(
getFcInstanceType(),
Index:
julia/src/org/objectweb/fractal/juliak/factory/AbstractGenericFactoryImpl.java
diff -u /dev/null
julia/src/org/objectweb/fractal/juliak/factory/AbstractGenericFactoryImpl.java:1.1
--- /dev/null Mon Dec 18 22:12:24 2006
+++
julia/src/org/objectweb/fractal/juliak/factory/AbstractGenericFactoryImpl.java
Mon Dec 18 22:12:24 2006
@@ -0,0 +1,565 @@
+/***
+ * Julia
+ * Copyright (C) 2005-2006 INRIA, France Telecom, 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: Lionel.Seinturier@xxxxxxx
+ *
+ * Author: Lionel Seinturier
+ */
+
+package org.objectweb.fractal.juliak.factory;
+
+import org.objectweb.fractal.api.Component;
+import org.objectweb.fractal.api.NoSuchInterfaceException;
+import org.objectweb.fractal.api.Type;
+import org.objectweb.fractal.api.control.BindingController;
+import org.objectweb.fractal.api.factory.GenericFactory;
+import org.objectweb.fractal.api.factory.InstantiationException;
+import org.objectweb.fractal.api.type.ComponentType;
+import org.objectweb.fractal.api.type.InterfaceType;
+import org.objectweb.fractal.julia.Julia;
+import org.objectweb.fractal.juliak.FractalHelper;
+import org.objectweb.fractal.juliak.platform.Platform;
+import org.objectweb.fractal.util.Fractal;
+
+
+/**
+ * Abstract super class for generic factories.
+ *
+ * This factory does not make any assumption on the way control membranes
+ * are implemented. This concern is deferred to subclasses.
+ *
+ * @author Lionel Seinturier <Lionel.Seinturier@xxxxxxx>
+ * @since 2.5
+ */
+public abstract class AbstractGenericFactoryImpl implements GenericFactory {
+
+ /**
+ * The property name which defines the implementation class for the
generic
+ * factory. The value must be a sub-class of GenericFactoryImpl.
+ * The default value is GenericFactoryImpl.
+ */
+ public static final String GEN_FACT_IMPL =
+ "org.objectweb.fractal.juliak.genericfactory";
+
+ /** The singleton instance of itself. */
+ private static GenericFactory singleton;
+
+ /*
+ * Initialize the singleton instance of itself.
+ */
+ static {
+ String gfClassName = System.getProperty(GEN_FACT_IMPL);
+ if( gfClassName==null || gfClassName.length()==0 ) {
+ singleton = new CompBasedMembraneGenericFactoryImpl();
+ }
+ else {
+ try {
+ Class cl = Platform.get().loadClass(gfClassName);
+ singleton = (GenericFactory) cl.newInstance();
+ }
+ catch( Exception e ) {
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+ }
+
+ /** Return the singleton instance of this class. */
+ public static GenericFactory get() {
+ return singleton;
+ }
+
+ /**
+ * The reference towards the Julia generic factory bootstrap component.
+ * Lazily created whenever an heterogeneous assembly containing Julia and
+ * AOKell components is instantiated.
+ *
+ * To obtain a Julia component from the AOKell factory, the convention is
+ * to insert the /julia/ prefix in front of the controller description
+ * (e.g. /julia/primitive).
+ */
+ private GenericFactory julia = null;
+
+
+ // -----------------------------------------------------------------
+ // Implementation of the GenericFactory interface
+ // -----------------------------------------------------------------
+
+ /**
+ * Create a new component.
+ *
+ * @param type the component type
+ * @param controllerDesc
+ * the description of the control membrane associated
+ * to the component
+ * @param contentDesc
+ * the description of the content of the component
+ */
+ public Component newFcInstance(
+ Type type, Object controllerDesc, Object contentDesc )
+ throws InstantiationException {
+
+ if( controllerDesc instanceof Component ) {
+ Component membrane = (Component) controllerDesc;
+ return newFcInstance(type, membrane, contentDesc);
+ }
+
+ /*
+ * When called from Fractal ADL, the component creation process may
be
+ * accompagnied with some hints. One of the them is the class loader
to
+ * be used. In this case, the controllerDesc parameter is an array
where
+ * the 1st element contains the class loader.
+ */
+ if( controllerDesc.getClass().isArray() ) {
+ Object[] array = (Object[]) controllerDesc;
+ Platform.get().setLoader( array[0] );
+ controllerDesc = array[1];
+ }
+
+ /*
+ * Naming convention: controller descriptions starting with /julia/
+ * correspond to components which must be isntantiated with Julia.
+ */
+ if( controllerDesc instanceof String ) {
+ String str = (String) controllerDesc;
+ if( str.startsWith("/julia/") ) {
+ String cdesc = str.substring(7);
+
+ // Instantiate Julia generic factory
+ if( julia == null ) {
+ Component boot = new Julia().newFcInstance();
+ try {
+ julia = Fractal.getGenericFactory(boot);
+ }
+ catch (NoSuchInterfaceException e) {
+ final String msg =
+ "Julia generic factory returned a
NoSuchInterfaceException for interface: " +
+ e.getMessage();
+ throw new InstantiationException(msg);
+ }
+ }
+
+ // Use Julia generic factory to instantiate the component
+ Component comp = julia.newFcInstance(type, cdesc,
contentDesc);
+ return comp;
+ }
+ }
+
+ checkFcType(type,controllerDesc);
+ checkFcControllerDesc(controllerDesc);
+ checkFcContentDesc(type,controllerDesc,contentDesc);
+
+ /*
+ * Create the content.
+ */
+ Object content = newFcContent(type,controllerDesc,contentDesc);
+
+ /*
+ * Create the membrane and bind it to the content.
+ */
+ Component compctrlitf =
+ newFcMembrane(type,controllerDesc,contentDesc,content);
+
+ return compctrlitf;
+ }
+
+
+ // -----------------------------------------------------------------
+ // Membrane related methods
+ // -----------------------------------------------------------------
+
+ /**
+ * Create a new control membrane.
+ *
+ * @param type the type of the component associated to this membrane
+ * @param controllerDesc
+ * the controller description of this membrane
+ * (typically a String such as "primitive" or any of the other
+ * registered membrane identifier)
+ * @param contentDesc
+ * the content description of the component associated
+ * to this membrane (e.g. the fully qualified name of the class
+ * implementing a primitive component)
+ * @param content
+ * the instance implementing the component associated
+ * to this membrane
+ *
+ * @return the fcinterface for the component interface
+ * exported by the given control membrane
+ */
+ abstract protected Component newFcMembrane(
+ Type type, Object controllerDesc, Object contentDesc,
+ Object content )
+ throws InstantiationException;
+
+ /**
+ * Bind a control membrane to the component defined by the given type,
+ * content description and content.
+ *
+ * @param type the type of the component associated to this membrane
+ * @param contentDesc
+ * the content description of the component associated
+ * to this membrane (e.g. the fully qualified name of the class
+ * implementing a primitive component)
+ * @param content
+ * the instance implementing the component associated
+ * to this membrane
+ * @param membrane the control membrane
+ *
+ * @return the fcinterface for the component interface
+ * exported by the given control membrane
+ */
+ abstract protected Component bindFcMembrane(
+ Type type, Object contentDesc, Object content, Component
membrane )
+ throws InstantiationException;
+
+
+ // -----------------------------------------------------------------
+ // Implementation specific
+ // -----------------------------------------------------------------
+
+ /**
+ * Create a new component.
+ *
+ * @param type the component type
+ * @param membrane the control membrane
+ * @param contentDesc the description of the content of the component
+ */
+ private Component newFcInstance(
+ Type type, Component membrane, Object contentDesc )
+ throws InstantiationException {
+
+ /*
+ * Instantiate the content.
+ * contentDesc may be null for composite components.
+ */
+ Object content = null;
+ if( contentDesc != null ) {
+ if( !(contentDesc instanceof String) ) {
+ final String msg = "contentDesc should be a String";
+ throw new InstantiationException(msg);
+ }
+ String contentDescStr = (String) contentDesc;
+ Class cl = loadClass(contentDescStr);
+ checkFcContentClassImplementsServerInterfaces(type,cl);
+ content = instanciateClass(cl);
+ }
+
+ /*
+ * Bind the membrane to the content.
+ */
+ Component compctrlitf =
+ bindFcMembrane(type,contentDesc,content,membrane);
+
+ return compctrlitf;
+ }
+
+
+ /**
+ * Instantiate the content part of a component.
+ */
+ protected Object newFcContent(
+ Type type, Object controllerDesc, Object contentDesc)
+ throws InstantiationException {
+
+ /*
+ * Adaptation for templates components.
+ * Templates are created with something like:
+ * Component cTmpl = cf.newFcInstance(
+ * cType, "flatPrimitiveTemplate",
+ * new Object[] { "flatPrimitive", "ClientImpl" });
+ */
+ if( controllerDesc instanceof String ) {
+ if( ((String)controllerDesc).endsWith("Template") ) {
+ Object[] cont = (Object[]) contentDesc;
+ contentDesc = cont[1];
+ }
+ }
+
+ if( contentDesc == null ) {
+ /*
+ * contentDesc is null for composite components (this is not
+ * mandatory, a composite can have a content, but this is the
case
+ * most of the time).
+ */
+ return null;
+ }
+
+ if( ! (contentDesc instanceof String) ) {
+ /*
+ * When contentDesc is not a String, Julia assumes that this is
not
+ * a content descriptor, but that this is the content.
+ */
+ return contentDesc;
+ }
+
+ String contentDescStr = (String) contentDesc;
+
+ /*
+ * Load the content class.
+ */
+ Class contentClass = null;
+ try {
+ contentClass = Platform.get().loadClass(contentDescStr);
+ }
+ catch(ClassNotFoundException cnfe) {
+ final String msg = "Content class "+contentDescStr+" not found";
+ throw new InstantiationException(msg);
+ }
+
+ /*
+ * Perform checkings on the content class.
+ */
+ checkFcContentClassImplementsServerInterfaces(type,contentClass);
+ if( isFcContentToBeCheckedForBC(controllerDesc) ) {
+ checkFcContentClassforBC(type,contentClass);
+ }
+
+ /*
+ * Instantiate the content class.
+ */
+ Object content = null;
+ try {
+ content = contentClass.newInstance();
+ }
+ catch(Exception e) {
+ final String msg =
+ e.getClass().getName()+" when instantiating content class "+
+ contentDescStr+". Message: "+e.getMessage();
+ throw new InstantiationException(msg);
+ }
+
+ return content;
+ }
+
+
+ // -----------------------------------------------------------------
+ // Checks
+ // -----------------------------------------------------------------
+
+ /**
+ * Check that the value given for the type is legal.
+ */
+ protected void checkFcType( Type type, Object controllerDesc )
+ throws InstantiationException {
+
+ /** type must be a component type. */
+ if( ! (type instanceof ComponentType) )
+ throw new InstantiationException(
+ "Argument type must be an instance of ComponentType");
+
+ /** The Java interfaces referenced in the type must exist. */
+ ComponentType ct = (ComponentType) type;
+ InterfaceType[] its = ct.getFcInterfaceTypes();
+ for (int i = 0; i < its.length; i++) {
+ String itSignature = its[i].getFcItfSignature();
+ try {
+ Platform.get().loadClass(itSignature);
+ }
+ catch(ClassNotFoundException cnfe) {
+ throw new InstantiationException(
+ "Interface "+itSignature+
+ " referenced in Fractal interface type "+
+ its[i].getFcItfName()+" not found");
+ }
+ }
+ }
+
+ /**
+ * Check that the given value is a legal controller description.
+ */
+ protected void checkFcControllerDesc( Object controllerDesc )
+ throws InstantiationException {
+
+ if( controllerDesc == null ) {
+ throw new InstantiationException(
+ "Argument controllerDesc must be non null");
+ }
+ }
+
+ /**
+ * Check that the value given for the content description is legal.
+ */
+ protected void checkFcContentDesc(
+ Type type, Object controllerDesc, Object contentDesc )
+ throws InstantiationException {
+
+ /** Check for primitive components. */
+ if( controllerDesc.equals("flatPrimitive") ||
+ controllerDesc.equals("primitive") ||
+ controllerDesc.equals("flatParametricPrimitive") ||
+ controllerDesc.equals("parametricPrimitive") ) {
+ if( contentDesc==null || !(contentDesc instanceof String) )
+ throw new InstantiationException(
+ "Argument contentDesc must be a non null String");
+ }
+
+ /** Check for template components. */
+ if( controllerDesc instanceof String ) {
+ if( ((String)controllerDesc).endsWith("Template") ) {
+
+ // Templates are created with something like:
+ // Component cTmpl = cf.newFcInstance(
+ // cType, "primitiveTemplate",
+ // new Object[] { "primitive", "ClientImpl" });
+
+ if( ! contentDesc.getClass().isArray() ) {
+ throw new InstantiationException(
+ "When instantiating a "+controllerDesc+
+ " contentDesc should be an array");
+ }
+
+ Object[] tab = (Object[]) contentDesc;
+ if( tab.length != 2 ) {
+ throw new InstantiationException(
+ "When instantiating a "+controllerDesc+
+ " contentDesc should be an array of size 2");
+ }
+
+ checkFcControllerDesc(tab[0]);
+ checkFcContentDesc(type,tab[0],tab[1]);
+ }
+ }
+ }
+
+ /**
+ * Check that the content class implements the server interfaces defined
in
+ * the type.
+ */
+ protected void checkFcContentClassImplementsServerInterfaces(
+ Type type, Class contentClass )
+ throws InstantiationException {
+
+ ComponentType ct = (ComponentType) type;
+ InterfaceType[] its = ct.getFcInterfaceTypes();
+ for (int i = 0; i < its.length; i++) {
+ String name = its[i].getFcItfName();
+ if( ! its[i].isFcClientItf() &&
+ FractalHelper.isFcImplementableInterface(name) ) {
+
+ String itSignature = its[i].getFcItfSignature();
+
+ // Checked before by checkFcType
+ // but we need the class here anyway
+ Class<?> itClass;
+ try {
+ itClass = Platform.get().loadClass(itSignature);
+ }
+ catch(ClassNotFoundException cnfe) {
+ throw new InstantiationException(
+ "Interface "+itSignature+
+ " referenced in Fractal interface type "+
+ its[i].getFcItfName()+" not found");
+ }
+
+ if( ! itClass.isAssignableFrom(contentClass) ) {
+ throw new InstantiationException(
+ "Component content class
"+contentClass.getName()+
+ " should implement the server interface "+
+ itSignature);
+ }
+ }
+ }
+ }
+
+ /**
+ * Return true if the content class associated to the given controller
+ * description must implement the BindingController interface when its
type
+ * defines at least one client interface.
+ */
+ protected boolean isFcContentToBeCheckedForBC( Object controllerDesc ) {
+
+ /*
+ * The content class of component controllers need not implement
+ * BindingController.
+ */
+ if( controllerDesc.equals("mPrimitive") ||
+ controllerDesc.equals("mComposite") ) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Check that the content class implements the BindingController
interface
+ * if at least one client interface is defined in its type.
+ */
+ protected void checkFcContentClassforBC( Type type, Class contentClass )
+ throws InstantiationException {
+
+ ComponentType ct = (ComponentType) type;
+ InterfaceType[] its = ct.getFcInterfaceTypes();
+ for (int i = 0; i < its.length; i++) {
+ if( its[i].isFcClientItf() ) {
+ if( ! BindingController.class.isAssignableFrom(contentClass)
) {
+ throw new InstantiationException(
+ "Content class "+contentClass.getName()+
+ " is associated to the client interface type "+
+ its[i].getFcItfName()+
+ " and must then implement the "+
+ BindingController.class.getName()+" interface");
+ }
+ }
+ }
+ }
+
+
+ // -----------------------------------------------------------------
+ // Utility methods
+ // -----------------------------------------------------------------
+
+ /**
+ * Load and instantiate the given class name.
+ */
+ private Class loadClass( String name ) throws InstantiationException {
+
+ try {
+ Class cl = Platform.get().loadClass(name);
+ return cl;
+ }
+ catch( ClassNotFoundException cnfe ) {
+ final String msg =
+ "ClassNotFoundException when instantiating class: "+name;
+ throw new InstantiationException(msg);
+ }
+ }
+
+ /**
+ * Load and instantiate the given class name.
+ */
+ private Object instanciateClass( Class cl ) throws
InstantiationException {
+
+ try {
+ Object o = cl.newInstance();
+ return o;
+ }
+ catch( java.lang.InstantiationException ie ) {
+ final String msg =
+ "java.lang.InstantiationException when instantiating class:
"+
+ cl.getName();
+ throw new InstantiationException(msg);
+ }
+ catch( IllegalAccessException iae ) {
+ final String msg =
+ "IllegalAccessException when instantiating class: "+
+ cl.getName();
+ throw new InstantiationException(msg);
+ }
+ }
+
+}
Index:
julia/src/org/objectweb/fractal/juliak/factory/CompBasedMembraneGenericFactoryImpl.java
diff -u /dev/null
julia/src/org/objectweb/fractal/juliak/factory/CompBasedMembraneGenericFactoryImpl.java:1.1
--- /dev/null Mon Dec 18 22:12:24 2006
+++
julia/src/org/objectweb/fractal/juliak/factory/CompBasedMembraneGenericFactoryImpl.java
Mon Dec 18 22:12:24 2006
@@ -0,0 +1,475 @@
+/***
+ * Julia
+ * Copyright (C) 2005-2006 INRIA, France Telecom, 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: Lionel.Seinturier@xxxxxxx
+ *
+ * Author: Lionel Seinturier
+ */
+
+package org.objectweb.fractal.juliak.factory;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.objectweb.fractal.adl.ADLException;
+import org.objectweb.fractal.adl.Factory;
+import org.objectweb.fractal.adl.FactoryFactory;
+import org.objectweb.fractal.api.Component;
+import org.objectweb.fractal.api.Interface;
+import org.objectweb.fractal.api.NoSuchInterfaceException;
+import org.objectweb.fractal.api.Type;
+import org.objectweb.fractal.api.control.ContentController;
+import org.objectweb.fractal.api.factory.GenericFactory;
+import org.objectweb.fractal.api.factory.InstantiationException;
+import org.objectweb.fractal.api.type.ComponentType;
+import org.objectweb.fractal.api.type.InterfaceType;
+import org.objectweb.fractal.julia.Controller;
+import org.objectweb.fractal.julia.InitializationContext;
+import org.objectweb.fractal.julia.type.BasicComponentType;
+import org.objectweb.fractal.julia.type.BasicInterfaceType;
+import org.objectweb.fractal.juliak.FractalHelper;
+import org.objectweb.fractal.juliak.membrane.MCompositeImpl;
+import org.objectweb.fractal.juliak.membrane.MPrimitiveImpl;
+import org.objectweb.fractal.juliak.membrane.MembraneDef;
+import org.objectweb.fractal.juliak.membrane.Membranes;
+
+
+/**
+ * A generic factory which instanciates components with a component-based
+ * membrane.
+ *
+ * @author Lionel Seinturier <Lionel.Seinturier@xxxxxxx>
+ * @since 2.5
+ */
+public class CompBasedMembraneGenericFactoryImpl
+ extends AbstractGenericFactoryImpl
+ implements GenericFactory {
+
+ // -----------------------------------------------------------------
+ // Membrane related methods
+ // -----------------------------------------------------------------
+
+ /**
+ * Create a new control membrane.
+ *
+ * @param type the type of the component associated to this membrane
+ * @param controllerDesc
+ * the controller description of this membrane
+ * (typically a String such as "primitive" or any of the other
+ * registered membrane identifier)
+ * @param contentDesc
+ * the content description of the component associated
+ * to this membrane (e.g. the fully qualified name of the class
+ * implementing a primitive component)
+ * @param content
+ * the instance implementing the component associated
+ * to this membrane
+ *
+ * @return the fcinterface for the component interface
+ * exported by the given control membrane
+ */
+ protected Component newFcMembrane(
+ Type type, Object controllerDesc, Object contentDesc,
+ Object content )
+ throws InstantiationException {
+
+ /*
+ * Control membrane for controllers, i.e. components associated to
the
+ * mPrimitive and mComposite controller descriptions.
+ */
+ if( controllerDesc.equals("mPrimitive") ) {
+ return new MPrimitiveImpl(type,content);
+ }
+ if( controllerDesc.equals("mComposite") ) {
+ return new MCompositeImpl(type,content);
+ }
+
+ /*
+ * Retrieve the membrane definition.
+ */
+ String adl = null;
+ Component membrane = null;
+ Membranes mr = Membranes.get();
+ MembraneDef mdef = mr.getMembraneDef(controllerDesc);
+ if( mdef == null ) {
+
+ /*
+ * No registered membrane for this descriptor.
+ * Assume the ADL of the membrane has been transmitted as a
+ * controller descriptor.
+ */
+ if( controllerDesc instanceof String ) {
+ adl = (String) controllerDesc;
+ membrane = newFcMembrane(adl);
+ }
+ else {
+ String msg = "Unknown controller description:
"+controllerDesc;
+ throw new InstantiationException(msg);
+ }
+ }
+ else {
+
+ /*
+ * Try to load a precompiled version of this membrane.
+ * If the membrane description has not been compiled, dynamically
+ * load the membrane.
+ */
+ try {
+ membrane = mdef.newFcStaticMembrane();
+ }
+ catch( InstantiationException ie ) {
+ adl = mdef.getAdl();
+ membrane = newFcMembrane(adl);
+ }
+ }
+
+ /*
+ * Instantiate the membrane.
+ * If a static version of the membrane can not be loaded, try to
load a
+ * dynamic one.
+ */
+
+ /*
+ * Bind the membrane to the content.
+ */
+ Component compctrlitf =
+ bindFcMembrane(type,contentDesc,content,membrane);
+
+ return compctrlitf;
+ }
+
+ /**
+ * Bind a control membrane to the component defined by the given type,
+ * content description and content.
+ *
+ * @param type the type of the component associated to this membrane
+ * @param contentDesc
+ * the content description of the component associated
+ * to this membrane (e.g. the fully qualified name of the class
+ * implementing a primitive component)
+ * @param content
+ * the instance implementing the component associated
+ * to this membrane
+ * @param membrane the control membrane
+ *
+ * @return the fcinterface for the component interface
+ * exported by the given control membrane
+ */
+ protected Component bindFcMembrane(
+ Type type, Object contentDesc, Object content, Component
membrane )
+ throws InstantiationException {
+
+ /*
+ * Check that the membrane fits all the requirements.
+ */
+ checkFcMembrane(membrane);
+
+ /*
+ * Create the initialisation context.
+ */
+ InitializationContext ic = new InitializationContext();
+ ic.controllers = new ArrayList();
+ ic.interfaces = new HashMap();
+ ic.internalInterfaces = new HashMap();
+
+ ic.interfaces.put("/content",content);
+
+ /*
+ * In the case of template components, Julia factory controller
expects
+ * ic.content to reference contentDesc (which is an array describing
+ * the component associated to the template). contentDesc may be null
+ * for composite components.
+ */
+ if( contentDesc == null ) {
+ ic.content = content;
+ }
+ else {
+ ic.content =
+ (contentDesc.getClass().isArray()) ? contentDesc : content;
+ }
+
+ /*
+ * Retrieve all controllers.
+ */
+ Map<Object,Component> ctrls = new HashMap<Object,Component>();
+ List l = new ArrayList();
+ FractalHelper.addAllSubComponents(membrane,l);
+ for (Iterator iter = l.iterator(); iter.hasNext();) {
+ Component comp = (Component) iter.next();
+
+ // composite control components may have a null content
+ Object ctrl = FractalHelper.getContent(comp);
+ if( ctrl != null ) {
+ ic.controllers.add(ctrl);
+ ctrls.put(ctrl,comp);
+ }
+ }
+
+ /*
+ * Initialize the interface and the interface type of the component
+ * controller.
+ */
+ Interface compctrlitf = (Interface)
+ FractalHelper.getFcInterface(membrane,"//component");
+ Component compctrl = (Component) compctrlitf;
+ InterfaceType srcit = (InterfaceType) compctrlitf.getFcItfType();
+ InterfaceType it = downToLevel0InterfaceType(srcit);
+ InterfaceInstanceGenerator itfgen = InterfaceInstanceGenerator.get();
+ Interface proxy = itfgen.generate(it,compctrl,compctrl,false);
+ Component proxyForCompctrl = (Component) proxy;
+ ic.interfaces.put( "component", proxyForCompctrl );
+
+ List<InterfaceType> fullITs = new ArrayList<InterfaceType>();
+ fullITs.add(it);
+
+ /*
+ * Proceed by initializing the interface and the interface type of
the
+ * other controllers.
+ */
+ Object[] itfs = membrane.getFcInterfaces();
+ for (int i = 0; i < itfs.length; i++) {
+ Interface itf = (Interface) itfs[i];
+ String name = itf.getFcItfName();
+
+ if( name.startsWith("//") && !name.equals("//component") ) {
+
+ srcit = (InterfaceType) itf.getFcItfType();
+ it = downToLevel0InterfaceType(srcit);
+
+ // Control (external) interface
+ proxy = itfgen.generate(it,proxyForCompctrl,itf,false);
+ name = name.substring(2); // strip //
+ ic.interfaces.put( name, proxy );
+
+ // Control (internal) interface
+ if( name.equals("factory") ) {
+ InterfaceType intit = newSymetricInterfaceType(it);
+ Interface internal =
itfgen.generate(intit,proxyForCompctrl,itf,true);
+ ic.internalInterfaces.put(name,internal);
+ }
+
+ // Component type
+ if( name.charAt(0) != '/' ) {
+ fullITs.add(it);
+ }
+ }
+ }
+
+ /*
+ * Build the component type by appending the business type to the
+ * already computed control type.
+ */
+ ComponentType comptype = (ComponentType) type;
+ InterfaceType[] its = comptype.getFcInterfaceTypes();
+ fullITs.addAll( Arrays.asList(its) );
+
+ InterfaceType[] full = new InterfaceType[fullITs.size()];
+ ic.type = new BasicComponentType( (InterfaceType[])
fullITs.toArray(full) );
+
+ /*
+ * Build business component interfaces and interceptors.
+ */
+ for (int i = 0; i < its.length; i++) {
+
+ String name = its[i].getFcItfName();
+
+ if( its[i].isFcCollectionItf() ) {
+ // Julia naming convention for collection interfaces
+ name = "/collection/"+name;
+ }
+
+ // External interface
+ Interface ext =
itfgen.generate(its[i],proxyForCompctrl,content,false);
+ ic.interfaces.put(name,ext);
+ if( its[i].isFcCollectionItf() ) {
+ /*
+ * Add a 2nd reference for this interface.
+ * It will be possible to retrieve this reference to
dynamically
+ * manage (add and remove) the interceptors associated to a
+ * collection interface. This is not possible with
+ * "/collection/"+name which is never returned as this by
+ * getFcInterface (a clone is always returned).
+ */
+ ic.interfaces.put("/juliak"+name,ext);
+ }
+
+ // Internal interface
+ if( ! name.equals("attribute-controller") ) {
+ InterfaceType intit = newSymetricInterfaceType(its[i]);
+ Interface internal =
+ itfgen.generate(intit,proxyForCompctrl,content,true);
+ ic.internalInterfaces.put(name,internal);
+ if( its[i].isFcCollectionItf() ) {
+ ic.internalInterfaces.put("/juliak"+name,internal);
+ }
+ }
+ }
+
+ /*
+ * Initialize controllers.
+ *
+ * Iterate on ic.controllers which is a List, to initialize the
+ * component controller first, and the ContentController second.
+ * When initialized, controllers (e.g. the interceptor controller)
may
+ * assume that the component controller has already been initialized
to
+ * retrieve for example, the component type or the array of internal
+ * fcinterfaces.
+ */
+ // First the Component control-component
+ for (Iterator iter = ic.controllers.iterator(); iter.hasNext();) {
+ Object o = iter.next();
+ if( o instanceof Component ) {
+ Controller ctrl = (Controller) o;
+ ctrl.initFcController(ic);
+ }
+ }
+
+ // Second the Content control-component
+ for (Iterator iter = ic.controllers.iterator(); iter.hasNext();) {
+ Object o = iter.next();
+ if( o instanceof ContentController ) {
+ Controller ctrl = (Controller) o;
+ Component ctrlcomp = (Component) ctrls.get(ctrl);
+ InitializationContext localic = new InitializationContext();
+ localic.content = ic.content;
+ localic.type = ic.type;
+ localic.interfaces = new ControlComponentBoundMap(ctrlcomp);
+ localic.internalInterfaces = ic.internalInterfaces;
+ ctrl.initFcController(ic);
+ }
+ }
+
+ // Remaining control-components
+ for (Iterator iter = ic.controllers.iterator(); iter.hasNext();) {
+
+ Object o = iter.next();
+
+ if( o instanceof Component || o instanceof ContentController ) {
+ continue;
+ }
+
+ if( !(o instanceof Controller) ) {
+ /*
+ * This is not mandatory for controllers to implement
+ * {@link Controller}. For example, simple controllers which
do
+ * not require any information from the initialization
context
+ * may avoid implementing {@link Controller}.
+ */
+ continue;
+ }
+
+ Controller ctrl = (Controller) o;
+ Component ctrlcomp = (Component) ctrls.get(ctrl);
+
+ InitializationContext localic = new InitializationContext();
+ localic.content = ic.content;
+ localic.type = ic.type;
+
+ /*
+ * It seems that Julia controllers do not use ic.controllers.
+ * This list seems to be only used by the generic factory.
+ */
+ localic.controllers = null;
+ localic.interfaces = new ControlComponentBoundMap(ctrlcomp);
+ localic.internalInterfaces = null;
+
+ ctrl.initFcController(localic);
+ }
+
+ return proxyForCompctrl;
+ }
+
+
+ // -----------------------------------------------------------------
+ // Implementation specific
+ // -----------------------------------------------------------------
+
+ /**
+ * Create a new control membrane by loading the given ADL description.
+ *
+ * @param adl the ADL description of the membrane to load
+ * @return the membrane
+ */
+ private Component newFcMembrane( String adl )
+ throws InstantiationException {
+
+ try {
+ Factory factory =
+ FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
+ Component membrane = (Component) factory.newComponent( adl, null
);
+ return membrane;
+ }
+ catch( ADLException e ) {
+ final String msg = "Unknown controller description: "+adl;
+ throw new InstantiationException(msg);
+ }
+ }
+
+ /**
+ * Check that the given control membrane fits all the requirements.
+ */
+ private void checkFcMembrane( Component membrane ) throws
InstantiationException {
+
+ try {
+ membrane.getFcInterface("//component");
+ }
+ catch (NoSuchInterfaceException nsie) {
+ final String msg =
+ "The given membrane should provide a "+nsie.getMessage()+
+ " interface";
+ throw new InstantiationException(msg);
+ }
+ }
+
+ /**
+ * Given the interface type of a control membrane, return the
corresponding
+ * type for the application (level 0) interface. This consists in
removing
+ * the 2 slash characters which are in front of the interface name.
+ */
+ private static InterfaceType downToLevel0InterfaceType( InterfaceType
srcit ) {
+ InterfaceType it =
+ new BasicInterfaceType(
+ srcit.getFcItfName().substring(2), // remove //
+ srcit.getFcItfSignature(),
+ srcit.isFcClientItf(),
+ srcit.isFcOptionalItf(),
+ srcit.isFcCollectionItf()
+ );
+ return it;
+ }
+
+ /**
+ * Return a copy of the given interface type where the client role is
+ * transformed into server and the server role is transformed into
client.
+ */
+ private static InterfaceType newSymetricInterfaceType( InterfaceType it
) {
+ return
+ new BasicInterfaceType(
+ it.getFcItfName(),
+ it.getFcItfSignature(),
+ ! it.isFcClientItf(), // client <-> server
+ it.isFcOptionalItf(),
+ it.isFcCollectionItf()
+ );
+ }
+
+}
Index: julia/src/org/objectweb/fractal/juliak/factory/GenericFactoryImpl.java
diff -u
julia/src/org/objectweb/fractal/juliak/factory/GenericFactoryImpl.java:1.9
julia/src/org/objectweb/fractal/juliak/factory/GenericFactoryImpl.java:removed
---
julia/src/org/objectweb/fractal/juliak/factory/GenericFactoryImpl.java:1.9
Sat Dec 16 21:01:14 2006
+++ julia/src/org/objectweb/fractal/juliak/factory/GenericFactoryImpl.java
Mon Dec 18 22:12:24 2006
@@ -1,925 +0,0 @@
-/***
- * Julia
- * Copyright (C) 2005-2006 INRIA, France Telecom, 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: Lionel.Seinturier@xxxxxxx
- *
- * Author: Lionel Seinturier
- */
-
-package org.objectweb.fractal.juliak.factory;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import org.objectweb.fractal.adl.ADLException;
-import org.objectweb.fractal.adl.Factory;
-import org.objectweb.fractal.adl.FactoryFactory;
-import org.objectweb.fractal.api.Component;
-import org.objectweb.fractal.api.Interface;
-import org.objectweb.fractal.api.NoSuchInterfaceException;
-import org.objectweb.fractal.api.Type;
-import org.objectweb.fractal.api.control.BindingController;
-import org.objectweb.fractal.api.control.ContentController;
-import org.objectweb.fractal.api.factory.GenericFactory;
-import org.objectweb.fractal.api.factory.InstantiationException;
-import org.objectweb.fractal.api.type.ComponentType;
-import org.objectweb.fractal.api.type.InterfaceType;
-import org.objectweb.fractal.julia.Controller;
-import org.objectweb.fractal.julia.InitializationContext;
-import org.objectweb.fractal.julia.Julia;
-import org.objectweb.fractal.julia.type.BasicComponentType;
-import org.objectweb.fractal.julia.type.BasicInterfaceType;
-import org.objectweb.fractal.juliak.FractalHelper;
-import org.objectweb.fractal.juliak.membrane.MCompositeImpl;
-import org.objectweb.fractal.juliak.membrane.MPrimitiveImpl;
-import org.objectweb.fractal.juliak.membrane.MembraneDef;
-import org.objectweb.fractal.juliak.membrane.Membranes;
-import org.objectweb.fractal.juliak.platform.Platform;
-import org.objectweb.fractal.util.Fractal;
-
-
-/**
- * The implementation of the generic factory.
- *
- * @author Lionel Seinturier <Lionel.Seinturier@xxxxxxx>
- * @since 2.5
- */
-public class GenericFactoryImpl implements GenericFactory {
-
- /**
- * The property name which defines the implementation class for the
generic
- * factory. The value must be a sub-class of GenericFactoryImpl.
- * The default value is GenericFactoryImpl.
- */
- public static final String GEN_FACT_IMPL =
- "org.objectweb.fractal.juliak.genericfactory";
-
- /** The singleton instance of itself. */
- private static GenericFactory singleton;
-
- /*
- * Initialize the singleton instance of itself.
- */
- static {
- String gfClassName = System.getProperty(GEN_FACT_IMPL);
- if( gfClassName==null || gfClassName.length()==0 ) {
- singleton = new GenericFactoryImpl();
- }
- else {
- try {
- Class cl = Platform.get().loadClass(gfClassName);
- singleton = (GenericFactory) cl.newInstance();
- }
- catch( Exception e ) {
- throw new RuntimeException(e.getMessage());
- }
- }
- }
-
- /** Return the singleton instance of this class. */
- public static GenericFactory get() {
- return singleton;
- }
-
- /**
- * The reference towards the Julia generic factory bootstrap component.
- * Lazily created whenever an heterogeneous assembly containing Julia and
- * AOKell components is instantiated.
- *
- * To obtain a Julia component from the AOKell factory, the convention is
- * to insert the /julia/ prefix in front of the controller description
- * (e.g. /julia/primitive).
- */
- private GenericFactory julia = null;
-
-
- // -----------------------------------------------------------------
- // Implementation of the GenericFactory interface
- // -----------------------------------------------------------------
-
- /**
- * Create a new component.
- *
- * @param type the component type
- * @param controllerDesc
- * the description of the control membrane associated
- * to the component
- * @param contentDesc
- * the description of the content of the component
- */
- public Component newFcInstance(
- Type type, Object controllerDesc, Object contentDesc )
- throws InstantiationException {
-
- if( controllerDesc instanceof Component ) {
- Component membrane = (Component) controllerDesc;
- return newFcInstance(type, membrane, contentDesc);
- }
-
- /*
- * When called from Fractal ADL, the component creation process may
be
- * accompagnied with some hints. One of the them is the class loader
to
- * be used. In this case, the controllerDesc parameter is an array
where
- * the 1st element contains the class loader.
- */
- if( controllerDesc.getClass().isArray() ) {
- Object[] array = (Object[]) controllerDesc;
- Platform.get().setLoader( array[0] );
- controllerDesc = array[1];
- }
-
- /*
- * Naming convention: controller descriptions starting with /julia/
- * correspond to components which must be isntantiated with Julia.
- */
- if( controllerDesc instanceof String ) {
- String str = (String) controllerDesc;
- if( str.startsWith("/julia/") ) {
- String cdesc = str.substring(7);
-
- // Instantiate Julia generic factory
- if( julia == null ) {
- Component boot = new Julia().newFcInstance();
- try {
- julia = Fractal.getGenericFactory(boot);
- }
- catch (NoSuchInterfaceException e) {
- final String msg =
- "Julia generic factory returned a
NoSuchInterfaceException for interface: " +
- e.getMessage();
- throw new InstantiationException(msg);
- }
- }
-
- // Use Julia generic factory to instantiate the component
- Component comp = julia.newFcInstance(type, cdesc,
contentDesc);
- return comp;
- }
- }
-
- checkFcType(type,controllerDesc);
- checkFcControllerDesc(controllerDesc);
- checkFcContentDesc(type,controllerDesc,contentDesc);
-
- /*
- * Create the content.
- */
- Object content = newFcContent(type,controllerDesc,contentDesc);
-
- /*
- * Create the membrane and bind it to the content.
- */
- Component compctrlitf =
- newFcMembrane(type,controllerDesc,contentDesc,content);
-
- return compctrlitf;
- }
-
-
- // -----------------------------------------------------------------
- // Implementation specific
- // -----------------------------------------------------------------
-
- /**
- * Create a new component.
- *
- * @param type the component type
- * @param membrane the control membrane
- * @param contentDesc the description of the content of the component
- */
- private Component newFcInstance(
- Type type, Component membrane, Object contentDesc )
- throws InstantiationException {
-
- /*
- * Instantiate the content.
- * contentDesc may be null for composite components.
- */
- Object content = null;
- if( contentDesc != null ) {
- if( !(contentDesc instanceof String) ) {
- final String msg = "contentDesc should be a String";
- throw new InstantiationException(msg);
- }
- String contentDescStr = (String) contentDesc;
- Class cl = loadClass(contentDescStr);
- checkFcContentClassImplementsServerInterfaces(type,cl);
- content = instanciateClass(cl);
- }
-
- /*
- * Bind the membrane to the content.
- */
- Component compctrlitf =
- bindFcMembrane(type,contentDesc,content,membrane);
-
- return compctrlitf;
- }
-
-
- /**
- * Create a new control membrane.
- *
- * @param type the type of the component associated to this membrane
- * @param controllerDesc
- * the controller description of this membrane
- * (typically a String such as "primitive" or any of the other
- * registered membrane identifier)
- * @param contentDesc
- * the content description of the component associated
- * to this membrane (e.g. the fully qualified name of the class
- * implementing a primitive component)
- * @param content
- * the instance implementing the component associated
- * to this membrane
- *
- * @return the fcinterface for the component interface
- * exported by the given control membrane
- */
- private Component newFcMembrane(
- Type type, Object controllerDesc, Object contentDesc,
- Object content )
- throws InstantiationException {
-
- /*
- * Control membrane for controllers, i.e. components associated to
the
- * mPrimitive and mComposite controller descriptions.
- */
- if( controllerDesc.equals("mPrimitive") ) {
- return new MPrimitiveImpl(type,content);
- }
- if( controllerDesc.equals("mComposite") ) {
- return new MCompositeImpl(type,content);
- }
-
- /*
- * Retrieve the membrane definition.
- */
- String adl = null;
- Component membrane = null;
- Membranes mr = Membranes.get();
- MembraneDef mdef = mr.getMembraneDef(controllerDesc);
- if( mdef == null ) {
-
- /*
- * No registered membrane for this descriptor.
- * Assume the ADL of the membrane has been transmitted as a
- * controller descriptor.
- */
- if( controllerDesc instanceof String ) {
- adl = (String) controllerDesc;
- membrane = newFcMembrane(adl);
- }
- else {
- String msg = "Unknown controller description:
"+controllerDesc;
- throw new InstantiationException(msg);
- }
- }
- else {
-
- /*
- * Try to load a precompiled version of this membrane.
- * If the membrane description has not been compiled, dynamically
- * load the membrane.
- */
- try {
- membrane = mdef.newFcStaticMembrane();
- }
- catch( InstantiationException ie ) {
- adl = mdef.getAdl();
- membrane = newFcMembrane(adl);
- }
- }
-
- /*
- * Instantiate the membrane.
- * If a static version of the membrane can not be loaded, try to
load a
- * dynamic one.
- */
-
- /*
- * Bind the membrane to the content.
- */
- Component compctrlitf =
- bindFcMembrane(type,contentDesc,content,membrane);
-
- return compctrlitf;
- }
-
- /**
- * Create a new control membrane by loading the given ADL description.
- *
- * @param adl the ADL description of the membrane to load
- * @return the membrane
- */
- private Component newFcMembrane( String adl )
- throws InstantiationException {
-
- try {
- Factory factory =
- FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
- Component membrane = (Component) factory.newComponent( adl, null
);
- return membrane;
- }
- catch( ADLException e ) {
- final String msg = "Unknown controller description: "+adl;
- throw new InstantiationException(msg);
- }
- }
-
- /**
- * Bind a control membrane to the component defined by the given type,
- * content description and content.
- *
- * @param type the type of the component associated to this membrane
- * @param contentDesc
- * the content description of the component associated
- * to this membrane (e.g. the fully qualified name of the class
- * implementing a primitive component)
- * @param content
- * the instance implementing the component associated
- * to this membrane
- * @param membrane the control membrane
- *
- * @return the fcinterface for the component interface
- * exported by the given control membrane
- */
- private Component bindFcMembrane(
- Type type, Object contentDesc, Object content, Component
membrane )
- throws InstantiationException {
-
- /*
- * Check that the membrane fits all the requirements.
- */
- checkFcMembrane(membrane);
-
- /*
- * Create the initialisation context.
- */
- InitializationContext ic = new InitializationContext();
- ic.controllers = new ArrayList();
- ic.interfaces = new HashMap();
- ic.internalInterfaces = new HashMap();
-
- ic.interfaces.put("/content",content);
-
- /*
- * In the case of template components, Julia factory controller
expects
- * ic.content to reference contentDesc (which is an array describing
- * the component associated to the template). contentDesc may be null
- * for composite components.
- */
- if( contentDesc == null ) {
- ic.content = content;
- }
- else {
- ic.content =
- (contentDesc.getClass().isArray()) ? contentDesc : content;
- }
-
- /*
- * Retrieve all controllers.
- */
- Map<Object,Component> ctrls = new HashMap<Object,Component>();
- List l = new ArrayList();
- FractalHelper.addAllSubComponents(membrane,l);
- for (Iterator iter = l.iterator(); iter.hasNext();) {
- Component comp = (Component) iter.next();
-
- // composite control components may have a null content
- Object ctrl = FractalHelper.getContent(comp);
- if( ctrl != null ) {
- ic.controllers.add(ctrl);
- ctrls.put(ctrl,comp);
- }
- }
-
- /*
- * Initialize the interface and the interface type of the component
- * controller.
- */
- Interface compctrlitf = (Interface)
- FractalHelper.getFcInterface(membrane,"//component");
- Component compctrl = (Component) compctrlitf;
- InterfaceType srcit = (InterfaceType) compctrlitf.getFcItfType();
- InterfaceType it = downToLevel0InterfaceType(srcit);
- InterfaceInstanceGenerator itfgen = InterfaceInstanceGenerator.get();
- Interface proxy = itfgen.generate(it,compctrl,compctrl,false);
- Component proxyForCompctrl = (Component) proxy;
- ic.interfaces.put( "component", proxyForCompctrl );
-
- List<InterfaceType> fullITs = new ArrayList<InterfaceType>();
- fullITs.add(it);
-
- /*
- * Proceed by initializing the interface and the interface type of
the
- * other controllers.
- */
- Object[] itfs = membrane.getFcInterfaces();
- for (int i = 0; i < itfs.length; i++) {
- Interface itf = (Interface) itfs[i];
- String name = itf.getFcItfName();
-
- if( name.startsWith("//") && !name.equals("//component") ) {
-
- srcit = (InterfaceType) itf.getFcItfType();
- it = downToLevel0InterfaceType(srcit);
-
- // Control (external) interface
- proxy = itfgen.generate(it,proxyForCompctrl,itf,false);
- name = name.substring(2); // strip //
- ic.interfaces.put( name, proxy );
-
- // Control (internal) interface
- if( name.equals("factory") ) {
- InterfaceType intit = newSymetricInterfaceType(it);
- Interface internal =
itfgen.generate(intit,proxyForCompctrl,itf,true);
- ic.internalInterfaces.put(name,internal);
- }
-
- // Component type
- if( name.charAt(0) != '/' ) {
- fullITs.add(it);
- }
- }
- }
-
- /*
- * Build the component type by appending the business type to the
- * already computed control type.
- */
- ComponentType comptype = (ComponentType) type;
- InterfaceType[] its = comptype.getFcInterfaceTypes();
- fullITs.addAll( Arrays.asList(its) );
-
- InterfaceType[] full = new InterfaceType[fullITs.size()];
- ic.type = new BasicComponentType( (InterfaceType[])
fullITs.toArray(full) );
-
- /*
- * Build business component interfaces and interceptors.
- */
- for (int i = 0; i < its.length; i++) {
-
- String name = its[i].getFcItfName();
-
- if( its[i].isFcCollectionItf() ) {
- // Julia naming convention for collection interfaces
- name = "/collection/"+name;
- }
-
- // External interface
- Interface ext =
itfgen.generate(its[i],proxyForCompctrl,content,false);
- ic.interfaces.put(name,ext);
- if( its[i].isFcCollectionItf() ) {
- /*
- * Add a 2nd reference for this interface.
- * It will be possible to retrieve this reference to
dynamically
- * manage (add and remove) the interceptors associated to a
- * collection interface. This is not possible with
- * "/collection/"+name which is never returned as this by
- * getFcInterface (a clone is always returned).
- */
- ic.interfaces.put("/juliak"+name,ext);
- }
-
- // Internal interface
- if( ! name.equals("attribute-controller") ) {
- InterfaceType intit = newSymetricInterfaceType(its[i]);
- Interface internal =
- itfgen.generate(intit,proxyForCompctrl,content,true);
- ic.internalInterfaces.put(name,internal);
- if( its[i].isFcCollectionItf() ) {
- ic.internalInterfaces.put("/juliak"+name,internal);
- }
- }
- }
-
- /*
- * Initialize controllers.
- *
- * Iterate on ic.controllers which is a List, to initialize the
- * component controller first, and the ContentController second.
- * When initialized, controllers (e.g. the interceptor controller)
may
- * assume that the component controller has already been initialized
to
- * retrieve for example, the component type or the array of internal
- * fcinterfaces.
- */
- // First the Component control-component
- for (Iterator iter = ic.controllers.iterator(); iter.hasNext();) {
- Object o = iter.next();
- if( o instanceof Component ) {
- Controller ctrl = (Controller) o;
- ctrl.initFcController(ic);
- }
- }
-
- // Second the Content control-component
- for (Iterator iter = ic.controllers.iterator(); iter.hasNext();) {
- Object o = iter.next();
- if( o instanceof ContentController ) {
- Controller ctrl = (Controller) o;
- Component ctrlcomp = (Component) ctrls.get(ctrl);
- InitializationContext localic = new InitializationContext();
- localic.content = ic.content;
- localic.type = ic.type;
- localic.interfaces = new ControlComponentBoundMap(ctrlcomp);
- localic.internalInterfaces = ic.internalInterfaces;
- ctrl.initFcController(ic);
- }
- }
-
- // Remaining control-components
- for (Iterator iter = ic.controllers.iterator(); iter.hasNext();) {
-
- Object o = iter.next();
-
- if( o instanceof Component || o instanceof ContentController ) {
- continue;
- }
-
- if( !(o instanceof Controller) ) {
- /*
- * This is not mandatory for controllers to implement
- * {@link Controller}. For example, simple controllers which
do
- * not require any information from the initialization
context
- * may avoid implementing {@link Controller}.
- */
- continue;
- }
-
- Controller ctrl = (Controller) o;
- Component ctrlcomp = (Component) ctrls.get(ctrl);
-
- InitializationContext localic = new InitializationContext();
- localic.content = ic.content;
- localic.type = ic.type;
-
- /*
- * It seems that Julia controllers do not use ic.controllers.
- * This list seems to be only used by the generic factory.
- */
- localic.controllers = null;
- localic.interfaces = new ControlComponentBoundMap(ctrlcomp);
- localic.internalInterfaces = null;
-
- ctrl.initFcController(localic);
- }
-
- return proxyForCompctrl;
- }
-
- /**
- * Check that the given control membrane fits all the requirements.
- */
- private void checkFcMembrane( Component membrane ) throws
InstantiationException {
-
- try {
- membrane.getFcInterface("//component");
- }
- catch (NoSuchInterfaceException nsie) {
- final String msg =
- "The given membrane should provide a "+nsie.getMessage()+
- " interface";
- throw new InstantiationException(msg);
- }
- }
-
- /**
- * Load and instantiate the given class name.
- */
- private Class loadClass( String name ) throws InstantiationException {
-
- try {
- Class cl = Platform.get().loadClass(name);
- return cl;
- }
- catch( ClassNotFoundException cnfe ) {
- final String msg =
- "ClassNotFoundException when instantiating class: "+name;
- throw new InstantiationException(msg);
- }
- }
-
- /**
- * Load and instantiate the given class name.
- */
- private Object instanciateClass( Class cl ) throws
InstantiationException {
-
- try {
- Object o = cl.newInstance();
- return o;
- }
- catch( java.lang.InstantiationException ie ) {
- final String msg =
- "java.lang.InstantiationException when instantiating class:
"+
- cl.getName();
- throw new InstantiationException(msg);
- }
- catch( IllegalAccessException iae ) {
- final String msg =
- "IllegalAccessException when instantiating class: "+
- cl.getName();
- throw new InstantiationException(msg);
- }
- }
-
- /**
- * Given the interface type of a control membrane, return the
corresponding
- * type for the application (level 0) interface. This consists in
removing
- * the 2 slash characters which are in front of the interface name.
- */
- private static InterfaceType downToLevel0InterfaceType( InterfaceType
srcit ) {
- InterfaceType it =
- new BasicInterfaceType(
- srcit.getFcItfName().substring(2), // remove //
- srcit.getFcItfSignature(),
- srcit.isFcClientItf(),
- srcit.isFcOptionalItf(),
- srcit.isFcCollectionItf()
- );
- return it;
- }
-
- /**
- * Return a copy of the given interface type where the client role is
- * transformed into server and the server role is transformed into
client.
- */
- private static InterfaceType newSymetricInterfaceType( InterfaceType it
) {
- return
- new BasicInterfaceType(
- it.getFcItfName(),
- it.getFcItfSignature(),
- ! it.isFcClientItf(), // client <-> server
- it.isFcOptionalItf(),
- it.isFcCollectionItf()
- );
- }
-
- /**
- * Instantiate the content part of a component.
- */
- protected Object newFcContent(
- Type type, Object controllerDesc, Object contentDesc)
- throws InstantiationException {
-
- /*
- * Adaptation for templates components.
- * Templates are created with something like:
- * Component cTmpl = cf.newFcInstance(
- * cType, "flatPrimitiveTemplate",
- * new Object[] { "flatPrimitive", "ClientImpl" });
- */
- if( controllerDesc instanceof String ) {
- if( ((String)controllerDesc).endsWith("Template") ) {
- Object[] cont = (Object[]) contentDesc;
- contentDesc = cont[1];
- }
- }
-
- if( contentDesc == null ) {
- /*
- * contentDesc is null for composite components (this is not
- * mandatory, a composite can have a content, but this is the
case
- * most of the time).
- */
- return null;
- }
-
- if( ! (contentDesc instanceof String) ) {
- /*
- * When contentDesc is not a String, Julia assumes that this is
not
- * a content descriptor, but that this is the content.
- */
- return contentDesc;
- }
-
- String contentDescStr = (String) contentDesc;
-
- /*
- * Load the content class.
- */
- Class contentClass = null;
- try {
- contentClass = Platform.get().loadClass(contentDescStr);
- }
- catch(ClassNotFoundException cnfe) {
- final String msg = "Content class "+contentDescStr+" not found";
- throw new InstantiationException(msg);
- }
-
- /*
- * Perform checkings on the content class.
- */
- checkFcContentClassImplementsServerInterfaces(type,contentClass);
- if( isFcContentToBeCheckedForBC(controllerDesc) ) {
- checkFcContentClassforBC(type,contentClass);
- }
-
- /*
- * Instantiate the content class.
- */
- Object content = null;
- try {
- content = contentClass.newInstance();
- }
- catch(Exception e) {
- final String msg =
- e.getClass().getName()+" when instantiating content class "+
- contentDescStr+". Message: "+e.getMessage();
- throw new InstantiationException(msg);
- }
-
- return content;
- }
-
-
- // -----------------------------------------------------------------
- // Checks
- // -----------------------------------------------------------------
-
- /**
- * Check that the value given for the type is legal.
- */
- protected void checkFcType( Type type, Object controllerDesc )
- throws InstantiationException {
-
- /** type must be a component type. */
- if( ! (type instanceof ComponentType) )
- throw new InstantiationException(
- "Argument type must be an instance of ComponentType");
-
- /** The Java interfaces referenced in the type must exist. */
- ComponentType ct = (ComponentType) type;
- InterfaceType[] its = ct.getFcInterfaceTypes();
- for (int i = 0; i < its.length; i++) {
- String itSignature = its[i].getFcItfSignature();
- try {
- Platform.get().loadClass(itSignature);
- }
- catch(ClassNotFoundException cnfe) {
- throw new InstantiationException(
- "Interface "+itSignature+
- " referenced in Fractal interface type "+
- its[i].getFcItfName()+" not found");
- }
- }
- }
-
- /**
- * Check that the given value is a legal controller description.
- */
- protected void checkFcControllerDesc( Object controllerDesc )
- throws InstantiationException {
-
- if( controllerDesc == null ) {
- throw new InstantiationException(
- "Argument controllerDesc must be non null");
- }
- }
-
- /**
- * Check that the value given for the content description is legal.
- */
- protected void checkFcContentDesc(
- Type type, Object controllerDesc, Object contentDesc )
- throws InstantiationException {
-
- /** Check for primitive components. */
- if( controllerDesc.equals("flatPrimitive") ||
- controllerDesc.equals("primitive") ||
- controllerDesc.equals("flatParametricPrimitive") ||
- controllerDesc.equals("parametricPrimitive") ) {
- if( contentDesc==null || !(contentDesc instanceof String) )
- throw new InstantiationException(
- "Argument contentDesc must be a non null String");
- }
-
- /** Check for template components. */
- if( controllerDesc instanceof String ) {
- if( ((String)controllerDesc).endsWith("Template") ) {
-
- // Templates are created with something like:
- // Component cTmpl = cf.newFcInstance(
- // cType, "primitiveTemplate",
- // new Object[] { "primitive", "ClientImpl" });
-
- if( ! contentDesc.getClass().isArray() ) {
- throw new InstantiationException(
- "When instantiating a "+controllerDesc+
- " contentDesc should be an array");
- }
-
- Object[] tab = (Object[]) contentDesc;
- if( tab.length != 2 ) {
- throw new InstantiationException(
- "When instantiating a "+controllerDesc+
- " contentDesc should be an array of size 2");
- }
-
- checkFcControllerDesc(tab[0]);
- checkFcContentDesc(type,tab[0],tab[1]);
- }
- }
- }
-
- /**
- * Check that the content class implements the server interfaces defined
in
- * the type.
- */
- protected void checkFcContentClassImplementsServerInterfaces(
- Type type, Class contentClass )
- throws InstantiationException {
-
- ComponentType ct = (ComponentType) type;
- InterfaceType[] its = ct.getFcInterfaceTypes();
- for (int i = 0; i < its.length; i++) {
- String name = its[i].getFcItfName();
- if( ! its[i].isFcClientItf() &&
- FractalHelper.isFcImplementableInterface(name) ) {
-
- String itSignature = its[i].getFcItfSignature();
-
- // Checked before by checkFcType
- // but we need the class here anyway
- Class<?> itClass;
- try {
- itClass = Platform.get().loadClass(itSignature);
- }
- catch(ClassNotFoundException cnfe) {
- throw new InstantiationException(
- "Interface "+itSignature+
- " referenced in Fractal interface type "+
- its[i].getFcItfName()+" not found");
- }
-
- if( ! itClass.isAssignableFrom(contentClass) ) {
- throw new InstantiationException(
- "Component content class
"+contentClass.getName()+
- " should implement the server interface "+
- itSignature);
- }
- }
- }
- }
-
- /**
- * Return true if the content class associated to the given controller
- * description must implement the BindingController interface when its
type
- * defines at least one client interface.
- */
- protected boolean isFcContentToBeCheckedForBC( Object controllerDesc ) {
-
- /*
- * The content class of component controllers need not implement
- * BindingController.
- */
- if( controllerDesc.equals("mPrimitive") ||
- controllerDesc.equals("mComposite") ) {
- return false;
- }
-
- return true;
- }
-
- /**
- * Check that the content class implements the BindingController
interface
- * if at least one client interface is defined in its type.
- */
- protected void checkFcContentClassforBC( Type type, Class contentClass )
- throws InstantiationException {
-
- ComponentType ct = (ComponentType) type;
- InterfaceType[] its = ct.getFcInterfaceTypes();
- for (int i = 0; i < its.length; i++) {
- if( its[i].isFcClientItf() ) {
- if( ! BindingController.class.isAssignableFrom(contentClass)
) {
- throw new InstantiationException(
- "Content class "+contentClass.getName()+
- " is associated to the client interface type "+
- its[i].getFcItfName()+
- " and must then implement the "+
- BindingController.class.getName()+" interface");
- }
- }
- }
- }
-}
Index:
julia/src/org/objectweb/fractal/juliak/membrane/backend/MembraneCompiler.java
diff -u
julia/src/org/objectweb/fractal/juliak/membrane/backend/MembraneCompiler.java:1.5
julia/src/org/objectweb/fractal/juliak/membrane/backend/MembraneCompiler.java:1.6
---
julia/src/org/objectweb/fractal/juliak/membrane/backend/MembraneCompiler.java:1.5
Sat Dec 16 21:45:30 2006
+++
julia/src/org/objectweb/fractal/juliak/membrane/backend/MembraneCompiler.java
Mon Dec 18 22:12:24 2006
@@ -40,7 +40,7 @@
import org.objectweb.fractal.api.type.ComponentType;
import org.objectweb.fractal.api.type.InterfaceType;
import org.objectweb.fractal.api.type.TypeFactory;
-import org.objectweb.fractal.juliak.factory.GenericFactoryImpl;
+import org.objectweb.fractal.juliak.factory.AbstractGenericFactoryImpl;
import org.objectweb.fractal.juliak.factory.TypeFactoryImpl;
import org.objectweb.fractal.juliak.membrane.JuliakMembranes;
import org.objectweb.fractal.juliak.membrane.MembraneDef;
@@ -165,7 +165,7 @@
pw.println("import "+ComponentType.class.getName()+";");
pw.println("import "+InterfaceType.class.getName()+";");
pw.println("import "+TypeFactory.class.getName()+";");
- pw.println("import "+GenericFactoryImpl.class.getName()+";");
+ pw.println("import "+AbstractGenericFactoryImpl.class.getName()+";");
pw.println("import "+TypeFactoryImpl.class.getName()+";");
pw.println("import "+Fractal.class.getName()+";");
pw.println("import "+NoSuchInterfaceException.class.getName()+";");
@@ -175,7 +175,7 @@
pw.println(" implements "+StaticMembraneFactory.class.getName()+"
{");
pw.println(" public Component newFcStaticMembrane() throws
Exception {");
pw.println(" TypeFactory typeFactory = TypeFactoryImpl.get();");
- pw.println(" GenericFactory genericFactory =
GenericFactoryImpl.get();");
+ pw.println(" GenericFactory genericFactory =
AbstractGenericFactoryImpl.get();");
}
protected void epilogueMembrane( PrintWriter pw, Object root )
| <-- Date Index --> | <-- Thread Index --> |
Powered by MHonArc.
Copyright © 2006-2007, OW2 Consortium | contact | webmaster.