You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by Rajender Vallapureddy <ra...@hnexus.com> on 2012/12/25 19:10:42 UTC

read list of available/registered domains

Hi Eric,

Merry Christmas to you :).

I am trying to read domains from the database from my custom matcher.

I have a written a helper class which is replica of
org.apache.james.domainlist.jpa.JPADomainList.java which has the below
method.

public List<String> getDomainListInternal() throws DomainListException {
		List<String> domains = new ArrayList<String>();

		EntityManager entityManager = entityManagerFactory
				.createEntityManager();
		LOGGER.info("entity manager created.......");

		final EntityTransaction transaction = entityManager.getTransaction();
		try {
			transaction.begin();
			domains = entityManager.createNamedQuery("listDomainNames")
					.getResultList();
			transaction.commit();
		} catch (PersistenceException e) {
			getLogger().error("Failed to list domains", e);
			if (transaction.isActive()) {
				transaction.rollback();
			}
			throw new DomainListException("Unable to retrieve domains", e);
		} finally {
			entityManager.close();
		}
		if (domains.size() == 0) {
			return null;
		} else {
			return new ArrayList<String>(domains);
		}


Now In my custom mailet class, I have called the above method, but I
am unable to retrieve the domains from the database table. Below is my
code from custom mailtet.


try{
			domainsList = oTestJPADomainsList.getDomainListInternal();
		}catch (Exception exp)
		{
			exp.printStackTrace();
		}
		if(domains != null && !domains.isEmpty()){
			domains.clear();
		}
		
		if(domainsList != null && !domainsList.isEmpty()){
			for(String aDomain : domainsList)
			{
				domains.add(aDomain);
			}	
		} else {
			LOGGER.error("Unable to read domains from JPA DOmains List...");
			throw new Exception("No Domains Found");
		}

When I try to send an email I am getting throw the no domains found
exception (my custom exception).

Please can you guide in using the right api to read the domains from
the database source.

Regards,
Rajender

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


Re: read list of available/registered domains

Posted by Eric Charles <er...@apache.org>.
Merry Christmas to you also Rajender.

The eaisest is to inject the DomainList domainList in you mailet

Just declare as field

     @Resource(name = "dnsservice")
     private DomainList domainList;

and the mailet container will inject it for you (see also [1] for an 
example using a setter method).

That way, your mailet will benefit from any change in the domain list 
code and from any change in the configuration (can be xml, jpa... based).

Thx, Eric

[1] 
https://svn.apache.org/repos/asf/james/server/trunk/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRecipientRewriteTable.java


On 25/12/2012 19:10, Rajender Vallapureddy wrote:
> Hi Eric,
>
> Merry Christmas to you :).
>
> I am trying to read domains from the database from my custom matcher.
>
> I have a written a helper class which is replica of
> org.apache.james.domainlist.jpa.JPADomainList.java which has the below
> method.
>
> public List<String> getDomainListInternal() throws DomainListException {
> 		List<String> domains = new ArrayList<String>();
>
> 		EntityManager entityManager = entityManagerFactory
> 				.createEntityManager();
> 		LOGGER.info("entity manager created.......");
>
> 		final EntityTransaction transaction = entityManager.getTransaction();
> 		try {
> 			transaction.begin();
> 			domains = entityManager.createNamedQuery("listDomainNames")
> 					.getResultList();
> 			transaction.commit();
> 		} catch (PersistenceException e) {
> 			getLogger().error("Failed to list domains", e);
> 			if (transaction.isActive()) {
> 				transaction.rollback();
> 			}
> 			throw new DomainListException("Unable to retrieve domains", e);
> 		} finally {
> 			entityManager.close();
> 		}
> 		if (domains.size() == 0) {
> 			return null;
> 		} else {
> 			return new ArrayList<String>(domains);
> 		}
>
>
> Now In my custom mailet class, I have called the above method, but I
> am unable to retrieve the domains from the database table. Below is my
> code from custom mailtet.
>
>
> try{
> 			domainsList = oTestJPADomainsList.getDomainListInternal();
> 		}catch (Exception exp)
> 		{
> 			exp.printStackTrace();
> 		}
> 		if(domains != null && !domains.isEmpty()){
> 			domains.clear();
> 		}
> 		
> 		if(domainsList != null && !domainsList.isEmpty()){
> 			for(String aDomain : domainsList)
> 			{
> 				domains.add(aDomain);
> 			}	
> 		} else {
> 			LOGGER.error("Unable to read domains from JPA DOmains List...");
> 			throw new Exception("No Domains Found");
> 		}
>
> When I try to send an email I am getting throw the no domains found
> exception (my custom exception).
>
> Please can you guide in using the right api to read the domains from
> the database source.
>
> Regards,
> Rajender
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
> For additional commands, e-mail: server-dev-help@james.apache.org
>

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