KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > services > ServiceConsumer


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 /*
47  * Created on Feb 1, 2004
48  * Manta LTD
49  */

50 package org.mr.kernel.services;
51
52 import java.io.IOException JavaDoc;
53
54 import org.mr.MantaAgent;
55 import org.mr.MantaException;
56 import org.mr.core.protocol.RecipientAddress;
57 import org.mr.core.util.byteable.Byteable;
58 import org.mr.core.util.byteable.ByteableInputStream;
59 import org.mr.core.util.byteable.ByteableOutputStream;
60 import org.mr.core.util.byteable.ByteableRegistry;
61
62 /**
63  * Holds the address info and service name for a Consumer role
64  * ServiceConsumer is a queue consumer or a topic consumer, as a consumer it can
65  * receive or get a copy form a queue and subscribe to a topic
66  * @since Feb 1, 2004
67  * @version 1.0
68  * @author Amir Shevat
69  *
70  */

71 public class ServiceConsumer extends ServiceActor implements RecipientAddress{// implements Serializable {
72

73     private String JavaDoc selectorStatment;
74     private boolean durable = false;
75     private byte acknowledgeMode;
76     
77     
78     /**
79      * Contractor for non durable Consumer
80      * @param agentName the name of the agent that created this object
81      * for local use agentName = MantaAgent.getInstance().getAgentName();
82      * @param serviceName the name of the queue or the topic
83      * @param domain the name of the domain this actor belongs to
84      * @param serviceType needed for dynamic service creation can be
85      * MantaService.SERVICE_TYPE_QUEUE or SERVICE_TYPE_TOPIC
86      * @param acknowledgeMode can be MantaAgentConstants.AUTO_ACK or MantaAgentConstants.CLIENT_ACK or MantaAgentConstants.DUPLICATE_ACK
87      */

88     public ServiceConsumer(String JavaDoc agentName, String JavaDoc domainName, String JavaDoc serviceName, byte serviceType, byte acknowledgeMode ) {
89         super(agentName, domainName, serviceName, serviceType);
90         this.acknowledgeMode = acknowledgeMode;
91         durable = false;
92     }
93     
94     /**
95      * Contractor for durable Consumer (good for durable sub to topics)
96      * @param agentName the name of the agent that created this object
97      * for local use agentName = MantaAgent.getInstance().getAgentName();
98      * @param domainName the name of the domain this actor belongs to
99      * @param serviceName the name of the queue or the topic
100      * @param serviceType needed for dynamic service creation can be
101      * MantaService.SERVICE_TYPE_QUEUE or SERVICE_TYPE_TOPIC
102      * @param acknowledgeMode can be MantaAgentConstants.AUTO_ACK or MantaAgentConstants.CLIENT_ACK or MantaAgentConstants.DUPLICATE_ACK
103      * @param durableRecipientId the unique id of this consumer
104      */

105     public ServiceConsumer(String JavaDoc agentName, String JavaDoc domainName, String JavaDoc serviceName, byte serviceType, byte acknowledgeMode,String JavaDoc durableRecipientId ) {
106         super(agentName, domainName, serviceName, serviceType);
107         this.acknowledgeMode = acknowledgeMode;
108         durable = true;
109         this.id = durableRecipientId;
110     }
111
112     /**
113      * a factory method that when given a MantaService (not null) it will create a local
114      * ServiceConsumer
115      * @param service the service this Consumer consumes
116      * @return a new ServiceConsumer for this layer and this service
117      * @throws MantaException if service is null
118      
119     public static ServiceConsumer createNew(MantaService service) throws MantaException{
120         if(service == null)
121             throw new MantaException("service is null" ,MantaException.ID_INVALID_ARGUMENTS );
122         String myAgentName = MantaAgent.getInstance().getAgentName(); //.getSingletonRepository().getWorldModeler().getMyAgentName();
123         ServiceConsumer result = new ServiceConsumer(myAgentName,MantaAgent.getInstance().getDomainName(), service.getServiceName(),service.getServiceType() );
124         return result;
125     }
126     */

127     
128     /* (non-Javadoc)
129      * @see org.mr.kernel.services.ServiceActor#getType()
130      */

131     public byte getType() {
132         return CONSUMER;
133     }
134
135     
136
137     /**
138      * @return Returns the selectorStatment.
139      */

140     public String JavaDoc getSelectorStatment() {
141         return selectorStatment;
142     }
143
144     /**
145      * for JMS use only
146      * @param selectorStatment The selectorStatment to set.
147      */

148     public void setSelectorStatment(String JavaDoc selectorStatment) {
149         this.selectorStatment = selectorStatment;
150     }
151
152     
153     /**
154      * @return Returns the durable.
155      */

156     public boolean isDurable() {
157         return durable;
158     }
159
160     /**
161      * @param persistent The persistent to set.
162      */

163     public void setDurable(boolean durable) {
164         this.durable = durable;
165     }
166     
167     private final static String JavaDoc byteableName = "SConsumer";
168     /* (non-Javadoc)
169      * @see org.mr.core.util.byteable.Byteable#getByteableName()
170      */

171     public final String JavaDoc getByteableName() {
172         
173         return byteableName;
174     }
175
176     /**
177      * network use
178      * @see org.mr.core.util.byteable.Byteable#toBytes(org.mr.core.util.byteable.ByteableOutputStream)
179      */

180     public void toBytes(ByteableOutputStream out) throws IOException JavaDoc {
181         out.writeASCIIString(getAgentName());
182         out.writeUTF(getServiceName());
183         out.writeByte(getServiceType());
184         out.writeByte(getAcknowledgeMode());
185         out.writeUTF(getId());
186         if(selectorStatment != null)
187             out.writeUTF(selectorStatment);
188         else
189             out.writeUTF("");
190         out.writeBoolean(durable);
191     }
192
193     /**
194      * network use
195      * @see org.mr.core.util.byteable.Byteable#createInstance(org.mr.core.util.byteable.ByteableInputStream)
196      */

197     public Byteable createInstance(ByteableInputStream in) throws IOException JavaDoc {
198         ServiceConsumer result = null;
199         String JavaDoc agentName = in.readASCIIString();
200         String JavaDoc serviceName = in.readUTF();
201         byte serviceType = in.readByte();
202         byte ackMode = in.readByte();
203         String JavaDoc id = in.readUTF();
204         String JavaDoc selectorStatment = in.readUTF();
205         boolean durable = in.readBoolean();
206         /*
207         if(MantaAgent.started && MantaAgent.getInstance().containsService(serviceName)){
208             MantaService localService = MantaAgent.getInstance().getService(serviceName,serviceType);
209             if(localService != null){
210                 result =(ServiceConsumer) localService.getActor(id);
211             }
212         }
213         */

214         
215         if(result == null){
216             result = new ServiceConsumer(agentName,MantaAgent.getInstance().getDomainName(), serviceName, serviceType,ackMode);
217             result.id = id;
218             result.selectorStatment = selectorStatment;
219             result.durable = durable;
220         }
221         return result;
222     }
223
224     /**
225      * network use
226      * @see org.mr.core.util.byteable.Byteable#registerToByteableRegistry()
227      */

228     public void registerToByteableRegistry() {
229         ByteableRegistry.registerByteableFactory(getByteableName() , this);
230     }
231     /**
232      * internal use
233      * DO NOT USE
234      *
235      */

236     public static void register(){
237         ServiceConsumer instance = new ServiceConsumer(null, null, null, (byte)0, (byte)0);
238         instance.registerToByteableRegistry();
239     }
240     
241     //adds to the super toString the durability
242
//method added by lital kasif
243
public String JavaDoc toString(){
244         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
245         buff.append(super.toString());
246         buff.append(" durability=");
247         buff.append(durable);
248         buff.append(" selector=");
249         buff.append(selectorStatment);
250         buff.append(" }");
251         return buff.toString();
252     }
253     public byte getAcknowledgeMode() {
254         return acknowledgeMode;
255     }
256 }
257
Popular Tags