12 Replies Latest reply on Sep 26, 2005 10:51 AM by bill.burke

    EJB 3.0: getCallerPrincipal in Entity Bean?

    gdellelce

      What is the best way to call getCallerPrincipal in an Entity Bean in EJB3.0 or,
      in other words, how I get a reference to an EJBContext?

      THANK YOU for all hints in advance
      Gianfranco

        • 1. Re: EJB 3.0: getCallerPrincipal in Entity Bean?

          You can inject the context like so:

          @Resource javax.ejb.SessionContext ctx;


          Darin

          • 2. Re: EJB 3.0: getCallerPrincipal in Entity Bean?
            milx

            I'm unable to inject the SessionContext either in the entity itself or in a separate class, it is null at the @PrePersist callback. Does security have to be configured in order to inject the SessionContext?

            I can access the EJBContext from the javax.ejb.InvocationContext in an @AroundInvoke interceptor method.

            • 3. Re: EJB 3.0: getCallerPrincipal in Entity Bean?
              epbernard

              There is no context for an entity bean.
              Only for Session and MDB

              • 4. Re: EJB 3.0: getCallerPrincipal in Entity Bean?
                gdellelce

                So the question is: What is the best way to know the caller principal in an Entity Bean in EJB3.0?

                • 5. Re: EJB 3.0: getCallerPrincipal in Entity Bean?
                  bill.burke

                   

                  "gdellelce" wrote:
                  So the question is: What is the best way to know the caller principal in an Entity Bean in EJB3.0?


                  Wrap your EntityBean access from within a @Stateless bean and get it from the SessionContext.

                  Or, get it from your ServletContext if you're invoking on a servlet.

                  • 6. Re: EJB 3.0: getCallerPrincipal in Entity Bean?
                    gdellelce

                    I've a table like this:

                    create table theEntity(
                    id integer primary key not null,
                    ....
                    // Audit fields
                    createdBy varchar(....) not null,
                    createDate timestamp not null,
                    modifiedBy varchar(...) not null,
                    modifyDate timestamp not null
                    );

                    so I would like to have an Entity with a @PrePersist and a @PreUpdate
                    like this


                    @PrePersist
                    public void prePersist() {
                    if (getCreateDate() == null)
                    setCreateDate(new java.util.Date());
                    if (getCreatedBy() == null)
                    setCreatedBy(ctx.getCallerPrincipal().getName());
                    preUpdate();
                    }

                    @PreUpdate
                    public void preUpdate() {
                    setModifyDate(new java.util.Date());
                    setModifiedBy(ctx.getCallerPrincipal().getName());
                    }

                    like a database trigger so I don't have to take care of this fields in the code.

                    Gianfranco

                    • 7. Re: EJB 3.0: getCallerPrincipal in Entity Bean?
                      bigm25

                      So it looks like it's not possible to have some "audit"-fields in EJB3.

                      But JBoss 3 had some "proprietary" extensions for such audit-fields.

                      Any chance that such extension will be available with JBoss3 AND EJB3 ??

                      Rgds.

                      • 8. Re: EJB 3.0: getCallerPrincipal in Entity Bean?
                        epbernard

                         

                        "bigm25" wrote:
                        So it looks like it's not possible to have some "audit"-fields in EJB3.


                        What are you talking about?

                        • 9. Re: EJB 3.0: getCallerPrincipal in Entity Bean?
                          bigm25

                          In Jboss 3.2 we were using the following stuff in "jbosscmp-jdb.xml":


                          <entity>
                          
                          ......
                          .....
                           <audit>
                           <created-by>
                           <column-name>createdby</column-name>
                           </created-by>
                           <created-time>
                           <column-name>createdtime</column-name>
                           </created-time>
                           <updated-by>
                           <column-name>updatedby</column-name>
                           </updated-by>
                           <updated-time>
                           <column-name>updatedtime</column-name>
                           </updated-time>
                           </audit>
                          </entity>


                          and JBoss happily stored the time/principial of any changes or creations, no matter which code actually triggerd the update/store.

                          So the question is, how to achieve the same effect with JBoss4 AND EJB3, since there is neither a "jbosscmp-jdbc.xml", nor can we get the callerPrincipal from within an entity.

                          Did i miss something or wouldn't this be a nice "extension" to implement?


                          • 10. Re: EJB 3.0: getCallerPrincipal in Entity Bean?
                            michael_c_small

                            Did you get an answer on this question? I'm trying to perform the same functionality. My only thought was to create a utility session bean that not only injects the session context through a setter method, but also provides a getter method.

                            • 11. Re: EJB 3.0: getCallerPrincipal in Entity Bean?
                              gdellelce

                              No answer until now. But I think that a session bean isn't the solution to every situation because you could have cascade persistence and in this case can
                              miss some modifyBy updates.

                              • 12. Re: EJB 3.0: getCallerPrincipal in Entity Bean?
                                bill.burke

                                You have to use a session bean in front or you can use

                                org.jboss.aspects.security.SecurityContext

                                which is proprietary

                                We also have JACC integration with CRUD operations but really haven't documented it well yet.