title: How to get WebSphere ConfigService in a servlet?
body: <p>I have a servlet in an enterprise application on WebSphere Application Server 7.<br>
  I want the servlet to read configuration parameters from a custom resource environment provider.</p>
  
  <p>This particular environment is not WebSphere Portal, and I'm not using Spring, but I'm trying to adapt the code from this page about using resource envirnment providers with Spring:<br>
  <a href="http://blogs.perficient.com/portals/2012/03/28/using-wps-style-resource-environment-providers-with-spring" rel="nofollow">http://blogs.perficient.com/portals/2012/03/28/using-wps-style-resource-environment-providers-with-spring</a></p>
  
  <p>I'm having a problem with the first line of my test method:</p>
  
  <pre><code>com.ibm.websphere.management.configservice.ConfigService service
      =com.ibm.websphere.management.configservice.ConfigServiceFactory.getConfigService();</code></pre>
  
  <p>The <code>getConfigService()</code> method in this line always returns <code>null</code> in my servlet. The method doesn't throw any exceptions, and no errors appear in the server logs; it just returns <code>null</code>.<br>
  (Note I show package names in the above code for clarity. In the real code, I <code>import</code> the relevant classes.)</p>
  
  <p>How do I get a <code>ConfigService</code> object in my servlet?</p>
  
  <p>The <code>ConfigServiceFactory</code> class also has a <code>createConfigService(boolean enable, java.util.Properties props)</code> method, but the Javadoc doesn't say what's expected for its arguments, and I can't find any examples using it.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I've tried using a <code>ConfigServiceProxy</code> as per <a href="http://www-01.ibm.com/support/docview.wss?uid=swg21411254" rel="nofollow">http://www-01.ibm.com/support/docview.wss?uid=swg21411254</a>, as suggested by Magic Wand, and can't get that to work either.<br>
  Details of the problems follow. Does anyone know how to make this work?</p>
  
  <p>Point 5 near the bottom of the page about using <code>ConfigServiceProxy</code> says:</p>
  
  <blockquote>
    <p>Modify the properties connectProps.setProperty(AdminClient.CONNECTOR_HOST, "localhost"); and connectProps.setProperty(AdminClient.CONNECTOR_PORT, "8880") if needed.</p>
  </blockquote>
  
  <p>But it doesn't say how to determine the correct host and port, and I haven't found that information elsewhere, so I'm just guessing.</p>
  
  <p>To find host names and ports, I've logged into the WebSphere Integrated Solutions Console, gone to Servers -> Server Types -> WebSphere application servers, clicked the app server my servlet runs on, and clicked "Ports".</p>
  
  <p>Here's the part of my code that tries to get a <code>ConfigServiceProxy</code>, in which I change only the host name and port number depending on which port I'm trying:</p>
  
  <pre><code> Properties connectProps = new Properties();
      connectProps.setProperty(AdminClient.CONNECTOR_TYPE,
          AdminClient.CONNECTOR_TYPE_SOAP);
      connectProps.setProperty(AdminClient.CONNECTOR_HOST,"localhost");
      connectProps.setProperty(AdminClient.CONNECTOR_PORT,"9634");
      AdminClient adminClient=AdminClientFactory.createAdminClient(connectProps);
      ConfigService service=new ConfigServiceProxy(adminClient);</code></pre>
  
  <p>First, I tried the only port for which the host is "localhost", because that's what the linked example uses. The port name for that is "IPC_CONNECTOR_ADDRESS", and the port number is 9634.<br>
  This produces an exception on calling <code>AdminClientFactory.createAdminClient</code>. The stack trace starts with:</p>
  
  <pre>com.ibm.websphere.management.exception.ConnectorException: ADMC0016E: The system cannot create a SOAP connector to connect to host localhost at port 9634.
      at com.ibm.websphere.management.AdminClientFactory.createAdminClientPrivileged(AdminClientFactory.java:634)
      at com.ibm.websphere.management.AdminClientFactory.access$000(AdminClientFactory.java:126)
      at com.ibm.websphere.management.AdminClientFactory$1.run(AdminClientFactory.java:209)
      at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:63)
      at com.ibm.websphere.management.AdminClientFactory.createAdminClient(AdminClientFactory.java:205)
      at com.isw.ResourceEnvironmentProviderPlaceHolderConfigurer.getConfigService(ResourceEnvironmentProviderPlaceHolderConfigurer.java:113)</pre>
  
  <p>And the nested causes are:</p>
  
  <pre>Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:56)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:527)
      at com.ibm.websphere.management.AdminClientFactory.createAdminClientPrivileged(AdminClientFactory.java:456)
      ... 38 more
  Caused by: com.ibm.websphere.management.exception.ConnectorNotAvailableException: [SOAPException: faultCode=SOAP-ENV:Client; msg=Connection reset; targetException=java.net.SocketException: Connection reset]
      at com.ibm.ws.management.connector.soap.SOAPConnectorClient.reconnect(SOAPConnectorClient.java:422)
      at com.ibm.ws.management.connector.soap.SOAPConnectorClient.(SOAPConnectorClient.java:222)
      ... 43 more
  Caused by: [SOAPException: faultCode=SOAP-ENV:Client; msg=Connection reset; targetException=java.net.SocketException: Connection reset]
      at org.apache.soap.transport.http.SOAPHTTPConnection.send(SOAPHTTPConnection.java:479)
      at org.apache.soap.rpc.Call.WASinvoke(Call.java:451)
      at com.ibm.ws.management.connector.soap.SOAPConnectorClient$4.run(SOAPConnectorClient.java:372)
      at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
      at com.ibm.ws.management.connector.soap.SOAPConnectorClient.reconnect(SOAPConnectorClient.java:365)
      ... 44 more</pre>
  
  <p>Next, I tried a port named "SOAP_CONNECTOR_ADDRESS", where the host is the server's fully qualified domain name, and the port number is 8881, so the lines setting host and port look like:</p>
  
  <pre><code> connectProps.setProperty(AdminClient.CONNECTOR_HOST,"server.company.net.au");
      connectProps.setProperty(AdminClient.CONNECTOR_PORT,"9634");</code></pre>
  
  <p>This produced an exception on trying to construct the <code>ConfigServiceProxy</code>. The stack trace starts with these lines, and has no cause:</p>
  
  <pre>javax.management.InstanceNotFoundException: WebSphere:process=InfraCluster_server1,type=ConfigService,*
      at com.ibm.websphere.management.configservice.ConfigServiceProxy.(ConfigServiceProxy.java:67)
      at com.isw.ResourceEnvironmentProviderPlaceHolderConfigurer.getConfigService(ResourceEnvironmentProviderPlaceHolderConfigurer.java:114)
      at com.isw.ResourceEnvironmentProviderPlaceHolderConfigurer.loadEnvironmentProviderProperties(ResourceEnvironmentProviderPlaceHolderConfigurer.java:205)
      at com.isw.insight.client.REPTest.doGet(REPTest.java:50)</pre>
  
  <p>Finally, I tried a port named "WC_adminhost", where the host is "*" and the port is 9062. I've tried this both with the host name set to "localhost" and the server's fully qualified domain name, and both fail with the same exception.<br>
  Like the IPC_CONNECTOR_ADDRESS port, this produces an exception on calling <code>AdminClientFactory.createAdminClient</code>. The stack trace starts with:</p>
  
  <pre>com.ibm.websphere.management.exception.ConnectorException: ADMC0016E: The system cannot create a SOAP connector to connect to host server.company.net.au at port 9062.
      at com.ibm.websphere.management.AdminClientFactory.createAdminClientPrivileged(AdminClientFactory.java:634)
      at com.ibm.websphere.management.AdminClientFactory.access$000(AdminClientFactory.java:126)
      at com.ibm.websphere.management.AdminClientFactory$1.run(AdminClientFactory.java:209)
      at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:63)
      at com.ibm.websphere.management.AdminClientFactory.createAdminClient(AdminClientFactory.java:205)
      at com.isw.ResourceEnvironmentProviderPlaceHolderConfigurer.getConfigService(ResourceEnvironmentProviderPlaceHolderConfigurer.java:113)</pre>
  
  <p>And the nested causes are:</p>
  
  <pre>Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:56)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:527)
      at com.ibm.websphere.management.AdminClientFactory.createAdminClientPrivileged(AdminClientFactory.java:456)
      ... 36 more
  Caused by: com.ibm.websphere.management.exception.ConnectorNotAvailableException: [SOAPException: faultCode=SOAP-ENV:Client; msg=Error opening socket: java.net.ConnectException: Connection refused; targetException=java.lang.IllegalArgumentException: Error opening socket: java.net.ConnectException: Connection refused]
      at com.ibm.ws.management.connector.soap.SOAPConnectorClient.reconnect(SOAPConnectorClient.java:422)
      at com.ibm.ws.management.connector.soap.SOAPConnectorClient.(SOAPConnectorClient.java:222)
      ... 41 more
  Caused by: [SOAPException: faultCode=SOAP-ENV:Client; msg=Error opening socket: java.net.ConnectException: Connection refused; targetException=java.lang.IllegalArgumentException: Error opening socket: java.net.ConnectException: Connection refused]
      at org.apache.soap.transport.http.SOAPHTTPConnection.send(SOAPHTTPConnection.java:475)
      at org.apache.soap.rpc.Call.WASinvoke(Call.java:451)
      at com.ibm.ws.management.connector.soap.SOAPConnectorClient$4.run(SOAPConnectorClient.java:372)
      at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
      at com.ibm.ws.management.connector.soap.SOAPConnectorClient.reconnect(SOAPConnectorClient.java:365)
      ... 42 more</pre>