KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > activity > Main


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 activity;
26
27 import org.objectweb.dream.Pull;
28 import org.objectweb.dream.control.activity.task.TaskManagerController;
29 import org.objectweb.dream.message.manager.MessageManagerImpl;
30 import org.objectweb.dream.message.manager.MessageManager;
31 import org.objectweb.dream.message.manager.MessageManagerAttributeController;
32 import org.objectweb.fractal.api.Component;
33 import org.objectweb.fractal.api.control.BindingController;
34 import org.objectweb.fractal.api.control.ContentController;
35 import org.objectweb.fractal.api.factory.GenericFactory;
36 import org.objectweb.fractal.api.type.ComponentType;
37 import org.objectweb.fractal.api.type.InterfaceType;
38 import org.objectweb.fractal.api.type.TypeFactory;
39 import org.objectweb.fractal.util.Fractal;
40
41 /**
42  * Main class
43  */

44 public final class Main
45 {
46
47   private Main()
48   {
49   }
50
51   /**
52    * Entry point.
53    *
54    * @param args ignored.
55    * @throws Exception if an error occurs.
56    */

57   public static void main(String JavaDoc[] args) throws Exception JavaDoc
58   {
59     Component bootstrap = org.objectweb.fractal.api.Fractal
60         .getBootstrapComponent();
61     GenericFactory genericFactory = Fractal.getGenericFactory(bootstrap);
62     TypeFactory typeFactory = Fractal.getTypeFactory(bootstrap);
63
64     // create interface types
65
InterfaceType outPullType = typeFactory.createFcItfType(
66         Pull.OUT_PULL_ITF_NAME, Pull.class.getName(), false, false, false);
67     InterfaceType inPullType = typeFactory.createFcItfType(
68         Pull.IN_PULL_ITF_NAME, Pull.class.getName(), true, false, false);
69     InterfaceType messageManagerClientType = typeFactory.createFcItfType(
70         MessageManager.ITF_NAME, MessageManager.class.getName(), true, false,
71         false);
72     InterfaceType messageManagerServerType = typeFactory.createFcItfType(
73         MessageManager.ITF_NAME, MessageManager.class.getName(), false, false,
74         false);
75     InterfaceType messageManagerAttributeControllerType = typeFactory
76         .createFcItfType("attribute-controller",
77             MessageManagerAttributeController.class.getName(), false, false,
78             false);
79     InterfaceType taskRegistrationClientType = typeFactory.createFcItfType(
80         "task-manager", TaskManagerController.class.getName(), true, false,
81         false);
82
83     // create component types
84
ComponentType clientType = typeFactory.createFcType(new InterfaceType[]{
85         inPullType, messageManagerClientType, taskRegistrationClientType});
86     ComponentType serverType = typeFactory.createFcType(new InterfaceType[]{
87         outPullType, messageManagerClientType});
88     ComponentType messageManagerType = typeFactory
89         .createFcType(new InterfaceType[]{messageManagerServerType,
90             messageManagerAttributeControllerType});
91     ComponentType compositeType = typeFactory
92         .createFcType(new InterfaceType[0]);
93
94     // instanciate components
95
Component client = genericFactory.newFcInstance(clientType,
96         "activeDreamPrimitive", ClientImpl.class.getName());
97     Component server = genericFactory.newFcInstance(serverType,
98         "dreamPrimitive", ServerImpl.class.getName());
99     Component messageManager = genericFactory.newFcInstance(messageManagerType,
100         "dreamPrimitive", MessageManagerImpl.class.getName());
101     Component composite = genericFactory.newFcInstance(compositeType,
102         "dreamComposite", null);
103     Component activityManager = genericFactory.newFcInstance(compositeType,
104         "threadPerTaskActivityComposite", null);
105
106     // set component names
107
Fractal.getNameController(composite).setFcName("Activity");
108     Fractal.getNameController(client).setFcName("client");
109     Fractal.getNameController(server).setFcName("server");
110     Fractal.getNameController(messageManager).setFcName("messageManager");
111     Fractal.getNameController(activityManager).setFcName("activityManager");
112
113     // set component attributes
114
MessageManagerAttributeController mmac = (MessageManagerAttributeController) messageManager
115         .getFcInterface("attribute-controller");
116     mmac.setId((short) 0);
117
118     // add sub components in composite.
119
ContentController compositeCC = Fractal.getContentController(composite);
120     compositeCC.addFcSubComponent(client);
121     compositeCC.addFcSubComponent(server);
122     compositeCC.addFcSubComponent(messageManager);
123     compositeCC.addFcSubComponent(activityManager);
124
125     // bind them
126
BindingController clientBC = Fractal.getBindingController(client);
127     clientBC.bindFc(Pull.IN_PULL_ITF_NAME, server
128         .getFcInterface(Pull.OUT_PULL_ITF_NAME));
129     clientBC.bindFc(MessageManager.ITF_NAME, messageManager
130         .getFcInterface(MessageManager.ITF_NAME));
131     clientBC.bindFc("task-manager", activityManager
132         .getFcInterface("task-manager-controller"));
133     BindingController serverBC = Fractal.getBindingController(server);
134     serverBC.bindFc(MessageManager.ITF_NAME, messageManager
135         .getFcInterface(MessageManager.ITF_NAME));
136
137     // and start
138
Fractal.getLifeCycleController(composite).startFc();
139     Thread.sleep(3000);
140     System.out.println("call to stop on server component ...");
141     Fractal.getLifeCycleController(server).stopFc();
142     System.out.println("stop returned");
143   }
144 }
Popular Tags