KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > bindings > TypeBindingLoader


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl.bindings;
25
26 import org.objectweb.fractal.adl.bindings.BindingLoader;
27 import org.objectweb.fractal.adl.interfaces.Interface;
28 import org.objectweb.fractal.adl.ADLException;
29 import org.objectweb.fractal.adl.Node;
30 import org.objectweb.fractal.adl.types.TypeInterface;
31
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 /**
36  * An extended {@link BindingLoader} for components with typed interfaces. In
37  * addition to the checks performed by {@link BindingLoader}, this
38  * implementation checks that from interfaces are client interfaces, that
39  * to interfaces are server interfaces, and that the signatures and contingency
40  * of client and server interfaces are compatible.
41  */

42
43 public class TypeBindingLoader extends BindingLoader {
44
45   // --------------------------------------------------------------------------
46
// Overriden methods
47
// --------------------------------------------------------------------------
48

49   void checkBinding (
50     final Binding binding,
51     final Interface fromItf,
52     final Interface toItf,
53     final Map JavaDoc context) throws ADLException
54   {
55     if (fromItf instanceof TypeInterface && toItf instanceof TypeInterface) {
56       TypeInterface cItf = (TypeInterface)fromItf;
57       TypeInterface sItf = (TypeInterface)toItf;
58       if (binding.getFrom().startsWith("this.")) {
59         if (!TypeInterface.SERVER_ROLE.equals(cItf.getRole())) {
60           throw new ADLException(
61             "Invalid binding: 'from' interface is not an internal client interface", (Node)binding);
62         }
63       } else {
64         if (!TypeInterface.CLIENT_ROLE.equals(cItf.getRole())) {
65           throw new ADLException(
66             "Invalid binding: 'from' interface is not a client interface", (Node)binding);
67         }
68       }
69       if (binding.getTo().startsWith("this.")) {
70         if (!TypeInterface.CLIENT_ROLE.equals(sItf.getRole())) {
71           throw new ADLException(
72             "Invalid binding: 'to' interface is not an internal server interface", (Node)binding);
73         }
74       } else {
75         if (!TypeInterface.SERVER_ROLE.equals(sItf.getRole())) {
76           throw new ADLException(
77             "Invalid binding: 'to' interface is not a server interface", (Node)binding);
78         }
79       }
80             
81       boolean cIsMandatory = true;
82       if (TypeInterface.OPTIONAL_CONTINGENCY.equals(cItf.getContingency())) {
83         cIsMandatory = false;
84       }
85       boolean sIsMandatory = true;
86       if (TypeInterface.OPTIONAL_CONTINGENCY.equals(sItf.getContingency())) {
87         sIsMandatory = false;
88       }
89       if (cIsMandatory && !sIsMandatory) {
90         throw new ADLException(
91           "Invalid binding: binding from mandatory to optional interface", (Node)binding);
92       }
93
94       try {
95         ClassLoader JavaDoc cl = getClassLoader(context);
96         Class JavaDoc cClass = cl.loadClass(cItf.getSignature());
97         Class JavaDoc sClass = cl.loadClass(sItf.getSignature());
98         if (!cClass.isAssignableFrom(sClass)) {
99           throw new ADLException(
100             "Invalid binding: incompatible signatures", (Node)binding);
101         }
102       } catch (ClassNotFoundException JavaDoc e) {
103       }
104     }
105   }
106
107   Interface getInterface (final String JavaDoc name, final Map JavaDoc itfs) {
108     Interface itf = super.getInterface(name, itfs);
109     if (itf == null) {
110       Iterator JavaDoc i = itfs.keySet().iterator();
111       while (i.hasNext()) {
112         String JavaDoc n = (String JavaDoc)i.next();
113         itf = (Interface)itfs.get(n);
114         if (itf instanceof TypeInterface) {
115           TypeInterface typeItf = (TypeInterface)itf;
116           String JavaDoc cardinality = typeItf.getCardinality();
117           if (TypeInterface.COLLECTION_CARDINALITY.equals(cardinality)) {
118             if (name.startsWith(n)) {
119               return itf;
120             }
121           }
122         }
123       }
124       return null;
125     } else {
126       return itf;
127     }
128   }
129 }
130
Popular Tags