001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.foedeployer.ejb.simple;
023:
024: import java.rmi.RemoteException;
025:
026: import javax.ejb.CreateException;
027: import javax.ejb.EJBException;
028: import javax.ejb.FinderException;
029: import javax.ejb.SessionBean;
030: import javax.ejb.SessionContext;
031: import javax.naming.InitialContext;
032: import javax.naming.NamingException;
033:
034: import org.apache.log4j.Category;
035:
036: import org.jboss.test.foedeployer.ejb.simple.SecretLocal;
037: import org.jboss.test.foedeployer.ejb.simple.SecretLocalHome;
038:
039: /**
040: * Secret Manager Session bean.
041: *
042: * @ejb.bean
043: * type="Stateless"
044: * name="SecretManager"
045: * jndi-name="ejb/SecretManager"
046: * view-type="remote"
047: * generate="true"
048: *
049: * @ejb.transaction type="Required"
050: *
051: * @ejb.ejb-ref
052: * ejb-name="Secret"
053: * view-type="local"
054: *
055: * @author <a href="mailto:loubyansky@hotmail.com">Alex Loubyansky</a>
056: */
057: public class SecretManagerSessionBean implements SessionBean {
058: // Attributes ----------------------------------------------------
059: private SessionContext context;
060: private static Category log = Category
061: .getInstance(SecretManagerSessionBean.class);
062:
063: // Public --------------------------------------------------------
064: /**
065: * Creates a new secret
066: *
067: * @ejb:interface-method
068: */
069: public void createSecret(String secretKey, String secret) {
070: SecretLocalHome secretLocalHome = getSecretLocalHome();
071: try {
072: SecretLocal secretLocal = secretLocalHome.create(secretKey,
073: secret);
074: } catch (CreateException ce) {
075: log.info("Exception creating secret with secretKey="
076: + secretKey, ce);
077: throw new EJBException(
078: "Exception creating secret with secretKey="
079: + secretKey + ":\n" + ce);
080: }
081: log.info("Created secret: secretKey=" + secretKey + ", secret="
082: + secret);
083: }
084:
085: /**
086: * Removes secret
087: *
088: * @ejb:interface-method
089: */
090: public void removeSecret(String secretKey) {
091: SecretLocalHome secretLocalHome = getSecretLocalHome();
092: try {
093: SecretLocal secretLocal = secretLocalHome
094: .findByPrimaryKey(secretKey);
095: secretLocal.remove();
096: } catch (Exception re) {
097: log.info("Remove(): secret with secretKey=" + secretKey
098: + " doesn't exist");
099: throw new EJBException(
100: "Can't remove secret: secret with secretKey="
101: + secretKey + " doesn't exist");
102: }
103: log.info("Removed secret: secretKey=" + secretKey);
104: }
105:
106: /**
107: * Returns secret
108: *
109: * @ejb:interface-method
110: */
111: public String getSecret(String secretKey) {
112: SecretLocalHome secretLocalHome = getSecretLocalHome();
113: try {
114: SecretLocal secretLocal = secretLocalHome
115: .findByPrimaryKey(secretKey);
116: return secretLocal.getSecret();
117: } catch (Exception re) {
118: log.info("getSecret(): secret with secretKey=" + secretKey
119: + " doesn't exist");
120: throw new EJBException("Can't find secret with secretKey="
121: + secretKey);
122: }
123: }
124:
125: // Private -------------------------------------------------------
126: private SecretLocalHome getSecretLocalHome() throws EJBException {
127: InitialContext initCtx = null;
128: try {
129: initCtx = new InitialContext();
130: SecretLocalHome secretLocalHome = (SecretLocalHome) initCtx
131: .lookup("ejb/SecretLocal");
132: return secretLocalHome;
133: } catch (NamingException ne) {
134: log.info("Failed to lookup SecretLocalHome.");
135: throw new EJBException(ne);
136: } finally {
137: try {
138: if (initCtx != null)
139: initCtx.close();
140: } catch (NamingException ne) {
141: log.info("Error closing context: " + ne);
142: throw new EJBException(ne);
143: }
144: }
145: }
146:
147: // SessionBean Implementation ------------------------------------
148: /**
149: * @ejb:create-method
150: */
151: public void ejbCreate() {
152: }
153:
154: public void setSessionContext(SessionContext sc) {
155: context = sc;
156: }
157:
158: public void unsetSessionContext() {
159: context = null;
160: }
161:
162: public void ejbRemove() {
163: }
164:
165: public void ejbActivate() {
166: }
167:
168: public void ejbPassivate() {
169: }
170: }
|