You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-user@james.apache.org by Riley Scott <ri...@live.com> on 2013/07/18 09:46:17 UTC

James 3: Can I add users remotely with JMX?

Hi,

We have a James 3 server running in our test lab. I need to add users remotely. I need to create a Java app that will remotely connect to the James 3 server and add users via JMX.  Is this possible with JMX? If so, can you point me to code samples / docs?  Thanks.
 		 	   		  

Re: James 3: Can I add users remotely with JMX?

Posted by Joshua Armstrong <ja...@centonline.com>.
I just wrote that for my company.  I sent the boss an e-mail asking 
permission to share the code.

On 7/18/2013 2:46 AM, Riley Scott wrote:
> Hi,
>
> We have a James 3 server running in our test lab. I need to add users remotely. I need to create a Java app that will remotely connect to the James 3 server and add users via JMX.  Is this possible with JMX? If so, can you point me to code samples / docs?  Thanks.
>   		 	   		

-- 
Joshua M. Armstrong
Software Engineer
Centurion, Inc.


---------------------------------------------------------------------
To unsubscribe, e-mail: server-user-unsubscribe@james.apache.org
For additional commands, e-mail: server-user-help@james.apache.org


Re: James 3: Can I add users remotely with JMX?

Posted by Joshua Armstrong <ja...@centonline.com>.
Here's the code (snippets) I wrote to add users remotely via JMX:

			/*
			 * Connect the server probe
			 */
			try {
				localInstanceProbe = new JmxServerProbe("hostname", 9999);
			} catch (IOException ioex) {
				System.err.println("Error connecting to JMX agent!");
				ioex.printStackTrace();
				System.exit(3);
			} catch (InterruptedException iex) {
				iex.printStackTrace();
				System.exit(1);
			} catch (Exception ex) {
				ex.printStackTrace();
				System.exit(1);
			}

		// check if mailbox exists
		try {
			String mailbox = emailAcct == null || emailAcct.length() < 1 ? (email == null
					|| email.length() < 1 ? mb : email)
					: emailAcct;
			String password = emailPass == null || emailPass.length() < 1 ? nmePass
					: emailPass;
			if (caller.checkIfMailboxExists(mailbox)) {
				// reset password
				caller.getLocalInstanceProbe().setPassword(mailbox, password);
			}

			else {
				// create new mailbox
				String domain = mailbox.substring(mailbox.indexOf('@') + 1,
						mailbox.length());
				Boolean domainExists = false;

				try {
					domainExists = caller.checkIfDomainExists(domain);
				}

				catch (NullPointerException npex) {
					// This is thrown by checkIfDomainExists because instead of
					// returning a reference to an empty array,
					// JmxServerProbe.listDomains() returns a null pointer
					domainExists = false;
				}

				if (!domainExists) {
					// if domain doesn't exist, create it
					caller.getLocalInstanceProbe().addDomain(domain);
				}

				caller.getLocalInstanceProbe().addUser(mailbox, password);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

On 7/18/2013 2:46 AM, Riley Scott wrote:
> Hi,
>
> We have a James 3 server running in our test lab. I need to add users remotely. I need to create a Java app that will remotely connect to the James 3 server and add users via JMX.  Is this possible with JMX? If so, can you point me to code samples / docs?  Thanks.
>   		 	   		

-- 
Joshua M. Armstrong
Software Engineer
Centurion, Inc.


---------------------------------------------------------------------
To unsubscribe, e-mail: server-user-unsubscribe@james.apache.org
For additional commands, e-mail: server-user-help@james.apache.org


Re: James 3: Can I add users remotely with JMX?

Posted by Phillip Odam <ph...@nitorgroup.com>.
Here's something to get started.

Firstly, yes you can create new users via JMX.

For performing the same operations that the james cli offers you'll find 
everything you need in james-server-cli-3.0-beta4.jar. Particular class 
of interest is org.apache.james.cli.probe.impl.JmxServerProbe

To find this out take a look at the source of the james cli shell 
script, it refers to the class org.apache.james.cli.ServerCmd

Depending on your remote connection, as JMX uses two ports, a registry 
and server port you may want to lock down what those two ports are as 
opposed to let server port be dynamically chosen... this can be a bit of 
a hassle when working through firewalls.

One way to lock down the server port in james, change 
conf/META-INF/org/apache/james/spring-server.xml

Out of the box JMX configuration in apache james 3.0-beta4

     <!--
       JMX Server
      -->
     <bean 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="ignoreUnresolvablePlaceholders" value="true"/>
         <property name="location" value="classpath:jmx.properties"/>
     </bean>
     <bean id="mbeanserver" 
class="org.springframework.jmx.support.MBeanServerFactoryBean" >
         <property name="locateExistingServerIfPossible" value="true"/>
         <property name="registerWithFactory" value="true"/>
     </bean>
     <bean id="serverConnector" 
class="org.springframework.jmx.support.ConnectorServerFactoryBean" 
depends-on="registry">
         <property name="objectName" value="connector:name=rmi" />
*<property name="serviceUrl" 
value="service:jmx:rmi://${jmx.address}/jndi/rmi://${jmx.address}:${jmx.port}/jmxrmi" 
/>*
     </bean>
     <bean id="registry" 
class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
*<property name="port" value="${jmx.port}" />*
         <property name="serverSocketFactory" 
ref="restrictedRmiSocketFactory"/>
         <property name="clientSocketFactory" 
ref="restrictedRmiSocketFactory"/>
     </bean>
     <bean id="restrictedRmiSocketFactory" 
class="org.apache.james.util.RestrictingRMISocketFactory">
         <constructor-arg value="${jmx.address}"/>
     </bean>

Change the bold lines to the following.

<property name="serviceUrl" 
value="service:jmx:rmi://${jmx.address}:${jmx.server.port}/jndi/rmi://${jmx.address}:${jmx.registry.port}/jmxrmi" 
/>

<property name="port" value="${jmx.registry.port}" />

And in jmx.properties, change

jmx.address=
jmx.port=

To

jmx.address=
jmx.registry.port=
jmx.server.port=

Hope this helps

Thanks
Phillip

On 7/18/13 3:46 AM, Riley Scott wrote:
> Hi,
>
> We have a James 3 server running in our test lab. I need to add users remotely. I need to create a Java app that will remotely connect to the James 3 server and add users via JMX.  Is this possible with JMX? If so, can you point me to code samples / docs?  Thanks.
>