View Javadoc

1   /***
2    * Cecilia ADL Compiler
3    * Copyright (C) 2006-2007 STMicroelectronics
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: fractal@objectweb.org
20   *
21   * Author: Matthieu Leclercq
22   */
23  
24  package org.objectweb.fractal.cecilia.adl.components;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.objectweb.fractal.adl.ADLException;
31  import org.objectweb.fractal.adl.Definition;
32  import org.objectweb.fractal.adl.components.Component;
33  import org.objectweb.fractal.adl.components.ComponentContainer;
34  import org.objectweb.fractal.api.NoSuchInterfaceException;
35  import org.objectweb.fractal.api.control.BindingController;
36  import org.objectweb.fractal.api.control.IllegalBindingException;
37  import org.objectweb.fractal.api.control.IllegalLifeCycleException;
38  import org.objectweb.fractal.cecilia.adl.Checker;
39  
40  /**
41   * Basic implementation of the {@link Checker} interface. This implementation
42   * recurse in component architecture and delegates check for each component to a
43   * {@link PrimitiveChecker}.
44   */
45  public class ComponentChecker implements Checker, BindingController {
46  
47    // --------------------------------------------------------------------------
48    // Client interface
49    // --------------------------------------------------------------------------
50  
51    /**
52     * The name of the {@link PrimitiveChecker} client interface used by this
53     * compiler.
54     */
55    public static final String PRIMITIVE_CHECKER_ITF = "primitive-checker";
56  
57    /** The primitive checker used by this compiler. */
58    protected PrimitiveChecker primitiveCheckerItf;
59  
60    // --------------------------------------------------------------------------
61    // Implementation of the Compiler interface
62    // --------------------------------------------------------------------------
63  
64    public void check(final Definition definition,
65        final Map<Object, Object> context) throws ADLException {
66      if (definition instanceof ComponentContainer) {
67        check(new ArrayList<ComponentContainer>(),
68            (ComponentContainer) definition, context);
69      }
70    }
71  
72    // --------------------------------------------------------------------------
73    // Compilation methods
74    // --------------------------------------------------------------------------
75  
76    void check(final List<ComponentContainer> path,
77        final ComponentContainer container, final Map<Object, Object> context)
78        throws ADLException {
79      primitiveCheckerItf.beforeSubComponentCheck(path, container, context);
80      path.add(container);
81      final Component[] comps = container.getComponents();
82      for (final Component element : comps) {
83        check(path, element, context);
84      }
85      path.remove(path.size() - 1);
86  
87      primitiveCheckerItf.afterSubComponentCheck(path, container, context);
88    }
89  
90    public void bindFc(final String itfName, final Object value)
91        throws NoSuchInterfaceException, IllegalBindingException,
92        IllegalLifeCycleException {
93  
94      if (itfName == null) {
95        throw new IllegalArgumentException("Interface name can't be null");
96      }
97  
98      if (itfName.equals(PRIMITIVE_CHECKER_ITF)) {
99        try {
100         this.primitiveCheckerItf = (PrimitiveChecker) value;
101       } catch (final RuntimeException e) {
102         e.printStackTrace();
103         throw new IllegalBindingException("Unmatching types for interface '"
104             + itfName + "'");
105       }
106     } else {
107       throw new NoSuchInterfaceException("There is no interface named '"
108           + itfName + "'");
109     }
110 
111   }
112 
113   public String[] listFc() {
114     return new String[]{PRIMITIVE_CHECKER_ITF};
115   }
116 
117   public Object lookupFc(final String itfName) throws NoSuchInterfaceException {
118 
119     if (itfName == null) {
120       throw new IllegalArgumentException("Interface name can't be null");
121     }
122 
123     if (itfName.equals(PRIMITIVE_CHECKER_ITF)) {
124 
125       return this.primitiveCheckerItf;
126     } else {
127       throw new NoSuchInterfaceException("There is no interface named '"
128           + itfName + "'");
129     }
130   }
131 
132   public void unbindFc(final String itfName) throws NoSuchInterfaceException,
133       IllegalBindingException, IllegalLifeCycleException {
134 
135     if (itfName == null) {
136       throw new IllegalArgumentException("Interface name can't be null");
137     }
138 
139     if (itfName.equals(PRIMITIVE_CHECKER_ITF)) {
140 
141       this.primitiveCheckerItf = null;
142     } else {
143       throw new NoSuchInterfaceException("There is no interface named '"
144           + itfName + "'");
145     }
146   }
147 }