001: /**
002: * Copyright 2004-2005 jManage.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.jmanage.core.services;
016:
017: import org.jmanage.core.auth.User;
018: import org.jmanage.core.remote.client.HttpServiceProxy;
019: import org.jmanage.core.data.OperationResultData;
020:
021: /**
022: * Factory to create ServiceContext object for invoking service methods
023: * in a remote jmanage instance.
024: * <p>
025: * The first thing to do is to set the URL of the remote jManage instance:
026: * <p>
027: * <code>
028: * RemoteServiceContextFactory.setJManageURL("http://localhost:9090");
029: * </code>
030: * <p>
031: * Next an instance of ServiceContext is required to invoke a service operation:
032: * <p>
033: * <code>
034: * ServiceContext context = RemoteServiceContextFactory.getServiceContext(
035: * "admin", "123456",
036: "testApp1", "jmanage:name=DataFormat,type=test");
037: * </code>
038: * <p>
039: * And finaly get a service object and execute a method:
040: * <p>
041: * <code>
042: * MBeanService mbeanService = ServiceFactory.getMBeanService(); <br/>
043: * OperationResultData[] result =
044: * mbeanService.invoke(context, "retrieveXMLData", new String[]{});
045: * </code>
046: * <p>
047: * Date: Nov 22, 2005
048: * @author Rakesh Kalra
049: */
050: public class RemoteServiceContextFactory {
051:
052: static {
053: ServiceFactory.init(ServiceFactory.MODE_REMOTE);
054: }
055:
056: /**
057: * Set the URL at which jManage is running. This needs to be done
058: * before calling a service layer method.
059: *
060: * @param jmanageURL remote jManage URL, e.g., http://localhost:9090
061: */
062: public static void setJManageURL(String jmanageURL) {
063: HttpServiceProxy.setRemoteURL(jmanageURL);
064: }
065:
066: /**
067: * Get a ServiceContext for invoking a Service function.
068: * The ServiceContext is built based on the given username,
069: * applicationName and the MBean name.
070: *
071: * @param username user that will be used for invoking service function
072: * @param password password for the given user
073: * @param applicationName application name (optional)
074: * @param mbeanName MBean name (optional)
075: * @return instance of ServiceContext which can be used to invoke
076: * service layer methods
077: */
078: public static ServiceContext getServiceContext(String username,
079: String password, String applicationName, String mbeanName) {
080: ServiceContextImpl context = new ServiceContextImpl();
081: /* set user */
082: User user = new User(username, null, null, null, 0);
083: user.setPlaintextPassword(password);
084: context.setUser(user);
085: /* set application name */
086: context.setApplicationName(applicationName);
087: /* set mbean name */
088: context.setMBeanName(mbeanName);
089: return context;
090: }
091:
092: /**
093: * runs a test against the jmanage test application.
094: */
095: public static void main(String[] args) {
096: RemoteServiceContextFactory
097: .setJManageURL("http://localhost:9090");
098: ServiceContext context = RemoteServiceContextFactory
099: .getServiceContext("admin", "123456", "testApp1",
100: "jmanage:name=DataFormat,type=test");
101: MBeanService mbeanService = ServiceFactory.getMBeanService();
102: OperationResultData[] result = mbeanService.invoke(context,
103: "retrieveXMLData", new String[] {});
104: /* this is not a cluster, so the number of results must be 1 */
105: assert result.length == 1;
106:
107: System.out.println(result[0].isError() ? "Error" : "OK");
108: System.out.println(result[0].getDisplayOutput());
109: }
110: }
|