KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > helloworld > 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): Vivien Quema
23  */

24
25 package helloworld;
26
27 import org.objectweb.dream.Push;
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 /** Launcher class. */
42 public final class Main
43 {
44
45   private Main()
46   {
47   }
48
49   /**
50    * Entry point
51    *
52    * @param args if no argument or one equals to <code>-mono</code> use the
53    * {@link ClientImpl}. If equals to <code>-pool</code> the the
54    * {@link PoolClientImpl}.
55    * @throws Exception if an exception occurs
56    */

57   public static void main(String JavaDoc[] args) throws Exception JavaDoc
58   {
59     if (args.length > 1)
60     {
61       usage();
62     }
63     String JavaDoc clientClass = null;
64     if (args.length == 0)
65     {
66       clientClass = ClientImpl.class.getName();
67     }
68     else
69     {
70       String JavaDoc arg = args[0];
71       if (arg.equalsIgnoreCase("-mono"))
72       {
73         clientClass = ClientImpl.class.getName();
74       }
75       else if (arg.equalsIgnoreCase("-pool"))
76       {
77         clientClass = PoolClientImpl.class.getName();
78       }
79       else
80       {
81         usage();
82       }
83     }
84
85     Component bootstrap = org.objectweb.fractal.api.Fractal
86         .getBootstrapComponent();
87     GenericFactory genericFactory = Fractal.getGenericFactory(bootstrap);
88     TypeFactory typeFactory = Fractal.getTypeFactory(bootstrap);
89
90     // create interface types
91
InterfaceType outPushType = typeFactory.createFcItfType(
92         Push.OUT_PUSH_ITF_NAME, Push.class.getName(), true, false, false);
93     InterfaceType inPushType = typeFactory.createFcItfType(
94         Push.IN_PUSH_ITF_NAME, Push.class.getName(), false, false, false);
95     InterfaceType messageManagerClientType = typeFactory.createFcItfType(
96         MessageManager.ITF_NAME, MessageManager.class.getName(), true, false,
97         false);
98     InterfaceType messageManagerServerType = typeFactory.createFcItfType(
99         MessageManager.ITF_NAME, MessageManager.class.getName(), false, false,
100         false);
101     InterfaceType messageManagerAttributeControllerType = typeFactory
102         .createFcItfType("attribute-controller",
103             MessageManagerAttributeController.class.getName(), false, false,
104             false);
105     InterfaceType taskRegistrationClientType = typeFactory.createFcItfType(
106         "task-manager", TaskManagerController.class.getName(), true, false,
107         false);
108
109     // create component types
110
ComponentType clientType = typeFactory.createFcType(new InterfaceType[]{
111         outPushType, messageManagerClientType, taskRegistrationClientType});
112     ComponentType serverType = typeFactory.createFcType(new InterfaceType[]{
113         inPushType, messageManagerClientType});
114     ComponentType messageManagerType = typeFactory
115         .createFcType(new InterfaceType[]{messageManagerServerType,
116             messageManagerAttributeControllerType});
117     ComponentType compositeType = typeFactory
118         .createFcType(new InterfaceType[0]);
119
120     // instanciate components
121
Component client = genericFactory.newFcInstance(clientType,
122         "activeDreamPrimitive", clientClass);
123     Component server = genericFactory.newFcInstance(serverType,
124         "dreamPrimitive", ServerImpl.class.getName());
125     Component messageManager = genericFactory.newFcInstance(messageManagerType,
126         "dreamPrimitive", MessageManagerImpl.class.getName());
127     Component composite = genericFactory.newFcInstance(compositeType,
128         "dreamComposite", null);
129     Component activityManager = genericFactory.newFcInstance(compositeType,
130         "threadPerTaskActivityComposite", null);
131
132     // set component names
133
Fractal.getNameController(composite).setFcName("HelloWorld");
134     Fractal.getNameController(client).setFcName("client");
135     Fractal.getNameController(server).setFcName("server");
136     Fractal.getNameController(messageManager).setFcName("messageManager");
137     Fractal.getNameController(activityManager).setFcName("activityManager");
138
139     // set component attributes
140
MessageManagerAttributeController mmac = (MessageManagerAttributeController) messageManager
141         .getFcInterface("attribute-controller");
142     mmac.setId((short) 0);
143
144     // add sub components in composite.
145
ContentController compositeCC = Fractal.getContentController(composite);
146     compositeCC.addFcSubComponent(client);
147     compositeCC.addFcSubComponent(server);
148     compositeCC.addFcSubComponent(messageManager);
149     compositeCC.addFcSubComponent(activityManager);
150
151     // bind them
152
BindingController clientBC = Fractal.getBindingController(client);
153     clientBC.bindFc(Push.OUT_PUSH_ITF_NAME, server
154         .getFcInterface(Push.IN_PUSH_ITF_NAME));
155     clientBC.bindFc(MessageManager.ITF_NAME, messageManager
156         .getFcInterface(MessageManager.ITF_NAME));
157     clientBC.bindFc("task-manager", activityManager
158         .getFcInterface("task-manager-controller"));
159     BindingController serverBC = Fractal.getBindingController(server);
160     serverBC.bindFc(MessageManager.ITF_NAME, messageManager
161         .getFcInterface(MessageManager.ITF_NAME));
162
163     // and start
164
Fractal.getLifeCycleController(composite).startFc();
165     Thread.sleep(3000);
166     System.out.println("call to stop...");
167     Fractal.getLifeCycleController(composite).stopFc();
168     System.out.println("stop returned");
169   }
170
171   private static void usage()
172   {
173     System.out.println("Usage : HelloWorld [-mono|-pool]");
174     System.out.println("Run the Helloworld example of Dream core framework");
175     System.out.println("Arguments : ");
176     System.out
177         .println(" -mono : use the mono-threaded client component (default) ");
178     System.out.println(" -pool : use the client component using thread pool");
179     System.exit(1);
180   }
181 }
Popular Tags