View Javadoc

1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
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 net.sf.nxqd.soap;
17  
18  import java.util.logging.Level;
19  import java.util.logging.Logger;
20  import java.util.ArrayList;
21  import java.util.Map;
22  import java.util.List;
23  import java.util.Timer;
24  
25  import org.w3c.dom.Document;
26  
27  import net.sf.nxqd.NxqdException;
28  import net.sf.nxqd.common.NxqdUtils;
29  import net.sf.nxqd.event.NxqdEvent;
30  import net.sf.nxqd.event.NxqdContainerEvent;
31  import net.sf.nxqd.event.NxqdManagerEvent;
32  import net.sf.nxqd.event.NxqdEventListener;
33  import net.sf.nxqd.event.NxqdManagerEventListener;
34  import net.sf.nxqd.event.NxqdContainerEventListener;
35  
36  // axis imports
37  import org.apache.axis.client.Call;
38  import org.apache.axis.client.Service;
39  import org.apache.axis.Constants;
40  
41  import javax.xml.namespace.QName;
42  import javax.xml.rpc.ParameterMode;
43  
44  public class AxisConnector {
45  
46      /**
47       * The constant <code>NXQD_QNAME</code> describes the namespace for
48       * the SOAP server functionality.
49       *
50       */
51      private static final String NXQD_QNAME = "http://nxqd.sourceforge.net";
52  
53      /**
54       * The constant <code>NXQD_PROTOCOL</code> describes the communication protocol
55       * which will be used to communicate with the server.
56       *
57       */
58      private static final String NXQD_PROTOCOL = "http://";
59  
60      /**
61       * The variable <code>logger</code> is used for logging events.
62       *
63       */
64      private static Logger logger = Logger.getLogger(AxisConnector.class.getName());
65  
66      private String hostName;
67  
68      private String hostPort;
69  
70      private String sessionId;
71  
72      public AxisConnector(String host, String port) {
73          this.hostName = host;
74          this.hostPort = port;
75      }
76  
77      /**
78       * The <code>getHostName</code> method returns the host name
79       * of the machine running the Nxqd server.
80       *
81       * @return a <code>String</code> value
82       */
83      public final String getHostName() {
84  	return hostName;
85      }
86  
87      /**
88       * The <code>getHostPort</code> method returns the host port
89       * of the remote Nxqd server.
90       *
91       * @return an <code>String</code> value
92       */
93      public final String getHostPort() {
94  	return hostPort;
95      }
96  
97      public boolean isConnected() {
98  	return (sessionId != null);
99      }
100 
101     public void connect() throws NxqdException {
102 	if (sessionId!=null) {
103 	    throw new NxqdException("Already connected");
104 	}
105 	sessionId = (String)invoke("register-client",
106 				   Constants.XSD_STRING,
107 				   new String[0],
108 				   new Object[0]);
109     }
110 
111     public void disconnect() throws NxqdException {
112 	if (sessionId==null) {
113 	    throw new NxqdException("Already disconnected");
114 	}
115 	logger.info("Disconnecting "+getSessionId());
116 	invoke("unregister-client",
117 	       Constants.XSD_STRING,
118 	       new String[]{"session-id"},
119 	       new Object[]{getSessionId()});
120 	sessionId = null;
121     }
122 
123     /**
124      * The <code>getSessionId</code> method returns the session id which
125      * identifies the client for this session.
126      *
127      * @return a <code>String</code> value
128      */
129     public String getSessionId() {
130 	return sessionId;
131     }
132 
133     public Object invoke(String operationName, Object[] parameters) throws NxqdException {
134 	return invoke(operationName,
135 		      null,
136 		      new String[0],
137 		      parameters);
138     }
139 
140     public Object invoke(String operationName,
141 			 String[] parameterNames,
142 			 Object[] parameters) throws NxqdException {
143 	return invoke(operationName,
144 		      Constants.XSD_STRING,
145 		      parameterNames,
146 		      parameters);
147     }
148 
149     public Object invoke(String operationName,
150 			 QName returnType, 
151 			 String[] parameterNames,
152 			 Object[] parameterValues) throws NxqdException {
153 	try {
154 	    String endpoint = NXQD_PROTOCOL+getHostName()+":"+getHostPort();
155 	    Service  service = new Service();
156 	    Call     call    = (Call) service.createCall();
157 		
158 	    call.setTargetEndpointAddress( new java.net.URL(endpoint) );
159 	    call.setOperationName(new QName(NXQD_QNAME, operationName));
160 		
161 	    for (int i = 0; i < parameterNames.length; i++) {
162 		call.addParameter(parameterNames[i], 
163 				  Constants.XSD_STRING,
164 				  ParameterMode.IN);
165 	    }
166 	    if (returnType != null) {
167 		call.setReturnType(returnType);
168 	    }
169 
170 	    Object ret = call.invoke( parameterValues );
171 	    return ret;
172 	} catch (Exception e) {
173 	    logger.severe("Error performing invoke ("+e+")");
174 	    throw new NxqdException(e);
175 	}
176 
177     }
178 
179     public Object invoke(String operationName,
180 			 QName returnType, 
181 			 String[] parameterNames,
182 			 QName[] parameterTypes,
183 			 Object[] parameterValues) throws NxqdException {
184 	try {
185 	    String endpoint = NXQD_PROTOCOL+getHostName()+":"+getHostPort();
186 	    Service  service = new Service();
187 	    Call     call    = (Call) service.createCall();
188 		
189 	    call.setTargetEndpointAddress( new java.net.URL(endpoint) );
190 	    call.setOperationName(new QName(NXQD_QNAME, operationName));
191 		
192 	    for (int i = 0; i < parameterNames.length; i++) {
193 		call.addParameter(parameterNames[i], 
194 				  parameterTypes[i],
195 				  ParameterMode.IN);
196 	    }
197 	    if (returnType != null) {
198 		call.setReturnType(returnType);
199 	    }
200 
201 	    Object ret = call.invoke( parameterValues );
202 	    return ret;
203 	} catch (Exception e) {
204 	    logger.severe("Error performing invoke ("+e+")");
205 	    throw new NxqdException(e);
206 	}
207 
208     }
209 
210 }