KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > implementations > ImplementationLoader


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.implementations;
25
26 import java.util.Map JavaDoc;
27
28 import org.objectweb.fractal.adl.ADLException;
29 import org.objectweb.fractal.adl.Definition;
30 import org.objectweb.fractal.adl.AbstractLoader;
31 import org.objectweb.fractal.adl.Node;
32 import org.objectweb.fractal.adl.attributes.Attributes;
33 import org.objectweb.fractal.adl.attributes.AttributesContainer;
34 import org.objectweb.fractal.adl.components.Component;
35 import org.objectweb.fractal.adl.components.ComponentContainer;
36 import org.objectweb.fractal.adl.interfaces.Interface;
37 import org.objectweb.fractal.adl.interfaces.InterfaceContainer;
38 import org.objectweb.fractal.adl.types.TypeInterface;
39
40 /**
41  * A {@link org.objectweb.fractal.adl.Loader} to check {@link Implementation}
42  * nodes in definitions. This loader checks that the Java classes specified in
43  * these nodes exist.
44  */

45
46 public class ImplementationLoader extends AbstractLoader {
47   
48   // --------------------------------------------------------------------------
49
// Implementation of the Loader interface
50
// --------------------------------------------------------------------------
51

52   public Definition load (final String JavaDoc name, final Map JavaDoc context)
53     throws ADLException
54   {
55     Definition d = clientLoader.load(name, context);
56     checkNode(d, context);
57     return d;
58   }
59   
60   // --------------------------------------------------------------------------
61
// Checking methods
62
// --------------------------------------------------------------------------
63

64   private void checkNode (final Object JavaDoc node, final Map JavaDoc context)
65     throws ADLException
66   {
67     if (node instanceof ImplementationContainer) {
68       checkImplementationContainer((ImplementationContainer)node, context);
69     }
70     if (node instanceof ControllerContainer) {
71       checkControllerContainer((ControllerContainer)node);
72     }
73     if (node instanceof ComponentContainer) {
74       Component[] comps = ((ComponentContainer)node).getComponents();
75       for (int i = 0; i < comps.length; i++) {
76         checkNode(comps[i], context);
77       }
78     }
79   }
80
81   private void checkImplementationContainer (
82     final ImplementationContainer container,
83     final Map JavaDoc context) throws ADLException
84   {
85     Implementation impl = container.getImplementation();
86     if (impl != null) {
87       String JavaDoc className = impl.getClassName();
88       if (className == null) {
89         throw new ADLException(
90           "Implementation class name missing", (Node)impl);
91       }
92       Class JavaDoc c;
93       try {
94         c = getClassLoader(context).loadClass(className);
95       } catch (ClassNotFoundException JavaDoc e) {
96         throw new ADLException(
97           "Invalid signature '" + className + "'", (Node)impl, e);
98       }
99       if (container instanceof InterfaceContainer) {
100         Interface[] itfs = ((InterfaceContainer)container).getInterfaces();
101         for (int i = 0; i < itfs.length; ++i) {
102           if (itfs[i] instanceof TypeInterface) {
103             TypeInterface itf = (TypeInterface)itfs[i];
104             if (itf.getRole().equals("server")) {
105               Class JavaDoc d;
106               try {
107                 d = getClassLoader(context).loadClass(itf.getSignature());
108               } catch (Exception JavaDoc e) {
109                 continue;
110               }
111               if (!d.isAssignableFrom(c)) {
112                 throw new ADLException(
113                   "The implementation class does not implement " +
114                   "all the server interfaces of the component",
115                   (Node)impl);
116               }
117             }
118           }
119         }
120       }
121       if (container instanceof AttributesContainer) {
122         Attributes attrs = ((AttributesContainer)container).getAttributes();
123         if (attrs != null) {
124           Class JavaDoc d;
125           try {
126             d = getClassLoader(context).loadClass(attrs.getSignature());
127           } catch (Exception JavaDoc e) {
128             return;
129           }
130           if (!d.isAssignableFrom(c)) {
131             throw new ADLException(
132               "The implementation class does not implement " +
133               "all the attribute controller interface of the component",
134               (Node)impl);
135           }
136         }
137       }
138     }
139   }
140
141   private void checkControllerContainer (
142     final ControllerContainer container) throws ADLException
143   {
144     Controller ctrl = container.getController();
145     if (ctrl != null) {
146       if (ctrl.getDescriptor() == null) {
147         throw new ADLException(
148           "Controller descriptor missing", (Node)ctrl);
149       }
150     }
151   }
152 }
153
Popular Tags