KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > coplet > impl > DefaultCopletFactory


1 /*
2  * Copyright 1999-2002,2004 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.coplet.impl;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.activity.Disposable;
24 import org.apache.avalon.framework.component.Component;
25 import org.apache.avalon.framework.configuration.Configurable;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.avalon.framework.logger.AbstractLogEnabled;
29 import org.apache.avalon.framework.service.ServiceException;
30 import org.apache.avalon.framework.service.ServiceManager;
31 import org.apache.avalon.framework.service.ServiceSelector;
32 import org.apache.avalon.framework.service.Serviceable;
33 import org.apache.avalon.framework.thread.ThreadSafe;
34 import org.apache.cocoon.ProcessingException;
35 import org.apache.cocoon.portal.PortalService;
36 import org.apache.cocoon.portal.aspect.AspectDataHandler;
37 import org.apache.cocoon.portal.aspect.AspectDataStore;
38 import org.apache.cocoon.portal.aspect.AspectDescription;
39 import org.apache.cocoon.portal.aspect.impl.DefaultAspectDataHandler;
40 import org.apache.cocoon.portal.aspect.impl.DefaultAspectDescription;
41 import org.apache.cocoon.portal.coplet.CopletData;
42 import org.apache.cocoon.portal.coplet.CopletFactory;
43 import org.apache.cocoon.portal.coplet.CopletInstanceData;
44 import org.apache.cocoon.portal.coplet.adapter.CopletAdapter;
45
46 /**
47  * This factory is for creating and managing coplet objects
48  *
49  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
50  *
51  * @version CVS $Id: DefaultCopletFactory.java 55961 2004-10-29 10:09:02Z cziegeler $
52  */

