KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > webservice > util > WebServiceFactory


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.webservice.util;
18
19 import java.io.InputStream JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import javax.xml.rpc.ServiceException JavaDoc;
23
24 import org.alfresco.webservice.accesscontrol.AccessControlServiceLocator;
25 import org.alfresco.webservice.accesscontrol.AccessControlServiceSoapBindingStub;
26 import org.alfresco.webservice.action.ActionServiceLocator;
27 import org.alfresco.webservice.action.ActionServiceSoapBindingStub;
28 import org.alfresco.webservice.administration.AdministrationServiceLocator;
29 import org.alfresco.webservice.administration.AdministrationServiceSoapBindingStub;
30 import org.alfresco.webservice.authentication.AuthenticationServiceLocator;
31 import org.alfresco.webservice.authentication.AuthenticationServiceSoapBindingStub;
32 import org.alfresco.webservice.authoring.AuthoringServiceLocator;
33 import org.alfresco.webservice.authoring.AuthoringServiceSoapBindingStub;
34 import org.alfresco.webservice.classification.ClassificationServiceLocator;
35 import org.alfresco.webservice.classification.ClassificationServiceSoapBindingStub;
36 import org.alfresco.webservice.content.ContentServiceLocator;
37 import org.alfresco.webservice.content.ContentServiceSoapBindingStub;
38 import org.alfresco.webservice.repository.RepositoryServiceLocator;
39 import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 import com.sun.org.apache.bcel.internal.util.ClassLoader;
44
45 /**
46  *
47  *
48  * @author Roy Wetherall
49  */

