001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.geronimo.clustering.wadi;
018
019 import org.codehaus.wadi.group.Dispatcher;
020 import org.codehaus.wadi.group.Peer;
021 import org.codehaus.wadi.servicespace.InvocationMetaData;
022 import org.codehaus.wadi.servicespace.ServiceAlreadyRegisteredException;
023 import org.codehaus.wadi.servicespace.ServiceProxyFactory;
024 import org.codehaus.wadi.servicespace.ServiceRegistry;
025 import org.codehaus.wadi.servicespace.ServiceSpace;
026 import org.codehaus.wadi.servicespace.admin.AdminServiceSpace;
027 import org.codehaus.wadi.servicespace.admin.AdminServiceSpaceHelper;
028
029
030 /**
031 *
032 * @version $Rev$ $Date$
033 */
034 public class NodeServiceHelper {
035 private final ServiceSpace serviceSpace;
036
037 public NodeServiceHelper(Dispatcher dispatcher) {
038 if (null == dispatcher) {
039 throw new IllegalArgumentException("dispatcher is required");
040 }
041 this.serviceSpace = getAdminServiceSpace(dispatcher);
042 }
043
044 public NodeServiceHelper(ServiceSpace serviceSpace) {
045 if (null == serviceSpace) {
046 throw new IllegalArgumentException("serviceSpace is required");
047 }
048 this.serviceSpace = serviceSpace;
049 }
050
051 public void registerNodeService(NodeService nodeService) {
052 ServiceRegistry serviceRegistry = serviceSpace.getServiceRegistry();
053 try {
054 serviceRegistry.register(NodeService.SERVICE_NAME, nodeService);
055 } catch (ServiceAlreadyRegisteredException e) {
056 throw new IllegalStateException("NodeService already registered.", e);
057 }
058 }
059
060 public NodeService getNodeServiceProxy(Peer peer) {
061 ServiceProxyFactory proxyFactory = serviceSpace.getServiceProxyFactory(NodeService.SERVICE_NAME,
062 new Class[] { NodeService.class });
063 InvocationMetaData invocationMetaData = proxyFactory.getInvocationMetaData();
064 invocationMetaData.setTargets(new Peer[] {peer});
065 return (NodeService) proxyFactory.getProxy();
066 }
067
068 protected ServiceSpace getAdminServiceSpace(Dispatcher dispatcher) {
069 AdminServiceSpaceHelper helper = new AdminServiceSpaceHelper();
070 AdminServiceSpace adminServiceSpace = helper.getAdminServiceSpace(dispatcher);
071 return adminServiceSpace;
072 }
073
074 }