KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > helloworld > AbstractClientImpl


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 helloworld;
26
27 import org.objectweb.dream.AbstractComponent;
28 import org.objectweb.dream.Push;
29 import org.objectweb.dream.control.activity.task.AbstractTask;
30 import org.objectweb.dream.message.MessageTypeImpl;
31 import org.objectweb.dream.message.Message;
32 import org.objectweb.dream.message.MessageType;
33 import org.objectweb.dream.message.manager.MessageManager;
34 import org.objectweb.fractal.api.NoSuchInterfaceException;
35 import org.objectweb.fractal.api.control.IllegalBindingException;
36 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
37
38 /** Client component implementation. */
39 public abstract class AbstractClientImpl extends AbstractComponent
40 {
41
42   Push outPushItf;
43   MessageManager messageManager;
44   MessageType msgType = new MessageTypeImpl(HelloWorldChunk.DEFAULT_NAME,
45                              HelloWorldChunk.TYPE);
46
47   protected class ClientTask extends AbstractTask
48   {
49
50     private String JavaDoc message;
51
52     /**
53      * Constructor
54      */

55     public ClientTask(String JavaDoc message)
56     {
57       super("client-task-" + message);
58       this.message = message;
59     }
60
61     /**
62      * @see org.objectweb.dream.control.activity.task.Task#execute(Object)
63      */

64     public Object JavaDoc execute(Object JavaDoc hints) throws InterruptedException JavaDoc
65     {
66       try
67       {
68         Message msg = messageManager.createMessage(msgType);
69         HelloWorldChunk chunk = (HelloWorldChunk) msg
70             .getChunk(HelloWorldChunk.DEFAULT_NAME);
71         chunk.setMessage(message);
72         outPushItf.push(msg, null);
73       }
74       catch (Exception JavaDoc e)
75       {
76         e.printStackTrace();
77         return STOP_EXECUTING;
78       }
79       Thread.sleep(1000);
80       return EXECUTE_AGAIN;
81     }
82   }
83
84   // -------------------------------------------------------------------------
85
// Implementation of the BindingController interface
86
// -------------------------------------------------------------------------
87

88   /**
89    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
90    * Object)
91    */

92   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
93       throws NoSuchInterfaceException, IllegalBindingException,
94       IllegalLifeCycleException
95   {
96     super.bindFc(clientItfName, serverItf);
97     if (clientItfName.equals(Push.OUT_PUSH_ITF_NAME))
98     {
99       outPushItf = (Push) serverItf;
100     }
101     else if (clientItfName.equals(MessageManager.ITF_NAME))
102     {
103       messageManager = (MessageManager) serverItf;
104     }
105   }
106
107   /**
108    * @see org.objectweb.fractal.api.control.BindingController#listFc()
109    */

110   public String JavaDoc[] listFc()
111   {
112     return new String JavaDoc[]{Push.OUT_PUSH_ITF_NAME, MessageManager.ITF_NAME};
113   }
114 }
Popular Tags