1 7 package org.jboss.test.foedeployer.ejb.simple; 8 9 import java.rmi.RemoteException ; 10 11 import javax.ejb.CreateException ; 12 import javax.ejb.EJBException ; 13 import javax.ejb.FinderException ; 14 import javax.ejb.SessionBean ; 15 import javax.ejb.SessionContext ; 16 import javax.naming.InitialContext ; 17 import javax.naming.NamingException ; 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 42 public class SecretManagerSessionBean 43 implements SessionBean 44 { 45 private SessionContext context; 47 private static Category log = Category.getInstance( SecretManagerSessionBean.class ); 48 49 55 public void createSecret( String secretKey, String secret ) 56 { 57 SecretLocalHome secretLocalHome = getSecretLocalHome(); 58 try 59 { 60 SecretLocal secretLocal = secretLocalHome.create( secretKey, secret ); 61 } 62 catch( CreateException ce ) 63 { 64 log.info("Exception creating secret with secretKey=" + secretKey, ce); 65 throw new EJBException ( "Exception creating secret with secretKey=" 66 + secretKey + ":\n" + ce ); 67 } 68 log.info("Created secret: secretKey=" + secretKey + ", secret=" + secret); 69 } 70 71 76 public void removeSecret( String secretKey ) 77 { 78 SecretLocalHome secretLocalHome = getSecretLocalHome(); 79 try 80 { 81 SecretLocal secretLocal = secretLocalHome.findByPrimaryKey( secretKey ); 82 secretLocal.remove(); 83 } 84 catch(Exception re) 85 { 86 log.info("Remove(): secret with secretKey=" + secretKey + " doesn't exist"); 87 throw new EJBException ( "Can't remove secret: secret with secretKey=" 88 + secretKey + " doesn't exist" ); 89 } 90 log.info( "Removed secret: secretKey=" + secretKey ); 91 } 92 93 98 public String getSecret(String secretKey) 99 { 100 SecretLocalHome secretLocalHome = getSecretLocalHome(); 101 try 102 { 103 SecretLocal secretLocal = secretLocalHome.findByPrimaryKey( secretKey ); 104 return secretLocal.getSecret(); 105 } 106 catch( Exception re ) 107 { 108 log.info( "getSecret(): secret with secretKey=" + secretKey + " doesn't exist" ); 109 throw new EJBException ( "Can't find secret with secretKey=" + secretKey ); 110 } 111 } 112 113 private SecretLocalHome getSecretLocalHome() 115 throws EJBException 116 { 117 InitialContext initCtx = null; 118 try 119 { 120 initCtx = new InitialContext (); 121 SecretLocalHome secretLocalHome = (SecretLocalHome) 122 initCtx.lookup("ejb/SecretLocal"); 123 return secretLocalHome; 124 } 125 catch( NamingException ne ) 126 { 127 log.info( "Failed to lookup SecretLocalHome." ); 128 throw new EJBException ( ne ); 129 } 130 finally 131 { 132 try 133 { 134 if( initCtx != null ) 135 initCtx.close(); 136 } 137 catch( NamingException ne ) 138 { 139 log.info( "Error closing context: " + ne ); 140 throw new EJBException ( ne ); 141 } 142 } 143 } 144 145 149 public void ejbCreate() { } 150 151 public void setSessionContext(SessionContext 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 |