KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > components > helloworld > HelloWorld


1 package org.objectweb.proactive.examples.components.helloworld;
2
3 /***
4 *
5 * Author: Eric Bruneton
6 * Modified by: Matthieu Morel
7 */

8 import org.objectweb.fractal.api.Component;
9 import org.objectweb.fractal.api.factory.GenericFactory;
10 import org.objectweb.fractal.api.type.ComponentType;
11 import org.objectweb.fractal.api.type.InterfaceType;
12 import org.objectweb.fractal.api.type.TypeFactory;
13 import org.objectweb.fractal.util.Fractal;
14
15
16 import org.objectweb.proactive.core.component.Constants;
17 import org.objectweb.proactive.core.component.ContentDescription;
18 import org.objectweb.proactive.core.component.ControllerDescription;
19
20
21 import org.objectweb.proactive.core.component.xml.Loader;
22
23 /**
24  * This example is taken from the examples in the Fractal distribution. It normally works with Julia, the reference
25  * implementation, but it can also work with ProActive. <br>
26  * Sections involving templates have been removed, because this implementation does not provide templates. <br>
27  * Currently, the ADL is not the standard FractalADL, but this will be changed soon, so that this implementation can
28  * also use the common tools of the Fractal community.<br>
29  * However, a functionality offered by ProActive is the automatic deployment of components onto remote locations.<br>
30  * When using the "distributed" option with the "parser" option, the ADL loader will load the "helloworld-distributed.xml" ADL,
31  * which affects virtual nodes to components, and the "deployment.xml" file, which maps the virtual nodes to real nodes.<br>
32  * If other cases, all components are instantiated locally, in the current virtual machine. <br>
33  *
34  *
35  */

