KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > adl > legacy > LegacyLoader


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
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: dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.adl.legacy;
26
27 import java.util.Map JavaDoc;
28
29 import org.objectweb.dream.adl.Checker;
30 import org.objectweb.dream.adl.Resolver;
31 import org.objectweb.fractal.adl.ADLException;
32 import org.objectweb.fractal.adl.AbstractLoader;
33 import org.objectweb.fractal.adl.Definition;
34 import org.objectweb.fractal.adl.Node;
35 import org.objectweb.fractal.adl.components.Component;
36 import org.objectweb.fractal.adl.components.ComponentContainer;
37
38 /**
39  *
40  */

41 public class LegacyLoader extends AbstractLoader
42 {
43
44   /** The name of the {@link Checker }client interface */
45   public static final String JavaDoc CHECKER_ITF_NAME = "checker";
46
47   /** The name of the {@link Resolver }client interface */
48   public static final String JavaDoc RESOLVER_ITF_NAME = "resolver";
49
50   private Checker checkerItf;
51
52   private Resolver resolverItf;
53
54   /**
55    * @see org.objectweb.fractal.adl.Loader#load(java.lang.String, java.util.Map)
56    */

57   public Definition load(final String JavaDoc name, final Map JavaDoc context)
58       throws ADLException
59   {
60     Object JavaDoc legacy = context.get("legacy:./");
61     if (legacy == null)
62     {
63       throw new ADLException("Unable to find legacy component in context", null);
64     }
65     Definition d = clientLoader.load(name, context);
66     checkNode((Node) d, legacy, "legacy:./", context);
67     return d;
68   }
69
70   private void checkNode(final Node node, final Object JavaDoc legacy,
71       final String JavaDoc path, final Map JavaDoc context) throws ADLException
72   {
73     if (legacy != null)
74     {
75       checkerItf.check(legacy, node);
76       node.astSetDecoration("legacy", legacy);
77     }
78     if (node instanceof ComponentContainer)
79     {
80       ComponentContainer container = (ComponentContainer) node;
81       Component[] tab = container.getComponents();
82       for (int i = 0; i < tab.length; i++)
83       {
84         Component subComponent = tab[i];
85         String JavaDoc componentPath = (path.endsWith("/")) ? path
86             + subComponent.getName() : path + "/" + subComponent.getName();
87         Object JavaDoc subLegacy = null;
88         if (subComponent instanceof LegacyContainer)
89         {
90           // shearch legacy component
91
Legacy l = ((LegacyContainer) subComponent).getLegacy();
92           if (l != null)
93           {
94             subLegacy = context.get(componentPath);
95             if (subLegacy == null && legacy != null)
96             {
97               if ("mandatory".equals(l.getContingency()))
98               {
99                 try
100                 {
101                   subLegacy = resolverItf.resolve(legacy, subComponent
102                       .getName(), context);
103                 }
104                 catch (ComponentNotFoundException e)
105                 {
106                   throw new ADLException(
107                       "Unable to find mandatory legacy component.",
108                       (Node) subComponent, e);
109                 }
110               }
111               else if ("optionnal".equals(l.getContingency()))
112               {
113                 try
114                 {
115                   subLegacy = resolverItf.resolve(legacy, subComponent
116                       .getName(), context);
117                 }
118                 catch (ComponentNotFoundException e)
119                 {
120                   // ignore, component is optional
121
break;
122                 }
123               }
124             }
125           }
126         }
127         checkNode((Node) subComponent, subLegacy, componentPath, context);
128       }
129     }
130     // TODO check binding between legacy component declared in the AST
131
}
132
133   // ------------------------------------------------------------------------
134
// Implementation of the BindingController interface
135
// ------------------------------------------------------------------------
136

137   /**
138    * @see org.objectweb.fractal.api.control.BindingController#listFc()
139    */

140   public String JavaDoc[] listFc()
141   {
142     return new String JavaDoc[]{LOADER_BINDING, CHECKER_ITF_NAME, RESOLVER_ITF_NAME};
143   }
144
145   /**
146    * @see org.objectweb.fractal.api.control.BindingController#lookupFc(java.lang.String)
147    */

148   public Object JavaDoc lookupFc(String JavaDoc s)
149   {
150     if (CHECKER_ITF_NAME.equals(s))
151     {
152       return checkerItf;
153     }
154     else if (RESOLVER_ITF_NAME.equals(s))
155     {
156       return resolverItf;
157     }
158     else
159     {
160       return super.lookupFc(s);
161     }
162   }
163
164   /**
165    * @see org.objectweb.fractal.api.control.BindingController#bindFc(java.lang.String,
166    * java.lang.Object)
167    */

168   public void bindFc(String JavaDoc s, Object JavaDoc o)
169   {
170     if (CHECKER_ITF_NAME.equals(s))
171     {
172       checkerItf = (Checker) o;
173     }
174     else if (RESOLVER_ITF_NAME.equals(s))
175     {
176       resolverItf = (Resolver) o;
177     }
178     else
179     {
180       super.bindFc(s, o);
181     }
182   }
183
184   /**
185    * @see org.objectweb.fractal.api.control.BindingController#unbindFc(java.lang.String)
186    */

187   public void unbindFc(String JavaDoc s)
188   {
189     if (CHECKER_ITF_NAME.equals(s))
190     {
191       checkerItf = null;
192     }
193     else if (RESOLVER_ITF_NAME.equals(s))
194     {
195       resolverItf = null;
196     }
197     else
198     {
199       super.unbindFc(s);
200     }
201   }
202
203 }
Popular Tags