KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > deployment > deploy > AllConnectionRegistrationTask


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: AllConnectionRegistrationTask.java 154 6 oct. 06 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.deployment.deploy;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.jbi.JBIException;
28 import javax.xml.namespace.QName JavaDoc;
29
30 import org.objectweb.petals.jbi.management.deployment.DeploymentContextConstants;
31 import org.objectweb.petals.jbi.management.deployment.DeploymentUtils;
32 import org.objectweb.petals.jbi.management.service.EndpointService;
33 import org.objectweb.petals.jbi.management.service.ManagementException;
34 import org.objectweb.petals.processor.Task;
35 import org.objectweb.petals.tools.jbicommon.descriptor.Connection;
36 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor;
37 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceAssembly;
38 import org.objectweb.petals.util.LoggingUtil;
39
40 /**
41  * This task registers connections if exist
42  *
43  * @author ofabre - EBM Websourcing
44  *
45  */

46 public class AllConnectionRegistrationTask implements Task {
47
48     protected EndpointService endpointService;
49
50     /**
51      * logger wrapper
52      */

53     protected LoggingUtil log;
54
55     public AllConnectionRegistrationTask(EndpointService endpointService,
56             LoggingUtil log) {
57         super();
58         this.endpointService = endpointService;
59         this.log = log;
60     }
61
62     public void execute(HashMap JavaDoc context) throws Exception JavaDoc {
63         JBIDescriptor descriptor = (JBIDescriptor) context
64                 .get(DeploymentContextConstants.SA_DESCRIPTOR);
65
66         List JavaDoc<Connection> connections = descriptor.getServiceAssembly()
67                 .getConnections();
68         if (connections != null) {
69             for (Connection connection : connections) {
70                 registerConnection(connection, descriptor);
71             }
72         }
73
74     }
75
76     /**
77      * Register a connection into the endpoint registry of the JBI container
78      *
79      * @param connection
80      * The {@link Connection} to register
81      * @param descriptor
82      * the service assembly descriptor
83      * @throws ManagementException
84      * when container fails to register a connection into the
85      * endpoint registry
86      */

87     protected void registerConnection(Connection connection,
88             JBIDescriptor descriptor) throws ManagementException {
89         String JavaDoc saName = DeploymentUtils.getServiceAssemblyName(descriptor);
90
91         QName JavaDoc consInterface = connection.getConsumerInterfaceName();
92         QName JavaDoc consService = connection.getConsumerServiceName();
93         String JavaDoc consEndpoint = connection.getConsumerEndpointName();
94         QName JavaDoc provService = connection.getProviderServiceName();
95         String JavaDoc provEndpoint = connection.getProviderEndpointName();
96
97         try {
98             if (consInterface != null) {
99
100                 endpointService.createConnection(consInterface, provService,
101                         provEndpoint);
102
103             } else {
104                 endpointService.createConnection(consService, consEndpoint,
105                         provService, provEndpoint);
106             }
107         } catch (JBIException e) {
108             String JavaDoc msg = "Error while registering a service unit connection for the service assembly: "
109                     + saName + ".";
110             log.error(msg, e);
111             throw new ManagementException(msg, e);
112         }
113
114     }
115
116     public void undo(HashMap JavaDoc context) {
117         JBIDescriptor descriptor = (JBIDescriptor) context
118                 .get(DeploymentContextConstants.SA_DESCRIPTOR);
119
120         ServiceAssembly serviceAssembly = descriptor.getServiceAssembly();
121
122         try {
123             removeConnections(serviceAssembly);
124         } catch (Exception JavaDoc e) {
125             String JavaDoc msg = "Failed to revert a AllConnectionRegistrationTask";
126             log.error(msg, e);
127         }
128     }
129
130     /**
131      * Remove all connections registered during the SA deployment process
132      *
133      * @param saDescriptor
134      * the service assembly production element
135      * @throws Exception
136      * if problem occurs during the deletion of a specific
137      * connection
138      */

139     protected void removeConnections(ServiceAssembly saDescriptor)
140         throws Exception JavaDoc {
141         List JavaDoc<Connection> connections = saDescriptor.getConnections();
142         if (connections != null) {
143             for (Connection connection : connections) {
144                 QName JavaDoc consInterface = connection.getConsumerInterfaceName();
145                 QName JavaDoc consService = connection.getConsumerServiceName();
146                 String JavaDoc consEndpoint = connection.getConsumerEndpointName();
147                 QName JavaDoc provService = connection.getProviderServiceName();
148                 String JavaDoc provEndpoint = connection.getProviderEndpointName();
149
150                 if (provEndpoint != null && provService != null) {
151                     if (consInterface != null) {
152                         endpointService.deleteConnection(consInterface,
153                                 provService, provEndpoint);
154                     } else if (consService != null && consEndpoint != null) {
155                         endpointService.deleteConnection(consService,
156                                 consEndpoint, provService, provEndpoint);
157                     }
158                 }
159             }
160         }
161     }
162
163 }
164
Popular Tags