36 public class HelloWorld {
37     private static String JavaDoc LOCAL_COMPONENTS_DESCRIPTOR =
38         HelloWorld
39             .class
40             .getResource("/org/objectweb/proactive/examples/components/helloworld/helloworld-local.xml")
41             .getPath();
42     private static String JavaDoc DISTRIBUTED_COMPONENTS_DESCRIPTOR =
43             HelloWorld
44                 .class
45                 .getResource("/org/objectweb/proactive/examples/components/helloworld/helloworld-distributed.xml")
46                 .getPath();
47     private static String JavaDoc DEPLOYMENT_DESCRIPTOR =
48         HelloWorld
49             .class
50             .getResource("/org/objectweb/proactive/examples/components/helloworld/deployment.xml")
51             .getPath();
52
53     public static void main(final String JavaDoc[] args) throws Exception JavaDoc {
54         boolean useParser = false;
55         boolean useTemplates = false;
56         boolean useWrapper = false;
57         boolean distributed = false;
58         
59         for (int i = 0; i < args.length; ++i) {
60             useParser |= args[i].equals("parser");
61             useTemplates |= args[i].equals("templates");
62             useWrapper |= args[i].equals("wrapper");
63             distributed |= args[i].equals("distributed");
64         }
65
66         useParser = true;
67         useWrapper = true;
68         distributed=false;
69
70         Component rComp = null;
71
72         if (useParser) {
73             // // -------------------------------------------------------------------
74
// // OPTION 1 : USE THE (custom) FRACTAL ADL
75
// // -------------------------------------------------------------------
76
Loader loader = new Loader();
77
78             // first step : parse the configuration files and load the components system
79
if (distributed) {
80                 loader.loadComponentsConfiguration(DISTRIBUTED_COMPONENTS_DESCRIPTOR, DEPLOYMENT_DESCRIPTOR);
81             } else {
82                 loader.loadComponentsConfiguration(LOCAL_COMPONENTS_DESCRIPTOR, DEPLOYMENT_DESCRIPTOR);
83             }
84             // second step : load the desired component
85
if (useWrapper) {
86                 rComp = loader.getComponent("ClientServerWrapper");
87             } else {
88                 rComp = loader.getComponent("ClientServer");
89             }
90             System.out.println();
91         } else {
92             // -------------------------------------------------------------------
93
// OPTION 2 : DO NOT USE THE FRACTAL ADL
94
// -------------------------------------------------------------------
95
Component boot = org.objectweb.fractal.api.Fractal.getBootstrapComponent();
96             TypeFactory tf = Fractal.getTypeFactory(boot);
97
98             // type of root component
99
ComponentType rType =
100                 tf.createFcType(
101                     new InterfaceType[] { tf.createFcItfType("m", Main.class.getName(), false, false, false)});
102
103             // type of client component
104
ComponentType cType =
105                 tf.createFcType(
106                     new InterfaceType[] {
107                         tf.createFcItfType("m", Main.class.getName(), false, false, false),
108                         tf.createFcItfType("s", Service.class.getName(), true, false, false)});
109
110             // type of server component
111
ComponentType sType =
112                 tf.createFcType(
113                     new InterfaceType[] {
114                         tf.createFcItfType("s", Service.class.getName(), false, false, false),
115                         tf.createFcItfType(
116                             "attribute-controller",
117                             ServiceAttributes.class.getName(),
118                             false,
119                             false,
120                             false)});
121
122             // #Julia specific code# GenericFactory cf = Fractal.getGenericFactory(boot);
123
GenericFactory cf = Fractal.getGenericFactory(boot);
124
125             if (!useTemplates) {
126                 // -------------------------------------------------------------------
127
// OPTION 2.1 : CREATE COMPONENTS DIRECTLY
128
// -------------------------------------------------------------------
129
// create root component
130
rComp =
131                     cf.newFcInstance(rType, new ControllerDescription("root", Constants.COMPOSITE), null);
132                 // create client component
133
Component cComp =
134                     cf.newFcInstance(
135                         cType,
136                         new ControllerDescription("client", Constants.PRIMITIVE),
137                         new ContentDescription(ClientImpl.class.getName())); // other properties could be added (activity for example)
138

139                 // create server component
140
Component sComp =
141                     cf.newFcInstance(
142                         sType,
143                         new ControllerDescription("server", Constants.PRIMITIVE),
144                         new ContentDescription(ServerImpl.class.getName()));
145                 ((ServiceAttributes) Fractal.getAttributeController(sComp)).setHeader("--------> ");
146                 ((ServiceAttributes) Fractal.getAttributeController(sComp)).setCount(1);
147
148                 if (useWrapper) {
149                     sType =
150                         tf.createFcType(
151                             new InterfaceType[] {
152                                  tf.createFcItfType("s", Service.class.getName(), false, false, false)});
153                     // create client component "wrapper" component
154
Component CComp =
155                         cf.newFcInstance(
156                             cType,
157                             new ControllerDescription("client-wrapper", Constants.COMPOSITE),
158                             null);
159
160                     // create server component "wrapper" component
161
Component SComp =
162                         cf.newFcInstance(
163                             sType,
164                             new ControllerDescription("server-wrapper", Constants.COMPOSITE),
165                             null);
166
167                     // component assembly
168
Fractal.getContentController(CComp).addFcSubComponent(cComp);
169                     Fractal.getContentController(SComp).addFcSubComponent(sComp);
170                     Fractal.getBindingController(CComp).bindFc("m", cComp.getFcInterface("m"));
171                     Fractal.getBindingController(cComp).bindFc(
172                         "s",
173                         Fractal.getContentController(CComp).getFcInternalInterface("s"));
174                     //Fractal.getBindingController(cComp).bindFc("s", CComp.getFcInterface("s"));
175
Fractal.getBindingController(SComp).bindFc("s", sComp.getFcInterface("s"));
176                     // replaces client and server components by "wrapper" components
177
// THIS CHANGES REFERENCES (STUBS)
178
cComp = CComp;
179                     sComp = SComp;
180                 }
181
182                 // component assembly
183
Fractal.getContentController(rComp).addFcSubComponent(cComp);
184                 Fractal.getContentController(rComp).addFcSubComponent(sComp);
185                 Fractal.getBindingController(rComp).bindFc("m", cComp.getFcInterface("m"));
186                 Fractal.getBindingController(cComp).bindFc("s", sComp.getFcInterface("s"));
187             }
188         }
189
190         // -----------------------------------------------------------------------
191
// COMMON PART
192
// -----------------------------------------------------------------------
193
// start root component
194
Fractal.getLifeCycleController(rComp).startFc();
195
196         // call main method
197
((Main) rComp.getFcInterface("m")).main(null);
198     }
199
200 }
201
Popular Tags