You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@beehive.apache.org by Pietro Di Bello <pi...@gmail.com> on 2005/01/27 16:06:39 UTC

Problem Instantiating a Control from a POJO

Hi all, 

Exploring how to build webapps with beehive, we succeed to use a
simple control from a Page Flow Controller, and we are now trying to
figure out how to access the same beehive control from a POJO

We are trying to follow the example (taken from the doc "Controls
Programming", chapter 6) in "Programmatic Instantiation with
Properties (Client Code)"

	import org.apache.beehive.controls.api.bean.Controls;
	import org.apache.beehive.controls.api.properties.PropertyMap;
	
	PropertyMap jmsAttr = new
(PropertyMap(JmsMessageControl.Destination.class);     <-------- ??
	jmsAttr.setProperty("name", "InvoiceQueue");					 <-------- ??
	JmsMessageControlBean myJmsBean = (JmsMessageControlBean)
	      Controls.instantiate(
	          cl, 
	          "org.apache.beehive.controls.examples.JmsMessageControlBean", 
	          jmsAttr
	      );  

But this code seems to be incorrect. 
Also, it seems that the setProperty() has as first argument a PropertyKey, 
while here it seems to be a String ("name").

Do you know where is the problem?

For the sake of completion, here is the control we try to use from the POJO

---------------------------

package spike.beehive.ldap;

import java.lang.annotation.*;

import org.apache.beehive.controls.api.bean.ControlInterface;
import org.apache.beehive.controls.api.properties.PropertySet;

@ControlInterface
public interface LdapAccess
{
    @PropertySet(prefix="LdapServer")
    @Target({ElementType.FIELD, ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface LdapServer
    {
        String userManagerName();
        String userManagerPassword();
        String serviceUrl();
        String searchDatabase();
    }

    boolean logIn(String user, String password);
    String[] search(String searchString);
}
---------------------------

and its current implementation:

---------------------------
package spike.beehive.ldap;

import java.util.ArrayList;
import java.util.List;

import org.apache.beehive.controls.api.bean.*;
import org.apache.beehive.controls.api.context.Context;
import org.apache.beehive.controls.api.context.ControlBeanContext;
import org.apache.beehive.controls.api.events.EventHandler;

import com.utils.LdapAuthenticator;

@ControlImplementation
public class LdapAccessImpl implements LdapAccess 
{
    @Context ControlBeanContext context;
    private LdapAuthenticator ldapAuthenticator;

    @EventHandler(
        field="context", 
        eventSet=ControlBeanContext.LifeCycle.class, 
        eventName="onCreate"
    )
    public void  onBeanCreate()
    {
        LdapServer ldapServer =
(LdapServer)context.getControlPropertySet(LdapAccess.LdapServer.class);
        ldapAuthenticator = new LdapAuthenticator(
                ldapServer.userManagerName(),
                ldapServer.serviceUrl(),
                ldapServer.userManagerPassword(),
                ldapServer.searchDatabase());
    }    

    public boolean logIn(String user, String password)
    {

        boolean isAuthorized = false;
        try
        {
            isAuthorized = ldapAuthenticator.doLogin(user, password);
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        return isAuthorized;
    }

    public String[] search(String searchString)
    {
        List<String> selectedUsers = new ArrayList<String>();
        String[] userList = ldapAuthenticator.getUserList();
        for (String user : userList)
        {
            if (user.contains(searchString))
                selectedUsers.add(user);
        }
        return selectedUsers.toArray(new String[selectedUsers.size()]);
    }
}
---------------------------

Thanks a lot in advance!!
Piero