Zimbra offers Open Source email server software and shared calendar for Linux and the Mac
Go Back   Zimbra :: Forums > Zimbra Collaboration Suite > Developers

Welcome to the Zimbra :: Forums!
Welcome, if you would like to post a comment please register. We also encourage you to explore all things Zimbra with our team and members of the community.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-02-2008, 08:58 PM
Loyal Member
 
Posts: 91
Default Test Delete Zimbra account coding

This is the sample code that can I found in the google.
This code is a unitTest for deleteAccount

Code:
package zimbra;

import java.util.HashMap;
import java.util.Map;

import com.zimbra.cs.account.Account;
import com.zimbra.cs.account.Provisioning;
import com.zimbra.cs.account.Provisioning.AccountBy;
import com.zimbra.cs.client.LmcSession;
import com.zimbra.cs.client.soap.LmcDeleteAccountRequest;
import com.zimbra.cs.db.DbMailbox;
import com.zimbra.cs.db.DbResults;
import com.zimbra.cs.db.DbUtil;
import com.zimbra.common.localconfig.LC;
import com.zimbra.cs.mailbox.Mailbox;
import com.zimbra.cs.mailbox.MailboxManager;
import com.zimbra.common.util.ZimbraLog;

import com.zimbra.qa.unittest.TestUtil;


/**
 * @author bburtin
 */
public class TestAccount{

    private static String USER_NAME = "TestAccount";
    // xxx move this to TestUtil
    private static String PASSWORD = "123456";

    public static void main(String [] args)
    throws Exception {
        cleanUp();
        
        Map<String, Object> attrs = new HashMap<String, Object>();
        attrs.put("https://3.244.4.16:7071/zimbraAdmin", LC.zimbra_server_hostname.value());
        attrs.put("cn", "TestAccount");
        attrs.put("displayName", "TestAccount unit test user");
        Provisioning.getInstance().createAccount(TestUtil.getAddress(USER_NAME), PASSWORD, attrs);
    }
    
    public void tearDown()
    throws Exception {
        cleanUp();
    }
    
    public void testDeleteAccount()
    throws Exception {
        ZimbraLog.test.debug("testDeleteAccount()");
        
        // Get the account and mailbox
        Account account = TestUtil.getAccount(USER_NAME);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        String dbName = DbMailbox.getDatabaseName(mbox);
        ZimbraLog.test.debug("Account=" + account.getId() + ", mbox=" + mbox.getId());
        
        // Confirm that the mailbox database exists
        DbResults results = DbUtil.executeQuery(
            "SELECT COUNT(*) FROM mailbox WHERE id = " + mbox.getId());
        System.out.println("Could not find row in mailbox table"+ " 1 " + results.getInt(1));
        
        results = DbUtil.executeQuery("SHOW DATABASES LIKE '" + dbName + "'");
        System.out.println("Could not find mailbox database"+" 1 " + results.size());
        
        // Delete the account
        LmcSession session = TestUtil.getAdminSoapSession();
        LmcDeleteAccountRequest req = new LmcDeleteAccountRequest(account.getId());
        req.setSession(session);
        req.invoke(TestUtil.getAdminSoapUrl());
        
        // Confirm that the mailbox was deleted
        results = DbUtil.executeQuery(
            "SELECT COUNT(*) FROM mailbox WHERE id = " + mbox.getId());
         System.out.println("Unexpected row in mailbox table"+" 0 "+ results.getInt(1));
    }
    
    private static void cleanUp()
    throws Exception {
        Provisioning prov = Provisioning.getInstance(); 
        Account account = prov.get(AccountBy.name, TestUtil.getAddress(USER_NAME));
        if (account != null) {
            prov.deleteAccount(account.getId());
        }
    }
}
The code no manage to executed...

Quote:
init:
deps-jar:
Compiling 1 source file to C:\Practice\Zimbra\build\classes
compile-single:
run-single:
log4j:WARN No appenders could be found for logger (zimbra.misc).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap
at com.zimbra.cs.account.AccountCache.<init>(AccountC ache.java:54)
at com.zimbra.cs.account.ldap.LdapProvisioning.<clini t>(LdapProvisioning.java:155)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.zimbra.cs.account.Provisioning.getInstance(Pro visioning.java:1538)
at zimbra.TestAccount.cleanUp(TestAccount.java:79)
at zimbra.TestAccount.main(TestAccount.java:33)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.map.LRUMap
at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)
... 7 more
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
Thank you
Reply With Quote
  #2 (permalink)  
