KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.byteable.Byteable;
57 import org.mr.core.util.byteable.ByteableInputStream;
58 import org.mr.core.util.byteable.ByteableOutputStream;
59 import org.mr.core.util.byteable.ByteableRegistry;
60
61 /**
62  * Holds the address info and service name for a Producer role
63  * ServiceProducer is a queue producer or a topic producer, as a producer it can
64  * receive or get a copy form a queue and subscribe to a topic
65  * @since Feb 1, 2004
66  * @version 1.0
67  * @author Amir Shevat
68  *
69  */

70 public class ServiceProducer extends ServiceActor{// implements Serializable {
71
/**
72      * remote producer will have there layer name attached to them
73      * @param agentName the name of the agent that created this object
74      * for local use agentName = MantaAgent.getInstance().getAgentName();
75      * @param serviceName the name of the queue or the topic
76      * @param serviceType needed for dynamic service creation can be
77      * MantaService.SERVICE_TYPE_QUEUE or SERVICE_TYPE_TOPIC
78      */

79     public ServiceProducer(String JavaDoc agentName, String JavaDoc serviceName , byte serviceType) {
80         super(agentName, MantaAgent.getInstance().getDomainName(), serviceName ,serviceType );
81         
82     }
83
84     public ServiceProducer(String JavaDoc agentName, String JavaDoc domainName, String JavaDoc serviceName , byte serviceType) {
85         super(agentName, domainName, serviceName ,serviceType );
86     }
87
88     /**
89      * returns type PRODUCER
90      */

91     public byte getType() {
92         return super.PRODUCER;
93     }
94     
95     /**
96      * a factory method that when given a MantaService (not null) it will create a local
97      * ServiceConsumer
98      * @param service the service this Producer consumes
99      * @return a new ServiceProducer for this layer and this service
100      * @throws MantaException if service is null
101      */

102     public static ServiceProducer createNew(MantaService service){
103         if(service == null)
104             throw new IllegalArgumentException JavaDoc("service is null" );
105         
106         String JavaDoc myAgentName = MantaAgent.getInstance().getAgentName();
107         ServiceProducer result = new ServiceProducer(myAgentName ,service.getServiceName() , service.getServiceType());
108         return result;
109     }
110
111     private final static String JavaDoc byteableName = "SProducer";
112     
113     /**
114      * do not use internal use only
115      */

116     public String JavaDoc getByteableName() {
117         
118         return byteableName;
119     }
120
121     /* (non-Javadoc)
122      * @see org.mr.core.util.byteable.Byteable#toBytes(org.mr.core.util.byteable.ByteableOutputStream)
123      */

124     public void toBytes(ByteableOutputStream out) throws IOException JavaDoc {
125         out.writeASCIIString(getAgentName());
126         out.writeUTF(getServiceName());
127         out.writeByte(getServiceType());
128         out.writeUTF(getId());
129     
130     }
131
132     /* (non-Javadoc)
133      * @see org.mr.core.util.byteable.Byteable#createInstance(org.mr.core.util.byteable.ByteableInputStream)
134      */

135     public Byteable createInstance(ByteableInputStream in) throws IOException JavaDoc {
136         ServiceProducer result = null;
137         String JavaDoc agentName = in.readASCIIString();
138         String JavaDoc serviceName = in.readUTF();
139         byte serviceType = in.readByte();
140         String JavaDoc id = in.readUTF();
141         if(MantaAgent.started && MantaAgent.getInstance().containsService(serviceName)){
142             MantaService localService = MantaAgent.getInstance().getService(serviceName,serviceType);
143             if(localService != null){
144                 result =(ServiceProducer) localService.getActor(id);
145             }
146         }
147         if(result == null){
148             result = new ServiceProducer(agentName,serviceName ,serviceType);
149             result.id = id;
150         }
151         return result;
152     }
153
154     /* (non-Javadoc)
155      * @see org.mr.core.util.byteable.Byteable#registerToByteableRegistry()
156      */

157     public void registerToByteableRegistry() {
158         ByteableRegistry.registerByteableFactory(getByteableName() , this);
159     }
160     
161     /**
162      * DO NOT USE
163      *
164      */

165     public static void register(){
166         ServiceProducer instance = new ServiceProducer(null , null, null, (byte)0);
167         instance.registerToByteableRegistry();
168     }
169 }
170
Popular Tags