KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > foedeployer > ejb > simple > SecretManagerSessionBean


1 /*
2 * JBoss, the OpenSource EJB server
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test.foedeployer.ejb.simple;
8
9 import java.rmi.RemoteException JavaDoc;
10
11 import javax.ejb.CreateException JavaDoc;
12 import javax.ejb.EJBException JavaDoc;
13 import javax.ejb.FinderException JavaDoc;
14 import javax.ejb.SessionBean JavaDoc;
15 import javax.ejb.SessionContext JavaDoc;
16 import javax.naming.InitialContext JavaDoc;
17 import javax.naming.NamingException JavaDoc;
18
19 import org.apache.log4j.Category;
20
21 import org.jboss.test.foedeployer.ejb.simple.SecretLocal;
22 import org.jboss.test.foedeployer.ejb.simple.SecretLocalHome;
23
24 /**
25  * Secret Manager Session bean.
26  *
27  * @ejb.bean
28  * type="Stateless"
29  * name="SecretManager"
30  * jndi-name="ejb/SecretManager"
31  * view-type="remote"
32  * generate="true"
33  *
34  * @ejb.transaction type="Required"
35  *
36  * @ejb.ejb-ref
37  * ejb-name="Secret"
38  * view-type="local"
39  *
40  * @author <a HREF="mailto:loubyansky@hotmail.com">Alex Loubyansky</a>
41  */

42 public class SecretManagerSessionBean
43    implements SessionBean JavaDoc
44 {
45    // Attributes ----------------------------------------------------
46
private SessionContext JavaDoc context;
47    private static Category log = Category.getInstance( SecretManagerSessionBean.class );
48
49    // Public --------------------------------------------------------
50
/**
51     * Creates a new secret
52     *
53     * @ejb:interface-method
54     */

55    public void createSecret( String JavaDoc secretKey, String JavaDoc secret )
56    {
57       SecretLocalHome secretLocalHome = getSecretLocalHome();
58       try
59       {
60          SecretLocal secretLocal = secretLocalHome.create( secretKey, secret );
61       }
62       catch( CreateException JavaDoc ce )
63       {
64          log.info("Exception creating secret with secretKey=" + secretKey, ce);
65          throw new EJBException JavaDoc( "Exception creating secret with secretKey="
66             + secretKey + ":\n" + ce );
67       }
68       log.info("Created secret: secretKey=" + secretKey + ", secret=" + secret);
69    }
70
71    /**
72     * Removes secret
73     *
74     * @ejb:interface-method
75     */

76    public void removeSecret( String JavaDoc secretKey )
77    {
78       SecretLocalHome secretLocalHome = getSecretLocalHome();
79       try
80       {
81          SecretLocal secretLocal = secretLocalHome.findByPrimaryKey( secretKey );
82          secretLocal.remove();
83       }
84       catch(Exception JavaDoc re)
85       {
86          log.info("Remove(): secret with secretKey=" + secretKey + " doesn't exist");
87          throw new EJBException JavaDoc( "Can't remove secret: secret with secretKey="
88             + secretKey + " doesn't exist" );
89       }
90       log.info( "Removed secret: secretKey=" + secretKey );
91    }
92
93    /**
94    * Returns secret
95    *
96    * @ejb:interface-method
97    */

98    public String JavaDoc getSecret(String JavaDoc secretKey)
99    {
100       SecretLocalHome secretLocalHome = getSecretLocalHome();
101       try
102       {
103          SecretLocal secretLocal = secretLocalHome.findByPrimaryKey( secretKey );
104          return secretLocal.getSecret();
105       }
106       catch( Exception JavaDoc re )
107       {
108          log.info( "getSecret(): secret with secretKey=" + secretKey + " doesn't exist" );
109          throw new EJBException JavaDoc( "Can't find secret with secretKey=" + secretKey );
110       }
111    }
112
113    // Private -------------------------------------------------------
114
private SecretLocalHome getSecretLocalHome()
115       throws EJBException JavaDoc
116    {
117       InitialContext JavaDoc initCtx = null;
118       try
119       {
120          initCtx = new InitialContext JavaDoc();
121          SecretLocalHome secretLocalHome = (SecretLocalHome)
122             initCtx.lookup("ejb/SecretLocal");
123          return secretLocalHome;
124       }
125       catch( NamingException JavaDoc ne )
126       {
127          log.info( "Failed to lookup SecretLocalHome." );
128          throw new EJBException JavaDoc( ne );
129       }
130       finally
131       {
132          try
133          {
134             if( initCtx != null )
135                initCtx.close();
136          }
137          catch( NamingException JavaDoc ne )
138          {
139             log.info( "Error closing context: " + ne );
140             throw new EJBException JavaDoc( ne );
141          }
142       }
143    }
144
145    // SessionBean Implementation ------------------------------------
146
/**
147     * @ejb:create-method
148     */

149    public void ejbCreate() { }
150
151    public void setSessionContext(SessionContext JavaDoc sc)
152    {
153       context = sc;
154    }
155
156    public void unsetSessionContext()
157    {
158       context = null;
159    }
160
161    public void ejbRemove() { }
162    public void ejbActivate() { }
163    public void ejbPassivate() { }
164 }
165
Popular Tags