Old 09-02-2008, 09:09 PM
Loyal Member
 
Posts: 91
Default

Problem solved : add the commons-collections-3.1.jar

But the code still didn't work:

Quote:
init:
deps-jar:
Compiling 1 source file to C:\Practice\Zimbra\build\classes
compile-single:
run-single:
log4j:WARN No appenders could be found for logger (zimbra.misc).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" com.zimbra.common.service.ServiceException: system failure: ZimbraLdapContext
ExceptionId:main:1220411105562:c823b76077bc7e50
Code:service.FAILURE
at com.zimbra.common.service.ServiceException.FAILURE (ServiceException.java:253)
at com.zimbra.cs.account.ldap.ZimbraLdapContext.<init >(ZimbraLdapContext.java:268)
at com.zimbra.cs.account.ldap.ZimbraLdapContext.<init >(ZimbraLdapContext.java:232)
at com.zimbra.cs.account.ldap.ZimbraLdapContext.<init >(ZimbraLdapContext.java:225)
at com.zimbra.cs.account.ldap.LdapProvisioning.getCon fig(LdapProvisioning.java:372)
at com.zimbra.qa.unittest.TestUtil.getDomain(TestUtil .java:106)
at com.zimbra.qa.unittest.TestUtil.getAddress(TestUti l.java:120)
at zimbra.TestAccount.cleanUp(TestAccount.java:80)
at zimbra.TestAccount.main(TestAccount.java:33)
Caused by: javax.naming.CommunicationException: localhost:389 [Root exception is java.net.ConnectException: Connection refused: connect]
at com.sun.jndi.ldap.Connection.<init>(Connection.jav a:197)
at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.jav a:118)
at com.sun.jndi.ldap.LdapClientFactory.createPooledCo nnection(LdapClientFactory.java:46)
at com.sun.jndi.ldap.pool.Connections.<init>(Connecti ons.java:97)
at com.sun.jndi.ldap.pool.Pool.getPooledConnection(Po ol.java:114)
at com.sun.jndi.ldap.LdapPoolManager.getLdapClient(Ld apPoolManager.java:310)
at com.sun.jndi.ldap.LdapClient.getInstance(LdapClien t.java:1572)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:261 6)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:287)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapC txFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Ldap CtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstanc e(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext (LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(N amingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(Init ialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.ja va:223)
at javax.naming.ldap.InitialLdapContext.<init>(Initia lLdapContext.java:134)
at com.zimbra.cs.account.ldap.ZimbraLdapContext.<init >(ZimbraLdapContext.java:247)
... 7 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
at java.net.Socket.connect(Socket.java:519)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.jndi.ldap.Connection.createSocket(Connecti on.java:336)
at com.sun.jndi.ldap.Connection.<init>(Connection.jav a:184)
... 24 more
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
I hope no file need to be added...

Thank you
Reply With Quote
  #3 (permalink)  
Old 10-22-2008, 01:40 AM
Active Member
 
Posts: 30
Default Try Soap provisioning API

try the following code:

Code:
public boolean DeleteAccount(String emailId) {
		boolean isAccountDeleted = false;

		ZimbraUser zimbraUser = new ZimbraUser();

		try {
			logger.debug("Delete account:" + emailId);
			SoapProvisioning sp = new SoapProvisioning();
			sp.soapSetURI(zimbraUser.ZIMBRA_URL_ADMIN
					+ ZimbraServlet.ADMIN_SERVICE_URI);
			sp.soapAdminAuthenticate(zimbraUser.ADMIN_USER,
					zimbraUser.ADMIN_PASSWORD);
			Account account = sp.get(AccountBy.name, emailId);
			sp.deleteAccount(account.getId());
			isAccountDeleted = true;
		} catch (Exception e) {
			isAccountDeleted = false;
			e.printStackTrace();
			logger.debug("COULD NOT DELETE ACCOUNT");
		}

		return isAccountDeleted;
	}
You can also try the script in the following link, if require bulk deletion:
Bulk Provisioning - Zimbra :: Wiki
Reply With Quote
  #4 (permalink)  
Old 11-14-2008, 08:08 AM
Junior Member
 
Posts: 6
Thumbs up Thank you very much.The code is working fine for me..

Thank you very much.The code is working fine for me..I was also facing problem in deleting account from zimbra...
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads

Why Join?

Registering let's you ask questions, makes it easier to search, displays any files attached to posts, and notifies you about replies.

blog.zimbra.com




 

SEO by vBSEO ©2011, Crawlability, Inc.