53 public class DefaultCopletFactory
54     extends AbstractLogEnabled
55     implements Component, ThreadSafe, CopletFactory, Serviceable, Disposable, Configurable {
56     
57     protected ServiceManager manager;
58     
59     protected Map JavaDoc coplets = new HashMap JavaDoc();
60     
61     protected List JavaDoc descriptions = new ArrayList JavaDoc();
62     
63     protected ServiceSelector storeSelector;
64
65     protected static long idCounter = System.currentTimeMillis();
66     
67     /* (non-Javadoc)
68      * @see org.apache.cocoon.portal.coplet.CopletFactory#prepare(org.apache.cocoon.portal.coplet.CopletData)
69      */

70     public void prepare(CopletData copletData)
71     throws ProcessingException {
72         if ( copletData != null ) {
73      
74             final String JavaDoc copletName = copletData.getName();
75             if ( copletName == null ) {
76                 throw new ProcessingException("CopletData "+copletData.getId()+" has no associated name.");
77             }
78             Object JavaDoc[] o = (Object JavaDoc[]) this.coplets.get( copletName );
79             
80             if ( o == null ) {
81                 throw new ProcessingException("CopletDescription with name " + copletName + " not found.");
82             }
83             DefaultCopletDescription copletDescription = (DefaultCopletDescription)o[0];
84
85             copletData.setDescription( copletDescription );
86             copletData.setAspectDataHandler((AspectDataHandler)o[1]);
87             
88         }
89     }
90     
91     /* (non-Javadoc)
92      * @see org.apache.cocoon.portal.coplet.CopletFactory#prepare(org.apache.cocoon.portal.coplet.CopletInstanceData)
93      */

94     public void prepare(CopletInstanceData copletInstanceData)
95     throws ProcessingException {
96         if ( copletInstanceData != null ) {
97      
98             final String JavaDoc copletName = copletInstanceData.getName();
99             if ( copletName == null ) {
100                 throw new ProcessingException("CopletInstanceData "+copletInstanceData.getId()+" has no associated name.");
101             }
102             Object JavaDoc[] o = (Object JavaDoc[]) this.coplets.get( copletName );
103             
104             if ( o == null ) {
105                 throw new ProcessingException("CopletDescription with name " + copletName + " not found.");
106             }
107             DefaultCopletDescription copletDescription = (DefaultCopletDescription)o[0];
108
109             copletInstanceData.setDescription( copletDescription );
110             copletInstanceData.setAspectDataHandler((AspectDataHandler)o[2]);
111             
112         }
113     }
114
115     
116     /* (non-Javadoc)
117      * @see org.apache.cocoon.portal.coplet.CopletFactory#newInstance(org.apache.cocoon.portal.coplet.CopletData)
118      */

119     public CopletInstanceData newInstance(CopletData copletData)
120     throws ProcessingException {
121         String JavaDoc name = copletData.getName();
122         Object JavaDoc[] o = (Object JavaDoc[]) this.coplets.get( name );
123             
124         if ( o == null ) {
125             throw new ProcessingException("CopletDescription with name " + name + " not found.");
126         }
127         DefaultCopletDescription copletDescription = (DefaultCopletDescription)o[0];
128         
129         CopletInstanceData instance = new CopletInstanceData();
130         
131         String JavaDoc id = null;
132         if ( copletDescription.createId() ) {
133             synchronized (this) {
134                 id = copletData.getId() + '-' + idCounter;
135                 idCounter += 1;
136             }
137         }
138         instance.initialize( name, id );
139         
140         instance.setDescription( copletDescription );
141         instance.setAspectDataHandler((AspectDataHandler)o[2]);
142         instance.setCopletData(copletData);
143         
144         // now lookup the adapter
145
final String JavaDoc adapterName = copletData.getCopletBaseData().getCopletAdapterName();
146         CopletAdapter adapter = null;
147         ServiceSelector adapterSelector = null;
148         try {
149             adapterSelector = (ServiceSelector) this.manager.lookup( CopletAdapter.ROLE + "Selector");
150             adapter = (CopletAdapter)adapterSelector.select( adapterName );
151             adapter.init( instance );
152             adapter.login( instance );
153         } catch (ServiceException ce) {
154             throw new ProcessingException("Unable to lookup coplet adapter selector or adaptor.", ce);
155         } finally {
156             if ( adapterSelector != null) {
157                 adapterSelector.release( adapter );
158             }
159             this.manager.release( adapterSelector );
160         }
161         
162         PortalService service = null;
163         try {
164             service = (PortalService)this.manager.lookup(PortalService.ROLE);
165             service.getComponentManager().getProfileManager().register(instance);
166         } catch (ServiceException ce) {
167             throw new ProcessingException("Unable to lookup profile manager.", ce);
168         } finally {
169             this.manager.release( service );
170         }
171         return instance;
172     }
173     
174     /* (non-Javadoc)
175      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
176      */

177     public void service(ServiceManager manager) throws ServiceException {
178         this.manager = manager;
179         this.storeSelector = (ServiceSelector)this.manager.lookup( AspectDataStore.ROLE+"Selector" );
180     }
181
182     /* (non-Javadoc)
183      * @see org.apache.avalon.framework.activity.Disposable#dispose()
184      */

185     public void dispose() {
186         if ( this.manager != null ) {
187             this.manager.release( this.storeSelector );
188             this.storeSelector = null;
189             this.manager = null;
190         }
191     }
192
193     /* (non-Javadoc)
194      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
195      */

196     public void configure(Configuration configuration)
197     throws ConfigurationException {
198         final Configuration[] copletsConf = configuration.getChild("coplets").getChildren("coplet");
199         if ( copletsConf != null ) {
200             for(int i=0; i < copletsConf.length; i++ ) {
201                 DefaultCopletDescription desc = new DefaultCopletDescription();
202                 DefaultCopletDescription instanceDesc = new DefaultCopletDescription();
203                 final String JavaDoc name = copletsConf[i].getAttribute("name");
204                 
205                 // unique test
206
if ( this.coplets.get(name) != null) {
207                     throw new ConfigurationException("Coplet name must be unique. Double definition for " + name);
208                 }
209                 desc.setName(copletsConf[i].getAttribute("name"));
210                 instanceDesc.setName(copletsConf[i].getAttribute("name"));
211                 instanceDesc.setCreateId(copletsConf[i].getAttributeAsBoolean("create-id", true));
212                 
213                 // and now the aspects
214
Configuration[] aspectsConf = copletsConf[i].getChild("coplet-data-aspects").getChildren("aspect");
215                 if (aspectsConf != null) {
216                     for(int m=0; m < aspectsConf.length; m++) {
217                         AspectDescription adesc = DefaultAspectDescription.newInstance(aspectsConf[m]);
218                         desc.addAspectDescription( adesc );
219                     }
220                 }
221
222                 // and now the aspects of the instances
223
aspectsConf = copletsConf[i].getChild("coplet-instance-data-aspects").getChildren("aspect");
224                 if (aspectsConf != null) {
225                     for(int m=0; m < aspectsConf.length; m++) {
226                         AspectDescription adesc = DefaultAspectDescription.newInstance(aspectsConf[m]);
227                         instanceDesc.addAspectDescription( adesc );
228                     }
229                 }
230
231                 DefaultAspectDataHandler handler = new DefaultAspectDataHandler(desc, this.storeSelector);
232                 DefaultAspectDataHandler instanceHandler = new DefaultAspectDataHandler(instanceDesc, this.storeSelector);
233                 this.coplets.put(desc.getName(), new Object JavaDoc[] {desc, handler, instanceHandler});
234                 this.descriptions.add(desc);
235             }
236         }
237     }
238
239     /* (non-Javadoc)
240      * @see org.apache.cocoon.portal.coplet.CopletFactory#remove(org.apache.cocoon.portal.coplet.CopletInstanceData)
241      */

242     public void remove(CopletInstanceData copletInstanceData)
243     throws ProcessingException {
244         if ( copletInstanceData != null ) {
245             // now lookup the adapter
246
final String JavaDoc adapterName = copletInstanceData.getCopletData().getCopletBaseData().getCopletAdapterName();
247             CopletAdapter adapter = null;
248             ServiceSelector adapterSelector = null;
249             try {
250                 adapterSelector = (ServiceSelector) this.manager.lookup( CopletAdapter.ROLE + "Selector");
251                 adapter = (CopletAdapter)adapterSelector.select( adapterName );
252                 adapter.logout( copletInstanceData );
253                 adapter.destroy( copletInstanceData );
254             } catch (ServiceException ce) {
255                 throw new ProcessingException("Unable to lookup coplet adapter selector or adaptor.", ce);
256             } finally {
257                 if ( adapterSelector != null) {
258                     adapterSelector.release( adapter );
259                 }
260                 this.manager.release( adapterSelector );
261             }
262             
263             PortalService service = null;
264             try {
265                 service = (PortalService)this.manager.lookup(PortalService.ROLE);
266                 service.getComponentManager().getProfileManager().unregister(copletInstanceData);
267             } catch (ServiceException ce) {
268                 throw new ProcessingException("Unable to lookup portal service.", ce);
269             } finally {
270                 this.manager.release( service );
271             }
272         }
273     }
274
275 }
276
Popular Tags