View Javadoc

1   /*
2    * $Id: Admin.java,v 1.1 2004/12/15 14:18:08 patforna Exp $
3    *
4    * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
5    * Berne University of Applied Sciences
6    * School of Engineering and Information Technology
7    * All rights reserved.
8    */
9   package bexee.admin;
10  
11  import java.io.ByteArrayOutputStream;
12  import java.io.File;
13  import java.io.FileInputStream;
14  import java.io.FileNotFoundException;
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.rmi.RemoteException;
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import javax.xml.rpc.ParameterMode;
23  import javax.xml.rpc.ServiceException;
24  
25  import org.apache.axis.client.Call;
26  import org.apache.axis.client.Service;
27  import org.apache.axis.encoding.XMLType;
28  import org.apache.commons.io.CopyUtils;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import bexee.util.BexeeProperties;
33  import bexee.util.Constants;
34  
35  /***
36   * This class acts as a convenient Java front-end for the
37   * {@link bexee.admin.Manager}class instead of calling the <code>Manager</code>
38   * web service directly.
39   * 
40   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:08 $
41   * @author Patric Fornasier
42   * @author Pawel Kowalski
43   */
44  public class Admin {
45  
46      private static Log log = LogFactory.getLog(Admin.class);
47  
48      private String url;
49  
50      /***
51       * Create an instance of the <code>Admin</code>
52       *  
53       */
54      public Admin() {
55          this.url = BexeeProperties.getProperty(Constants.OPT_MANAGER_URL,
56                  Constants.OPT_MANAGER_URL_DEF);
57      }
58  
59      /***
60       * Create an instance of the <code>Admin</code> with a give
61       * <code>URL</code> where bexee is located.
62       * 
63       * @param url
64       *            bexee location
65       */
66      public Admin(String url) {
67          this.url = url;
68      }
69  
70      /***
71       * Deploy a BPEL business process together with its WSDL.
72       * 
73       * @param bpel
74       *            BPEL document
75       * @param wsdl
76       *            WSDL description
77       * @return @throws
78       *         AdminException
79       */
80      public String deploy(File bpel, File wsdl) throws AdminException {
81          return deploy(bpel, wsdl, new ArrayList());
82      }
83  
84      /***
85       * Deploy a BPEL business process together with its WSDL.
86       * 
87       * @param bpel
88       *            BPEL document stream
89       * @param wsdl
90       *            WSDL description stream
91       * @return @throws
92       *         AdminException
93       */
94      public String deploy(InputStream bpel, InputStream wsdl)
95              throws AdminException {
96          return deploy(bpel, wsdl, new ArrayList());
97      }
98  
99      /***
100      * Deploy a BPEL business process together with its WSDL and one partner
101      * WSDL description.
102      * 
103      * @param bpel
104      *            BPEL document
105      * @param wsdl
106      *            WSDL description
107      * @param partnerWSDL
108      *            partner WSDL description
109      * @return @throws
110      *         AdminException
111      */
112     public String deploy(File bpel, File wsdl, File partnerWSDL)
113             throws AdminException {
114         List list = new ArrayList();
115         list.add(partnerWSDL);
116 
117         return deploy(bpel, wsdl, list);
118     }
119 
120     /***
121      * Deploy a BPEL business process together with its WSDL and its partner
122      * WSDL descriptions.
123      * 
124      * @param bpel
125      *            BPEL document
126      * @param wsdl
127      *            WSDL description
128      * @param partnerWSDL
129      *            partner WSDL descriptions
130      * @return @throws
131      *         AdminException
132      */
133     public String deploy(File bpel, File wsdl, List partnerWSDL)
134             throws AdminException {
135 
136         InputStream bpelIs = null;
137         InputStream wsdlIs = null;
138         List list = null;
139 
140         try {
141             bpelIs = new FileInputStream(bpel);
142             wsdlIs = new FileInputStream(wsdl);
143             list = new ArrayList();
144             for (Iterator iter = partnerWSDL.iterator(); iter.hasNext();) {
145                 File file = (File) iter.next();
146                 list.add(new FileInputStream(file));
147             }
148         } catch (FileNotFoundException e) {
149             throw new AdminException("Unable to locate files", e);
150         }
151 
152         return deploy(bpelIs, wsdlIs, list);
153     }
154 
155     /***
156      * Deploys a new BPEL process to bexee.
157      * 
158      * @see #deploy(InputStream, InputStream, List)
159      */
160     public String deploy(InputStream bpel, InputStream wsdl,
161             InputStream partnerWSDL) throws AdminException {
162 
163         List list = new ArrayList();
164         list.add(partnerWSDL);
165 
166         return deploy(bpel, wsdl, list);
167     }
168 
169     /***
170      * Deploys a new BPEL process to bexee.This method will invoke the
171      * <code>Manager</code> web service and pass it the documents.
172      * 
173      * @param bpel
174      * @param wsdl
175      * @param partnerWSDL
176      * @return @throws
177      *         AdminException
178      */
179     public String deploy(InputStream bpel, InputStream wsdl, List partnerWSDL)
180             throws AdminException {
181 
182         String[] input = null;
183         String output = null;
184 
185         // build call
186         Call call = createCall("deploy");
187         call.addParameter("input", XMLType.SOAP_ARRAY, ParameterMode.IN);
188         call.setReturnType(XMLType.XSD_STRING);
189 
190         // build input parameter
191         try {
192             input = buildInputArray(bpel, wsdl, partnerWSDL);
193         } catch (IOException e) {
194             throw new AdminException("Unable to get all input data", e);
195         }
196 
197         // invoke service
198         try {
199             output = (String) call.invoke(new Object[] { input });
200         } catch (RemoteException e) {
201             throw new AdminException("Error on invoking Manager deploy()", e);
202         }
203 
204         return output;
205     }
206 
207     /***
208      * Undeploys an existing service
209      * 
210      * @param name
211      *            the name of the service to be undeployed
212      * @return the message from the Manager service
213      * @throws AdminException
214      *             if something went wrong
215      * @see {@link Manager}
216      */
217     public String undeploy(String name) throws AdminException {
218 
219         String[] input = null;
220         String output = null;
221 
222         // build call
223         Call call = createCall("undeploy");
224         call.addParameter("input", XMLType.XSD_STRING, ParameterMode.IN);
225         call.setReturnType(XMLType.XSD_STRING);
226 
227         // invoke service
228         try {
229             output = (String) call.invoke(new Object[] { name });
230         } catch (RemoteException e) {
231             throw new AdminException("Error on invoking Manager undeploy()", e);
232         }
233 
234         return output;
235     }
236 
237     /***
238      * Create Axis call to service.
239      * 
240      * @return a <code>Call</code> object
241      * @throws AdminException
242      *             if the <code>Call</code> can not be created
243      */
244     protected Call createCall(String operation) throws AdminException {
245 
246         if (url == null) {
247             throw new AdminException("No url to Manager service provided");
248         }
249 
250         // create service and call
251         Service service = new Service();
252         Call call;
253         try {
254             call = (Call) service.createCall();
255         } catch (ServiceException e) {
256             throw new AdminException("Unable to create Call", e);
257         }
258         call.setTargetEndpointAddress(url);
259         call.setOperation(operation);
260         return call;
261     }
262 
263     protected String[] buildInputArray(InputStream bpel, InputStream wsdl,
264             List partnerWSDL) throws IOException {
265 
266         int length = partnerWSDL == null ? 2 : partnerWSDL.size() + 2;
267         String[] result = new String[length];
268 
269         result[0] = getString(bpel);
270         result[1] = getString(wsdl);
271 
272         for (int i = 0, j = 2; i < partnerWSDL.size(); i++, j++) {
273             InputStream is = (InputStream) partnerWSDL.get(i);
274             result[j] = getString(is);
275         }
276 
277         return result;
278     }
279 
280     /***
281      * Copies an <code>InputStream</code> into a <code>String</code>.
282      * 
283      * @param is
284      *            the <code>InputStream</code> to be read from.
285      * @return a <code>String</code>
286      * @throws IOException
287      *             in case of an I/O problem
288      */
289     protected String getString(InputStream is) throws IOException {
290 
291         ByteArrayOutputStream out = new ByteArrayOutputStream();
292         CopyUtils.copy(is, out);
293         return out.toString();
294     }
295 }