KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > pojo > PojoServiceUnitListener


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
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.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: PojoServiceUnitListener.java 676 2006-06-27 15:44:03Z alouis $
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.engine.pojo;
24
25 import static org.objectweb.petals.component.common.util.ClassLoaderUtil.createClassLoader;
26
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.objectweb.petals.component.common.PEtALSComponentSDKException;
31 import org.objectweb.petals.component.common.su.ServiceUnitListener;
32 import org.objectweb.petals.component.common.util.PetalsExtensionsUtil;
33 import org.objectweb.petals.tools.jbicommon.descriptor.Extensions;
34 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor;
35 import org.objectweb.petals.tools.jbicommon.descriptor.Provides;
36
37 public class PojoServiceUnitListener implements ServiceUnitListener {
38
39     private Map JavaDoc<String JavaDoc, Pojo> pojos;
40     
41     private PojoSE pojoSE;
42
43     
44     public PojoServiceUnitListener(PojoSE pojoSE, Map JavaDoc<String JavaDoc, Pojo> pojos){
45         this.pojos = pojos;
46         this.pojoSE = pojoSE;
47     }
48     
49     public void onSUDeployed(String JavaDoc serviceUnitName, String JavaDoc suRootPath,
50         JBIDescriptor descriptor) throws PEtALSComponentSDKException {
51
52         // Retrieve provides extensions
53
List JavaDoc<Provides> providesList = descriptor.getServices().getProvides();
54         if(providesList.size() != 1){
55             throw new PEtALSComponentSDKException(
56             "One, and only one, 'provides' section is required un the JBI.XML descriptor.");
57         }
58         Extensions extensions = providesList.get(0).getExtensions();
59         
60         // retrieve pojo' class name
61
String JavaDoc className =null;
62         className = PetalsExtensionsUtil.extractValueFromKeyValueExtension(
63                 extensions, "class-name");
64         if(className == null){
65             throw new PEtALSComponentSDKException(
66             "'class-name' property not found in the JBI.XML descriptor");
67         }
68         
69         // create a classloader from the SU path
70
ClassLoader JavaDoc classLoader = createClassLoader(suRootPath, this.getClass().getClassLoader());
71         
72         // load the pojo class from the classloader
73
Class JavaDoc clazz =null;
74         try {
75             clazz = classLoader.loadClass(className);
76         } catch (ClassNotFoundException JavaDoc e) {
77             throw new PEtALSComponentSDKException(
78             className+ " class not found in the service-unit libraries.",e);
79         }
80
81         // create the pojo
82
Pojo pojo=null;
83         try {
84             pojo = new Pojo(clazz.newInstance(), pojoSE);
85         } catch (Exception JavaDoc e) {
86             throw new PEtALSComponentSDKException(
87                 className+ " found, but can not create an instance.",e);
88         }
89         
90         //init the pojo
91
pojo.init();
92         pojos.put(serviceUnitName,pojo);
93     }
94
95     public void onSUStarted(String JavaDoc serviceUnitName)
96         throws PEtALSComponentSDKException {
97         pojos.get(serviceUnitName).start();
98     }
99
100     public void onSUStopped(String JavaDoc serviceUnitName)
101         throws PEtALSComponentSDKException {
102         pojos.get(serviceUnitName).stop();
103     }
104
105     public void onSUUndeployed(String JavaDoc serviceUnitName, String JavaDoc suRootPath,
106         JBIDescriptor descriptor) throws PEtALSComponentSDKException {
107         pojos.remove(serviceUnitName);
108     }
109
110 }
111
Popular Tags