开发者

Using a Singleton to create a default administrative account

I never used the @Singleton new feature of JavaEE 6 and i want to give it a try.

I was thinking in creating s Singleton to just hold a password that will allow the app adiministrator(The person that knows the password),to access some content of the app.

I tried to implement it following this tutorial, but it does not work. This is what i did:

I created the singleton bean:

@Singleton
@Startup
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class AdminAcountEJB implements IAdminAcountEJB {

    private String password;

    @PostConstruct
    public void init() {
        password = "password";
    }


    @Lock(LockType.READ)
    public String getPassword() {
        return password;
    }
}

I extracted an interface

public interface IAdminAcountEJB {

    public abstract String getPassword();
}

Then i try to inject the singleton in a managed bean using @EJB

@Named("managementBB")
@SessionScoped
public class ManagementBB implements Serializable{

    @EJB
    private IAdminAcountEJB managementEJB;    
    private String input;       
    private boolean authorized;

    public String seeWhatsUp() {
        if开发者_Python百科(input.equals(managementEJB.getPassword())) {
            authorized = true;
            return "manage?faces-redirect=true;";
        }
        return "index?faces-redirect=true;";
    }

  //Get set methods...

}

The last thing i do is create some markup that is displayed in case the correct password is entered:

<h:form rendered="#{managementBB.authorized == false}">
            <h:inputSecret value="#{managementEJB.input}"/>
            <h:commandButton value="..." action="#{managementEJB.seeWhatsUp}"/>
        </h:form>

<h:form rendered="#{managementBB.authorized}">  
             CORRECT PASSWORD!!
</h:form>

It all seems ok to me, but when i access the page, the console says:

javax.naming.NameNotFoundException: ejbinterfaces.IAdminAcountEJB#ejbinterfaces.IAdminAcountEJB not found

I don't understand why it don't work, this is how i inject other EJB's that are not Singletones but with @Singleton it does not work.

-How can i fix it?

-I am also interested in knowing what do you think about using a singletone for this purpose, you think is a good and safe idea?


I guess the problem is, that since you refer to your singleton within an EL expression in the view, it has to be annotated with @Named. If you use your beans only within others, this is not necessary.

Concerning your design, my 2 pennies are these:

  • Since you are using Java EE 6, you won't need to specify an interface for it. If you want/need it nevertheless, don't call it ISomething (except you work for Apple ;-) but give it a domain related name.
  • Using a singleton which allows concurrent read access is ok for core data. Only, I wouldn't put the password within the code, but into a database table, preferrably hashed and use the singleton as a provider for that which reads the table at startup.
  • Singletons in general may always introduce a bottleneck into the application because by definition they don't scale. So for your use case it's ok, since we can assume the access rate is very low. The other problem which might be introduced are race conditions (also not in your case) if we have data that changes, since we only have one instance being called in parallel.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