50 public final class WebServiceFactory
51 {
52     /** Log */
53     private static Log logger = LogFactory.getLog(WebServiceFactory.class);
54     
55     /** Property file name */
56     private static final String JavaDoc PROPERTY_FILE_NAME = "alfresco/webserviceclient.properties";
57     private static final String JavaDoc REPO_LOCATION = "repository.location";
58     
59     /** Default endpoint address **/
60     private static final String JavaDoc DEFAULT_ENDPOINT_ADDRESS = "http://localhost:8080";
61     
62     /** Service addresses */
63     private static final String JavaDoc AUTHENTICATION_SERVICE_ADDRESS = "/alfresco/api/AuthenticationService";
64     private static final String JavaDoc REPOSITORY_SERVICE_ADDRESS = "/alfresco/api/RepositoryService";
65     private static final String JavaDoc CONTENT_SERVICE_ADDRESS = "/alfresco/api/ContentService";
66     private static final String JavaDoc AUTHORING_SERVICE_ADDRESS = "/alfresco/api/AuthoringService";
67     private static final String JavaDoc CLASSIFICATION_SERVICE_ADDRESS = "/alfresco/api/ClassificationService";
68     private static final String JavaDoc ACTION_SERVICE_ADDRESS = "/alfresco/api/ActionService";
69     private static final String JavaDoc ACCESS_CONTROL_ADDRESS = "/alfresco/api/AccessControlService";
70     private static final String JavaDoc ADMINISTRATION_ADDRESS = "/alfresco/api/AdministrationService";
71     
72     /** Services */
73     private static AuthenticationServiceSoapBindingStub authenticationService = null;
74     private static RepositoryServiceSoapBindingStub repositoryService = null;
75     private static ContentServiceSoapBindingStub contentService = null;
76     private static AuthoringServiceSoapBindingStub authoringService = null;
77     private static ClassificationServiceSoapBindingStub classificationService = null;
78     private static ActionServiceSoapBindingStub actionService = null;
79     private static AccessControlServiceSoapBindingStub accessControlService = null;
80     private static AdministrationServiceSoapBindingStub administrationService = null;
81     
82     /**
83      * Get the authentication service
84      *
85      * @return
86      */

87     public static AuthenticationServiceSoapBindingStub getAuthenticationService()
88     {
89         if (authenticationService == null)
90         {
91             try
92             {
93                 // Get the authentication service
94
AuthenticationServiceLocator locator = new AuthenticationServiceLocator();
95                 locator.setAuthenticationServiceEndpointAddress(getEndpointAddress() + AUTHENTICATION_SERVICE_ADDRESS);
96                 authenticationService = (AuthenticationServiceSoapBindingStub)locator.getAuthenticationService();
97             }
98             catch (ServiceException JavaDoc jre)
99             {
100                 if (logger.isDebugEnabled() == true)
101                 {
102                     if (jre.getLinkedCause() != null)
103                     {
104                         jre.getLinkedCause().printStackTrace();
105                     }
106                 }
107    
108                 throw new WebServiceException("Error creating authentication service: " + jre.getMessage(), jre);
109             }
110             
111             // Time out after a minute
112
authenticationService.setTimeout(60000);
113         }
114         
115         return authenticationService;
116     }
117     
118     /**
119      * Get the repository service
120      *
121      * @return
122      */

123     public static RepositoryServiceSoapBindingStub getRepositoryService()
124     {
125         if (repositoryService == null)
126         {
127             try
128             {
129                 // Get the repository service
130
RepositoryServiceLocator locator = new RepositoryServiceLocator(AuthenticationUtils.getEngineConfiguration());
131                 locator.setRepositoryServiceEndpointAddress(getEndpointAddress() + REPOSITORY_SERVICE_ADDRESS);
132                 repositoryService = (RepositoryServiceSoapBindingStub)locator.getRepositoryService();
133             }
134             catch (ServiceException JavaDoc jre)
135             {
136                 if (logger.isDebugEnabled() == true)
137                 {
138                     if (jre.getLinkedCause() != null)
139                     {
140                         jre.getLinkedCause().printStackTrace();
141                     }
142                 }
143    
144                 throw new WebServiceException("Error creating repositoryService service: " + jre.getMessage(), jre);
145             }
146             
147             // Time out after a minute
148
repositoryService.setTimeout(60000);
149         }
150         
151         return repositoryService;
152     }
153     
154     /**
155      * Get the authoring service
156      *
157      * @return
158      */

159     public static AuthoringServiceSoapBindingStub getAuthoringService()
160     {
161         if (authoringService == null)
162         {
163             try
164             {
165                 // Get the authoring service
166
AuthoringServiceLocator locator = new AuthoringServiceLocator(AuthenticationUtils.getEngineConfiguration());
167                 locator.setAuthoringServiceEndpointAddress(getEndpointAddress() + AUTHORING_SERVICE_ADDRESS);
168                 authoringService = (AuthoringServiceSoapBindingStub)locator.getAuthoringService();
169             }
170             catch (ServiceException JavaDoc jre)
171             {
172                 if (logger.isDebugEnabled() == true)
173                 {
174                     if (jre.getLinkedCause() != null)
175                     {
176                         jre.getLinkedCause().printStackTrace();
177                     }
178                 }
179    
180                 throw new WebServiceException("Error creating authoring service: " + jre.getMessage(), jre);
181             }
182             
183             // Time out after a minute
184
authoringService.setTimeout(60000);
185         }
186         
187         return authoringService;
188     }
189     
190     /**
191      * Get the classification service
192      *
193      * @return
194      */

195     public static ClassificationServiceSoapBindingStub getClassificationService()
196     {
197         if (classificationService == null)
198         {
199             try
200             {
201                 // Get the classification service
202
ClassificationServiceLocator locator = new ClassificationServiceLocator(AuthenticationUtils.getEngineConfiguration());
203                 locator.setClassificationServiceEndpointAddress(getEndpointAddress() + CLASSIFICATION_SERVICE_ADDRESS);
204                 classificationService = (ClassificationServiceSoapBindingStub)locator.getClassificationService();
205             }
206             catch (ServiceException JavaDoc jre)
207             {
208                 if (logger.isDebugEnabled() == true)
209                 {
210                     if (jre.getLinkedCause() != null)
211                     {
212                         jre.getLinkedCause().printStackTrace();
213                     }
214                 }
215    
216                 throw new WebServiceException("Error creating classification service: " + jre.getMessage(), jre);
217             }
218             
219             // Time out after a minute
220
classificationService.setTimeout(60000);
221         }
222         
223         return classificationService;
224     }
225     
226     /**
227      * Get the action service
228      *
229      * @return
230      */

231     public static ActionServiceSoapBindingStub getActionService()
232     {
233         if (actionService == null)
234         {
235             try
236             {
237                 // Get the action service
238
ActionServiceLocator locator = new ActionServiceLocator(AuthenticationUtils.getEngineConfiguration());
239                 locator.setActionServiceEndpointAddress(getEndpointAddress() + ACTION_SERVICE_ADDRESS);
240                 actionService = (ActionServiceSoapBindingStub)locator.getActionService();
241             }
242             catch (ServiceException JavaDoc jre)
243             {
244                 if (logger.isDebugEnabled() == true)
245                 {
246                     if (jre.getLinkedCause() != null)
247                     {
248                         jre.getLinkedCause().printStackTrace();
249                     }
250                 }
251    
252                 throw new WebServiceException("Error creating action service: " + jre.getMessage(), jre);
253             }
254             
255             // Time out after a minute
256
actionService.setTimeout(60000);
257         }
258         
259         return actionService;
260     }
261     
262     /**
263      * Get the content service
264      *
265      * @return the content service
266      */

267     public static ContentServiceSoapBindingStub getContentService()
268     {
269         if (contentService == null)
270         {
271             try
272             {
273                 // Get the content service
274
ContentServiceLocator locator = new ContentServiceLocator(AuthenticationUtils.getEngineConfiguration());
275                 locator.setContentServiceEndpointAddress(getEndpointAddress() + CONTENT_SERVICE_ADDRESS);
276                 contentService = (ContentServiceSoapBindingStub)locator.getContentService();
277             }
278             catch (ServiceException JavaDoc jre)
279             {
280                 if (logger.isDebugEnabled() == true)
281                 {
282                     if (jre.getLinkedCause() != null)
283                     {
284                         jre.getLinkedCause().printStackTrace();
285                     }
286                 }
287    
288                 throw new WebServiceException("Error creating content service: " + jre.getMessage(), jre);
289             }
290             
291             // Time out after a minute
292
contentService.setTimeout(60000);
293         }
294         
295         return contentService;
296     }
297     
298     /**
299      * Get the access control service
300      *
301      * @return the access control service
302      */

303     public static AccessControlServiceSoapBindingStub getAccessControlService()
304     {
305         if (accessControlService == null)
306         {
307             try
308             {
309                 // Get the access control service
310
AccessControlServiceLocator locator = new AccessControlServiceLocator(AuthenticationUtils.getEngineConfiguration());
311                 locator.setAccessControlServiceEndpointAddress(getEndpointAddress() + ACCESS_CONTROL_ADDRESS);
312                 accessControlService = (AccessControlServiceSoapBindingStub)locator.getAccessControlService();
313             }
314             catch (ServiceException JavaDoc jre)
315             {
316                 if (logger.isDebugEnabled() == true)
317                 {
318                     if (jre.getLinkedCause() != null)
319                     {
320                         jre.getLinkedCause().printStackTrace();
321                     }
322                 }
323    
324                 throw new WebServiceException("Error creating access control service: " + jre.getMessage(), jre);
325             }
326             
327             // Time out after a minute
328
accessControlService.setTimeout(60000);
329         }
330         
331         return accessControlService;
332     }
333     
334     /**
335      * Get the administation service
336      *
337      * @return the administration service
338      */

339     public static AdministrationServiceSoapBindingStub getAdministrationService()
340     {
341         if (administrationService == null)
342         {
343             try
344             {
345                 // Get the adminstration service
346
AdministrationServiceLocator locator = new AdministrationServiceLocator(AuthenticationUtils.getEngineConfiguration());
347                 locator.setAdministrationServiceEndpointAddress(getEndpointAddress() + ADMINISTRATION_ADDRESS);
348                 administrationService = (AdministrationServiceSoapBindingStub)locator.getAdministrationService();
349             }
350             catch (ServiceException JavaDoc jre)
351             {
352                 if (logger.isDebugEnabled() == true)
353                 {
354                     if (jre.getLinkedCause() != null)
355                     {
356                         jre.getLinkedCause().printStackTrace();
357                     }
358                 }
359    
360                 throw new WebServiceException("Error creating administration service: " + jre.getMessage(), jre);
361             }
362             
363             // Time out after a minute
364
administrationService.setTimeout(60000);
365         }
366         
367         return administrationService;
368     }
369     
370     /**
371      * Gets the end point address from the properties file
372      *
373      * @return
374      */

375     private static String JavaDoc getEndpointAddress()
376     {
377         String JavaDoc endPoint = DEFAULT_ENDPOINT_ADDRESS;
378         
379         InputStream JavaDoc is = ClassLoader.getSystemResourceAsStream(PROPERTY_FILE_NAME);
380         if (is != null)
381         {
382             Properties JavaDoc props = new Properties JavaDoc();
383             try
384             {
385                 props.load(is);
386                 endPoint = props.getProperty(REPO_LOCATION);
387             }
388             catch (Exception JavaDoc e)
389             {
390                 // Do nothing, just use the default endpoint
391
if (logger.isDebugEnabled() == true)
392                 {
393                     logger.debug("Unable to file web service client proerties file. Using default.");
394                 }
395             }
396         }
397         
398         return endPoint;
399     }
400 }
401
Popular Tags