1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
43
44
45
46 public class ImplementationCodeLoaderSelector extends AbstractPluginUser
47 implements
48 ImplementationCodeLoader {
49
50
51
52
53
54
55 public static final String CLIENT_LOADER_ITF = "client-loader";
56
57
58 public static final String DEFAULT_LOADER_ITF = "default-loader";
59
60
61
62
63
64 public ImplementationCodeLoader defaultImplementationCodeLoaderItf;
65
66
67 public ImplementationCodeLoaderSelector() {
68 super(CLIENT_LOADER_ITF);
69 }
70
71
72
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
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 }