KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > activity > ClientImpl


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): Vivien Quema
22  * Contributor(s):
23  */

24
25 package activity;
26
27 import java.util.Collections JavaDoc;
28
29 import org.objectweb.dream.AbstractComponent;
30 import org.objectweb.dream.InterruptedPullException;
31 import org.objectweb.dream.Pull;
32 import org.objectweb.dream.PullException;
33 import org.objectweb.dream.control.activity.Util;
34 import org.objectweb.dream.control.activity.task.AbstractTask;
35 import org.objectweb.dream.message.Message;
36 import org.objectweb.dream.message.manager.MessageManager;
37 import org.objectweb.fractal.api.Component;
38 import org.objectweb.fractal.api.NoSuchInterfaceException;
39 import org.objectweb.fractal.api.control.IllegalBindingException;
40 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
41 import org.objectweb.util.monolog.api.BasicLevel;
42
43 /**
44  * TODO
45  */

46 public class ClientImpl extends AbstractComponent
47 {
48
49   protected Pull inPullItf;
50   protected MessageManager messageManagerItf;
51
52   // -------------------------------------------------------------------------
53
// Overriden methods
54
// -------------------------------------------------------------------------
55

56   /**
57    * @see org.objectweb.dream.AbstractComponent#beforeFirstStart(org.objectweb.fractal.api.Component)
58    */

59   protected void beforeFirstStart(Component componentItf)
60       throws IllegalLifeCycleException
61   {
62     try
63     {
64       Util.addTask(componentItf, new PullTask(), Collections.EMPTY_MAP);
65       logger.log(BasicLevel.INFO, "task added");
66     }
67     catch (Exception JavaDoc e)
68     {
69       throw new IllegalLifeCycleException("Can't add task");
70     }
71   }
72
73   protected class PullTask extends AbstractTask
74   {
75
76     PullTask()
77     {
78       super("PullTask");
79     }
80
81     /**
82      * @see org.objectweb.dream.control.activity.task.Task#execute(Object)
83      */

84     public Object JavaDoc execute(Object JavaDoc hints) throws InterruptedException JavaDoc
85     {
86       Message message = null;
87       try
88       {
89         message = inPullItf.pull(null);
90         System.out.println("PullComponent has received a message : " + message);
91       }
92       catch (PullException e)
93       {
94         System.out.println("An exeception occurs while pulling a message : "
95             + e);
96         if (e instanceof InterruptedPullException)
97         {
98           System.out.println("The pull method has been interrupted");
99         }
100         return STOP_EXECUTING;
101       }
102       return EXECUTE_AGAIN;
103     }
104   } // -------------------------------------------------------------------------
105

106   // -------------------------------------------------------------------------
107
// Implementation of the BindingController interface
108
// -------------------------------------------------------------------------
109

110   /**
111    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
112    * Object)
113    */

114   public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
115       throws NoSuchInterfaceException, IllegalBindingException,
116       IllegalLifeCycleException
117   {
118     super.bindFc(clientItfName, serverItf);
119     if (clientItfName.equals(Pull.IN_PULL_ITF_NAME))
120     {
121       inPullItf = (Pull) serverItf;
122     }
123     else if (clientItfName.equals(MessageManager.ITF_NAME))
124     {
125       messageManagerItf = (MessageManager) serverItf;
126     }
127   }
128
129   /**
130    * @see org.objectweb.fractal.api.control.BindingController#listFc()
131    */

132   public String JavaDoc[] listFc()
133   {
134     return new String JavaDoc[]{Pull.IN_PULL_ITF_NAME, MessageManager.ITF_NAME};
135   }
136
137 }
Popular Tags