/** * $AIST_Release: 4.2.4 $ * $AIST_Copyright: * Copyright 2003, 2004, 2005, 2006 Grid Technology Research Center, * National Institute of Advanced Industrial Science and Technology * Copyright 2003, 2004, 2005, 2006 National Institute of Informatics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * $ * $RCSfile: MDS4QueryClient.java.in,v $ $Revision: 1.1 $ $Date: 2008/08/19 10:53:28 $ */ package org.apgrid.grpc.ng; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import javax.xml.rpc.Stub; import org.apache.axis.message.MessageElement; /* Modified not to use PosixParser, and CommandLine import org.apache.commons.cli.ParseException; */ import org.globus.wsrf.WSRFConstants; import org.globus.wsrf.client.BaseClient; import org.oasis.wsrf.properties.InvalidQueryExpressionFaultType; import org.oasis.wsrf.properties.InvalidResourcePropertyQNameFaultType; import org.oasis.wsrf.properties.QueryEvaluationErrorFaultType; import org.oasis.wsrf.properties.QueryExpressionType; import org.oasis.wsrf.properties.QueryResourceProperties_Element; import org.oasis.wsrf.properties.QueryResourceProperties_PortType; import org.oasis.wsrf.properties.QueryResourcePropertiesResponse; import org.oasis.wsrf.properties.ResourceUnknownFaultType; import org.oasis.wsrf.properties.UnknownQueryExpressionDialectFaultType; import org.oasis.wsrf.properties.WSResourcePropertiesServiceAddressingLocator; import org.apache.axis.message.addressing.Address; import org.apache.axis.message.addressing.EndpointReferenceType; import org.apache.axis.types.URI.MalformedURIException; import org.globus.wsrf.impl.security.authorization.HostAuthorization; import org.globus.wsrf.impl.security.authorization.IdentityAuthorization; import org.gridforum.gridrpc.GrpcException; public class MDS4QueryClient extends BaseClient { /* variables */ private static final int INVALID_TIMEOUT = -1; private int timeout = INVALID_TIMEOUT; /** * */ private MDS4QueryClient() { /* nothing */ } /** * @param mdsServiceUrlString * @param subject * @return * @throws GrpcException * @throws Exception */ static MDS4QueryClient newMDS4QueryClient( final String mdsServiceUrlString, final String subject) throws GrpcException { String authTarget = (subject != null ? subject : "host"); final MDS4QueryClient client = new MDS4QueryClient(); /* Modified not to use PosixParser, and CommandLine final String[] args = new String[] {"-a", "-z", authTarget, "-s", mdsServiceUrlString}; try { client.parse(args); } catch (final ParseException e) { throw e; } catch (final Exception e) { throw e; } */ /* set Anonymous */ client.anonymous = Boolean.TRUE; /* set AUTHZ */ if (authTarget.equals("host")) { client.authorization = HostAuthorization.getInstance(); } else { client.authorization = new IdentityAuthorization(authTarget); } /* set EPR */ client.endpoint = new EndpointReferenceType(); try { client.endpoint.setAddress(new Address(mdsServiceUrlString)); } catch (MalformedURIException e) { throw new NgInitializeGrpcHandleException(e); } return client; } /** * @param xPath * @return * @throws Exception */ MessageElement[] query(final String xPath) throws GrpcException { final WSResourcePropertiesServiceAddressingLocator locator = new WSResourcePropertiesServiceAddressingLocator(); final QueryExpressionType query = new QueryExpressionType(); try { query.setDialect(WSRFConstants.XPATH_1_DIALECT); } catch (MalformedURIException e) { throw new NgInitializeGrpcHandleException(e); } query.setValue(xPath); QueryResourceProperties_PortType port; try { port = locator.getQueryResourcePropertiesPort(getEPR()); } catch (ServiceException e1) { throw new NgInitializeGrpcHandleException(e1); } try { this.setOptions((Stub)port); } catch (Exception e2) { throw new NgInitializeGrpcHandleException(e2); } final QueryResourceProperties_Element request = new QueryResourceProperties_Element(); request.setQueryExpression(query); /* set timeout of query (If it's necessary) */ if (this.timeout != INVALID_TIMEOUT) { ((org.apache.axis.client.Stub)port).setTimeout(this.timeout * 1000); } QueryResourcePropertiesResponse response; try { response = port.queryResourceProperties(request); } catch (InvalidResourcePropertyQNameFaultType e3) { throw new NgInitializeGrpcHandleException(e3); } catch (InvalidQueryExpressionFaultType e3) { throw new NgInitializeGrpcHandleException(e3); } catch (QueryEvaluationErrorFaultType e3) { throw new NgInitializeGrpcHandleException(e3); } catch (ResourceUnknownFaultType e3) { throw new NgInitializeGrpcHandleException(e3); } catch (UnknownQueryExpressionDialectFaultType e3) { throw new NgInitializeGrpcHandleException(e3); } catch (RemoteException e3) { throw new NgInitializeGrpcHandleException(e3); } return response.get_any(); } /** * @param timeout */ protected void setTimeout(int timeout) { this.timeout = timeout; } /* // for test public static void main(final String[] args) throws Exception { final MDS4QueryClient client = MDS4QueryClient.newMDS4QueryClient("https://example.org:8443/wsrf/services/org/apgrid/ninf/ng4/grpcinfo/GrpcInfoService"); final String xPath = "//*[namespace-uri()=" + "'http://ninf.apgrid.org/ng4/grpcinfo/types' and local-name()='grpcInfoSet']" + "/*[namespace-uri()='http://ninf.apgrid.org/ng4' and local-name()='hostInfo']"; System.out.println(client.query(xPath)); } */ }