KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > services > RemoteServiceContextFactory


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.services;
17
18 import org.jmanage.core.auth.User;
19 import org.jmanage.core.remote.client.HttpServiceProxy;
20 import org.jmanage.core.data.OperationResultData;
21
22 /**
23  * Factory to create ServiceContext object for invoking service methods
24  * in a remote jmanage instance.
25  * <p>
26  * The first thing to do is to set the URL of the remote jManage instance:
27  * <p>
28  * <code>
29  * RemoteServiceContextFactory.setJManageURL("http://localhost:9090");
30  * </code>
31  * <p>
32  * Next an instance of ServiceContext is required to invoke a service operation:
33  * <p>
34  * <code>
35  * ServiceContext context = RemoteServiceContextFactory.getServiceContext(
36  * "admin", "123456",
37                 "testApp1", "jmanage:name=DataFormat,type=test");
38  * </code>
39  * <p>
40  * And finaly get a service object and execute a method:
41  * <p>
42  * <code>
43  * MBeanService mbeanService = ServiceFactory.getMBeanService(); <br/>
44  * OperationResultData[] result =
45  * mbeanService.invoke(context, "retrieveXMLData", new String[]{});
46  * </code>
47  * <p>
48  * Date: Nov 22, 2005
49  * @author Rakesh Kalra
50  */

51 public class RemoteServiceContextFactory {
52
53     static{
54         ServiceFactory.init(ServiceFactory.MODE_REMOTE);
55     }
56
57     /**
58      * Set the URL at which jManage is running. This needs to be done
59      * before calling a service layer method.
60      *
61      * @param jmanageURL remote jManage URL, e.g., http://localhost:9090
62      */

63     public static void setJManageURL(String JavaDoc jmanageURL){
64         HttpServiceProxy.setRemoteURL(jmanageURL);
65     }
66
67     /**
68      * Get a ServiceContext for invoking a Service function.
69      * The ServiceContext is built based on the given username,
70      * applicationName and the MBean name.
71      *
72      * @param username user that will be used for invoking service function
73      * @param password password for the given user
74      * @param applicationName application name (optional)
75      * @param mbeanName MBean name (optional)
76      * @return instance of ServiceContext which can be used to invoke
77      * service layer methods
78      */

79     public static ServiceContext getServiceContext(String JavaDoc username,
80                                                    String JavaDoc password,
81                                                    String JavaDoc applicationName,
82                                                    String JavaDoc mbeanName){
83         ServiceContextImpl context = new ServiceContextImpl();
84         /* set user */
85         User user = new User(username,
86                 null, null, null, 0);
87         user.setPlaintextPassword(password);
88         context.setUser(user);
89         /* set application name */
90         context.setApplicationName(applicationName);
91         /* set mbean name */
92         context.setMBeanName(mbeanName);
93         return context;
94     }
95
96     /**
97      * runs a test against the jmanage test application.
98      */

99     public static void main(String JavaDoc[] args){
100         RemoteServiceContextFactory.setJManageURL("http://localhost:9090");
101         ServiceContext context = RemoteServiceContextFactory.getServiceContext(
102                 "admin", "123456",
103                 "testApp1", "jmanage:name=DataFormat,type=test");
104         MBeanService mbeanService = ServiceFactory.getMBeanService();
105         OperationResultData[] result =
106                 mbeanService.invoke(context, "retrieveXMLData", new String JavaDoc[]{});
107         /* this is not a cluster, so the number of results must be 1 */
108         assert result.length == 1;
109
110         System.out.println(result[0].isError()?"Error": "OK");
111         System.out.println(result[0].getDisplayOutput());
112     }
113 }
114
Popular Tags