KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > pushwithreturn > ProducerImpl


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 pushwithreturn;
26
27 import java.util.Collections JavaDoc;
28
29 import org.objectweb.dream.AbstractComponent;
30 import org.objectweb.dream.PushWithReturn;
31 import org.objectweb.dream.control.activity.Util;
32 import org.objectweb.dream.control.activity.task.AbstractTask;
33 import org.objectweb.dream.control.activity.task.Task;
34 import org.objectweb.dream.message.Message;
35 import org.objectweb.dream.message.MessageType;
36 import org.objectweb.dream.message.MessageTypeImpl;
37 import org.objectweb.dream.message.manager.MessageManager;
38 import org.objectweb.fractal.api.Component;
39 import org.objectweb.fractal.api.NoSuchInterfaceException;
40 import org.objectweb.fractal.api.control.IllegalBindingException;
41 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
42 import org.objectweb.util.monolog.api.BasicLevel;
43
44 /**
45  * Implementation of the Producer component.
46  */

47 public class ProducerImpl extends AbstractComponent
48 {
49
50   PushWithReturn outPushWithReturnItf;
51   MessageManager messageManagerItf;
52   MessageType msgType = new MessageTypeImpl(TestChunk.DEFAULT_NAME,
53                              TestChunk.TYPE);
54
55   Task tasks;
56
57   // -------------------------------------------------------------------------
58
// Overriden methods
59
// -------------------------------------------------------------------------
60

61   /**
62    * @see org.objectweb.dream.AbstractComponent#beforeFirstStart(org.objectweb.fractal.api.Component)
63    */

64   protected void beforeFirstStart(Component componentItf)
65       throws IllegalLifeCycleException
66   {
67     try
68     {
69       Util.addTask(componentItf, new ProducerTask("Producer task"),
70           Collections.EMPTY_MAP);
71       logger.log(BasicLevel.DEBUG, "task added");
72     }
73     catch (Exception JavaDoc e)
74     {
75       throw new IllegalLifeCycleException("Can't add task");
76     }
77   }
78
79   // ---------------------------------------------------------------------------
80
// Implementation of the BindingController interface
81
// ---------------------------------------------------------------------------
82

83   /**
84    * @see org.objectweb.fractal.api.control.BindingController#bindFc(String,
85    * Object)
86    */

87   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
88       throws NoSuchInterfaceException, IllegalBindingException,
89       IllegalLifeCycleException
90   {
91     super.bindFc(clientItfName, serverItf);
92     if (clientItfName.equals(PushWithReturn.OUT_PUSH_WITH_RETURN_ITF_NAME))
93     {
94       outPushWithReturnItf = (PushWithReturn) serverItf;
95     }
96     else if (clientItfName.equals(MessageManager.ITF_NAME))
97     {
98       messageManagerItf = (MessageManager) serverItf;
99     }
100   }
101
102   /**
103    * @see org.objectweb.fractal.api.control.BindingController#listFc()
104    */

105   public String JavaDoc[] listFc()
106   {
107     return new String JavaDoc[]{PushWithReturn.OUT_PUSH_WITH_RETURN_ITF_NAME,
108         MessageManager.ITF_NAME};
109   }
110
111   private class ProducerTask extends AbstractTask
112   {
113
114     private String JavaDoc message = "Producer";
115
116     /**
117      * Constructor
118      */

119     public ProducerTask(String JavaDoc name)
120     {
121       super(name);
122     }
123
124     /**
125      * @see Task#execute(Object)
126      */

127     public Object JavaDoc execute(Object JavaDoc hints) throws InterruptedException JavaDoc
128     {
129       try
130       {
131         Message msg = messageManagerItf.createMessage(msgType);
132         TestChunk chunk = (TestChunk) msg.getChunk(TestChunk.DEFAULT_NAME);
133         chunk.setMessage(message);
134         logger.log(BasicLevel.INFO, "Producer sends message " + msg);
135         Message returnMessage = outPushWithReturnItf.pushWithReturn(msg, null);
136         logger.log(BasicLevel.INFO, "Producer receives return message "
137             + returnMessage);
138       }
139       catch (Exception JavaDoc e)
140       {
141         e.printStackTrace();
142         return STOP_EXECUTING;
143       }
144       Thread.sleep(1000);
145       return EXECUTE_AGAIN;
146     }
147   }
148
149 }
Popular Tags