java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4085
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Using different data sources for different users E-mail
User Rating: / 0
PoorBest 

Within an EJB, there is a way to provide different data sources to different user sessions. This is one way to prevent unauthorized users from updating restricted data. Since the EJB security model provides for the discovery of the invoking client, you can easily determine a user's identity and assigned roles. Programmatically restricting data access to specific users can make your code less flexible and maintainable. In order to switch data sources for a particular user, first of all we have to determine which user is calling an EJB method. In our sample we expect to accounts access 'User1' and 'User2'. Deployment descriptor has corresponding declarations:

////////////// EJB code retreiving data source connection:

public class UserDBBean implements SessionBean {
  private SessionContext _session;
  
  ...
  public void setSessionContext(SessionContext session) {
    _session = session;
  }
  
  public Connection getConnection() throws SQLException {
    try {
      Principal user = _session.getCallerPrincipal();
      String username = user.getName();
      
      String source = "java:comp/env/" + username + "ejbDB";
      InitialContext context = new InitialContext();
      
      System.out.println("Retreive connection:" + source);
      System.out.println("Principal name:" + username);

      return (DataSourcecontext.lookup(source)).getConnection();

    catch (NamingException error) {
      error.printStackTrace();
      throw new EJBException(ne);
    }
  }
  ...
}

////////////// necessary configuration in deployment descriptor:

<ejb-jar>
  <enterprise-beans>
    <!-- main bean definitions -->
    <session>
      <ejb-name>UserDBEJB</ejb-name>
      <home>test.UserDBHome</home>
      <remote>test.UserDB</remote>
      <ejb-class>test.UserDBBean</ejb-class>
      <session-type>Stateless</session-type>
      
      <transaction-type>Container</transaction-type>
      
      <!-- definition of resources bean referes -->
      
      <resource-ref>
        <res-ref-name>User1ejbDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
      </resource-ref>
      
      <resource-ref>
        <res-ref-name>User2ejbDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
      </resource-ref>
    </session>

  </enterprise-beans>
</ejb-jar>


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.