View Javadoc

1   /***
2    * Cecilia ADL Compiler
3    * Copyright (C) 2006-2008 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.implementations;
25  
26  import static java.lang.System.arraycopy;
27  
28  import java.util.Map;
29  
30  import org.objectweb.fractal.adl.ADLException;
31  import org.objectweb.fractal.adl.CompilerError;
32  import org.objectweb.fractal.adl.error.GenericErrors;
33  import org.objectweb.fractal.adl.implementations.ImplementationCodeLoader;
34  import org.objectweb.fractal.api.NoSuchInterfaceException;
35  import org.objectweb.fractal.api.control.IllegalBindingException;
36  import org.objectweb.fractal.api.control.IllegalLifeCycleException;
37  import org.objectweb.fractal.cecilia.adl.plugin.AbstractPluginUser;
38  import org.objectweb.fractal.cecilia.adl.plugin.PluginErrors;
39  import org.objectweb.fractal.cecilia.adl.plugin.PluginException;
40  
41  /**
42   * Delegates implementation code loading to a client plugin depending on the
43   * language. More over, if the <code>language</code> is <code>null</code>,
44   * this selector use a default client loader.
45   */
46  public class ImplementationCodeLoaderSelector extends AbstractPluginUser
47      implements
48        ImplementationCodeLoader {
49  
50    // ---------------------------------------------------------------------------
51    // Client interface.
52    // ---------------------------------------------------------------------------
53  
54    /** the client interface prefix for loaded client loader plugins. */
55    public static final String      CLIENT_LOADER_ITF  = "client-loader";
56  
57    /** the name of the default loader client interface. */
58    public static final String      DEFAULT_LOADER_ITF = "default-loader";
59  
60    /**
61     * The default {@link ImplementationCodeLoader} used if the language is
62     * <code>null</code>.
63     */
64    public ImplementationCodeLoader defaultImplementationCodeLoaderItf;
65  
66    /** Default constructor. */
67    public ImplementationCodeLoaderSelector() {
68      super(CLIENT_LOADER_ITF);
69    }
70  
71    // ---------------------------------------------------------------------------
72    // Implementation of the ImplementationCodeLoader interface
73    // ---------------------------------------------------------------------------
74  
75    public Object loadImplementation(final String signature,
76        final String language, final Map<Object, Object> context)
77        throws ADLException {
78  
79      if (language == null)
80        return defaultImplementationCodeLoaderItf.loadImplementation(signature,
81            language, context);
82  
83      try {
84        return getPlugin(language, context, ImplementationCodeLoader.class)
85            .loadImplementation(signature, language, context);
86      } catch (final PluginException e) {
87        if (e.getError().getTemplate() == PluginErrors.PLUGIN_NOT_FOUND) {
88          throw new ADLException(
89              ImplementationErrors.IMPL_LOADER_PLUGIN_NOT_FOUND, language);
90        } else {
91          throw new CompilerError(GenericErrors.INTERNAL_ERROR, e,
92              "Unable to load ImplementationCodeLoader plugin for language \""
93                  + language + "\"");
94        }
95      }
96    }
97  
98    // ---------------------------------------------------------------------------
99    // Implementation of the BindingController interface
100   // ---------------------------------------------------------------------------
101 
102   @Override
103   public void bindFc(final String itf, final Object value)
104       throws NoSuchInterfaceException, IllegalBindingException,
105       IllegalLifeCycleException {
106 
107     if (itf == null) {
108       throw new IllegalArgumentException("Interface name can't be null");
109     }
110 
111     if (itf.equals(DEFAULT_LOADER_ITF)) {
112       this.defaultImplementationCodeLoaderItf = (ImplementationCodeLoader) value;
113     } else {
114       super.bindFc(itf, value);
115     }
116   }
117 
118   @Override
119   public String[] listFc() {
120     final String[] superItfNames = super.listFc();
121     final String[] itfNames = new String[superItfNames.length + 1];
122     arraycopy(superItfNames, 0, itfNames, 0, superItfNames.length);
123     itfNames[superItfNames.length] = DEFAULT_LOADER_ITF;
124     return itfNames;
125   }
126 
127   @Override
128   public Object lookupFc(final String itf) throws NoSuchInterfaceException {
129     if (itf == null) {
130       throw new IllegalArgumentException("Interface name can't be null");
131     }
132 
133     if (itf.equals(DEFAULT_LOADER_ITF)) {
134       return this.defaultImplementationCodeLoaderItf;
135     } else {
136       return super.lookupFc(itf);
137     }
138 
139   }
140 
141   @Override
142   public void unbindFc(final String itf) throws NoSuchInterfaceException,
143       IllegalBindingException, IllegalLifeCycleException {
144     if (itf == null) {
145       throw new IllegalArgumentException("Interface name can't be null");
146     }
147 
148     if (itf.equals(DEFAULT_LOADER_ITF)) {
149       this.defaultImplementationCodeLoaderItf = null;
150     } else {
151       super.unbindFc(itf);
152     }
153   }
154 
155 }