KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > csv > Csv


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: Csv.java 1216 2006-11-14 18:27:09Z dutoo $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.engine.csv;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import javax.jbi.JBIException;
30 import javax.jbi.component.Component;
31 import javax.jbi.component.ComponentContext;
32 import javax.jbi.component.ComponentLifeCycle;
33 import javax.jbi.component.ServiceUnitManager;
34 import javax.jbi.messaging.DeliveryChannel;
35 import javax.jbi.messaging.MessageExchange;
36 import javax.jbi.servicedesc.ServiceEndpoint;
37 import javax.management.ObjectName JavaDoc;
38 import javax.xml.namespace.QName JavaDoc;
39
40 import org.objectweb.petals.component.common.listener.MessageExchangeListener;
41 import org.objectweb.petals.component.common.serviceunitmanager.manager.PetalsServiceUnitManager;
42 import org.objectweb.petals.component.common.util.ComponentLogger;
43 import org.objectweb.petals.component.common.util.XMLHelper;
44
45 import org.w3c.dom.Document JavaDoc;
46 import org.w3c.dom.DocumentFragment JavaDoc;
47 import org.w3c.dom.Node JavaDoc;
48
49 /**
50  * This component transforms a CSV document in an XML document. It uses the
51  * JavaCSV library to do this transformation. NB. To transform the XML output,
52  * use another XSLT service engine SU.
53  *
54  * @version $Rev: 1068 $Date: {date}
55  * @since Petals 1.0
56  * @author Marc Dutoo - Open Wide
57  *
58  */

59 public class Csv implements Component, ComponentLifeCycle {
60
61     protected DeliveryChannel channel;
62
63     private HashMap JavaDoc<String JavaDoc, Properties JavaDoc> mapEndpointCsv;
64
65     protected PetalsServiceUnitManager suManager;
66
67     protected ComponentContext context;
68
69     private MessageExchangeListener listener;
70
71     protected Logger JavaDoc logger;
72
73     public ObjectName JavaDoc getExtensionMBeanName() {
74         return null;
75     }
76
77     public ComponentLifeCycle getLifeCycle() {
78         return this;
79     }
80
81     /**
82      * return the wsdl description
83      */

84     public Document JavaDoc getServiceDescription(ServiceEndpoint arg0) {
85         return suManager.getServiceDescription(arg0);
86     }
87
88     public ServiceUnitManager getServiceUnitManager() {
89         logger.log(Level.INFO,
90             "Component instance asked to provide a ServiceUnitManager.");
91         return suManager;
92     }
93
94     public void init(ComponentContext ctx) throws JBIException {
95         this.context = ctx;
96         Logger JavaDoc javaLogger = ctx.getLogger("", null);
97         logger = new ComponentLogger(javaLogger, javaLogger.getName(),
98             javaLogger.getResourceBundleName(), ctx.getComponentName());
99
100         logger.log(Level.INFO, "init");
101         this.channel = ctx.getDeliveryChannel();
102         this.mapEndpointCsv = new HashMap JavaDoc<String JavaDoc, Properties JavaDoc>();
103     }
104
105     public boolean isExchangeWithConsumerOkay(ServiceEndpoint arg0,
106         MessageExchange arg1) {
107         return true;
108     }
109
110     public boolean isExchangeWithProviderOkay(ServiceEndpoint arg0,
111         MessageExchange arg1) {
112         return false;
113     }
114
115     /**
116      * If requested service is HelloworldService, return the endpoint
117      */

118     public ServiceEndpoint resolveEndpointReference(DocumentFragment JavaDoc arg0) {
119         ServiceEndpoint result = null;
120
121         if (arg0 != null) {
122             String JavaDoc prefix = arg0.getPrefix();
123             if (prefix == null) {
124                 prefix = new String JavaDoc();
125             } else {
126                 prefix += ":";
127             }
128             Node JavaDoc service = XMLHelper.findChild(arg0, prefix + "service", true);
129
130             if (service != null) {
131                 String JavaDoc serviceRequested = XMLHelper.getAttributeValue(service,
132                     "name");
133                 if (serviceRequested != null) {
134                     result = suManager.findEndpointForService(QName
135                         .valueOf(serviceRequested));
136                 }
137             }
138         }
139         return result;
140     }
141
142     public void shutDown() throws JBIException {
143         logger.log(Level.INFO, "shutDown");
144         listener = null;
145     }
146
147     public void start() throws JBIException {
148         logger.log(Level.INFO, "start");
149         CsvProcessor processor = new CsvProcessor(channel, logger,
150             mapEndpointCsv);
151         listener = new MessageExchangeListener(channel, processor,
152             MessageExchangeListener.IgnoredStatus.DONE_AND_ERROR_IGNORED, 0);
153         listener.listen();
154         CsvSUHandler SUHandler = new CsvSUHandler(context, this.mapEndpointCsv,
155             logger);
156         this.suManager = new PetalsServiceUnitManager(context, logger);
157
158         suManager.addNewHandler(SUHandler);
159     }
160
161     public void stop() throws JBIException {
162         logger.log(Level.INFO, "stop");
163         this.listener.terminate();
164     }
165
166 }
167
Popular Tags