KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > pushwithreturn > KeyGeneratorIntegerImpl


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 org.objectweb.dream.pushwithreturn;
26
27 import org.objectweb.dream.AbstractComponent;
28 import org.objectweb.dream.message.ExtensibleMessage;
29 import org.objectweb.dream.message.Message;
30 import org.objectweb.dream.message.manager.MessageManager;
31 import org.objectweb.fractal.api.NoSuchInterfaceException;
32 import org.objectweb.fractal.api.control.IllegalBindingException;
33 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
34
35 /**
36  * This generator generates <code>KeyInteger</code> objects with an increasing
37  * integer. It adds a <code>KeyChunk</code> chunk to the message.
38  */

39 public class KeyGeneratorIntegerImpl extends AbstractComponent
40     implements
41       KeyGenerator
42 {
43   /** The "All" Integer key. */
44   public static final KeyInteger ALL_INTEGER_KEY = new KeyInteger(0, true);
45
46   /** The integer contained in the last generated key. */
47   int lastInSequence = 0;
48
49   /** The client interface used to access the message manager. */
50   MessageManager messageManagerItf;
51
52   // ---------------------------------------------------------------------------
53
// Implementation of the KeyGenerator interface.
54
// ---------------------------------------------------------------------------
55
/**
56    * @see org.objectweb.dream.pushwithreturn.KeyGenerator#generateKey(org.objectweb.dream.message.Message)
57    */

58   public Key generateKey(Message message) throws Exception JavaDoc
59   {
60     KeyChunk keyChunk = (KeyChunk) message.getChunk(KeyChunk.DEFAULT_NAME);
61     if (keyChunk != null)
62     {
63       return keyChunk.getKey();
64     }
65     if (message instanceof ExtensibleMessage)
66     {
67       KeyInteger key = new KeyInteger(++lastInSequence, false);
68       keyChunk = (KeyChunk) messageManagerItf.createChunk(KeyChunk.TYPE);
69       keyChunk.setKey(key);
70       ((ExtensibleMessage) message).addChunk(KeyChunk.DEFAULT_NAME,
71           KeyChunk.TYPE, keyChunk);
72       return key;
73     }
74     else
75     {
76       throw new Exception JavaDoc("Unable to add KeyChunk to message " + message);
77     }
78   }
79
80   // ---------------------------------------------------------------------------
81
// Implementation of the BindingController interface.
82
// ---------------------------------------------------------------------------
83

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

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

102   public String JavaDoc[] listFc()
103   {
104     return new String JavaDoc[]{MessageManager.ITF_NAME};
105   }
106
107 }
Popular Tags