KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > wsrp > consumer > ProducerRegistryImpl


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.wsrp.consumer;
17
18 import java.util.Hashtable JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.configuration.Configurable;
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.logger.LogEnabled;
26 import org.apache.avalon.framework.logger.Logger;
27 import org.apache.cocoon.portal.wsrp.adapter.WSRPAdapter;
28 import org.apache.wsrp4j.consumer.ConsumerEnvironment;
29 import org.apache.wsrp4j.consumer.Producer;
30 import org.apache.wsrp4j.consumer.driver.GenericProducerRegistryImpl;
31 import org.apache.wsrp4j.consumer.driver.ProducerImpl;
32 import org.apache.wsrp4j.exception.WSRPException;
33
34 /**
35  * A producer registry storing all producers in a {@link java.util.Hashtable}
36  * in memory.<br/>
37  * On startup/login the registry is full by the wsrp adapter.
38  *
39  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
40  * @author <a HREF="mailto:malessandrini@s-und-n.de">Michel Alessandrini</a>
41  *
42  * @version $Id: ProducerRegistryImpl.java 264755 2005-08-30 10:29:21Z cziegeler $
43  **/

44 public class ProducerRegistryImpl
45     extends GenericProducerRegistryImpl
46     implements LogEnabled, Configurable, RequiresConsumerEnvironment, RequiresWSRPAdapter {
47
48     /** The logger. */
49     protected Logger logger;
50
51     /** The environment. */
52     protected ConsumerEnvironment environment;
53
54     /** All producer descriptions. */
55     protected Map JavaDoc descriptions = new Hashtable JavaDoc();
56
57     /** Initialized? */
58     protected boolean initialized = false;
59
60     /** The wsrp adapter. */
61     protected WSRPAdapter adapter;
62
63     /**
64      * @see org.apache.cocoon.portal.wsrp.consumer.RequiresConsumerEnvironment#setConsumerEnvironment(org.apache.wsrp4j.consumer.ConsumerEnvironment)
65      */

66     public void setConsumerEnvironment(ConsumerEnvironment env) {
67         this.environment = env;
68     }
69
70     /**
71      * @see org.apache.cocoon.portal.wsrp.consumer.RequiresWSRPAdapter#setWSRPAdapter(org.apache.cocoon.portal.wsrp.adapter.WSRPAdapter)
72      */

73     public void setWSRPAdapter(WSRPAdapter adapter) {
74         this.adapter = adapter;
75     }
76
77     /**
78      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
79      */

80     public void configure(Configuration c) throws ConfigurationException {
81         if ( c != null ) {
82             Configuration config = c.getChild("producers", true);
83             // create a list of descriptions
84
Configuration[] children = config.getChildren("producer");
85             for(int i=0; i<children.length; i++) {
86                 final Configuration current = children[i];
87                 final ProducerDescription desc = ProducerDescription.fromConfiguration(current, this.environment);
88                 this.descriptions.put(desc.getId(), desc);
89             }
90         }
91     }
92
93     /**
94      * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
95      */

96     public void enableLogging(Logger newLogger) {
97         this.logger = newLogger;
98     }
99
100     /**
101      * Check if we have read our configuration already.
102      * If not, read the config and invoke the configure method.
103      */

104     protected void checkInitialized() {
105         if ( !this.initialized ) {
106             synchronized (this) {
107                 if (! this.initialized ) {
108                     this.initialized = true;
109                     try {
110                         this.configure(this.adapter.getWsrpConfiguration());
111                     } catch (ConfigurationException ce) {
112                         this.logger.error("Unable to read wsrp configuration.", ce);
113                     }
114                 }
115             }
116         }
117     }
118
119     /**
120      * Add a new producer<br/>
121      *
122      * @param desc The producer description.
123      * @return Returns true if the producer could be added.
124      */

125     public boolean addProducer(ProducerDescription desc) {
126         this.checkInitialized();
127         try {
128             final Producer producer = new ProducerImpl(desc.getId(),
129                                         desc.getMarkupInterfaceUrl(),
130                                         desc.getServiceDescriptionInterfaceUrl(),
131                                         desc.getRegistrationInterfaceUrl(),
132                                         desc.getPortletManagementInterfaceUrl(),
133                                         desc.getRegistrationData());
134             producer.setName(desc.getName());
135             producer.setDescription(desc.getDescription());
136             this.addProducer(producer);
137             return true;
138         } catch (WSRPException we) {
139             this.logger.error("Unable to add wsrp producer: " + desc.getId()
140                             + " - Continuing without configured producer.", we);
141             return false;
142         }
143     }
144
145     /**
146      * @see org.apache.wsrp4j.consumer.ProducerRegistry#addProducer(org.apache.wsrp4j.consumer.Producer)
147      */

148     public void addProducer(Producer producer) {
149         this.checkInitialized();
150         // remove the description
151
this.descriptions.remove(producer.getID());
152         super.addProducer(producer);
153     }
154
155     /**
156      * @see org.apache.wsrp4j.consumer.ProducerRegistry#existsProducer(java.lang.String)
157      */

158     public boolean existsProducer(String JavaDoc id) {
159         this.checkInitialized();
160         if ( this.descriptions.containsKey(id) ) {
161             return true;
162         }
163         return super.existsProducer(id);
164     }
165
166     /**
167      * @see org.apache.wsrp4j.consumer.ProducerRegistry#getAllProducers()
168      */

169     public Iterator JavaDoc getAllProducers() {
170         this.checkInitialized();
171         // create all producers from pending descriptions
172
if ( this.descriptions.size() > 0 ) {
173             final Iterator JavaDoc i = this.descriptions.values().iterator();
174             while ( i.hasNext() ) {
175                 final ProducerDescription desc = (ProducerDescription)i.next();
176                 this.addProducer(desc);
177             }
178             this.descriptions.clear();
179         }
180         return super.getAllProducers();
181     }
182
183     /**
184      * @see org.apache.wsrp4j.consumer.ProducerRegistry#getProducer(java.lang.String)
185      */

186     public Producer getProducer(String JavaDoc id) {
187         this.checkInitialized();
188         // create pending description
189
ProducerDescription desc = (ProducerDescription)this.descriptions.remove(id);
190         if ( desc != null ) {
191             this.addProducer(desc);
192         }
193         return super.getProducer(id);
194     }
195
196     /**
197      * @see org.apache.wsrp4j.consumer.ProducerRegistry#removeAllProducers()
198      */

199     public void removeAllProducers() {
200         this.checkInitialized();
201         this.descriptions.clear();
202         super.removeAllProducers();
203     }
204
205     /**
206      * @see org.apache.wsrp4j.consumer.ProducerRegistry#removeProducer(java.lang.String)
207      */

208     public Producer removeProducer(String JavaDoc id) {
209         this.checkInitialized();
210         // unfortunately we have to return the producer, so
211
// we have to create a pending producer first just
212
// to be able to remove it later on
213
if ( this.descriptions.containsKey(id) ) {
214             this.getProducer(id);
215         }
216         return super.removeProducer(id);
217     }
218 }
219
Popular Tags