KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > helloworld > HelloWorldEngine


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 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$
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.engine.helloworld;
24
25 import java.net.URI JavaDoc;
26 import java.util.logging.Level JavaDoc;
27
28 import javax.jbi.JBIException;
29 import javax.jbi.servicedesc.ServiceEndpoint;
30 import javax.naming.NameAlreadyBoundException JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.xml.namespace.QName JavaDoc;
33
34 import org.objectweb.petals.component.common.HandlingException;
35 import org.objectweb.petals.component.common.MEPConstants;
36 import org.objectweb.petals.component.common.se.AbstractServiceEngine;
37 import org.objectweb.petals.component.common.util.MessageExchangeWrapper;
38 import org.objectweb.petals.component.common.util.WSDLHelper;
39 import org.objectweb.petals.tools.jbicommon.descriptor.Extensions;
40 import org.w3c.dom.Document JavaDoc;
41
42 public class HelloWorldEngine extends AbstractServiceEngine {
43
44     private static final String JavaDoc PRINT_MESS = "printMessage";
45
46     private static final String JavaDoc BIND_MESS = "bindMessage";
47
48     private static final String JavaDoc SAY_HELLO = "sayHello";
49
50     private Helloworld helloworld;
51
52     private ServiceEndpoint endpoint;
53
54     private static final String JavaDoc INTERFACE = "HelloworldInterface";
55
56     private static final String JavaDoc SERVICE = "HelloworldService";
57
58     private static final String JavaDoc ENDPOINT = "HelloworldEndpoint";
59
60     private static final QName JavaDoc SERVICE_NAME = new QName JavaDoc(
61             "http://petals.objectweb.org/", SERVICE);
62
63     private static final QName JavaDoc INTERFACE_NAME = new QName JavaDoc(
64             "http://petals.objectweb.org/", INTERFACE);
65
66     private Document JavaDoc desc;
67
68     @Override JavaDoc
69     protected boolean onExchange(final MessageExchangeWrapper exchange,
70             final Extensions extensions) throws HandlingException {
71
72         String JavaDoc outContent = null;
73         boolean outResponse = false;
74
75         // Get Operation name
76
String JavaDoc opName = exchange.getOperationName();
77
78         // Get Message Exchange Pattern
79
URI JavaDoc pattern = exchange.getExchangePattern();
80
81         // Get in Message content
82
String JavaDoc stringContent = exchange.getInMessageContent();
83
84         if (PRINT_MESS.equalsIgnoreCase(opName)
85                 && pattern.equals(MEPConstants.IN_ONLY_PATTERN.value())) {
86             helloworld.printMessage(stringContent);
87         } else if (BIND_MESS.equalsIgnoreCase(opName)
88                 && pattern.equals(MEPConstants.IN_ONLY_PATTERN.value())) {
89             helloworld.bindMessage(stringContent);
90         } else if (SAY_HELLO.equalsIgnoreCase(opName)
91                 && pattern.equals(MEPConstants.IN_OUT_PATTERN.value())) {
92             outContent = helloworld.sayHello(stringContent);
93             outResponse = true;
94         } else {
95             throw new HandlingException("Operation not allowed (" + opName
96                     + ") with the following pattern (" + pattern + ")");
97         }
98
99         if (outResponse) {
100             exchange.setOutMessageContent(outContent);
101         }
102
103         return outResponse;
104     }
105
106     @Override JavaDoc
107     public void start() throws JBIException {
108         super.start();
109         // Register in the Helloworld entry Jndi context
110
try {
111
112             getContext().getNamingContext().createSubcontext(
113                     HelloworldImpl.HELLOWORLD_CONTEXT);
114
115         } catch (NameAlreadyBoundException JavaDoc e) {
116             getLogger().log(Level.INFO,
117                     "the HelloWorld jndi context is already bound.");
118         } catch (NamingException JavaDoc e) {
119             throw new JBIException(e);
120         }
121
122         desc = WSDLHelper.createLightWSDL20(INTERFACE_NAME, SERVICE_NAME,
123                 ENDPOINT);
124         // Create a new Instance of the Helloworld implementation
125
helloworld = new HelloworldImpl(getContext(), getLogger());
126
127         // Activate a default endpoint
128
endpoint = getContext().activateEndpoint(SERVICE_NAME, ENDPOINT);
129     }
130
131     @Override JavaDoc
132     protected String JavaDoc getResourceBundleName() {
133         return "hello";
134     }
135
136     @Override JavaDoc
137     public void stop() throws JBIException {
138         super.stop();
139         getContext().deactivateEndpoint(endpoint);
140         helloworld = null;
141     }
142
143     public Document JavaDoc getServiceDescription(final ServiceEndpoint endpoint) {
144         return desc;
145     }
146
147 }
148
Popular Tags